-
Notifications
You must be signed in to change notification settings - Fork 5
/
AbstractTsDependencies.java
218 lines (178 loc) · 5.39 KB
/
AbstractTsDependencies.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
package org.genericsystem.kernel;
import java.util.Spliterators;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.genericsystem.api.core.Filters;
import org.genericsystem.api.core.Filters.IndexFilter;
import org.genericsystem.common.AbstractIterator;
import org.genericsystem.common.Generic;
import org.genericsystem.kernel.AbstractServer.RootServerHandler;
/**
* @author Nicolas Feybesse
*
*/
abstract class AbstractTsDependencies {
private static interface Index {
public void add(Generic generic);
public boolean remove(Generic generic);
public Stream<Generic> stream(long ts);
}
private class IndexImpl implements Index {
private Node head = null;
private Node tail = null;
private final long ts;
private final IndexFilter<Generic> filter;
IndexImpl(IndexFilter<Generic> filter, long ts) {
this.ts = ts;
this.filter = filter;
if (!Filters.NO_FILTER.equals(filter))
indexs.get(Filters.NO_FILTER, ts).stream(ts).forEach(generic -> {
if (filter.test(generic))
add(generic);
});
}
@Override
public void add(Generic generic) {
assert generic != null;
// assert getLifeManager().isWriteLockedByCurrentThread();
Node newNode = new Node(generic);
if (head == null)
head = newNode;
else
tail.next = newNode;
tail = newNode;
Generic result = map.put(generic, generic);
// assert result == null;
}
@Override
public boolean remove(Generic generic) {
assert generic != null : "generic is null";
assert head != null : "head is null";
Node currentNode = head;
Generic currentContent = currentNode.content;
if (generic.equals(currentContent)) {
Node next = currentNode.next;
head = next != null ? next : null;
return true;
}
Node nextNode = currentNode.next;
while (nextNode != null) {
Generic nextGeneric = nextNode.content;
Node nextNextNode = nextNode.next;
if (generic.equals(nextGeneric)) {
nextNode.content = null;
if (nextNextNode == null)
tail = currentNode;
currentNode.next = nextNextNode;
map.remove(generic);
return true;
}
currentNode = nextNode;
nextNode = nextNextNode;
}
return false;
}
@Override
public Stream<Generic> stream(long ts) {
if (ts < this.ts)
indexs.updateIndex(filter, ts);
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(new InternalIterator(ts), 0), false);
}
private class InternalIterator extends AbstractIterator<Node, Generic> {
private final long ts;
private InternalIterator(long iterationTs) {
ts = iterationTs;
}
@Override
protected void advance() {
for (;;) {
Node nextNode = (next == null) ? head : next.next;
if (nextNode == null) {
LifeManager lifeManager = getLifeManager();
lifeManager.readLock();
try {
nextNode = (next == null) ? head : next.next;
if (nextNode == null) {
next = null;
lifeManager.atomicAdjustLastReadTs(ts);
return;
}
} finally {
lifeManager.readUnlock();
}
}
next = nextNode;
Generic content = next.content;
if (content != null && ((RootServerHandler) content.getProxyHandler()).getLifeManager().isAlive(ts))
break;
}
}
@Override
protected Generic project() {
return next.content;
}
}
}
private final ConcurrentHashMap<Generic, Generic> map = new ConcurrentHashMap<>();
public abstract LifeManager getLifeManager();
public Generic get(Generic generic, long ts) {
Generic result = map.get(generic);// this no lock read requires a concurrent hash map
if (result == null) {
LifeManager lifeManager = getLifeManager();
lifeManager.readLock();
try {
result = map.get(generic);
lifeManager.atomicAdjustLastReadTs(ts);
} finally {
lifeManager.readUnlock();
}
}
if (result != null && ((RootServerHandler) result.getProxyHandler()).getLifeManager().isAlive(ts))
return result;
return null;
}
public Stream<Generic> stream(long ts, IndexFilter<Generic> filter) {
return indexs.get(filter, ts).stream(ts);
}
public Stream<Generic> stream(long ts) {
return indexs.get(Filters.NO_FILTER, ts).stream(ts);
}
private final ExtendedMap indexs = new ExtendedMap() {
private static final long serialVersionUID = 1067462646727512744L;
{
put((IndexFilter<Generic>) Filters.NO_FILTER, new IndexImpl((IndexFilter<Generic>) Filters.NO_FILTER, 0));
}
};
private class ExtendedMap extends ConcurrentHashMap<IndexFilter<Generic>, Index> {
private static final long serialVersionUID = 2969395358619269789L;
public Index get(Object key, long ts) {
return super.computeIfAbsent((IndexFilter<Generic>) key, k -> new IndexImpl(k, ts));
};
public Index updateIndex(IndexFilter<Generic> key, long ts) {
return super.put(key, new IndexImpl(key, ts));
}
}
public void add(Generic generic) {
indexs.entrySet().forEach(entry -> {
if (entry.getKey().test(generic)) {
entry.getValue().add(generic);
}
});
}
public boolean remove(Generic generic) {
boolean[] result = new boolean[] { false };
indexs.entrySet().forEach(entry -> {
if (entry.getKey().test(generic))
result[0] = result[0] | entry.getValue().remove(generic);
});
return result[0];
}
private static class Node {
Generic content;
Node next;
private Node(Generic content) {
this.content = content;
}
}
}