-
Notifications
You must be signed in to change notification settings - Fork 5
/
Differential.java
162 lines (135 loc) · 5.31 KB
/
Differential.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
package org.genericsystem.common;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import org.genericsystem.api.core.FiltersBuilder;
import org.genericsystem.api.core.IndexFilter;
import org.genericsystem.api.core.Snapshot;
import org.genericsystem.api.core.exceptions.ConcurrencyControlException;
import org.genericsystem.api.core.exceptions.OptimisticLockConstraintViolationException;
import org.genericsystem.api.core.exceptions.RollbackException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.reactivex.Observable;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
/**
* @author Nicolas Feybesse
*
*/
public class Differential implements IDifferential<Generic> {
private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private final IDifferential<Generic> subDifferential;
protected final PseudoConcurrentCollection<Generic> adds = new PseudoConcurrentCollection<>();
protected final PseudoConcurrentCollection<Generic> removes = new PseudoConcurrentCollection<>();
public Differential(IDifferential<Generic> subDifferential) {
this.subDifferential = subDifferential;
}
public Stream<Generic> getLivingToRespawn() {
return adds.stream().filter(g -> {
return g.getTs() == Long.MAX_VALUE && g.isAlive();
});
}
public PseudoConcurrentCollection<Generic> getAdds() {
return adds;
}
public IDifferential<Generic> getSubDifferential() {
return subDifferential;
}
@Override
public AbstractCache getCache() {
return subDifferential.getCache();
}
public int getCacheLevel() {
return subDifferential instanceof Differential ? ((Differential) subDifferential).getCacheLevel() + 1 : 0;
}
public IntegerProperty getCacheLevelObservable() {
return subDifferential instanceof Differential ? new SimpleIntegerProperty(((Differential) subDifferential).getCacheLevel() + 1) : new SimpleIntegerProperty(0);
}
@SuppressWarnings("unchecked")
@Override
public Observable<Differential> getDifferentialObservable() {
return (Observable<Differential>) subDifferential.getDifferentialObservable();
}
void checkConstraints(Checker checker) throws RollbackException {
adds.forEach(x -> checker.checkAfterBuild(true, true, x));
removes.forEach(x -> checker.checkAfterBuild(false, true, x));
}
protected Generic plug(Generic generic) {
// assert generic.getOtherTs()[0] == Long.MAX_VALUE;
adds.add(generic);
return generic;
}
protected void unplug(Generic generic) {
if (!adds.remove(generic))
removes.add(generic);
}
@Override
public Snapshot<Generic> getDependencies(Generic generic) {
return new Snapshot<Generic>() {
@Override
public Generic get(Object o) {
Generic result = adds.get(o);
if (result != null)
return generic.isDirectAncestorOf(result) ? result : null;
return !removes.contains(o) ? subDifferential.getDependencies(generic).get(o) : null;
}
@Override
public Stream<Generic> unfilteredStream() {
return Stream.concat(adds.contains(generic) ? Stream.empty() : subDifferential.getDependencies(generic).filter(new IndexFilter(FiltersBuilder.NOT_CONTAINED_IN_PARAM, new ArrayList<>(removes.toList()))).stream(),
adds.filter(new IndexFilter(FiltersBuilder.IS_DIRECT_DEPENDENCY_OF, generic)).stream());
}
@Override
public Snapshot<Generic> filter(List<IndexFilter> filters) {
List<IndexFilter> filters_ = new ArrayList<>(filters);
filters_.add(new IndexFilter(FiltersBuilder.NOT_CONTAINED_IN_PARAM, new ArrayList<>(removes.toList())));
return Snapshot.super.filter(filters_);
}
@Override
public Observable<Generic> getAdds() {
return getDifferentialObservable().switchMap(diff -> diff.getAdds(generic).filter(g -> getCache().isAlive(g)))
.replay().refCount();
}
@Override
public Observable<Generic> getRemovals() {
return getDifferentialObservable().switchMap(diff -> diff.getRemovals(generic).filter(g -> !getCache().isAlive(g)))
.replay().refCount();
}
};
}
void apply() throws ConcurrencyControlException, OptimisticLockConstraintViolationException {
getSubDifferential().apply(removes, adds);
}
@Override
public void apply(Snapshot<Generic> removes, Snapshot<Generic> adds) throws ConcurrencyControlException, OptimisticLockConstraintViolationException {
this.removes.disableInvalidations();
this.adds.disableInvalidations();
for (Generic generic : removes)
unplug(generic);
for (Generic generic : adds)
plug(generic);
this.removes.enableInvalidations();
this.adds.enableInvalidations();
}
public void unApply() {
adds.forEach(add -> adds.remove(add));
removes.forEach(rem -> removes.remove(rem));
}
@Override
public long getTs() {
return getSubDifferential().getTs();
}
@Override
public Observable<Generic> getAdds(Generic generic) {
return Observable.merge(getSubDifferential().getAdds(generic),
adds.getFilteredAdds(generic::isDirectAncestorOf),
removes.getFilteredRemoves(generic::isDirectAncestorOf)).replay().refCount();
}
@Override
public Observable<Generic> getRemovals(Generic generic) {
return Observable.merge(getSubDifferential().getRemovals(generic),
removes.getFilteredAdds(generic::isDirectAncestorOf),
adds.getFilteredRemoves(generic::isDirectAncestorOf)).replay().refCount();
}
}