aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store/src/main/java/tools/refinery/store/map/internal/delta/VersionedMapDeltaImpl.java
blob: c19cc81722610a6a25114652c6a947a93ec7ca03 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
 * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.store.map.internal.delta;

import java.util.*;

import tools.refinery.store.map.*;
import tools.refinery.store.map.IteratorAsCursor;

public class VersionedMapDeltaImpl<K, V> implements VersionedMap<K, V> {
	protected final VersionedMapStoreDeltaImpl<K, V> store;

	final Map<K, V> current;

	final UncommittedDeltaStore<K, V> uncommittedStore;
	MapTransaction<K, V> previous;

	protected final V defaultValue;

	public VersionedMapDeltaImpl(VersionedMapStoreDeltaImpl<K, V> store, boolean summarizeChanges, V defaultValue) {
		this.store = store;
		this.defaultValue = defaultValue;

		current = new HashMap<>();
		if (summarizeChanges) {
			this.uncommittedStore = new UncommittedDeltaMapStore<>(this);
		} else {
			this.uncommittedStore = new UncommittedDeltaArrayStore<>();
		}
	}

	@Override
	public V getDefaultValue() {
		return defaultValue;
	}

	@Override
	public Version commit() {
		MapDelta<K, V>[] deltas = uncommittedStore.extractAndDeleteDeltas();
		final MapTransaction<K,V> committedTransaction = this.store.appendTransaction(deltas, previous);
		this.previous = committedTransaction;
		return committedTransaction;
	}

	@Override
	public void restore(Version state) {
		// 1. restore uncommitted states
		MapDelta<K, V>[] uncommitted = this.uncommittedStore.extractAndDeleteDeltas();
		if (uncommitted != null) {
			backward(uncommitted);
		}

		// 2. get common ancestor
		final MapTransaction<K,V> parent;
		List<MapDelta<K, V>[]> forward = new ArrayList<>();
		if (this.previous == null) {
			parent = this.store.getPath(state, forward);
			this.forward(forward);
		} else {
			List<MapDelta<K, V>[]> backward = new ArrayList<>();
			parent = this.store.getPath(this.previous, state, backward, forward);
			this.backward(backward);
			this.forward(forward);
		}
		this.previous = parent;
	}

	protected void forward(List<MapDelta<K, V>[]> changes) {
		for (int i = changes.size() - 1; i >= 0; i--) {
			forward(changes.get(i));
		}
	}

	protected void backward(List<MapDelta<K, V>[]> changes) {
		//Currently, this loop statement is faster.
		//noinspection ForLoopReplaceableByForEach
		for (int i = 0; i < changes.size(); i++) {
			backward(changes.get(i));
		}
	}

	protected void forward(MapDelta<K, V>[] changes) {
		//Currently, this loop statement is faster.
		//noinspection ForLoopReplaceableByForEach
		for (int i = 0; i < changes.length; i++) {
			final MapDelta<K, V> change = changes[i];
			K key = change.getKey();
			V newValue = change.getNewValue();

			if(newValue == defaultValue) {
				current.remove(key);
			} else {
				current.put(key,newValue);
			}
		}
	}

	protected void backward(MapDelta<K, V>[] changes) {
		for (int i = changes.length - 1; i >= 0; i--) {
			final MapDelta<K, V> change = changes[i];
			K key = change.getKey();
			V oldValue = change.oldValue();

			if(oldValue == defaultValue) {
				current.remove(key);
			} else {
				current.put(key,oldValue);
			}
		}
	}

	@Override
	public V get(K key) {
		return current.getOrDefault(key, defaultValue);
	}

	@Override
	public Cursor<K, V> getAll() {
		return new IteratorAsCursor<>(this, current);
	}

	@Override
	public V put(K key, V value) {
		final V oldValue;
		if (Objects.equals(value, defaultValue)) {
			final V res = current.remove(key);
			if (res == null) {
				// no changes: default > default
				oldValue = defaultValue;
			} else {
				oldValue = res;
			}
		} else {
			final var mapValue = current.put(key, value);
			if (mapValue == null) {
				oldValue = defaultValue;
			} else {
				oldValue = mapValue;
			}
		}
		if(!Objects.equals(oldValue,value)) {
			uncommittedStore.processChange(key, oldValue, value);
		}
		return oldValue;
	}

	@Override
	public void putAll(Cursor<K, V> cursor) {
		if (cursor.getDependingMaps().contains(this)) {
			List<K> keys = new ArrayList<>();
			List<V> values = new ArrayList<>();
			while (cursor.move()) {
				keys.add(cursor.getKey());
				values.add(cursor.getValue());
			}
			for (int i = 0; i < keys.size(); i++) {
				this.put(keys.get(i), values.get(i));
			}
		} else {
			while (cursor.move()) {
				this.put(cursor.getKey(), cursor.getValue());
			}
		}
	}

	@Override
	public long getSize() {
		return current.size();
	}

	@Override
	public DiffCursor<K, V> getDiffCursor(Version state) {
		MapDelta<K, V>[] backward = this.uncommittedStore.extractDeltas();
		List<MapDelta<K, V>[]> backwardTransactions = new ArrayList<>();
		List<MapDelta<K, V>[]> forwardTransactions = new ArrayList<>();

		if (backward != null) {
			backwardTransactions.add(backward);
		}

		if (this.previous != null) {
			store.getPath(this.previous, state, backwardTransactions, forwardTransactions);
		} else {
			store.getPath(state, forwardTransactions);
		}

		return new DeltaDiffCursor<>(backwardTransactions, forwardTransactions);
	}

	@Override
	public int contentHashCode(ContentHashCode mode) {
		return this.current.hashCode();
	}

	@Override
	public boolean contentEquals(AnyVersionedMap other) {
		if (other instanceof VersionedMapDeltaImpl<?, ?> versioned) {
			if (versioned == this) {
				return true;
			} else {
				return Objects.equals(this.defaultValue, versioned.defaultValue) && Objects.equals(this.current, versioned.current);
			}
		} else {
			throw new UnsupportedOperationException("Comparing different map implementations is ineffective.");
		}
	}

	@Override
	public void checkIntegrity() {
		this.uncommittedStore.checkIntegrity();

		for (var entry : this.current.entrySet()) {
			var value = entry.getValue();
			if (value == this.defaultValue) {
				throw new IllegalStateException("Default value stored in map!");
			} else if (value == null) {
				throw new IllegalStateException("null value stored in map!");
			}
		}
		MapTransaction<K,V> transaction = this.previous;
		while(transaction != null) {
			MapTransaction<K,V> parent = transaction.parent();
			if(parent != null) {
				if(parent.depth() != transaction.depth()-1) {
					throw new IllegalStateException("Parent depths are inconsistent!");
				}
			} else {
				if(transaction.depth() != 0) {
					throw new IllegalArgumentException("Root depth is not 0!");
				}
			}
			transaction = transaction.parent();
		}
	}
}