Repository navigation
Expand file tree
/
Copy pathQueues.java
More file actions
482 lines (383 loc) · 13.3 KB
/
Copy pathQueues.java
File metadata and controls
482 lines (383 loc) · 13.3 KB
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*
* Copyright (c) 2012 Jan Kotek
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mapdb;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* Various queue algorithms
*/
public final class Queues {
private Queues(){}
public static abstract class SimpleQueue<E> implements BlockingQueue<E> {
protected final boolean useLocks;
protected final ReentrantLock[] locks;
protected static final int TICK = 10*1000;
protected final Engine engine;
protected final Serializer<E> serializer;
protected final Atomic.Long head;
protected static class NodeSerializer<E> implements Serializer<Node<E>> {
private final Serializer<E> serializer;
public NodeSerializer(Serializer<E> serializer) {
this.serializer = serializer;
}
@Override
public void serialize(DataOutput out, Node<E> value) throws IOException {
if(value==Node.EMPTY) return;
DataOutput2.packLong(out,value.next);
serializer.serialize(out, value.value);
}
@Override
public Node<E> deserialize(DataInput in, int available) throws IOException {
if(available==0)return (Node<E>) Node.EMPTY;
return new Node<E>(DataInput2.unpackLong(in), serializer.deserialize(in,-1));
}
@Override
public int fixedSize() {
return -1;
}
}
protected final Serializer<Node<E>> nodeSerializer;
public SimpleQueue(Engine engine, Serializer<E> serializer, long headRecidRef, boolean useLocks) {
this.engine = engine;
this.serializer = serializer;
head = new Atomic.Long(engine,headRecidRef);
nodeSerializer = new NodeSerializer(serializer);
this.useLocks = useLocks;
if(useLocks){
locks = new ReentrantLock[CC.CONCURRENCY];
for(int i=0;i<locks.length;i++)
locks[i] = new ReentrantLock(CC.FAIR_LOCKS);
}else{
locks = null;
}
}
/**
* Closes underlying storage and releases all resources.
* Used mostly with temporary collections where engine is not accessible.
*/
public void close(){
engine.close();
}
@Override
public E peek() {
final long head2 = head.get();
if(useLocks)locks[Store.lockPos(head2)].lock();
try{
Node n = engine.get(head2,nodeSerializer);
if(n==Node.EMPTY)
return null; //empty queue
return (E) n.value;
}finally{
if(useLocks)locks[Store.lockPos(head2)].unlock();
}
}
@Override
public E poll() {
for(;;){
final long head2 = head.get();
if(useLocks)locks[Store.lockPos(head2)].lock();
try{
Node n = engine.get(head2,nodeSerializer);
if(n==Node.EMPTY)
return null; //empty queue
//update head
if(head.compareAndSet(head2,n.next)){
//updated fine, so we can take a value
if(useLocks){
engine.delete(head2,nodeSerializer);
}else{
engine.update(head2, (Node<E>) Node.EMPTY,nodeSerializer);
}
return (E) n.value;
}
}finally{
if(useLocks)locks[Store.lockPos(head2)].unlock();
}
}
}
protected static final class Node<E>{
protected static final Node<?> EMPTY = new Node(0L, null);
final protected long next;
final protected E value;
public Node(long next, E value) {
this.next = next;
this.value = value;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Node node = (Node) o;
if (next != node.next) return false;
if (value != null ? !value.equals(node.value) : node.value != null) return false;
return true;
}
@Override
public int hashCode() {
int result = (int) (next ^ (next >>> 32));
result = 31 * result + (value != null ? value.hashCode() : 0);
return result;
}
}
@Override
public void clear() {
while(!isEmpty())
poll();
}
@Override
public E remove() {
E ret = poll();
if(ret == null) throw new NoSuchElementException();
return ret;
}
@Override
public E element() {
E ret = peek();
if(ret == null) throw new NoSuchElementException();
return ret;
}
@Override
public boolean offer(E e) {
try {
return add(e);
}catch (IllegalStateException ee){
return false;
}
}
@Override
public void put(E e) throws InterruptedException {
while(!offer(e)){
Thread.sleep(0,TICK);
}
}
@Override
public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException {
if(offer(e)) return true;
long target = System.currentTimeMillis() + unit.toMillis(timeout);
while(target>=System.currentTimeMillis()){
if(offer(e))
return true;
Thread.sleep(0,TICK);
}
return false;
}
@Override
public E take() throws InterruptedException {
E e = poll();
while(e==null){
Thread.sleep(0,TICK);
e = poll();
}
return e;
}
@Override
public E poll(long timeout, TimeUnit unit) throws InterruptedException {
E e = poll();
if(e!=null) return e;
long target = System.currentTimeMillis() + unit.toMillis(timeout);
while(target>=System.currentTimeMillis()){
Thread.sleep(0,TICK);
e = poll();
if(e!=null)
return e;
}
return null;
}
@Override
public int drainTo(Collection<? super E> c) {
return drainTo(c,Integer.MAX_VALUE);
}
@Override
public int drainTo(Collection<? super E> c, int maxElements) {
int counter=0;
while(counter<maxElements){
E e = poll();
if(e==null)
return counter;
c.add(e);
counter++;
}
return counter;
}
@Override
public int remainingCapacity() {
return Integer.MAX_VALUE;
}
@Override
public boolean isEmpty() {
return peek()==null;
}
@Override
public int size() {
throw new UnsupportedOperationException();
}
@Override
public boolean contains(Object o) {
throw new UnsupportedOperationException();
}
@Override
public Iterator<E> iterator() {
throw new UnsupportedOperationException();
}
@Override
public Object[] toArray() {
throw new UnsupportedOperationException();
}
@Override
public <T> T[] toArray(T[] a) {
throw new UnsupportedOperationException();
}
@Override
public boolean remove(Object o) {
throw new UnsupportedOperationException();
}
@Override
public boolean containsAll(Collection<?> c) {
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(Collection<? extends E> c) {
throw new UnsupportedOperationException();
}
@Override
public boolean removeAll(Collection<?> c) {
throw new UnsupportedOperationException();
}
@Override
public boolean retainAll(Collection<?> c) {
throw new UnsupportedOperationException();
}
}
/**
* Last in first out lock-free queue
*
* @param <E>
*/
public static class Stack<E> extends SimpleQueue<E> {
public Stack(Engine engine, Serializer<E> serializer, long headerRecidRef, boolean useLocks) {
super(engine, serializer, headerRecidRef, useLocks);
}
@Override
public boolean add(E e) {
long head2 = head.get();
Node<E> n = new Node<E>(head2, e);
long recid = engine.put(n, nodeSerializer);
while(!head.compareAndSet(head2, recid)){
//failed to update head, so read new value and start over
head2 = head.get();
n = new Node<E>(head2, e);
engine.update(recid, n, nodeSerializer);
}
return true;
}
}
/**
* First in first out lock-free queue
*
* @param <E>
*/
public static class Queue<E> extends SimpleQueue<E> {
protected final Atomic.Long tail;
public Queue(Engine engine, Serializer<E> serializer, long headerRecid,
long nextTailRecid, boolean useLocks) {
super(engine, serializer,headerRecid,useLocks);
tail = new Atomic.Long(engine,nextTailRecid);
}
@Override
public boolean add(E e) {
long nextTail = engine.put((Node<E>) Node.EMPTY,nodeSerializer);
long tail2 = tail.get();
while(!tail.compareAndSet(tail2,nextTail)){
tail2 = tail.get();
}
//now we have tail2 just for us
Node<E> n = new Node(nextTail,e);
engine.update(tail2,n,nodeSerializer);
return true;
}
}
public static class CircularQueue<E> extends SimpleQueue<E> {
protected final Atomic.Long headInsert;
//TODO is there a way to implement this without global locks?
protected final Lock lock = new ReentrantLock(CC.FAIR_LOCKS);
protected final long size;
public CircularQueue(Engine engine, Serializer<E> serializer, long headRecid, long headInsertRecid, long size) {
super(engine, serializer, headRecid,false);
headInsert = new Atomic.Long(engine, headInsertRecid);
this.size = size;
}
@Override
public boolean add(Object o) {
lock.lock();
try{
long nRecid = headInsert.get();
Node<E> n = engine.get(nRecid, nodeSerializer);
n = new Node<E>(n.next, (E) o);
engine.update(nRecid, n, nodeSerializer);
headInsert.set(n.next);
//move 'poll' head if it points to currently replaced item
head.compareAndSet(nRecid, n.next);
return true;
}finally {
lock.unlock();
}
}
@Override
public void clear() {
// praise locking
lock.lock();
try {
for (int i = 0; i < size; i++) {
poll();
}
} finally {
lock.unlock();
}
}
@Override
public E poll() {
lock.lock();
try{
long nRecid = head.get();
Node<E> n = engine.get(nRecid, nodeSerializer);
engine.update(nRecid, new Node<E>(n.next, null), nodeSerializer);
head.set(n.next);
return n.value;
}finally {
lock.unlock();
}
}
@Override
public E peek() {
lock.lock();
try{
long nRecid = head.get();
Node<E> n = engine.get(nRecid, nodeSerializer);
return n.value;
}finally {
lock.unlock();
}
}
}
}