-
Notifications
You must be signed in to change notification settings - Fork 5
/
AbstractCache.java
378 lines (312 loc) · 11 KB
/
AbstractCache.java
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package org.genericsystem.common;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Supplier;
import org.genericsystem.api.core.Snapshot;
import org.genericsystem.api.core.exceptions.CacheNoStartedException;
import org.genericsystem.api.core.exceptions.ConcurrencyControlException;
import org.genericsystem.api.core.exceptions.OptimisticLockConstraintViolationException;
import org.genericsystem.api.core.exceptions.RollbackException;
import org.genericsystem.common.GenericBuilder.AddBuilder;
import org.genericsystem.common.GenericBuilder.MergeBuilder;
import org.genericsystem.common.GenericBuilder.SetBuilder;
import org.genericsystem.common.GenericBuilder.UpdateBuilder;
import org.genericsystem.defaults.DefaultCache;
import io.reactivex.Observable;
import io.reactivex.rxjavafx.observables.JavaFxObservable;
import javafx.beans.binding.Bindings;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableIntegerValue;
import javafx.beans.value.ObservableLongValue;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
/**
* @author Nicolas Feybesse
*
*/
public abstract class AbstractCache extends CheckedContext implements DefaultCache<Generic> {
public Generic setMeta(int dim) {
return setInstance(null, Collections.emptyList(), getRoot().getValue(), Arrays.asList(rootComponents(dim)));
}
public <U> U safeSupply(Supplier<U> safeExecution) {
start();
try {
return safeExecution.get();
} finally {
stop();
}
}
public <U> void safeConsum(Consumer<U> safeExecution) {
start();
try {
safeExecution.accept(null);
} finally {
stop();
}
}
@SuppressWarnings("unchecked")
public final <U extends AbstractCache> U start() {
return (U) getRoot().start(this);
}
public final void stop() {
getRoot().stop(this);
}
private final Restructurator restructurator;
protected final ObjectProperty<IDifferential<Generic>> transactionProperty;
protected final ObjectProperty<Differential> differentialProperty = new SimpleObjectProperty<>();
protected final Observable<Differential> differentialObservable = JavaFxObservable.valuesOf(differentialProperty).distinctUntilChanged();
private ObservableIntegerValue cacheLevel;
private ObservableLongValue ts;
private final ContextEventListener<Generic> listener;
private Map<Generic, ObservableList<Generic>> dependenciesAsOservableListCacheMap = new HashMap<>();
public Map<Generic, ObservableList<Generic>> getDependenciesAsOservableListCacheMap() {
return dependenciesAsOservableListCacheMap;
}
@Override
public long shiftTs() throws RollbackException {
transactionProperty.set(buildTransaction());
listener.triggersRefreshEvent();
return getTs();
}
@Override
public boolean contains(Generic generic) {
return differentialProperty.getValue().getAdds().contains(generic);
}
protected abstract IDifferential<Generic> buildTransaction();
public AbstractCache(Root root) {
this(root, new ContextEventListener<Generic>() {
});
}
public AbstractCache(Root root, ContextEventListener<Generic> listener) {
super(root);
this.restructurator = buildRestructurator();
this.listener = listener;
transactionProperty = new SimpleObjectProperty<>(buildTransaction());
cacheLevel = Bindings.createIntegerBinding(() -> differentialProperty.get().getCacheLevel(), differentialProperty);
ts = Bindings.createLongBinding(() -> transactionProperty.get().getTs(), transactionProperty);
initialize();
}
protected Differential getDifferential() {
return differentialProperty.get();
}
Restructurator getRestructurator() {
return restructurator;
}
public ObservableValue<IDifferential<Generic>> getTransactionProperty() {
return transactionProperty;
}
public IDifferential<Generic> getTransaction() {
return transactionProperty.get();
}
public long getTs() {
return getTransaction().getTs();
}
@Override
public ObservableLongValue getTsObservableValue() {
return ts;
}
@Override
public Snapshot<Generic> getDependencies(Generic generic) {
return getDifferential().getDependencies(generic);
}
protected Restructurator buildRestructurator() {
return new Restructurator(this);
}
protected void initialize() {
differentialProperty.set(buildDifferential(getDifferential() == null ? buildTransactionDifferential() : getDifferential().getSubDifferential()));
}
protected Differential buildDifferential(IDifferential<Generic> subCache) {
return new Differential(subCache);
}
protected TransactionDifferential buildTransactionDifferential() {
return new TransactionDifferential();
}
@Override
public void tryFlush() throws ConcurrencyControlException {
if (!equals(getRoot().getCurrentCache()))
discardWithException(new CacheNoStartedException("The Cache isn't started"));
try {
checkConstraints();
doSynchronizedApplyInSubContext();
initialize();
listener.triggersFlushEvent();
} catch (OptimisticLockConstraintViolationException exception) {
discardWithException(exception);
}
}
@Override
public void flush() {
// System.out.println("FLUSH");
Throwable cause = null;
for (int attempt = 0; attempt < Statics.ATTEMPTS; attempt++) {
try {
// System.out.println("TRYFLUSH");
// TODO reactivate this
// if (getEngine().pickNewTs() - getTs() >= timeOut)
// throw new ConcurrencyControlException("The timestamp cache (" + getTs() + ") is bigger than the life
// time out : " + Statics.LIFE_TIMEOUT);
tryFlush();
return;
} catch (ConcurrencyControlException e) {
cause = e;
try {
Thread.sleep(Statics.ATTEMPT_SLEEP);
shiftTs();
} catch (InterruptedException ex) {
discardWithException(ex);
}
}
}
discardWithException(cause);
}
protected void doSynchronizedApplyInSubContext()
throws ConcurrencyControlException, OptimisticLockConstraintViolationException {
Differential originalCacheElement = getDifferential();
// if (getDifferential().getSubDifferential() instanceof Differential)
// this.differentialProperty.set((Differential) getDifferential().getSubDifferential());
// try {
synchronizedApply(originalCacheElement);
// } finally {
// this.differentialProperty.set(originalCacheElement);
// }
}
private void synchronizedApply(Differential cacheElement)
throws ConcurrencyControlException, OptimisticLockConstraintViolationException {
synchronized (getRoot()) {
cacheElement.apply();
}
}
@Override
public void clear() {
initialize();
listener.triggersClearEvent();
listener.triggersRefreshEvent();
}
@Override
public void mount() {
differentialProperty.set(buildDifferential(getDifferential()));
}
@Override
public void unmount() {
IDifferential<Generic> subCache = getDifferential().getSubDifferential();
differentialProperty
.set(subCache instanceof Differential ? (Differential) subCache : new Differential(subCache));
listener.triggersClearEvent();
listener.triggersRefreshEvent();
}
@Override
protected void triggersMutation(Generic oldDependency, Generic newDependency) {
if (listener != null)
listener.triggersMutationEvent(oldDependency, newDependency);
}
public Generic buildAndPlug(Class<?> clazz, Generic meta, List<Generic> supers, Serializable value, List<Generic> components) {
return plug(getRoot().build(null, clazz, meta, supers, value, components));
}
protected Generic plug(Generic generic) {
assert generic.getBirthTs() == Long.MAX_VALUE || generic.getBirthTs() == 0L : generic.info()
+ generic.getBirthTs();
getDifferential().plug(generic);
getChecker().checkAfterBuild(true, false, generic);
return generic;
}
protected void unplug(Generic generic) {
getChecker().checkAfterBuild(false, false, generic);
getDifferential().unplug(generic);
}
protected void checkConstraints() throws RollbackException {
getDifferential().checkConstraints(getChecker());
}
@Override
public void discardWithException(Throwable exception) throws RollbackException {
clear();
throw new RollbackException(exception);
}
public int getCacheLevel() {
return getDifferential().getCacheLevel();
}
@Override
public ObservableIntegerValue getCacheLevelObservableValue() {
return cacheLevel;
}
@Override
public Generic setInstance(Generic meta, List<Generic> overrides, Serializable value, List<Generic> components) {
return new SetBuilder(this, meta, overrides, value, components).resolve();
}
@Override
public Generic addInstance(Generic meta, List<Generic> overrides, Serializable value, List<Generic> components) {
return new AddBuilder(this, meta, overrides, value, components).resolve();
}
@Override
public Generic update(Generic update, List<Generic> overrides, Serializable newValue, List<Generic> newComponents) {
return new UpdateBuilder(this, update, update.getMeta(), overrides, newValue, newComponents).resolve();
}
@Override
public Generic merge(Generic update, List<Generic> overrides, Serializable newValue, List<Generic> newComponents) {
return new MergeBuilder(this, update, update.getMeta(), overrides, newValue, newComponents).resolve();
}
@Override
public void forceRemove(Generic generic) {
getRestructurator().rebuildAll(null, null, computeDependencies(generic));
}
@Override
public void remove(Generic generic) {
getRestructurator().rebuildAll(null, null, computeRemoveDependencies(generic));
}
@Override
public void conserveRemove(Generic generic) {
getRestructurator().rebuildAll(generic, () -> generic, computeDependencies(generic));
}
protected class TransactionDifferential implements IDifferential<Generic> {
@Override
public void apply(Snapshot<Generic> removes, Snapshot<Generic> adds)
throws ConcurrencyControlException, OptimisticLockConstraintViolationException {
getTransaction().apply(removes, adds);
}
@Override
public ObjectProperty<IDifferential<Generic>> getDifferentialProperty() {
return (ObjectProperty) AbstractCache.this.differentialProperty;
}
@Override
public Observable<IDifferential<Generic>> getDifferentialObservable() {
return (Observable) AbstractCache.this.differentialObservable;
}
@Override
public Map<Generic, ObservableList<Generic>> getDependenciesAsOservableListCacheMap() {
return AbstractCache.this.getDependenciesAsOservableListCacheMap();
}
@Override
public Snapshot<Generic> getDependencies(Generic generic) {
return getTransaction().getDependencies(generic);
}
@Override
public long getTs() {
return getTransaction().getTs();
}
@Override
public Observable<Generic> getAddsObservable(Generic generic) {
// TODO
return Observable.never();
}
@Override
public Observable<Generic> getRemovesObservable(Generic generic) {
// TODO
return Observable.never();
}
}
public static interface ContextEventListener<X> {
default void triggersMutationEvent(X oldDependency, X newDependency) {
}
default void triggersRefreshEvent() {
}
default void triggersClearEvent() {
}
default void triggersFlushEvent() {
}
}
}