diff options
Diffstat (limited to 'model-data/src/main/java/org')
4 files changed, 50 insertions, 94 deletions
diff --git a/model-data/src/main/java/org/eclipse/viatra/solver/data/map/VersionedMapStoreImpl.java b/model-data/src/main/java/org/eclipse/viatra/solver/data/map/VersionedMapStoreImpl.java index c551ffe7..7b69ae54 100644 --- a/model-data/src/main/java/org/eclipse/viatra/solver/data/map/VersionedMapStoreImpl.java +++ b/model-data/src/main/java/org/eclipse/viatra/solver/data/map/VersionedMapStoreImpl.java | |||
@@ -12,67 +12,50 @@ import org.eclipse.viatra.solver.data.map.internal.MapDiffCursor; | |||
12 | import org.eclipse.viatra.solver.data.map.internal.Node; | 12 | import org.eclipse.viatra.solver.data.map.internal.Node; |
13 | import org.eclipse.viatra.solver.data.map.internal.VersionedMapImpl; | 13 | import org.eclipse.viatra.solver.data.map.internal.VersionedMapImpl; |
14 | 14 | ||
15 | public class VersionedMapStoreImpl<KEY,VALUE> implements VersionedMapStore<KEY, VALUE> { | 15 | public class VersionedMapStoreImpl<KEY, VALUE> implements VersionedMapStore<KEY, VALUE> { |
16 | // Configuration | 16 | // Configuration |
17 | final private boolean immutableWhenCommiting; | 17 | private final boolean immutableWhenCommiting; |
18 | 18 | ||
19 | // Static data | 19 | // Static data |
20 | protected final ContinousHashProvider<? super KEY> hashProvider; | 20 | protected final ContinousHashProvider<? super KEY> hashProvider; |
21 | protected final VALUE defaultValue; | 21 | protected final VALUE defaultValue; |
22 | 22 | ||
23 | // Dynamic data | 23 | // Dynamic data |
24 | final protected Map<Long, ImmutableNode<KEY,VALUE>> states; | 24 | protected final Map<Long, ImmutableNode<KEY, VALUE>> states = new HashMap<>(); |
25 | final protected Map<Node<KEY,VALUE>, ImmutableNode<KEY,VALUE>> nodeCache; | 25 | protected final Map<Node<KEY, VALUE>, ImmutableNode<KEY, VALUE>> nodeCache; |
26 | protected long nextID; | 26 | protected long nextID = 0; |
27 | 27 | ||
28 | public VersionedMapStoreImpl( | 28 | public VersionedMapStoreImpl(ContinousHashProvider<? super KEY> hashProvider, VALUE defaultValue, |
29 | ContinousHashProvider<? super KEY> hashProvider, | 29 | VersionedMapStoreConfiguration config) { |
30 | VALUE defaultValue, | ||
31 | VersionedMapStoreConfiguration config) | ||
32 | { | ||
33 | this.immutableWhenCommiting = config.immutableWhenCommiting; | 30 | this.immutableWhenCommiting = config.immutableWhenCommiting; |
34 | |||
35 | this.hashProvider = hashProvider; | 31 | this.hashProvider = hashProvider; |
36 | this.defaultValue = defaultValue; | 32 | this.defaultValue = defaultValue; |
37 | 33 | if (config.sharedNodeCacheInStore) { | |
38 | states = new HashMap<>(); | ||
39 | nextID = 0; | ||
40 | if(config.sharedNodeCacheInStore) { | ||
41 | nodeCache = new HashMap<>(); | 34 | nodeCache = new HashMap<>(); |
42 | } else { | 35 | } else { |
43 | nodeCache = null; | 36 | nodeCache = null; |
44 | } | 37 | } |
45 | } | 38 | } |
46 | private VersionedMapStoreImpl( | 39 | |
47 | ContinousHashProvider<? super KEY> hashProvider, | 40 | private VersionedMapStoreImpl(ContinousHashProvider<? super KEY> hashProvider, VALUE defaultValue, |
48 | VALUE defaultValue, | 41 | Map<Node<KEY, VALUE>, ImmutableNode<KEY, VALUE>> nodeCache, VersionedMapStoreConfiguration config) { |
49 | Map<Node<KEY,VALUE>, ImmutableNode<KEY,VALUE>> nodeCache, | ||
50 | VersionedMapStoreConfiguration config) | ||
51 | { | ||
52 | this.immutableWhenCommiting = config.immutableWhenCommiting; | 42 | this.immutableWhenCommiting = config.immutableWhenCommiting; |
53 | |||
54 | this.hashProvider = hashProvider; | 43 | this.hashProvider = hashProvider; |
55 | this.defaultValue = defaultValue; | 44 | this.defaultValue = defaultValue; |
56 | |||
57 | states = new HashMap<>(); | ||
58 | nextID = 0; | ||
59 | this.nodeCache = nodeCache; | 45 | this.nodeCache = nodeCache; |
60 | } | 46 | } |
61 | 47 | ||
62 | public VersionedMapStoreImpl(ContinousHashProvider<KEY> hashProvider, VALUE defaultValue) { | 48 | public VersionedMapStoreImpl(ContinousHashProvider<KEY> hashProvider, VALUE defaultValue) { |
63 | this(hashProvider,defaultValue,new VersionedMapStoreConfiguration()); | 49 | this(hashProvider, defaultValue, new VersionedMapStoreConfiguration()); |
64 | } | 50 | } |
65 | 51 | ||
66 | public static <MAP,KEY,VALUE> List<VersionedMapStore<KEY,VALUE>> createSharedVersionedMapStores( | 52 | public static <KEY, VALUE> List<VersionedMapStore<KEY, VALUE>> createSharedVersionedMapStores(int amount, |
67 | int amount, | 53 | ContinousHashProvider<? super KEY> hashProvider, VALUE defaultValue, |
68 | ContinousHashProvider<? super KEY> hashProvider, | 54 | VersionedMapStoreConfiguration config) { |
69 | VALUE defaultValue, | 55 | List<VersionedMapStore<KEY, VALUE>> result = new ArrayList<>(amount); |
70 | VersionedMapStoreConfiguration config) | 56 | if (config.sharedNodeCacheInStoreGroups) { |
71 | { | 57 | Map<Node<KEY, VALUE>, ImmutableNode<KEY, VALUE>> nodeCache; |
72 | List<VersionedMapStore<KEY,VALUE>> result = new ArrayList<>(amount); | 58 | if (config.sharedNodeCacheInStore) { |
73 | if(config.sharedNodeCacheInStoreGroups) { | ||
74 | Map<Node<KEY,VALUE>, ImmutableNode<KEY,VALUE>> nodeCache; | ||
75 | if(config.sharedNodeCacheInStore) { | ||
76 | nodeCache = new HashMap<>(); | 59 | nodeCache = new HashMap<>(); |
77 | } else { | 60 | } else { |
78 | nodeCache = null; | 61 | nodeCache = null; |
@@ -87,69 +70,55 @@ public class VersionedMapStoreImpl<KEY,VALUE> implements VersionedMapStore<KEY, | |||
87 | } | 70 | } |
88 | return result; | 71 | return result; |
89 | } | 72 | } |
90 | public static <MAP,KEY,VALUE> List<VersionedMapStore<KEY,VALUE>> createSharedVersionedMapStores( | 73 | |
91 | int amount, | 74 | public static <KEY, VALUE> List<VersionedMapStore<KEY, VALUE>> createSharedVersionedMapStores(int amount, |
92 | ContinousHashProvider<? super KEY> hashProvider, | 75 | ContinousHashProvider<? super KEY> hashProvider, VALUE defaultValue) { |
93 | VALUE defaultValue) | 76 | return createSharedVersionedMapStores(amount, hashProvider, defaultValue, new VersionedMapStoreConfiguration()); |
94 | { | ||
95 | return createSharedVersionedMapStores(amount,hashProvider,defaultValue,new VersionedMapStoreConfiguration()); | ||
96 | } | 77 | } |
97 | 78 | ||
98 | synchronized Set<Long> getStates() { | 79 | synchronized Set<Long> getStates() { |
99 | return states.keySet(); | 80 | return states.keySet(); |
100 | } | 81 | } |
101 | 82 | ||
102 | @Override | 83 | @Override |
103 | public VersionedMap<KEY,VALUE> createMap() { | 84 | public VersionedMap<KEY, VALUE> createMap() { |
104 | return new VersionedMapImpl<KEY,VALUE>(this,hashProvider,defaultValue); | 85 | return new VersionedMapImpl<KEY, VALUE>(this, hashProvider, defaultValue); |
105 | } | 86 | } |
87 | |||
106 | @Override | 88 | @Override |
107 | public VersionedMap<KEY,VALUE> createMap(long state) { | 89 | public VersionedMap<KEY, VALUE> createMap(long state) { |
108 | ImmutableNode<KEY, VALUE> data = revert(state); | 90 | ImmutableNode<KEY, VALUE> data = revert(state); |
109 | return new VersionedMapImpl<KEY,VALUE>(this,hashProvider,defaultValue,data); | 91 | return new VersionedMapImpl<KEY, VALUE>(this, hashProvider, defaultValue, data); |
110 | } | 92 | } |
111 | 93 | ||
112 | synchronized public ImmutableNode<KEY,VALUE> revert(long state) { | 94 | public synchronized ImmutableNode<KEY, VALUE> revert(long state) { |
113 | if(states.containsKey(state)) { | 95 | if (states.containsKey(state)) { |
114 | return states.get(state); | 96 | return states.get(state); |
115 | } else { | 97 | } else { |
116 | ArrayList<Long> existingKeys = new ArrayList<Long>(states.keySet()); | 98 | ArrayList<Long> existingKeys = new ArrayList<Long>(states.keySet()); |
117 | Collections.sort(existingKeys); | 99 | Collections.sort(existingKeys); |
118 | throw new IllegalArgumentException( | 100 | throw new IllegalArgumentException("Store does not contain state " + state + "! Avaliable states: " |
119 | "Store does not contain state "+state+"! Avaliable states: "+existingKeys.toArray().toString()); | 101 | + existingKeys.toArray().toString()); |
120 | } | 102 | } |
121 | } | 103 | } |
122 | synchronized public long commit(Node<KEY,VALUE> data, VersionedMapImpl<KEY, VALUE> mapToUpdateRoot) { | 104 | |
123 | ImmutableNode<KEY,VALUE> immutable; | 105 | public synchronized long commit(Node<KEY, VALUE> data, VersionedMapImpl<KEY, VALUE> mapToUpdateRoot) { |
124 | if(data != null) { | 106 | ImmutableNode<KEY, VALUE> immutable; |
125 | if(this.nodeCache != null) { | 107 | if (data != null) { |
126 | immutable = data.toImmutable(this.nodeCache); | 108 | immutable = data.toImmutable(this.nodeCache); |
127 | } else { | ||
128 | immutable = data.toImmutable(); | ||
129 | } | ||
130 | } else { | 109 | } else { |
131 | immutable = null; | 110 | immutable = null; |
132 | } | 111 | } |
133 | 112 | ||
134 | 113 | if (nextID == Long.MAX_VALUE) | |
135 | if(nextID == Long.MAX_VALUE) throw new IllegalStateException( | 114 | throw new IllegalStateException("Map store run out of Id-s"); |
136 | "Map store run out of Id-s"); | ||
137 | long id = nextID++; | 115 | long id = nextID++; |
138 | this.states.put(id, immutable); | 116 | this.states.put(id, immutable); |
139 | if(this.immutableWhenCommiting) { | 117 | if (this.immutableWhenCommiting) { |
140 | mapToUpdateRoot.setRoot(immutable); | 118 | mapToUpdateRoot.setRoot(immutable); |
141 | } | 119 | } |
142 | return id; | 120 | return id; |
143 | } | 121 | } |
144 | |||
145 | // public Map<Node<KEY,VALUE>, ImmutableNode<KEY,VALUE>> getStore() { | ||
146 | // return this.nodeCache; | ||
147 | // } | ||
148 | // public long addState(ImmutableNode<KEY,VALUE> data) { | ||
149 | // | ||
150 | // states.put(id,data); | ||
151 | // return id; | ||
152 | // } | ||
153 | 122 | ||
154 | @Override | 123 | @Override |
155 | public DiffCursor<KEY, VALUE> getDiffCursor(long fromState, long toState) { | 124 | public DiffCursor<KEY, VALUE> getDiffCursor(long fromState, long toState) { |
@@ -157,6 +126,6 @@ public class VersionedMapStoreImpl<KEY,VALUE> implements VersionedMapStore<KEY, | |||
157 | VersionedMap<KEY, VALUE> map2 = createMap(toState); | 126 | VersionedMap<KEY, VALUE> map2 = createMap(toState); |
158 | Cursor<KEY, VALUE> cursor1 = map1.getCursor(); | 127 | Cursor<KEY, VALUE> cursor1 = map1.getCursor(); |
159 | Cursor<KEY, VALUE> cursor2 = map2.getCursor(); | 128 | Cursor<KEY, VALUE> cursor2 = map2.getCursor(); |
160 | return new MapDiffCursor<KEY, VALUE>(this.hashProvider,this.defaultValue,cursor1,cursor2); | 129 | return new MapDiffCursor<KEY, VALUE>(this.hashProvider, this.defaultValue, cursor1, cursor2); |
161 | } | 130 | } |
162 | } | 131 | } |
diff --git a/model-data/src/main/java/org/eclipse/viatra/solver/data/map/internal/ImmutableNode.java b/model-data/src/main/java/org/eclipse/viatra/solver/data/map/internal/ImmutableNode.java index 087c12a1..832742d0 100644 --- a/model-data/src/main/java/org/eclipse/viatra/solver/data/map/internal/ImmutableNode.java +++ b/model-data/src/main/java/org/eclipse/viatra/solver/data/map/internal/ImmutableNode.java | |||
@@ -202,11 +202,6 @@ public class ImmutableNode<KEY, VALUE> extends Node<KEY, VALUE> { | |||
202 | protected MutableNode<KEY,VALUE> toMutable() { | 202 | protected MutableNode<KEY,VALUE> toMutable() { |
203 | return new MutableNode<KEY,VALUE>(this); | 203 | return new MutableNode<KEY,VALUE>(this); |
204 | } | 204 | } |
205 | |||
206 | @Override | ||
207 | public ImmutableNode<KEY,VALUE> toImmutable() { | ||
208 | return this; | ||
209 | } | ||
210 | 205 | ||
211 | @Override | 206 | @Override |
212 | public ImmutableNode<KEY, VALUE> toImmutable( | 207 | public ImmutableNode<KEY, VALUE> toImmutable( |
diff --git a/model-data/src/main/java/org/eclipse/viatra/solver/data/map/internal/MutableNode.java b/model-data/src/main/java/org/eclipse/viatra/solver/data/map/internal/MutableNode.java index 963ce111..d32e5fc9 100644 --- a/model-data/src/main/java/org/eclipse/viatra/solver/data/map/internal/MutableNode.java +++ b/model-data/src/main/java/org/eclipse/viatra/solver/data/map/internal/MutableNode.java | |||
@@ -127,8 +127,6 @@ public class MutableNode<KEY,VALUE> extends Node<KEY,VALUE> { | |||
127 | } | 127 | } |
128 | } | 128 | } |
129 | } | 129 | } |
130 | |||
131 | |||
132 | 130 | ||
133 | private Node<KEY, VALUE> addEntry(KEY key, VALUE value, int selectedHashFragment) { | 131 | private Node<KEY, VALUE> addEntry(KEY key, VALUE value, int selectedHashFragment) { |
134 | content[2*selectedHashFragment] = key; | 132 | content[2*selectedHashFragment] = key; |
@@ -255,11 +253,6 @@ public class MutableNode<KEY,VALUE> extends Node<KEY,VALUE> { | |||
255 | } | 253 | } |
256 | 254 | ||
257 | @Override | 255 | @Override |
258 | public ImmutableNode<KEY,VALUE> toImmutable() { | ||
259 | return ImmutableNode.constructImmutable(this,null); | ||
260 | } | ||
261 | |||
262 | @Override | ||
263 | public ImmutableNode<KEY, VALUE> toImmutable(Map<Node<KEY, VALUE>, ImmutableNode<KEY, VALUE>> cache) { | 256 | public ImmutableNode<KEY, VALUE> toImmutable(Map<Node<KEY, VALUE>, ImmutableNode<KEY, VALUE>> cache) { |
264 | return ImmutableNode.constructImmutable(this,cache); | 257 | return ImmutableNode.constructImmutable(this,cache); |
265 | } | 258 | } |
diff --git a/model-data/src/main/java/org/eclipse/viatra/solver/data/map/internal/Node.java b/model-data/src/main/java/org/eclipse/viatra/solver/data/map/internal/Node.java index 3695825d..f19ca06f 100644 --- a/model-data/src/main/java/org/eclipse/viatra/solver/data/map/internal/Node.java +++ b/model-data/src/main/java/org/eclipse/viatra/solver/data/map/internal/Node.java | |||
@@ -62,7 +62,6 @@ public abstract class Node<KEY,VALUE>{ | |||
62 | abstract public long getSize(); | 62 | abstract public long getSize(); |
63 | 63 | ||
64 | abstract MutableNode<KEY, VALUE> toMutable(); | 64 | abstract MutableNode<KEY, VALUE> toMutable(); |
65 | public abstract ImmutableNode<KEY, VALUE> toImmutable(); | ||
66 | public abstract ImmutableNode<KEY, VALUE> toImmutable( | 65 | public abstract ImmutableNode<KEY, VALUE> toImmutable( |
67 | Map<Node<KEY, VALUE>,ImmutableNode<KEY, VALUE>> cache); | 66 | Map<Node<KEY, VALUE>,ImmutableNode<KEY, VALUE>> cache); |
68 | 67 | ||