diff options
author | 2023-02-10 01:41:04 +0100 | |
---|---|---|
committer | 2023-02-10 01:41:04 +0100 | |
commit | 07dfaba3102075d7669927b2b189dffe95044e94 (patch) | |
tree | eb76dea32b75163a89af7d2229218b368fc66034 /subprojects/store/src/main | |
parent | Delta store commit (diff) | |
download | refinery-07dfaba3102075d7669927b2b189dffe95044e94.tar.gz refinery-07dfaba3102075d7669927b2b189dffe95044e94.tar.zst refinery-07dfaba3102075d7669927b2b189dffe95044e94.zip |
VersionedMapStoreBuilder for delta and state based stores
Diffstat (limited to 'subprojects/store/src/main')
3 files changed, 108 insertions, 25 deletions
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 @@ | |||
1 | package tools.refinery.store.map; | ||
2 | |||
3 | import java.util.ArrayList; | ||
4 | import java.util.List; | ||
5 | import java.util.Optional; | ||
6 | |||
7 | public class VersionedMapStoreBuilder<K, V> { | ||
8 | public enum StoreStrategy { | ||
9 | STATE, DELTA | ||
10 | } | ||
11 | |||
12 | public enum DeltaStorageStrategy { | ||
13 | LIST, SET | ||
14 | } | ||
15 | |||
16 | public enum StateStorageStrategy { | ||
17 | NO_NODE_CACHE, SHARED_NODE_CACHE, SHARED_NODE_CACHE_IN_GROUP | ||
18 | } | ||
19 | |||
20 | protected Optional<V> defaultValue = Optional.empty(); | ||
21 | protected StoreStrategy strategy = StoreStrategy.DELTA; | ||
22 | protected Boolean stateBasedImmutableWhenCommitting = false; | ||
23 | protected StateStorageStrategy stateBasedNodeSharingStrategy = StateStorageStrategy.SHARED_NODE_CACHE_IN_GROUP; | ||
24 | protected Optional<ContinousHashProvider<K>> hashProvider = Optional.empty(); | ||
25 | protected DeltaStorageStrategy deltaStorageStrategy = DeltaStorageStrategy.LIST; | ||
26 | |||
27 | public void setDefaultValue(V defaultValue) { | ||
28 | this.defaultValue = Optional.of(defaultValue); | ||
29 | } | ||
30 | |||
31 | public void setStrategy(StoreStrategy strategy) { | ||
32 | this.strategy = strategy; | ||
33 | } | ||
34 | |||
35 | public void setHashProvider(ContinousHashProvider<K> hashProvider) { | ||
36 | this.hashProvider = Optional.of(hashProvider); | ||
37 | } | ||
38 | |||
39 | public void setStateBasedImmutableWhenCommitting(boolean toImmutableWhenCommitting) { | ||
40 | this.stateBasedImmutableWhenCommitting = toImmutableWhenCommitting; | ||
41 | } | ||
42 | |||
43 | public void setStateBasedNodeSharingStrategy(StateStorageStrategy strategy) { | ||
44 | this.stateBasedNodeSharingStrategy = strategy; | ||
45 | } | ||
46 | |||
47 | public VersionedMapStore<K, V> buildOne() { | ||
48 | return switch (strategy) { | ||
49 | case DELTA -> new VersionedMapStoreDeltaImpl<>( | ||
50 | this.deltaStorageStrategy == DeltaStorageStrategy.SET, | ||
51 | this.defaultValue.orElseThrow()); | ||
52 | case STATE -> new VersionedMapStoreImpl<>( | ||
53 | this.hashProvider.orElseThrow(), | ||
54 | this.defaultValue.orElseThrow(), | ||
55 | new VersionedMapStoreConfiguration( | ||
56 | this.stateBasedImmutableWhenCommitting, | ||
57 | this.stateBasedNodeSharingStrategy != StateStorageStrategy.NO_NODE_CACHE, | ||
58 | this.stateBasedNodeSharingStrategy == StateStorageStrategy.SHARED_NODE_CACHE_IN_GROUP)); | ||
59 | }; | ||
60 | } | ||
61 | |||
62 | public List<VersionedMapStore<K, V>> buildGroup(int amount) { | ||
63 | if (this.strategy == StoreStrategy.STATE && | ||
64 | this.stateBasedNodeSharingStrategy == StateStorageStrategy.SHARED_NODE_CACHE_IN_GROUP) { | ||
65 | return VersionedMapStoreImpl.createSharedVersionedMapStores( | ||
66 | amount, | ||
67 | this.hashProvider.orElseThrow(), | ||
68 | this.defaultValue.orElseThrow(), | ||
69 | new VersionedMapStoreConfiguration( | ||
70 | this.stateBasedImmutableWhenCommitting, | ||
71 | true, | ||
72 | true)); | ||
73 | } else { | ||
74 | List<VersionedMapStore<K, V>> result = new ArrayList<>(amount); | ||
75 | for (int i = 0; i < amount; i++) { | ||
76 | result.add(buildOne()); | ||
77 | } | ||
78 | return result; | ||
79 | } | ||
80 | } | ||
81 | } | ||
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; | |||
2 | 2 | ||
3 | import java.util.*; | 3 | import java.util.*; |
4 | 4 | ||
5 | import tools.refinery.store.map.internal.DeltaDiffCursor; | 5 | import tools.refinery.store.map.internal.*; |
6 | import tools.refinery.store.map.internal.MapDelta; | 6 | |
7 | import tools.refinery.store.map.internal.MapTransaction; | 7 | public class VersionedMapStoreDeltaImpl<K, V> implements VersionedMapStore<K, V> { |
8 | import tools.refinery.store.map.internal.VersionedMapDeltaImpl; | 8 | // Configuration |
9 | protected final boolean summarizeChanges; | ||
9 | 10 | ||
10 | public class VersionedMapStoreDeltaImpl<K, V> implements VersionedMapStore<K, V>{ | ||
11 | // Static data | 11 | // Static data |
12 | protected final V defaultValue; | 12 | protected final V defaultValue; |
13 | 13 | ||
14 | // Dynamic data | 14 | // Dynamic data |
15 | protected final Map<Long,MapTransaction<K, V>> states = new HashMap<>(); | 15 | protected final Map<Long, MapTransaction<K, V>> states = new HashMap<>(); |
16 | protected long nextID = 0; | 16 | protected long nextID = 0; |
17 | 17 | ||
18 | public VersionedMapStoreDeltaImpl(V defaultValue) { | 18 | public VersionedMapStoreDeltaImpl(boolean summarizeChanges, V defaultValue) { |
19 | super(); | 19 | this.summarizeChanges = summarizeChanges; |
20 | this.defaultValue = defaultValue; | 20 | this.defaultValue = defaultValue; |
21 | } | 21 | } |
22 | 22 | ||
23 | @Override | 23 | @Override |
24 | public VersionedMap<K, V> createMap() { | 24 | public VersionedMap<K, V> createMap() { |
25 | return new VersionedMapDeltaImpl<>(this, defaultValue); | 25 | return new VersionedMapDeltaImpl<>(this, this.summarizeChanges, this.defaultValue); |
26 | } | 26 | } |
27 | 27 | ||
28 | @Override | 28 | @Override |
29 | public VersionedMap<K, V> createMap(long state) { | 29 | public VersionedMap<K, V> createMap(long state) { |
30 | VersionedMapDeltaImpl<K, V> result = new VersionedMapDeltaImpl<>(this, defaultValue); | 30 | VersionedMapDeltaImpl<K, V> result = new VersionedMapDeltaImpl<>(this, this.summarizeChanges, this.defaultValue); |
31 | result.restore(state); | 31 | result.restore(state); |
32 | return result; | 32 | return result; |
33 | } | 33 | } |
@@ -35,7 +35,7 @@ public class VersionedMapStoreDeltaImpl<K, V> implements VersionedMapStore<K, V> | |||
35 | public synchronized MapTransaction<K, V> appendTransaction(MapDelta<K, V>[] deltas, MapTransaction<K, V> previous, long[] versionContainer) { | 35 | public synchronized MapTransaction<K, V> appendTransaction(MapDelta<K, V>[] deltas, MapTransaction<K, V> previous, long[] versionContainer) { |
36 | long version = nextID++; | 36 | long version = nextID++; |
37 | versionContainer[0] = version; | 37 | versionContainer[0] = version; |
38 | if(deltas == null) { | 38 | if (deltas == null) { |
39 | states.put(version, previous); | 39 | states.put(version, previous); |
40 | return previous; | 40 | return previous; |
41 | } else { | 41 | } else { |
@@ -45,26 +45,25 @@ public class VersionedMapStoreDeltaImpl<K, V> implements VersionedMapStore<K, V> | |||
45 | } | 45 | } |
46 | } | 46 | } |
47 | 47 | ||
48 | private synchronized MapTransaction<K,V> getState(long state) { | 48 | private synchronized MapTransaction<K, V> getState(long state) { |
49 | return states.get(state); | 49 | return states.get(state); |
50 | } | 50 | } |
51 | 51 | ||
52 | public void getPath(long to, List<MapDelta<K, V>[]> forwardTransactions) { | 52 | public void getPath(long to, List<MapDelta<K, V>[]> forwardTransactions) { |
53 | MapTransaction<K,V> toTransaction = getState(to); | 53 | MapTransaction<K, V> toTransaction = getState(to); |
54 | while(toTransaction != null) { | 54 | while (toTransaction != null) { |
55 | forwardTransactions.add(toTransaction.deltas()); | 55 | forwardTransactions.add(toTransaction.deltas()); |
56 | toTransaction = toTransaction.parent(); | 56 | toTransaction = toTransaction.parent(); |
57 | } | 57 | } |
58 | } | 58 | } |
59 | 59 | ||
60 | public void getPath(long from, long to, | 60 | public void getPath(long from, long to, |
61 | List<MapDelta<K, V>[]> backwardTransactions, | 61 | List<MapDelta<K, V>[]> backwardTransactions, |
62 | List<MapDelta<K, V>[]> forwardTransactions) | 62 | List<MapDelta<K, V>[]> forwardTransactions) { |
63 | { | 63 | MapTransaction<K, V> fromTransaction = getState(from); |
64 | MapTransaction<K,V> fromTransaction = getState(from); | 64 | MapTransaction<K, V> toTransaction = getState(to); |
65 | MapTransaction<K,V> toTransaction = getState(to); | 65 | while (fromTransaction != toTransaction) { |
66 | while(fromTransaction != toTransaction) { | 66 | if (fromTransaction == null || fromTransaction.version() < toTransaction.version()) { |
67 | if(fromTransaction == null || fromTransaction.version() < toTransaction.version()) { | ||
68 | forwardTransactions.add(toTransaction.deltas()); | 67 | forwardTransactions.add(toTransaction.deltas()); |
69 | toTransaction = toTransaction.parent(); | 68 | toTransaction = toTransaction.parent(); |
70 | } else { | 69 | } 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<K, V> implements VersionedMap<K, V> { | |||
14 | 14 | ||
15 | protected final V defaultValue; | 15 | protected final V defaultValue; |
16 | 16 | ||
17 | public VersionedMapDeltaImpl(VersionedMapStoreDeltaImpl<K, V> store, V defaultValue) { | 17 | public VersionedMapDeltaImpl(VersionedMapStoreDeltaImpl<K, V> store, boolean summarizeChanges, V defaultValue) { |
18 | this.store = store; | 18 | this.store = store; |
19 | this.defaultValue = defaultValue; | 19 | this.defaultValue = defaultValue; |
20 | 20 | ||
21 | current = new HashMap<>(); | 21 | current = new HashMap<>(); |
22 | uncommittedStore = new UncommittedDeltaArrayStore<>(); | 22 | if(summarizeChanges) { |
23 | this.uncommittedStore = new UncommittedDeltaMapStore<>(this); | ||
24 | } else { | ||
25 | this.uncommittedStore = new UncommittedDeltaArrayStore<>(); | ||
26 | } | ||
23 | } | 27 | } |
24 | 28 | ||
25 | @Override | 29 | @Override |
@@ -146,8 +150,7 @@ public class VersionedMapDeltaImpl<K, V> implements VersionedMap<K, V> { | |||
146 | if (versioned == this) { | 150 | if (versioned == this) { |
147 | return true; | 151 | return true; |
148 | } else { | 152 | } else { |
149 | return Objects.equals(this.defaultValue, versioned.defaultValue) && | 153 | return Objects.equals(this.defaultValue, versioned.defaultValue) && Objects.equals(this.current, versioned.current); |
150 | Objects.equals(this.current, versioned.current); | ||
151 | } | 154 | } |
152 | } else { | 155 | } else { |
153 | throw new UnsupportedOperationException("Comparing different map implementations is ineffective."); | 156 | throw new UnsupportedOperationException("Comparing different map implementations is ineffective."); |