Repository navigation
Expand file tree
/
Copy pathBTreeKeySerializer.java
More file actions
2113 lines (1797 loc) · 65.4 KB
/
Copy pathBTreeKeySerializer.java
File metadata and controls
2113 lines (1797 loc) · 65.4 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
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
package org.mapdb;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Comparator;
import java.util.UUID;
/**
* Custom serializer for BTreeMap keys which enables [Delta encoding](https://en.wikipedia.org/wiki/Delta_encoding).
*
* Keys in BTree Nodes are sorted, this enables number of tricks to save disk space.
* For example for numbers we may store only difference between subsequent numbers, for string we can only take suffix, etc...
*
* @param <KEY> type of key
* @param <KEYS> type of object which holds multiple keys (
*/
public abstract class BTreeKeySerializer<KEY,KEYS>{
/**
* Serialize keys from single BTree Node.
*
* @param out output stream where to put ata
* @param keys An object which represents keys
*
* @throws IOException in case of an writting error
*/
public abstract void serialize(DataOutput out, KEYS keys) throws IOException;
/**
* Deserializes keys for single BTree Node. To
*
* @param in input stream to read data from
* @param nodeSize number of keys in deserialized node
* @return an object which represents keys
*
* @throws IOException in case of an reading error
*/
public abstract KEYS deserialize(DataInput in, int nodeSize) throws IOException;
public abstract int compare(KEYS keys, int pos1, int pos2);
public abstract int compare(KEYS keys, int pos, KEY key);
public boolean compareIsSmaller(KEYS keys, int pos, KEY key) {
//TODO override in Strings and other implementations
return compare(keys,pos,key)<0;
}
public abstract KEY getKey(KEYS keys, int pos);
public static final BTreeKeySerializer<Object, Object[]> BASIC = new BTreeKeySerializer.BasicKeySerializer(Serializer.BASIC, Fun.COMPARATOR);
public abstract Comparator<?> comparator();
public abstract KEYS emptyKeys();
public abstract int length(KEYS keys);
/** expand keys array by one and put {@code newKey} at position {@code pos}
*
* @param keys array of keys to put new key into
* @param pos of new key
* @param newKey new key to insert
*
* @return array of keys with new key at given position
*/
public abstract KEYS putKey(KEYS keys, int pos, KEY newKey);
public abstract KEYS copyOfRange(KEYS keys, int from, int to);
public abstract KEYS deleteKey(KEYS keys, int pos);
/**
* Find the first children node with a key equal or greater than the given key.
* If all items are smaller it returns {@code keyser.length(keys)}
*
* @param node BTree Node to find position in
* @param key key whose position needs to be find
* @return position of key in node
*/
public int findChildren(final BTreeMap.BNode node, final Object key) {
KEYS keys = (KEYS) node.keys;
int keylen = length(keys);
int left = 0;
int right = keylen;
int middle;
//$DELAY$
// binary search
for(;;) {
//$DELAY$
middle = (left + right) / 2;
if(middle==keylen)
return middle+node.leftEdgeInc(); //null is positive infinitive
if (compareIsSmaller(keys,middle, (KEY) key)) {
left = middle + 1;
} else {
right = middle;
}
if (left >= right) {
return right+node.leftEdgeInc();
}
}
}
public int findChildren2(final BTreeMap.BNode node, final Object key) {
KEYS keys = (KEYS) node.keys;
int keylen = length(keys);
int left = 0;
int right = keylen;
int comp;
int middle;
//$DELAY$
// binary search
while (true) {
//$DELAY$
middle = (left + right) / 2;
if(middle==keylen)
return -1-(middle+node.leftEdgeInc()); //null is positive infinitive
comp = compare(keys, middle, (KEY) key);
if(comp==0){
//try one before last, in some cases it might be duplicate of last
if(!node.isRightEdge() && middle==keylen-1 && middle>0
&& compare(keys,middle-1,(KEY)key)==0){
middle--;
}
return middle+node.leftEdgeInc();
} else if ( comp< 0) {
left = middle +1;
} else {
right = middle;
}
if (left >= right) {
return -1-(right+node.leftEdgeInc());
}
}
}
public abstract KEYS arrayToKeys(Object[] keys);
public Object[] keysToArray(KEYS keys) {
//$DELAY$
Object[] ret = new Object[length(keys)];
for (int i = 0; i <ret.length; i++) {
ret[i] = getKey(keys,i);
}
return ret;
}
public boolean isTrusted() {
return false;
}
/**
* Basic Key Serializer which just writes data without applying any compression.
* Is used by default if no other Key Serializer is specified.
*/
public static final class BasicKeySerializer extends BTreeKeySerializer<Object, Object[]> implements Serializable {
private static final long serialVersionUID = 1654710710946309279L;
protected final Serializer serializer;
protected final Comparator comparator;
public BasicKeySerializer(Serializer serializer, Comparator comparator) {
if(serializer == null || comparator == null)
throw new NullPointerException();
this.serializer = serializer;
this.comparator = comparator;
}
/** used for deserialization*/
BasicKeySerializer(SerializerBase serializerBase, DataInput is, SerializerBase.FastArrayList<Object> objectStack) throws IOException {
objectStack.add(this);
serializer = (Serializer) serializerBase.deserialize(is,objectStack);
comparator = (Comparator) serializerBase.deserialize(is,objectStack);
if(serializer == null || comparator == null)
throw new NullPointerException();
}
@Override
public void serialize(DataOutput out, Object[] keys) throws IOException {
for(Object o:keys){
//$DELAY$
serializer.serialize(out, o);
}
}
@Override
public Object[] deserialize(DataInput in, int nodeSize) throws IOException {
//$DELAY$
Object[] keys = new Object[nodeSize];
for(int i=0;i<keys.length;i++) {
//$DELAY$
keys[i] = serializer.deserialize(in, -1);
}
return keys;
}
@Override
public int compare(Object[] keys, int pos1, int pos2) {
return comparator.compare(keys[pos1], keys[pos2]);
}
@Override
public int compare(Object[] keys, int pos, Object key) {
return comparator.compare(keys[pos],key);
}
@Override
public Object getKey(Object[] keys, int pos) {
return keys[pos];
}
@Override
public Comparator<?> comparator() {
return comparator;
}
@Override
public Object[] emptyKeys() {
return new Object[0];
}
@Override
public int length(Object[] keys) {
return keys.length;
}
@Override
public Object[] putKey(Object[] keys, int pos, Object newKey) {
return BTreeMap.arrayPut(keys, pos, newKey);
}
@Override
public Object[] arrayToKeys(Object[] keys) {
return keys;
}
@Override
public Object[] copyOfRange(Object[] keys, int from, int to) {
return Arrays.copyOfRange(keys,from,to);
}
@Override
public Object[] deleteKey(Object[] keys, int pos) {
Object[] keys2 = new Object[keys.length-1];
System.arraycopy(keys,0,keys2, 0, pos);
//$DELAY$
System.arraycopy(keys, pos+1, keys2, pos, keys2.length-pos);
//$DELAY$
return keys2;
}
}
/**
* Applies delta packing on {@code java.lang.Long}.
* Difference between consequential numbers is also packed itself, so for small diffs it takes only single byte per
* number.
*/
public static final BTreeKeySerializer<Long,long[]> LONG = new BTreeKeySerializer<Long,long[]>() {
@Override
public void serialize(DataOutput out, long[] keys) throws IOException {
DataIO.DataOutputByteArray out2 = (DataIO.DataOutputByteArray) out; //TODO fallback option if cast fails
long prev = keys[0];
out2.packLong(prev);
for(int i=1;i<keys.length;i++){
long curr = keys[i];
//$DELAY$
out2.packLong(curr-prev);
prev = curr;
}
}
@Override
public long[] deserialize(DataInput in, int nodeSize) throws IOException {
DataIO.DataInputInternal in2 = (DataIO.DataInputInternal) in; //TODO fallback option if cast fails
return in2.unpackLongArrayDeltaCompression(nodeSize);
// long[] ret = new long[nodeSize];
// long prev = 0 ;
// for(int i = 0; i<nodeSize; i++){
// //$DELAY$
// prev += in2.unpackLong();
// ret[i] = prev;
// }
// return ret;
}
@Override
public int compare(long[] keys, int pos1, int pos2) {
return Fun.compareLong(keys[pos1], keys[pos2]);
}
@Override
public int compare(long[] keys, int pos, Long preDigestedKey) {
return Fun.compareLong(keys[pos], preDigestedKey);
}
@Override
public boolean compareIsSmaller(long[] keys, int pos, Long key) {
return keys[pos]<key;
}
@Override
public Long getKey(long[] keys, int pos) {
return new Long(keys[pos]);
}
@Override
public Comparator<?> comparator() {
return Fun.COMPARATOR;
}
@Override
public long[] emptyKeys() {
return new long[0];
}
@Override
public int length(long[] keys) {
return keys.length;
}
@Override
public long[] putKey(long[] keys, int pos, Long newKey) {
final long[] ret = Arrays.copyOf(keys,keys.length+1);
if(pos<keys.length){
System.arraycopy(keys,pos,ret,pos+1,keys.length-pos);
}
ret[pos] = newKey;
return ret;
}
@Override
public long[] copyOfRange(long[] keys, int from, int to) {
return Arrays.copyOfRange(keys,from,to);
}
@Override
public long[] arrayToKeys(Object[] keys) {
long[] ret = new long[keys.length];
for(int i=keys.length-1;i>=0;i--) {
//$DELAY$
ret[i] = (Long) keys[i];
}
return ret;
}
@Override
public long[] deleteKey(long[] keys, int pos) {
long[] keys2 = new long[keys.length-1];
System.arraycopy(keys,0,keys2, 0, pos);
//$DELAY$
System.arraycopy(keys, pos+1, keys2, pos, keys2.length-pos);
//$DELAY$
return keys2;
}
@Override
public final int findChildren(final BTreeMap.BNode node, final Object key) {
long[] keys = (long[]) node.keys;
long key2 = (Long)key;
int left = 0;
int right = keys.length;
int middle;
//$DELAY$
// binary search
for(;;) {
//$DELAY$
middle = (left + right) / 2;
if(middle==keys.length)
return middle+node.leftEdgeInc(); //null is positive infinitive
if (keys[middle]<key2) {
left = middle + 1;
} else {
right = middle;
}
if (left >= right) {
return right+node.leftEdgeInc();
}
}
}
@Override
public final int findChildren2(final BTreeMap.BNode node, final Object key) {
long[] keys = (long[]) node.keys;
long key2 = (Long)key;
int left = 0;
int right = keys.length;
int middle;
//$DELAY$
// binary search
while (true) {
//$DELAY$
middle = (left + right) / 2;
if(middle==keys.length)
return -1-(middle+node.leftEdgeInc()); //null is positive infinitive
if(keys[middle]==key2){
//try one before last, in some cases it might be duplicate of last
if(!node.isRightEdge() && middle==keys.length-1 && middle>0
&& keys[middle-1]==key2){
middle--;
}
return middle+node.leftEdgeInc();
} else if ( keys[middle]<key2) {
left = middle +1;
} else {
right = middle;
}
if (left >= right) {
return -1-(right+node.leftEdgeInc());
}
}
}
};
/**
* @deprecated use {@link org.mapdb.BTreeKeySerializer#LONG}
*/
public static final BTreeKeySerializer ZERO_OR_POSITIVE_LONG = LONG;
/**
* Applies delta packing on {@code java.lang.Integer}.
* Difference between consequential numbers is also packed itself, so for small diffs it takes only single byte per
* number.
*/
public static final BTreeKeySerializer<Integer,int[]> INTEGER = new BTreeKeySerializer<Integer,int[]>() {
@Override
public void serialize(DataOutput out, int[] keys) throws IOException {
int prev = keys[0];
DataIO.packIntBigger(out, prev);
//$DELAY$
for(int i=1;i<keys.length;i++){
int curr = keys[i];
//$DELAY$
DataIO.packInt(out, curr - prev);
prev = curr;
}
}
@Override
public int[] deserialize(DataInput in, int nodeSize) throws IOException {
int[] ret = new int[nodeSize];
int prev = 0 ;
for(int i = 0; i<nodeSize; i++){
//$DELAY$
prev += DataIO.unpackInt(in);
ret[i] = prev;
}
return ret;
}
@Override
public int compare(int[] keys, int pos1, int pos2) {
return Fun.compareInt(keys[pos1], keys[pos2]);
}
@Override
public int compare(int[] keys, int pos, Integer preDigestedKey) {
return Fun.compareInt(keys[pos], preDigestedKey);
}
@Override
public boolean compareIsSmaller(int[] keys, int pos, Integer key) {
return keys[pos]<key;
}
@Override
public Integer getKey(int[] keys, int pos) {
return new Integer(keys[pos]);
}
@Override
public Comparator<?> comparator() {
return Fun.COMPARATOR;
}
@Override
public int[] emptyKeys() {
return new int[0];
}
@Override
public int length(int[] keys) {
return keys.length;
}
@Override
public int[] putKey(int[] keys, int pos, Integer newKey) {
final int[] ret = Arrays.copyOf(keys,keys.length+1);
//$DELAY$
if(pos<keys.length){
System.arraycopy(keys,pos,ret,pos+1,keys.length-pos);
}
ret[pos] = newKey;
return ret;
}
@Override
public int[] copyOfRange(int[] keys, int from, int to) {
return Arrays.copyOfRange(keys,from,to);
}
@Override
public int[] arrayToKeys(Object[] keys) {
int[] ret = new int[keys.length];
for(int i=keys.length-1;i>=0;i--)
//$DELAY$
ret[i] = (Integer)keys[i];
return ret;
}
@Override
public int[] deleteKey(int[] keys, int pos) {
int[] keys2 = new int[keys.length-1];
System.arraycopy(keys,0,keys2, 0, pos);
//$DELAY$
System.arraycopy(keys, pos+1, keys2, pos, keys2.length-pos);
return keys2;
}
@Override
public final int findChildren(final BTreeMap.BNode node, final Object key) {
int[] keys = (int[]) node.keys;
int key2 = (Integer)key;
int left = 0;
int right = keys.length;
int middle;
//$DELAY$
// binary search
for(;;) {
//$DELAY$
middle = (left + right) / 2;
if(middle==keys.length)
return middle+node.leftEdgeInc(); //null is positive infinitive
if (keys[middle]<key2) {
left = middle + 1;
} else {
right = middle;
}
if (left >= right) {
return right+node.leftEdgeInc();
}
}
}
@Override
public final int findChildren2(final BTreeMap.BNode node, final Object key) {
int[] keys = (int[]) node.keys;
int key2 = (Integer)key;
int left = 0;
int right = keys.length;
int middle;
//$DELAY$
// binary search
while (true) {
//$DELAY$
middle = (left + right) / 2;
if(middle==keys.length)
return -1-(middle+node.leftEdgeInc()); //null is positive infinitive
if(keys[middle]==key2){
//try one before last, in some cases it might be duplicate of last
if(!node.isRightEdge() && middle==keys.length-1 && middle>0
&& keys[middle-1]==key2){
middle--;
}
return middle+node.leftEdgeInc();
} else if ( keys[middle]<key2) {
left = middle +1;
} else {
right = middle;
}
if (left >= right) {
return -1-(right+node.leftEdgeInc());
}
}
}
};
/**
* @deprecated use {@link org.mapdb.BTreeKeySerializer#INTEGER}
*/
public static final BTreeKeySerializer ZERO_OR_POSITIVE_INT = INTEGER;
public static final BTreeKeySerializer ARRAY2 = new ArrayKeySerializer(
new Comparator[]{Fun.COMPARATOR,Fun.COMPARATOR},
new Serializer[]{Serializer.BASIC, Serializer.BASIC}
);
public static final BTreeKeySerializer ARRAY3 = new ArrayKeySerializer(
new Comparator[]{Fun.COMPARATOR,Fun.COMPARATOR,Fun.COMPARATOR},
new Serializer[]{Serializer.BASIC, Serializer.BASIC, Serializer.BASIC}
);
public static final BTreeKeySerializer ARRAY4 = new ArrayKeySerializer(
new Comparator[]{Fun.COMPARATOR,Fun.COMPARATOR,Fun.COMPARATOR,Fun.COMPARATOR},
new Serializer[]{Serializer.BASIC, Serializer.BASIC, Serializer.BASIC, Serializer.BASIC}
);
public final static class ArrayKeySerializer extends BTreeKeySerializer<Object[],Object[]> implements Serializable{
private static final long serialVersionUID = 998929894238939892L;
protected final int tsize;
protected final Comparator[] comparators;
protected final Serializer[] serializers;
protected final Comparator comparator;
public ArrayKeySerializer(Comparator[] comparators, Serializer[] serializers) {
if(comparators.length!=serializers.length){
throw new IllegalArgumentException("array sizes do not match");
}
this.tsize = comparators.length;
this.comparators = comparators;
this.serializers = serializers;
this.comparator = new Fun.ArrayComparator(comparators);
}
/* used for deserialization, extra is to avoid argument collision */
public ArrayKeySerializer(SerializerBase serializerBase, DataInput is,
SerializerBase.FastArrayList<Object> objectStack) throws IOException {
objectStack.add(this);
tsize = DataIO.unpackInt(is);
comparators = new Comparator[tsize];
serializers = new Serializer[tsize];
for(int i=0;i<tsize;i++){
comparators[i] = (Comparator) serializerBase.deserialize(is,objectStack);
serializers[i] = (Serializer) serializerBase.deserialize(is,objectStack);
}
this.comparator = new Fun.ArrayComparator(comparators);
}
@Override
public void serialize(DataOutput out, Object[] keys) throws IOException {
int[] counts = new int[tsize-1];
//$DELAY$
for(int i=0;i<keys.length;i+=tsize){
for(int j=0;j<tsize-1;j++){
//$DELAY$
if(counts[j]==0){
Object orig = keys[i+j];
serializers[j].serialize(out,orig);
counts[j]=1;
while(i+j+counts[j]*tsize<keys.length &&
comparators[j].compare(orig,keys[i+j+counts[j]*tsize])==0){
counts[j]++;
}
DataIO.packInt(out,counts[j]);
}
}
//write last value from tuple
serializers[serializers.length-1].serialize(out,keys[i+tsize-1]);
//decrement all
//$DELAY$
for(int j=counts.length-1;j>=0;j--){
counts[j]--;
}
}
}
@Override
public Object[] deserialize(DataInput in, int nodeSize) throws IOException {
Object[] ret = new Object[nodeSize*tsize];
Object[] curr = new Object[tsize];
int[] counts = new int[tsize-1];
//$DELAY$
for(int i=0;i<ret.length;i+=tsize){
for(int j=0;j<tsize-1;j++){
if(counts[j]==0){
//$DELAY$
curr[j] = serializers[j].deserialize(in,-1);
counts[j] = DataIO.unpackInt(in);
}
}
curr[tsize-1] = serializers[tsize-1].deserialize(in,-1);
System.arraycopy(curr,0,ret,i,tsize);
//$DELAY$
for(int j=counts.length-1;j>=0;j--){
counts[j]--;
}
}
if(CC.ASSERT){
for(int j:counts){
if(j!=0)
throw new DBException.DataCorruption("inconsistent counts");
}
}
return ret;
}
@Override
public int compare(Object[] keys, int pos1, int pos2) {
pos1 *=tsize;
pos2 *=tsize;
int res;
//$DELAY$
for(Comparator c:comparators){
//$DELAY$
res = c.compare(keys[pos1++],keys[pos2++]);
if(res!=0) {
return res;
}
}
return 0;
}
@Override
public int compare(Object[] keys, int pos, Object[] tuple) {
pos*=tsize;
int len = Math.min(tuple.length, tsize);
int r;
//$DELAY$
for(int i=0;i<len;i++){
//$DELAY$
r = comparators[i].compare(keys[pos++],tuple[i]);
if(r!=0)
return r;
}
return Fun.compareInt(tsize, tuple.length);
}
@Override
public Object[] getKey(Object[] keys, int pos) {
pos*=tsize;
return Arrays.copyOfRange(keys,pos,pos+tsize);
}
@Override
public Comparator<?> comparator() {
return comparator;
}
@Override
public Object[] emptyKeys() {
return new Object[0];
}
@Override
public int length(Object[] objects) {
return objects.length/tsize;
}
@Override
public Object[] putKey(Object[] keys, int pos, Object[] newKey) {
if(CC.ASSERT && newKey.length!=tsize)
throw new DBException.DataCorruption("inconsistent size");
pos*=tsize;
Object[] ret = new Object[keys.length+tsize];
System.arraycopy(keys, 0, ret, 0, pos);
//$DELAY$
System.arraycopy(newKey,0,ret,pos,tsize);
//$DELAY$
System.arraycopy(keys,pos,ret,pos+tsize,keys.length-pos);
return ret;
}
@Override
public Object[] arrayToKeys(Object[] keys) {
Object[] ret = new Object[keys.length*tsize];
int pos=0;
//$DELAY$
for(Object o:keys){
if(CC.ASSERT && ((Object[])o).length!=tsize)
throw new DBException.DataCorruption("keys have wrong size");
System.arraycopy(o,0,ret,pos,tsize);
//$DELAY$
pos+=tsize;
}
return ret;
}
@Override
public Object[] copyOfRange(Object[] keys, int from, int to) {
from*=tsize;
to*=tsize;
return Arrays.copyOfRange(keys,from,to);
}
@Override
public Object[] deleteKey(Object[] keys, int pos) {
pos*=tsize;
Object[] ret = new Object[keys.length-tsize];
System.arraycopy(keys,0,ret,0,pos);
//$DELAY$
System.arraycopy(keys,pos+tsize,ret,pos,ret.length-pos);
return ret;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ArrayKeySerializer that = (ArrayKeySerializer) o;
//$DELAY$
if (tsize != that.tsize) return false;
if (!Arrays.equals(comparators, that.comparators)) return false;
//$DELAY$
return Arrays.equals(serializers, that.serializers);
}
@Override
public int hashCode() {
int result = tsize;
result = 31 * result + Arrays.hashCode(comparators);
result = 31 * result + Arrays.hashCode(serializers);
return result;
}
}
public static final BTreeKeySerializer<java.util.UUID,long[]> UUID = new BTreeKeySerializer<java.util.UUID,long[]>() {
@Override
public void serialize(DataOutput out, long[] longs) throws IOException {
//$DELAY$
for(long l:longs){
out.writeLong(l);
}
}
@Override
public long[] deserialize(DataInput in, int nodeSize) throws IOException {
long[] ret= new long[nodeSize<<1];
//$DELAY$
for(int i=0;i<ret.length;i++){
ret[i]=in.readLong();
}
return ret;
}
@Override
public int compare(long[] longs, int pos1, int pos2) {
pos1<<=1;
pos2<<=1;
//$DELAY$
int r = Fun.compareLong(longs[pos1++],longs[pos2++]);
if(r!=0)
return r;
return Fun.compareLong(longs[pos1],longs[pos2]);
}
@Override
public int compare(long[] longs, int pos, java.util.UUID uuid) {
pos<<=1;
//$DELAY$
int r = Fun.compareLong(longs[pos++],uuid.getMostSignificantBits());
if(r!=0)
return r;
return Fun.compareLong(longs[pos],uuid.getLeastSignificantBits());
}
@Override
public UUID getKey(long[] longs, int pos) {
pos<<=1;
return new UUID(longs[pos++],longs[pos]);
}
@Override
public Comparator<?> comparator() {
return Fun.COMPARATOR;
}
@Override
public long[] emptyKeys() {
return new long[0];
}
@Override
public int length(long[] longs) {
return longs.length/2;
}
@Override
public long[] putKey(long[] keys, int pos, UUID newKey) {
pos <<= 1; //*2
long[] ret = new long[keys.length+2];
System.arraycopy(keys, 0, ret, 0, pos);
//$DELAY$
ret[pos++] = newKey.getMostSignificantBits();
ret[pos++] = newKey.getLeastSignificantBits();
System.arraycopy(keys,pos-2,ret,pos,ret.length-pos);
return ret;
}
@Override
public long[] arrayToKeys(Object[] keys) {
long[] ret = new long[keys.length<<1]; //*2
int i=0;
//$DELAY$
for(Object o:keys){
java.util.UUID u = (java.util.UUID) o;
ret[i++]=u.getMostSignificantBits();
ret[i++]=u.getLeastSignificantBits();
}
return ret;
}
@Override
public long[] copyOfRange(long[] longs, int from, int to) {
return Arrays.copyOfRange(longs,from<<1,to<<1);
}
@Override
public long[] deleteKey(long[] keys, int pos) {
pos <<= 1; //*2
long[] ret = new long[keys.length-2];
System.arraycopy(keys,0,ret,0,pos);
//$DELAY$
System.arraycopy(keys,pos+2,ret,pos,ret.length-pos);
return ret;
}
};
public interface StringArrayKeys {
int commonPrefixLen();
int length();
int[] getOffset();
BTreeKeySerializer.StringArrayKeys deleteKey(int pos);
BTreeKeySerializer.StringArrayKeys copyOfRange(int from, int to);
BTreeKeySerializer.StringArrayKeys putKey(int pos, String newKey);
int compare(int pos1, String string);
int compare(int pos1, int pos2);
String getKeyString(int pos);
boolean hasUnicodeChars();
void serialize(DataOutput out, int prefixLen) throws IOException;
}
//TODO right now byte[] contains 7 bit characters, but it should be expandable to 8bit.
public static final class ByteArrayKeys implements StringArrayKeys {
final int[] offset;
final byte[] array;
ByteArrayKeys(int[] offset, byte[] array) {
this.offset = offset;
this.array = array;
if(CC.ASSERT && ! (array.length==0 || array.length == offset[offset.length-1]))
throw new DBException.DataCorruption("inconsistent array size");
}
ByteArrayKeys(DataInput in, int[] offsets, int prefixLen) throws IOException {
this.offset = offsets;
array = new byte[offsets[offsets.length-1]];
in.readFully(array, 0, prefixLen);
for(int i=0; i<offsets.length-1;i++){
System.arraycopy(array,0,array,offsets[i],prefixLen);
}
//$DELAY$
//read suffixes