All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.kenshoo.pl.entity.internal.ChangesContainer Maven / Gradle / Ivy

Go to download

A Java persistence layer based on JOOQ for high performance and business flow support.

There is a newer version: 0.1.121-jooq-3.16.3
Show newest version
package com.kenshoo.pl.entity.internal;

import com.kenshoo.jooq.DataTable;
import com.kenshoo.pl.data.AbstractRecordCommand;
import com.kenshoo.pl.data.AffectedRows;
import com.kenshoo.pl.data.CommandsExecutor;
import com.kenshoo.pl.data.CreateRecordCommand;
import com.kenshoo.pl.data.DeleteRecordCommand;
import com.kenshoo.pl.data.UpdateRecordCommand;
import com.kenshoo.pl.entity.EntityChange;
import com.kenshoo.pl.entity.PersistentLayerStats;

import java.util.*;
import java.util.function.Supplier;


public class ChangesContainer {

    private final CreateRecordCommand.OnDuplicateKey onDuplicateKey;
    private final Map> deletes = new HashMap<>();
    private final Map> updates = new HashMap<>();
    private final Map> inserts = new HashMap<>();
    private final Map> insertsOnDuplicateUpdate = new HashMap<>();

    public ChangesContainer(CreateRecordCommand.OnDuplicateKey onDuplicateKey) {
        this.onDuplicateKey = onDuplicateKey;
    }

    public AbstractRecordCommand getDelete(DataTable table, EntityChange entityChange, Supplier commandCreator) {
        //noinspection RedundantTypeArguments IntelliJ fails to compile without specification
        return getOrCreate(deletes, IdToCommandMap< DeleteRecordCommand>::new, commandCreator, table, entityChange);
    }

    public AbstractRecordCommand getUpdate(DataTable table, EntityChange entityChange, Supplier commandCreator) {
        //noinspection RedundantTypeArguments IntelliJ fails to compile without specification
        return getOrCreate(updates, IdToCommandMap< UpdateRecordCommand>::new, commandCreator, table, entityChange);
    }

    public AbstractRecordCommand getInsert(DataTable table, EntityChange entityChange, Supplier commandCreator) {
        //noinspection RedundantTypeArguments IntelliJ fails to compile without specification
        return getOrCreate(inserts, IdToCommandMap::new, commandCreator, table, entityChange);
    }

    public Optional getInsert(final DataTable table, final EntityChange entityChange) {
        return Optional.ofNullable(inserts.get(table))
                       .map(idToCmdMap -> idToCmdMap.get(entityChange));
    }

    public AbstractRecordCommand getInsertOnDuplicateUpdate(DataTable table, EntityChange entityChange, Supplier commandCreator) {
        //noinspection RedundantTypeArguments IntelliJ fails to compile without specification
        return getOrCreate(insertsOnDuplicateUpdate, IdToCommandMap::new, commandCreator, table, entityChange);
    }

    public void commit(CommandsExecutor commandsExecutor, PersistentLayerStats stats) {
        for (Map.Entry> entry : deletes.entrySet()) {
            DataTable table = entry.getKey();
            AffectedRows affectedRows = commandsExecutor.executeDeletes(table, entry.getValue().map.values());
            stats.addAffectedRows(table.getName(), affectedRows);
        }
        for (Map.Entry> entry : inserts.entrySet()) {
            DataTable table = entry.getKey();
            Collection commands = entry.getValue().map.values();
            AffectedRows affectedRows;
            switch (onDuplicateKey) {
                case IGNORE:
                    affectedRows = commandsExecutor.executeInsertsOnDuplicateKeyIgnore(table, commands);
                    break;
                case UPDATE:
                    affectedRows = commandsExecutor.executeInsertsOnDuplicateKeyUpdate(table, commands);
                    break;
                case FAIL:
                default:
                    affectedRows = commandsExecutor.executeInserts(table, commands);
                    break;
            }
            stats.addAffectedRows(table.getName(), affectedRows);
        }
        for (Map.Entry> entry : updates.entrySet()) {
            DataTable table = entry.getKey();
            AffectedRows affectedRows = commandsExecutor.executeUpdates(table, entry.getValue().map.values());
            stats.addAffectedRows(table.getName(), affectedRows);
        }
        for (Map.Entry> entry : insertsOnDuplicateUpdate.entrySet()) {
            DataTable table = entry.getKey();
            AffectedRows affectedRows = commandsExecutor.executeInsertsOnDuplicateKeyUpdate(table, entry.getValue().map.values());
            stats.addAffectedRows(table.getName(), affectedRows);
        }
    }

     private  AbstractRecordCommand getOrCreate(Map> map, Supplier> creator, Supplier commandCreator, DataTable table, EntityChange entityChange) {
        //noinspection unchecked
         IdToCommandMap< RC> idToCommandMap = map.computeIfAbsent(table, k -> creator.get());
         return idToCommandMap.getOrCreate(entityChange, commandCreator);
    }

    private static class IdToCommandMap {
        private final Map map = new HashMap<>();

        AbstractRecordCommand getOrCreate(EntityChange entityChange, Supplier commandCreator) {
            return map.computeIfAbsent(entityChange, k -> commandCreator.get());
        }

        RC get(EntityChange entityChange) {
            return map.get(entityChange);
        }
    }
}






© 2015 - 2024 Weber Informatics LLC | Privacy Policy