From 07dfaba3102075d7669927b2b189dffe95044e94 Mon Sep 17 00:00:00 2001 From: OszkarSemerath Date: Fri, 10 Feb 2023 01:41:04 +0100 Subject: VersionedMapStoreBuilder for delta and state based stores --- .../store/map/VersionedMapStoreBuilder.java | 81 ++++++++++++++++++++++ .../store/map/VersionedMapStoreDeltaImpl.java | 41 ++++++----- .../store/map/internal/VersionedMapDeltaImpl.java | 11 +-- 3 files changed, 108 insertions(+), 25 deletions(-) create mode 100644 subprojects/store/src/main/java/tools/refinery/store/map/VersionedMapStoreBuilder.java (limited to 'subprojects/store') diff --git a/subprojects/store/src/main/java/tools/refinery/store/map/VersionedMapStoreBuilder.java b/subprojects/store/src/main/java/tools/refinery/store/map/VersionedMapStoreBuilder.java new file mode 100644 index 00000000..7e413a86 --- /dev/null +++ b/subprojects/store/src/main/java/tools/refinery/store/map/VersionedMapStoreBuilder.java @@ -0,0 +1,81 @@ +package tools.refinery.store.map; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +public class VersionedMapStoreBuilder { + public enum StoreStrategy { + STATE, DELTA + } + + public enum DeltaStorageStrategy { + LIST, SET + } + + public enum StateStorageStrategy { + NO_NODE_CACHE, SHARED_NODE_CACHE, SHARED_NODE_CACHE_IN_GROUP + } + + protected Optional defaultValue = Optional.empty(); + protected StoreStrategy strategy = StoreStrategy.DELTA; + protected Boolean stateBasedImmutableWhenCommitting = false; + protected StateStorageStrategy stateBasedNodeSharingStrategy = StateStorageStrategy.SHARED_NODE_CACHE_IN_GROUP; + protected Optional> hashProvider = Optional.empty(); + protected DeltaStorageStrategy deltaStorageStrategy = DeltaStorageStrategy.LIST; + + public void setDefaultValue(V defaultValue) { + this.defaultValue = Optional.of(defaultValue); + } + + public void setStrategy(StoreStrategy strategy) { + this.strategy = strategy; + } + + public void setHashProvider(ContinousHashProvider hashProvider) { + this.hashProvider = Optional.of(hashProvider); + } + + public void setStateBasedImmutableWhenCommitting(boolean toImmutableWhenCommitting) { + this.stateBasedImmutableWhenCommitting = toImmutableWhenCommitting; + } + + public void setStateBasedNodeSharingStrategy(StateStorageStrategy strategy) { + this.stateBasedNodeSharingStrategy = strategy; + } + + public VersionedMapStore buildOne() { + return switch (strategy) { + case DELTA -> new VersionedMapStoreDeltaImpl<>( + this.deltaStorageStrategy == DeltaStorageStrategy.SET, + this.defaultValue.orElseThrow()); + case STATE -> new VersionedMapStoreImpl<>( + this.hashProvider.orElseThrow(), + this.defaultValue.orElseThrow(), + new VersionedMapStoreConfiguration( + this.stateBasedImmutableWhenCommitting, + this.stateBasedNodeSharingStrategy != StateStorageStrategy.NO_NODE_CACHE, + this.stateBasedNodeSharingStrategy == StateStorageStrategy.SHARED_NODE_CACHE_IN_GROUP)); + }; + } + + public List> buildGroup(int amount) { + if (this.strategy == StoreStrategy.STATE && + this.stateBasedNodeSharingStrategy == StateStorageStrategy.SHARED_NODE_CACHE_IN_GROUP) { + return VersionedMapStoreImpl.createSharedVersionedMapStores( + amount, + this.hashProvider.orElseThrow(), + this.defaultValue.orElseThrow(), + new VersionedMapStoreConfiguration( + this.stateBasedImmutableWhenCommitting, + true, + true)); + } else { + List> result = new ArrayList<>(amount); + for (int i = 0; i < amount; i++) { + result.add(buildOne()); + } + return result; + } + } +} diff --git a/subprojects/store/src/main/java/tools/refinery/store/map/VersionedMapStoreDeltaImpl.java b/subprojects/store/src/main/java/tools/refinery/store/map/VersionedMapStoreDeltaImpl.java index 2bd758e2..98dec2bb 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/map/VersionedMapStoreDeltaImpl.java +++ b/subprojects/store/src/main/java/tools/refinery/store/map/VersionedMapStoreDeltaImpl.java @@ -2,32 +2,32 @@ package tools.refinery.store.map; import java.util.*; -import tools.refinery.store.map.internal.DeltaDiffCursor; -import tools.refinery.store.map.internal.MapDelta; -import tools.refinery.store.map.internal.MapTransaction; -import tools.refinery.store.map.internal.VersionedMapDeltaImpl; +import tools.refinery.store.map.internal.*; + +public class VersionedMapStoreDeltaImpl implements VersionedMapStore { + // Configuration + protected final boolean summarizeChanges; -public class VersionedMapStoreDeltaImpl implements VersionedMapStore{ // Static data protected final V defaultValue; // Dynamic data - protected final Map> states = new HashMap<>(); + protected final Map> states = new HashMap<>(); protected long nextID = 0; - public VersionedMapStoreDeltaImpl(V defaultValue) { - super(); + public VersionedMapStoreDeltaImpl(boolean summarizeChanges, V defaultValue) { + this.summarizeChanges = summarizeChanges; this.defaultValue = defaultValue; } @Override public VersionedMap createMap() { - return new VersionedMapDeltaImpl<>(this, defaultValue); + return new VersionedMapDeltaImpl<>(this, this.summarizeChanges, this.defaultValue); } @Override public VersionedMap createMap(long state) { - VersionedMapDeltaImpl result = new VersionedMapDeltaImpl<>(this, defaultValue); + VersionedMapDeltaImpl result = new VersionedMapDeltaImpl<>(this, this.summarizeChanges, this.defaultValue); result.restore(state); return result; } @@ -35,7 +35,7 @@ public class VersionedMapStoreDeltaImpl implements VersionedMapStore public synchronized MapTransaction appendTransaction(MapDelta[] deltas, MapTransaction previous, long[] versionContainer) { long version = nextID++; versionContainer[0] = version; - if(deltas == null) { + if (deltas == null) { states.put(version, previous); return previous; } else { @@ -45,26 +45,25 @@ public class VersionedMapStoreDeltaImpl implements VersionedMapStore } } - private synchronized MapTransaction getState(long state) { + private synchronized MapTransaction getState(long state) { return states.get(state); } public void getPath(long to, List[]> forwardTransactions) { - MapTransaction toTransaction = getState(to); - while(toTransaction != null) { + MapTransaction toTransaction = getState(to); + while (toTransaction != null) { forwardTransactions.add(toTransaction.deltas()); toTransaction = toTransaction.parent(); } } public void getPath(long from, long to, - List[]> backwardTransactions, - List[]> forwardTransactions) - { - MapTransaction fromTransaction = getState(from); - MapTransaction toTransaction = getState(to); - while(fromTransaction != toTransaction) { - if(fromTransaction == null || fromTransaction.version() < toTransaction.version()) { + List[]> backwardTransactions, + List[]> forwardTransactions) { + MapTransaction fromTransaction = getState(from); + MapTransaction toTransaction = getState(to); + while (fromTransaction != toTransaction) { + if (fromTransaction == null || fromTransaction.version() < toTransaction.version()) { forwardTransactions.add(toTransaction.deltas()); toTransaction = toTransaction.parent(); } else { diff --git a/subprojects/store/src/main/java/tools/refinery/store/map/internal/VersionedMapDeltaImpl.java b/subprojects/store/src/main/java/tools/refinery/store/map/internal/VersionedMapDeltaImpl.java index deedf134..6f2996e1 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/map/internal/VersionedMapDeltaImpl.java +++ b/subprojects/store/src/main/java/tools/refinery/store/map/internal/VersionedMapDeltaImpl.java @@ -14,12 +14,16 @@ public class VersionedMapDeltaImpl implements VersionedMap { protected final V defaultValue; - public VersionedMapDeltaImpl(VersionedMapStoreDeltaImpl store, V defaultValue) { + public VersionedMapDeltaImpl(VersionedMapStoreDeltaImpl store, boolean summarizeChanges, V defaultValue) { this.store = store; this.defaultValue = defaultValue; current = new HashMap<>(); - uncommittedStore = new UncommittedDeltaArrayStore<>(); + if(summarizeChanges) { + this.uncommittedStore = new UncommittedDeltaMapStore<>(this); + } else { + this.uncommittedStore = new UncommittedDeltaArrayStore<>(); + } } @Override @@ -146,8 +150,7 @@ public class VersionedMapDeltaImpl implements VersionedMap { if (versioned == this) { return true; } else { - return Objects.equals(this.defaultValue, versioned.defaultValue) && - Objects.equals(this.current, versioned.current); + return Objects.equals(this.defaultValue, versioned.defaultValue) && Objects.equals(this.current, versioned.current); } } else { throw new UnsupportedOperationException("Comparing different map implementations is ineffective."); -- cgit v1.2.3-54-g00ecf