-
Notifications
You must be signed in to change notification settings - Fork 56
/
InternalOakMap.java
1220 lines (987 loc) · 41.9 KB
/
InternalOakMap.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
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2018 Oath Inc.
* Licensed under the terms of the Apache 2.0 license.
* Please see LICENSE file in the project root for terms.
*/
package com.oath.oak;
import java.nio.ByteBuffer;
import java.util.AbstractMap;
import java.util.Comparator;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.function.Function;
class InternalOakMap<K, V> {
/*-------------- Members --------------*/
final ConcurrentSkipListMap<Object, Chunk<K, V>> skiplist; // skiplist of chunks for fast navigation
private final AtomicReference<Chunk<K, V>> head;
private final ByteBuffer minKey;
private final Comparator<Object> comparator;
private final MemoryManager memoryManager;
private final AtomicInteger size;
private final OakSerializer<K> keySerializer;
private final OakSerializer<V> valueSerializer;
private final ThreadIndexCalculator threadIndexCalculator;
// The reference count is used to count the upper objects wrapping this internal map:
// OakMaps (including subMaps and Views) when all of the above are closed,
// his map can be closed and memory released.
private final AtomicInteger referenceCount = new AtomicInteger(1);
/*-------------- Constructors --------------*/
/**
* init with capacity = 2g
*/
InternalOakMap(
K minKey,
OakSerializer<K> keySerializer,
OakSerializer<V> valueSerializer,
Comparator comparator,
MemoryManager memoryManager,
int chunkMaxItems,
int chunkBytesPerItem,
ThreadIndexCalculator threadIndexCalculator) {
this.size = new AtomicInteger(0);
this.memoryManager = memoryManager;
this.keySerializer = keySerializer;
this.valueSerializer = valueSerializer;
this.comparator = comparator;
this.minKey = ByteBuffer.allocate(this.keySerializer.calculateSize(minKey));
this.minKey.position(0);
this.keySerializer.serialize(minKey, this.minKey);
this.skiplist = new ConcurrentSkipListMap<>(this.comparator);
Chunk<K, V> head = new Chunk<K, V>(this.minKey, null, this.comparator, memoryManager, chunkMaxItems,
this.size, keySerializer, valueSerializer, threadIndexCalculator);
this.skiplist.put(head.minKey, head); // add first chunk (head) into skiplist
this.head = new AtomicReference<>(head);
this.threadIndexCalculator = threadIndexCalculator;
}
/*-------------- Closable --------------*/
/**
* cleans off heap memory
*/
void close() {
int res = referenceCount.decrementAndGet();
// once reference count is zeroed, the map meant to be deleted and should not be used.
// reference count will never grow again
if (res == 0) {
memoryManager.close();
}
}
// yet another object started to refer to this internal map
void open() {
while (true) {
int res = referenceCount.get();
// once reference count is zeroed, the map meant to be deleted and should not be used.
// reference count should never grow again and the referral is not allowed
if (res == 0) {
throw new ConcurrentModificationException();
}
// although it is costly CAS is used here on purpose so we never increase
// zeroed reference count
if (referenceCount.compareAndSet(res, res + 1)) {
break;
}
}
}
/*-------------- size --------------*/
/**
* @return current off heap memory usage in bytes
*/
long memorySize() {
return memoryManager.allocated();
}
int entries() {
return size.get();
}
/*-------------- Methods --------------*/
/**
* finds and returns the chunk where key should be located, starting from given chunk
*/
private Chunk<K, V> iterateChunks(Chunk<K, V> c, Object key) {
// find chunk following given chunk (next)
Chunk<K, V> next = c.next.getReference();
// since skiplist isn't updated atomically in split/compaction, our key might belong in the next chunk
// we need to iterate the chunks until we find the correct one
while ((next != null) && (comparator.compare(next.minKey, key) <= 0)) {
c = next;
next = c.next.getReference();
}
return c;
}
private Rebalancer.RebalanceResult rebalance(Chunk<K, V> c) {
if (c == null) {
return null;
}
Rebalancer<K,V> rebalancer = new Rebalancer<>(c, comparator, true, memoryManager, keySerializer,
valueSerializer, threadIndexCalculator);
rebalancer = rebalancer.engageChunks(); // maybe we encountered a different rebalancer
// freeze all the engaged range.
// When completed, all update (put, next pointer update) operations on the engaged range
// will be redirected to help the rebalance procedure
rebalancer.freeze();
Rebalancer.RebalanceResult result = rebalancer.createNewChunks(); // split or compact
// if returned true then this thread was responsible for the creation of the new chunks
// and it inserted the put
// lists may be generated by another thread
List<Chunk<K, V>> newChunks = rebalancer.getNewChunks();
List<Chunk<K, V>> engaged = rebalancer.getEngagedChunks();
connectToChunkList(engaged, newChunks);
updateIndexAndNormalize(engaged, newChunks);
engaged.forEach(Chunk::release);
return result;
}
private void checkRebalance(Chunk c) {
if (c.shouldRebalance()) {
rebalance(c);
}
}
private void connectToChunkList(List<Chunk<K, V>> engaged, List<Chunk<K, V>> children) {
updateLastChild(engaged, children);
int countIterations = 0;
Chunk<K, V> firstEngaged = engaged.get(0);
// replace in linked list - we now need to find previous chunk to our chunk
// and CAS its next to point to c1, which is the same c1 for all threads who reached this point
// since prev might be marked (in compact itself) - we need to repeat this until successful
while (true) {
countIterations++;
assert (countIterations < 10000); // this loop is not supposed to be infinite
// start with first chunk (i.e., head)
Map.Entry<Object, Chunk<K, V>> lowerEntry = skiplist.lowerEntry(firstEngaged.minKey);
Chunk<K, V> prev = lowerEntry != null ? lowerEntry.getValue() : null;
Chunk<K, V> curr = (prev != null) ? prev.next.getReference() : null;
// if didn't succeed to find prev through the skiplist - start from the head
if (prev == null || curr != firstEngaged) {
prev = null;
curr = skiplist.firstEntry().getValue(); // TODO we can store&update head for a little efficiency
// iterate until found chunk or reached end of list
while ((curr != firstEngaged) && (curr != null)) {
prev = curr;
curr = curr.next.getReference();
}
}
// chunk is head, we need to "add it to the list" for linearization point
if (curr == firstEngaged && prev == null) {
this.head.compareAndSet(firstEngaged, children.get(0));
break;
}
// chunk is not in list (someone else already updated list), so we're done with this part
if ((curr == null) || (prev == null)) {
//TODO Never reached
break;
}
// if prev chunk is marked - it is deleted, need to help split it and then continue
if (prev.next.isMarked()) {
rebalance(prev);
continue;
}
// try to CAS prev chunk's next - from chunk (that we split) into c1
// c1 is the old chunk's replacement, and is already connected to c2
// c2 is already connected to old chunk's next - so all we need to do is this replacement
if ((prev.next.compareAndSet(firstEngaged, children.get(0), false, false)) ||
(!prev.next.isMarked())) {
// if we're successful, or we failed but prev is not marked - so it means someone else was successful
// then we're done with loop
break;
}
}
}
private void updateLastChild(List<Chunk<K, V>> engaged, List<Chunk<K, V>> children) {
Chunk<K, V> lastEngaged = engaged.get(engaged.size() - 1);
Chunk<K, V> nextToLast = lastEngaged.markAndGetNext(); // also marks last engaged chunk as deleted
Chunk<K, V> lastChild = children.get(children.size() - 1);
lastChild.next.compareAndSet(null, nextToLast, false, false);
}
private void updateIndexAndNormalize(List<Chunk<K, V>> engagedChunks, List<Chunk<K, V>> children) {
Iterator<Chunk<K, V>> iterEngaged = engagedChunks.iterator();
Iterator<Chunk<K, V>> iterChildren = children.iterator();
Chunk firstEngaged = iterEngaged.next();
Chunk firstChild = iterChildren.next();
// need to make the new chunks available, before removing old chunks
skiplist.replace(firstEngaged.minKey, firstEngaged, firstChild);
// remove all old chunks from index.
while (iterEngaged.hasNext()) {
Chunk engagedToRemove = iterEngaged.next();
skiplist.remove(engagedToRemove.minKey, engagedToRemove); // conditional remove is used
}
// now after removing old chunks we can start normalizing
firstChild.normalize();
// for simplicity - naive lock implementation
// can be implemented without locks using versions on next pointer in skiplist
while (iterChildren.hasNext()) {
Chunk childToAdd;
synchronized (childToAdd = iterChildren.next()) {
if (childToAdd.state() == Chunk.State.INFANT) { // make sure it wasn't add before
skiplist.putIfAbsent(childToAdd.minKey, childToAdd);
childToAdd.normalize();
}
// has a built in fence, so no need to add one here
}
}
}
private boolean rebalanceRemove(Chunk<K, V> c, K key) {
Rebalancer.RebalanceResult result = rebalance(c);
//TODO YONIGO - is it ok?
return result.success;
}
// Returns old handle if someone helped before pointToValue happened, or null if
private Handle finishAfterPublishing(Chunk.OpData opData, Chunk<K, V> c) {
// set pointer to value
Handle oldHandle = c.pointToValue(opData);
c.unpublish();
checkRebalance(c);
return oldHandle;
}
/*-------------- OakMap Methods --------------*/
V put(K key, V value, Function<ByteBuffer, V> transformer) {
if (key == null || value == null) {
throw new NullPointerException();
}
Chunk<K, V> c = findChunk(key); // find chunk matching key
Chunk.LookUp lookUp = c.lookUp(key);
if (lookUp != null && lookUp.handle != null) {
V v = (transformer != null) ? (V) lookUp.handle.transform(transformer) : null;
lookUp.handle.put(value, valueSerializer, memoryManager);
return v;
}
// if chunk is frozen or infant, we can't add to it
// we need to help rebalancer first, then proceed
Chunk.State state = c.state();
if (state == Chunk.State.INFANT) {
// the infant is already connected so rebalancer won't add this put
rebalance(c.creator());
put(key, value, transformer);
return null;
}
if (state == Chunk.State.FROZEN || state == Chunk.State.RELEASED) {
rebalance(c);
put(key, value, transformer);
return null;
}
int ei = -1;
int prevHi = -1;
if (lookUp != null) {
ei = lookUp.entryIndex;
assert ei > 0;
prevHi = lookUp.handleIndex;
}
if (ei == -1) {
ei = c.allocateEntryAndKey(key);
if (ei == -1) {
rebalance(c);
put(key, value, transformer);
return null;
}
int prevEi = c.linkEntry(ei, true, key);
if (prevEi != ei) {
ei = prevEi;
prevHi = c.getHandleIndex(prevEi);
}
}
int hi = c.allocateHandle();
if (hi == -1) {
rebalance(c);
put(key, value, transformer);
return null;
}
c.writeValue(hi, value); // write value in place
Chunk.OpData opData = new Chunk.OpData(Operation.PUT, ei, hi, prevHi, null);
// publish put
if (!c.publish()) {
c.freeHandle(hi);
rebalance(c);
put(key, value, transformer);
return null;
}
finishAfterPublishing(opData, c);
return null;
}
Result<V> putIfAbsent(K key, V value, Function<ByteBuffer, V> transformer) {
if (key == null || value == null) {
throw new NullPointerException();
}
Chunk c = findChunk(key); // find chunk matching key
Chunk.LookUp lookUp = c.lookUp(key);
if (lookUp != null && lookUp.handle != null) {
if (transformer == null) return Result.withFlag(false);
return Result.withValue(lookUp.handle.transform(transformer));
}
// if chunk is frozen or infant, we can't add to it
// we need to help rebalancer first, then proceed
Chunk.State state = c.state();
if (state == Chunk.State.INFANT) {
// the infant is already connected so rebalancer won't add this put
rebalance(c.creator());
return putIfAbsent(key, value, transformer);
}
if (state == Chunk.State.FROZEN || state == Chunk.State.RELEASED) {
rebalance(c);
return putIfAbsent(key, value, transformer);
}
int ei = -1;
int prevHi = -1;
if (lookUp != null) {
assert lookUp.handle == null;
ei = lookUp.entryIndex;
assert ei > 0;
prevHi = lookUp.handleIndex;
}
if (ei == -1) {
ei = c.allocateEntryAndKey(key);
if (ei == -1) {
rebalance(c);
return putIfAbsent(key, value, transformer);
}
int prevEi = c.linkEntry(ei, true, key);
if (prevEi != ei) {
prevHi = c.getHandleIndex(prevEi);
if (prevHi != -1 ) {
if (transformer == null) return Result.withFlag(false);
return Result.withValue(c.getHandle(prevEi).transform(transformer));
} else {
ei = prevEi;
}
}
}
int hi = c.allocateHandle();
if (hi == -1) {
rebalance(c);
return putIfAbsent(key, value, transformer);
}
c.writeValue(hi, value); // write value in place
Chunk.OpData opData = new Chunk.OpData(Operation.PUT_IF_ABSENT, ei, hi, prevHi, null);
// publish put
if (!c.publish()) {
c.freeHandle(hi);
rebalance(c);
return putIfAbsent(key, value, transformer);
}
Handle oldHandle = finishAfterPublishing(opData, c);
if (oldHandle != null) {
c.freeHandle(hi);
}
if (transformer == null) return Result.withFlag(oldHandle == null);
return Result.withValue((oldHandle != null) ? oldHandle.transform(transformer) : null);
}
boolean putIfAbsentComputeIfPresent(K key, V value, Consumer<OakWBuffer> computer) {
if (key == null || value == null || computer == null) {
throw new NullPointerException();
}
Chunk c = findChunk(key); // find chunk matching key
Chunk.LookUp lookUp = c.lookUp(key);
if (lookUp != null && lookUp.handle != null) {
if (lookUp.handle.compute(computer)) {
// compute was successful and handle wasn't found deleted; in case
// this handle was already found as deleted, continue to construct another handle
return false;
}
}
// if chunk is frozen or infant, we can't add to it
// we need to help rebalancer first, then proceed
Chunk.State state = c.state();
if (state == Chunk.State.INFANT) {
// the infant is already connected so rebalancer won't add this put
rebalance(c.creator());
return putIfAbsentComputeIfPresent(key, value, computer);
}
if (state == Chunk.State.FROZEN || state == Chunk.State.RELEASED) {
rebalance(c);
return putIfAbsentComputeIfPresent(key, value, computer);
}
// we come here when no key was found, which can be in 3 cases:
// 1. no entry in the linked list at all
// 2. entry in the linked list, but handle is not attached
// 3. entry in the linked list, handle attached, but handle is marked deleted
int ei = -1;
int prevHi = -1;
if (lookUp != null) {
ei = lookUp.entryIndex;
assert ei > 0;
prevHi = lookUp.handleIndex;
}
if (ei == -1) {
ei = c.allocateEntryAndKey(key);
if (ei == -1) {
rebalance(c);
return putIfAbsentComputeIfPresent(key, value, computer);
}
int prevEi = c.linkEntry(ei, true, key);
if (prevEi != ei) {
prevHi = c.getHandleIndex(prevEi);
if (prevHi != -1) {
if (c.getHandle(prevEi).compute(computer)) {
// compute was successful and handle wasn't found deleted; in case
// this handle was already found as deleted, continue to construct another handle
return false;
}
} else {
ei = prevEi;
}
}
}
int hi = c.allocateHandle();
if (hi == -1) {
rebalance(c);
return putIfAbsentComputeIfPresent(key, value, computer);
}
c.writeValue(hi, value); // write value in place
Chunk.OpData opData = new Chunk.OpData(Operation.COMPUTE, ei, hi, prevHi, computer);
// publish put
if (!c.publish()) {
c.freeHandle(hi);
rebalance(c);
return putIfAbsentComputeIfPresent(key, value, computer);
}
Handle ret = finishAfterPublishing(opData, c);
if (ret == null) {
return true;
} else {
// lost a race
c.freeHandle(hi);
return false;
}
}
V remove(K key, V oldValue, Function<ByteBuffer, V> transformer) {
if (key == null) {
throw new NullPointerException();
}
boolean logical = true; // when logical is false, means we have marked the handle as deleted
Handle prev = null;
V v = null;
while (true) {
Chunk c = findChunk(key); // find chunk matching key
Chunk.LookUp lookUp = c.lookUp(key);
if (lookUp != null && logical) {
prev = lookUp.handle; // remember previous handle
}
if (!logical && lookUp != null && prev != lookUp.handle) {
return v; // someone else used this entry
}
if (lookUp == null || lookUp.handle == null) {
return v; // there is no such key
}
if (logical) {
// we have marked this handle as deleted (successful remove)
V vv = (transformer != null) ? (V) lookUp.handle.transform(transformer) : null;
if (oldValue != null && !oldValue.equals(vv))
return null;
if (!lookUp.handle.remove(memoryManager)) {
// we didn't succeed to remove the handle was marked as deleted already
return null;
}
v = vv;
}
// if chunk is frozen or infant, we can't update it (remove deleted key, set handle index to -1)
// we need to help rebalancer first, then proceed
Chunk.State state = c.state();
if (state == Chunk.State.INFANT) {
// the infant is already connected so rebalancer won't add this put
rebalance(c.creator());
logical = false;
continue;
}
if (state == Chunk.State.FROZEN || state == Chunk.State.RELEASED) {
if (!rebalanceRemove(c, key)) {
logical = false;
continue;
}
return v;
}
assert lookUp.entryIndex > 0;
assert lookUp.handleIndex > 0;
Chunk.OpData opData = new Chunk.OpData(Operation.REMOVE, lookUp.entryIndex, -1, lookUp.handleIndex, null);
// publish
if (!c.publish()) {
if (!rebalanceRemove(c, key)) {
logical = false;
continue;
}
return v;
}
finishAfterPublishing(opData, c);
}
}
OakRBuffer get(K key) {
if (key == null) {
throw new NullPointerException();
}
Chunk<K, V> c = findChunk(key); // find chunk matching key
Chunk.LookUp lookUp = c.lookUp(key);
if (lookUp == null || lookUp.handle == null) {
return null;
}
return new OakRValueBufferImpl(lookUp.handle);
}
<T> T getValueTransformation(K key, Function<ByteBuffer, T> transformer) {
if (key == null || transformer == null) {
throw new NullPointerException();
}
Chunk<K, V> c = findChunk(key); // find chunk matching key
Chunk.LookUp lookUp = c.lookUp(key);
if (lookUp == null || lookUp.handle == null) {
return null;
}
T transformation = (T) lookUp.handle.transform(transformer);
return transformation;
}
<T> T getKeyTransformation(K key, Function<ByteBuffer, T> transformer) {
if (key == null || transformer == null) {
throw new NullPointerException();
}
Chunk<K, V> c = findChunk(key); // find chunk matching key
Chunk.LookUp lookUp = c.lookUp(key);
if (lookUp == null || lookUp.handle == null || lookUp.entryIndex == -1) {
return null;
}
ByteBuffer serializedKey = c.readKey(lookUp.entryIndex).slice();
return transformer.apply(serializedKey);
}
ByteBuffer getKey(K key) {
if (key == null) {
throw new NullPointerException();
}
Chunk<K, V> c = findChunk(key);
Chunk.LookUp lookUp = c.lookUp(key);
if (lookUp == null || lookUp.handle == null || lookUp.entryIndex == -1) {
return null;
}
return c.readKey(lookUp.entryIndex).slice();
}
ByteBuffer getMinKey() {
Chunk<K, V> c = skiplist.firstEntry().getValue();
return c.readMinKey().slice();
}
<T> T getMinKeyTransformation(Function<ByteBuffer, T> transformer) {
if (transformer == null) {
throw new NullPointerException();
}
Chunk<K, V> c = skiplist.firstEntry().getValue();
ByteBuffer serializedMinKey = c.readMinKey();
return (serializedMinKey != null) ? transformer.apply(serializedMinKey) : null;
}
ByteBuffer getMaxKey() {
Chunk<K, V> c = skiplist.lastEntry().getValue();
Chunk<K, V> next = c.next.getReference();
// since skiplist isn't updated atomically in split/compaction, the max key might belong in the next chunk
// we need to iterate the chunks until we find the last one
while (next != null) {
c = next;
next = c.next.getReference();
}
return c.readMaxKey().slice();
}
<T> T getMaxKeyTransformation(Function<ByteBuffer, T> transformer) {
if (transformer == null) {
throw new NullPointerException();
}
Chunk<K, V> c = skiplist.lastEntry().getValue();
Chunk<K, V> next = c.next.getReference();
// since skiplist isn't updated atomically in split/compaction, the max key might belong in the next chunk
// we need to iterate the chunks until we find the last one
while (next != null) {
c = next;
next = c.next.getReference();
}
ByteBuffer serializedMaxKey = c.readMaxKey();
return (serializedMaxKey != null) ? transformer.apply(serializedMaxKey) : null;
}
boolean computeIfPresent(K key, Consumer<OakWBuffer> computer) {
if (key == null || computer == null) {
throw new NullPointerException();
}
Chunk c = findChunk(key); // find chunk matching key
Chunk.LookUp lookUp = c.lookUp(key);
if (lookUp == null || lookUp.handle == null) return false;
return lookUp.handle.compute(computer);
}
// encapsulates finding of the chunk in the skip list and later chunk list traversal
private Chunk findChunk(Object key) {
Chunk c = skiplist.floorEntry(key).getValue();
c = iterateChunks(c, key);
return c;
}
V replace(K key, V value, Function<ByteBuffer, V> valueDeserializeTransformer) {
Chunk<K, V> c = findChunk(key); // find chunk matching key
Chunk.LookUp lookUp = c.lookUp(key);
if (lookUp == null || lookUp.handle == null)
return null;
Function<ByteBuffer, V> replaceTransform = bb -> {
// mutatingTransform guarantees that this is write-synchronous handle is not deleted
V v = valueDeserializeTransformer.apply(bb);
lookUp.handle.put(value, valueSerializer, memoryManager);
return v;
};
// will return null if handle was deleted between prior lookup and the next call
return (V) lookUp.handle.mutatingTransform(replaceTransform);
}
boolean replace(K key, V oldValue, V newValue, Function<ByteBuffer, V> valueDeserializeTransformer) {
Chunk<K, V> c = findChunk(key); // find chunk matching key
Chunk.LookUp lookUp = c.lookUp(key);
if (lookUp == null || lookUp.handle == null)
return false;
Function<ByteBuffer, Boolean> replaceTransform = bb -> {
// mutatingTransform guarantees that this is write-synchronous handle is not deleted
V v = valueDeserializeTransformer.apply(bb);
if (!v.equals(oldValue))
return false;
lookUp.handle.put(newValue, valueSerializer, memoryManager);
return true;
};
// res can be null if handle was deleted between lookup and the next call
Boolean res = (Boolean) lookUp.handle.mutatingTransform(replaceTransform);
return (res != null) ? res : false;
}
public Map.Entry<K, V> lowerEntry(K key) {
Map.Entry<Object, Chunk<K, V>> lowerChunkEntry = skiplist.lowerEntry(key);
if (lowerChunkEntry == null) {
/* we were looking for the minimal key */
return new AbstractMap.SimpleImmutableEntry<>(null, null);
}
Chunk c = lowerChunkEntry.getValue();
/* Iterate chunk to find prev(key) */
Chunk.AscendingIter chunkIter = c.ascendingIter();
int prevIndex = chunkIter.next();
while (chunkIter.hasNext()) {
int nextIndex = chunkIter.next();
int cmp = comparator.compare(c.readKey(nextIndex), key);
if (cmp >= 0) {
break;
}
prevIndex = nextIndex;
}
/* Edge case: we're looking for the lowest key in the map and it's still greater than minkey
(in which case prevKey == key) */
ByteBuffer prevKey = c.readKey(prevIndex);
if (comparator.compare(prevKey, key) == 0) {
return new AbstractMap.SimpleImmutableEntry<>(null, null);
}
c.getHandle(prevIndex);
return new AbstractMap.SimpleImmutableEntry<>(
keySerializer.deserialize(prevKey),
valueSerializer.deserialize(c.getHandle(prevIndex).getSlicedReadOnlyByteBuffer()));
}
/*-------------- Iterators --------------*/
private static class IteratorState<K, V> {
private Chunk<K, V> chunk;
private Chunk.ChunkIter chunkIter;
private int index;
public void set(Chunk<K, V> chunk, Chunk.ChunkIter chunkIter, int index) {
this.chunk = chunk;
this.chunkIter = chunkIter;
this.index = index;
}
private IteratorState(Chunk<K, V> nextChunk, Chunk.ChunkIter nextChunkIter, int nextIndex) {
this.chunk = nextChunk;
this.chunkIter = nextChunkIter;
this.index = nextIndex;
}
public Chunk<K, V> getChunk() {
return chunk;
}
public Chunk.ChunkIter getChunkIter() {
return chunkIter;
}
public int getIndex() {
return index;
}
public static <K, V> IteratorState<K, V> newInstance(Chunk<K, V> nextChunk, Chunk.ChunkIter nextChunkIter) {
return new IteratorState<>(nextChunk, nextChunkIter, Chunk.NONE);
}
}
/**
* Base of iterator classes:
*/
abstract class Iter<T> implements Iterator<T> {
private K lo;
/**
* upper bound key, or null if to end
*/
private K hi;
/**
* inclusion flag for lo
*/
private boolean loInclusive;
/**
* inclusion flag for hi
*/
private boolean hiInclusive;
/**
* direction
*/
private final boolean isDescending;
/**
* the next node to return from next();
*/
private IteratorState<K, V> state;
/**
* Initializes ascending iterator for entire range.
*/
Iter(K lo, boolean loInclusive, K hi, boolean hiInclusive, boolean isDescending) {
if (lo != null && hi != null &&
comparator.compare(lo, hi) > 0)
throw new IllegalArgumentException("inconsistent range");
this.lo = lo;
this.loInclusive = loInclusive;
this.hi = hi;
this.hiInclusive = hiInclusive;
this.isDescending = isDescending;
initState(isDescending, lo, loInclusive, hi, hiInclusive);
}
boolean tooLow(Object key) {
int c;
return (lo != null && ((c = comparator.compare(key, lo)) < 0 ||
(c == 0 && !loInclusive)));
}
boolean tooHigh(Object key) {
int c;
return (hi != null && ((c = comparator.compare(key, hi)) > 0 ||
(c == 0 && !hiInclusive)));
}
boolean inBounds(Object key) {
if (!isDescending) {
return !tooHigh(key);
} else {
return !tooLow(key);
}
}
public final boolean hasNext() {
return (state != null);
}
private void initAfterRebalance() {
//TODO - refactor to use ByeBuffer without deserializing.
K nextKey = keySerializer.deserialize(state.getChunk().readKey(state.getIndex()).slice());
if (isDescending) {
hiInclusive = true;
hi = nextKey;
} else {
loInclusive = true;
lo = nextKey;
}
// Update the state to point to last returned key.
initState(isDescending, lo, loInclusive, hi, hiInclusive);
if (state == null) {
throw new ConcurrentModificationException();
}
}
// the actual next()
abstract public T next();
/**
* Advances next to higher entry.
* Return previous index
*/
Map.Entry<ByteBuffer, Handle> advance() {
if (state == null) {
throw new NoSuchElementException();
}
Chunk.State chunkState = state.getChunk().state();
if (chunkState == Chunk.State.RELEASED) {
initAfterRebalance();
}
ByteBuffer bb = state.getChunk().readKey(state.getIndex()).slice();
Handle currentHandle = state.getChunk().getHandle(state.getIndex());
advanceState();
return new AbstractMap.SimpleImmutableEntry<>(bb, currentHandle);
}
private void initState(boolean isDescending, K lowerBound, boolean lowerInclusive,
K upperBound, boolean upperInclusive) {
Chunk.ChunkIter nextChunkIter = null;
Chunk<K, V> nextChunk;
if (!isDescending) {
if (lowerBound != null)
nextChunk = skiplist.floorEntry(lowerBound).getValue();
else
nextChunk = skiplist.floorEntry(minKey).getValue();
if (nextChunk != null) {
nextChunkIter = lowerBound != null ?
nextChunk.ascendingIter(lowerBound, lowerInclusive) : nextChunk.ascendingIter();
} else {
state = null;
return;
}
} else {
nextChunk = upperBound != null ? skiplist.floorEntry(upperBound).getValue()
: skiplist.lastEntry().getValue();
if (nextChunk != null) {
nextChunkIter = upperBound != null ?
nextChunk.descendingIter(upperBound, upperInclusive) : nextChunk.descendingIter();
} else {
state = null;
return;
}