Repository navigation
Expand file tree
/
Copy pathHTreeMap2Test.java
More file actions
1159 lines (900 loc) · 34.5 KB
/
Copy pathHTreeMap2Test.java
File metadata and controls
1159 lines (900 loc) · 34.5 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 org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.nio.ByteBuffer;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import static org.junit.Assert.*;
@SuppressWarnings({ "unchecked", "rawtypes" })
public class HTreeMap2Test {
Engine engine;
DB db;
@Before public void init2(){
engine = DBMaker.memoryDB().transactionDisable().makeEngine();
db = new DB(engine);
}
@After
public void close(){
db.close();
}
@Test public void testDirSerializer() throws IOException {
Object dir = new int[4];
for(int slot=1;slot<127;slot+=1 +slot/5){
dir = HTreeMap.dirPut(dir,slot,slot*1111);
}
DataIO.DataOutputByteArray out = new DataIO.DataOutputByteArray();
HTreeMap.DIR_SERIALIZER.serialize(out,dir);
DataIO.DataInputByteBuffer in = swap(out);
int[] dir2 = (int[]) HTreeMap.DIR_SERIALIZER.deserialize(in, -1);
assertTrue(Arrays.equals((int[])dir,dir2));
for(int slot=1;slot<127;slot+=1 +slot/5){
int offset = HTreeMap.dirOffsetFromSlot(dir2,slot);
assertEquals(slot*1111, HTreeMap.dirGet(dir2, offset));
}
}
DataIO.DataInputByteBuffer swap(DataIO.DataOutputByteArray d){
byte[] b = d.copyBytes();
return new DataIO.DataInputByteBuffer(ByteBuffer.wrap(b),0);
}
@Test public void ln_serialization() throws IOException {
HTreeMap.LinkedNode n = new HTreeMap.LinkedNode(123456, 1111L, 123L, 456L);
DataIO.DataOutputByteArray out = new DataIO.DataOutputByteArray();
HTreeMap m = db.hashMapCreate("test").make();
m.LN_SERIALIZER.serialize(out, n);
DataIO.DataInputByteBuffer in = swap(out);
HTreeMap.LinkedNode n2 = (HTreeMap.LinkedNode) m.LN_SERIALIZER.deserialize(in, -1);
assertEquals(123456, n2.next);
assertEquals(0L, n2.expireLinkNodeRecid);
assertEquals(123L,n2.key);
assertEquals(456L,n2.value);
}
@Test public void test_simple_put(){
Engine[] engines = HTreeMap.fillEngineArray(engine);
HTreeMap m = new HTreeMap(engines,
false, null,0,HTreeMap.preallocateSegments(engines),Serializer.BASIC, Serializer.BASIC,0,0,0,0,0,null,null,null, null, 0L,false, null);
m.put(111L, 222L);
m.put(333L, 444L);
assertTrue(m.containsKey(111L));
assertTrue(!m.containsKey(222L));
assertTrue(m.containsKey(333L));
assertTrue(!m.containsKey(444L));
assertEquals(222L, m.get(111L));
assertEquals(null, m.get(222L));
assertEquals(444l, m.get(333L));
}
@Test public void test_hash_collision(){
Engine[] engines = HTreeMap.fillEngineArray(engine);
HTreeMap m = new HTreeMap(engines,
false, null,0,HTreeMap.preallocateSegments(engines),Serializer.BASIC, Serializer.BASIC,0,0,0,0,0,null,null,null,null, 0L,false, null){
@Override
protected int hash(Object key) {
return 0;
}
};
for(long i = 0;i<20;i++){
m.put(i,i+100);
}
for(long i = 0;i<20;i++){
assertTrue(m.containsKey(i));
assertEquals(i+100, m.get(i));
}
m.put(11L, 1111L);
assertEquals(1111L,m.get(11L) );
}
@Test public void test_hash_dir_expand(){
Engine[] engines = HTreeMap.fillEngineArray(engine);
HTreeMap m = new HTreeMap(engines,
false, null,0,HTreeMap.preallocateSegments(engines),Serializer.BASIC, Serializer.BASIC,0,0,0,0,0,null,null,null,null, 0L,false, null){
@Override
protected int hash(Object key) {
return 0;
}
};
for(long i = 0;i< HTreeMap.BUCKET_OVERFLOW;i++){
m.put(i,i);
}
//segment should not be expanded
int[] l = (int[]) engine.get(m.segmentRecids[0], HTreeMap.DIR_SERIALIZER);
assertEquals(4+1, l.length);
long recid = l[4];
assertEquals(1, recid&1); //last bite indicates leaf
assertEquals(1,l[0]);
//all others should be null
for(int i=1;i<4;i++)
assertEquals(0,l[i]);
recid = recid>>>1;
for(long i = HTreeMap.BUCKET_OVERFLOW -1; i>=0; i--){
assertTrue(recid!=0);
HTreeMap.LinkedNode n = (HTreeMap.LinkedNode) engine.get(recid, m.LN_SERIALIZER);
assertEquals(i, n.key);
assertEquals(i, n.value);
recid = n.next;
}
//adding one more item should trigger dir expansion to next level
m.put((long) HTreeMap.BUCKET_OVERFLOW, (long) HTreeMap.BUCKET_OVERFLOW);
recid = m.segmentRecids[0];
l = (int[]) engine.get(recid, HTreeMap.DIR_SERIALIZER);
assertEquals(4+1, l.length);
recid = l[4];
assertEquals(0, recid&1); //last bite indicates leaf
assertEquals(1,l[0]);
//all others should be null
for(int i=1;i<4;i++)
assertEquals(0,l[i]);
recid = recid>>>1;
l = (int[]) engine.get(recid, HTreeMap.DIR_SERIALIZER);
assertEquals(4+1, l.length);
recid = l[4];
assertEquals(1, recid&1); //last bite indicates leaf
assertEquals(1,l[0]);
//all others should be null
for(int i=1;i<4;i++)
assertEquals(0,l[i]);
recid = recid>>>1;
for(long i = 0; i<= HTreeMap.BUCKET_OVERFLOW; i++){
assertTrue(recid!=0);
HTreeMap.LinkedNode n = (HTreeMap.LinkedNode) engine.get(recid, m.LN_SERIALIZER);
assertNotNull(n);
assertEquals(i, n.key);
assertEquals(i, n.value);
recid = n.next;
}
}
@Test public void test_delete(){
Engine[] engines = HTreeMap.fillEngineArray(engine);
HTreeMap m = new HTreeMap(engines,
false, null,0,HTreeMap.preallocateSegments(engines),Serializer.BASIC, Serializer.BASIC,0,0,0,0,0,null,null,null,null,0L, false,null){
@Override
protected int hash(Object key) {
return 0;
}
};
for(long i = 0;i<20;i++){
m.put(i,i+100);
}
for(long i = 0;i<20;i++){
assertTrue(m.containsKey(i));
assertEquals(i+100, m.get(i));
}
for(long i = 0;i<20;i++){
m.remove(i);
}
for(long i = 0;i<20;i++){
assertTrue(!m.containsKey(i));
assertEquals(null, m.get(i));
}
}
@Test public void clear(){
Engine[] engines = HTreeMap.fillEngineArray(engine);
HTreeMap m = new HTreeMap(engines,
false, null,0,HTreeMap.preallocateSegments(engines),Serializer.BASIC, Serializer.BASIC,0,0,0,0,0,null,null,null,null, 0L,false,null);
for(Integer i=0;i<100;i++){
m.put(i,i);
}
m.clear();
assertTrue(m.isEmpty());
assertEquals(0, m.size());
}
@Test //(timeout = 10000)
public void testIteration(){
if(HTreeMap.SEG==1)
return;
Engine[] engines = HTreeMap.fillEngineArray(engine);
HTreeMap m = new HTreeMap(engines,
false, null,0,HTreeMap.preallocateSegments(engines),Serializer.BASIC,Serializer.BASIC,0,0,0,0,0,null,null,null,null,0L, false,null){
@Override
protected int hash(Object key) {
return (Integer) key;
}
};
final int max = 140;
final int inc = 111111;
for(Integer i=0;i<max;i++){
m.put(i,i+inc);
}
Iterator<Integer> keys = m.keySet().iterator();
for(Integer i=0;i<max;i++){
assertTrue(keys.hasNext());
assertEquals(i, keys.next());
}
assertTrue(!keys.hasNext());
Iterator<Integer> vals = m.values().iterator();
for(Integer i=inc;i<max+inc;i++){
assertTrue(vals.hasNext());
assertEquals(i, vals.next());
}
assertTrue(!vals.hasNext());
//great it worked, test stuff spread across segments
m.clear();
assertTrue(m.isEmpty());
for(int i = 0;i<max;i++){
m.put((1<<30)+i, i+inc);
m.put((2<<30)+i, i+inc);
m.put((3<<30)+i, i+inc);
}
assertEquals(max*3,m.size());
int countSegments = 0;
for(long segmentRecid:m.segmentRecids){
int[] segment = (int[]) engine.get(segmentRecid, HTreeMap.DIR_SERIALIZER);
if(segment!=null && segment.length>4){
countSegments++;
}
}
assertEquals(3, countSegments);
keys = m.keySet().iterator();
for(int i=1;i<=3;i++){
for(int j=0;j<max;j++){
assertTrue(keys.hasNext());
assertEquals(Integer.valueOf((i<<30)+j), keys.next());
}
}
assertTrue(!keys.hasNext());
}
static final Long ZERO = 0L;
@Test public void expire_link_simple_add_remove(){
HTreeMap m = db.hashMapCreate("test").expireMaxSize(100).make();
m.segmentLocks[0].writeLock().lock();
assertEquals(ZERO, engine.get(m.expireHeads[0], Serializer.LONG));
assertEquals(ZERO, engine.get(m.expireTails[0], Serializer.LONG));
m.expireLinkAdd(0,m.engines[0].put(HTreeMap.ExpireLinkNode.EMPTY, HTreeMap.ExpireLinkNode.SERIALIZER), 111L,222);
Long recid = engine.get(m.expireHeads[0], Serializer.LONG);
assertFalse(ZERO.equals(recid));
assertEquals(recid, engine.get(m.expireTails[0], Serializer.LONG));
HTreeMap.ExpireLinkNode n = engine.get(recid, HTreeMap.ExpireLinkNode.SERIALIZER);
assertEquals(0, n.prev);
assertEquals(0, n.next);
assertEquals(111L, n.keyRecid);
assertEquals(222, n.hash);
assertTrue(Arrays.equals(new int[]{222},getExpireList(m,0)));
n = m.expireLinkRemoveLast(0);
assertEquals(0, n.prev);
assertEquals(0, n.next);
assertEquals(111L, n.keyRecid);
assertEquals(222L, n.hash);
assertEquals(ZERO, engine.get(m.expireHeads[0], Serializer.LONG));
assertEquals(ZERO, engine.get(m.expireTails[0], Serializer.LONG));
assertTrue(Arrays.equals(new int[]{},getExpireList(m,0)));
m.segmentLocks[0].writeLock().unlock();
}
@Test public void expire_link_test(){
final int s = HTreeMap.SEG==1?0:2;
HTreeMap m = db.hashMapCreate("test").expireMaxSize(100).make();
m.segmentLocks[s].writeLock().lock();
long[] recids = new long[10];
for(int i=1;i<10;i++){
recids[i] = m.engines[0].put(HTreeMap.ExpireLinkNode.EMPTY, HTreeMap.ExpireLinkNode.SERIALIZER);
m.expireLinkAdd(s, recids[i],i*10,i*100);
}
assertTrue(Arrays.equals(new int[]{100, 200, 300, 400, 500, 600, 700, 800, 900}, getExpireList(m, s)));
m.expireLinkBump(s, recids[8], true);
assertTrue(Arrays.equals(new int[]{100, 200, 300, 400, 500, 600, 700, 900, 800}, getExpireList(m, s)));
m.expireLinkBump(s, recids[5], true);
assertTrue(Arrays.equals(new int[]{100, 200, 300, 400, 600, 700, 900, 800, 500}, getExpireList(m, s)));
m.expireLinkBump(s, recids[1], true);
assertTrue(Arrays.equals(new int[]{200, 300, 400, 600, 700, 900, 800, 500, 100}, getExpireList(m, s)));
assertEquals(200, m.expireLinkRemoveLast(s).hash);
assertTrue(Arrays.equals(new int[]{300,400,600,700,900,800,500,100},getExpireList(m,s)));
assertEquals(300, m.expireLinkRemoveLast(s).hash);
assertTrue(Arrays.equals(new int[]{400,600,700,900,800,500,100},getExpireList(m,s)));
assertEquals(600, m.expireLinkRemove(s,recids[6]).hash);
assertTrue(Arrays.equals(new int[]{400,700,900,800,500,100},getExpireList(m,s)));
assertEquals(400, m.expireLinkRemove(s,recids[4]).hash);
assertTrue(Arrays.equals(new int[]{700,900,800,500,100},getExpireList(m,s)));
assertEquals(100, m.expireLinkRemove(s,recids[1]).hash);
assertTrue(Arrays.equals(new int[]{700,900,800,500},getExpireList(m,s)));
m.segmentLocks[s].writeLock().unlock();
}
int[] getExpireList(HTreeMap m, int segment){
int[] ret = new int[0];
long recid = engine.get(m.expireTails[segment], Serializer.LONG);
long prev = 0;
//System.out.println("--");
while(recid!=0){
HTreeMap.ExpireLinkNode n = engine.get(recid, HTreeMap.ExpireLinkNode.SERIALIZER);
//System.out.println(n.hash);
assertEquals(prev,n.prev);
prev = recid;
recid = n.next;
ret=Arrays.copyOf(ret,ret.length+1);
ret[ret.length-1] = n.hash;
}
//System.out.println(Arrays.toString(ret));
assertEquals(new Long(prev), engine.get(m.expireHeads[segment], Serializer.LONG));
return ret;
}
@Test (timeout = 20000)
public void expire_put() {
HTreeMap m = db.hashMapCreate("test")
.expireAfterWrite(100)
.make();
m.put("aa","bb");
//should be removed in a moment or timeout
while(m.get("aa")!=null){
}
}
@Test(timeout = 20000)
public void expire_max_size() throws InterruptedException {
HTreeMap m = db.hashMapCreate("test")
.expireMaxSize(1000)
.make();
for(int i=0;i<1100;i++){
m.put(""+i,i);
}
//first should be removed soon
while(m.size()>1050){
m.get("aa"); //so internal tasks have change to run
Thread.sleep(1);
}
Thread.sleep(500);
m.get("aa"); //so internal tasks have change to run
long size = m.size();
assertTrue(""+size,size>900 && size<=1050);
}
@Test public void testSingleIter(){
Map m = DBMaker.tempHashMap();
m.put("aa","bb");
Iterator iter = m.keySet().iterator();
assertTrue(iter.hasNext());
assertEquals("aa",iter.next());
assertFalse(iter.hasNext());
}
@Test public void testMinMaxExpiryTime(){
HTreeMap m = db.hashMapCreate("test")
.expireAfterWrite(10000)
.expireAfterAccess(100000)
.make();
long t = System.currentTimeMillis();
assertEquals(0L, m.getMaxExpireTime());
assertEquals(0L, m.getMinExpireTime());
m.put("11","11");
m.put("12","12");
assertTrue(Math.abs(m.getMaxExpireTime()-t-10000)<300);
assertTrue(Math.abs(m.getMinExpireTime()-t-10000)<300);
m.get("11");
assertTrue(Math.abs(m.getMaxExpireTime()-t-100000)<300);
assertTrue(Math.abs(m.getMinExpireTime()-t-10000)<300);
m.remove("11");
m.remove("12");
assertEquals(0L, m.getMaxExpireTime());
assertEquals(0L, m.getMinExpireTime());
}
@Test (timeout = 20000)
public void cache_load_time_expire(){
if(TT.scale()==0)
return;
DB db =
DBMaker.memoryDB()
.transactionDisable()
.make();
HTreeMap m = db.hashMapCreate("test")
//.expireMaxSize(11000000)
.expireAfterWrite(100)
.make();
long time = System.currentTimeMillis();
long counter = 0;
while(time+5000>System.currentTimeMillis()){
m.put(counter++,counter++);
}
m.clear();
}
@Test(timeout = 20000)
public void cache_load_size_expire(){
if(TT.scale()==0)
return;
DB db = DBMaker.memoryDB()
.transactionDisable()
.make();
HTreeMap m = db.hashMapCreate("test")
//.expireMaxSize(11000000)
.expireMaxSize(10000)
.make();
long time = System.currentTimeMillis();
long counter = 0;
while(time+5000>System.currentTimeMillis()){
m.put(counter++,counter++);
// if(counter%1000<2) System.out.println(m.size());
}
m.clear();
}
@Test public void divMod8(){
for(int i= 0;i<1000000;i++){
assertEquals(i/8,i>>HTreeMap.DIV8);
assertEquals(i%8,i&HTreeMap.MOD8);
}
}
@Test public void hasher(){
HTreeMap m =
DBMaker.memoryDB().transactionDisable().make()
.hashMapCreate("test")
.keySerializer(Serializer.INT_ARRAY)
.make();
for(int i=0;i<1e5;i++){
m.put(new int[]{i,i,i},i);
}
for(Integer i=0;i<1e5;i++){
assertEquals(i,m.get(new int[]{i,i,i}));
}
}
@Test public void mod_listener_lock(){
DB db = DBMaker.memoryDB().transactionDisable().make();
final HTreeMap m = db.hashMap("name");
final int seg = m.hash("aa")>>>28;
final AtomicInteger counter = new AtomicInteger();
m.modificationListenerAdd(new Bind.MapListener() {
@Override
public void update(Object key, Object oldVal, Object newVal) {
for (int i = 0; i < m.segmentLocks.length; i++) {
assertEquals(seg == i, m.segmentLocks[i].isWriteLockedByCurrentThread());
}
counter.incrementAndGet();
}
});
m.put("aa","aa");
m.put("aa", "bb");
m.remove("aa");
m.put("aa","aa");
m.remove("aa","aa");
m.putIfAbsent("aa","bb");
m.replace("aa","bb","cc");
m.replace("aa","cc");
assertEquals(8, counter.get());
}
@Test
public void test_iterate_and_remove(){
final long max= (long) 1e5;
Set m = DBMaker.memoryDB().transactionDisable().make().hashSet("test");
for(long i=0;i<max;i++){
m.add(i);
}
Set control = new HashSet();
Iterator iter = m.iterator();
for(long i=0;i<max/2; i++){
assertTrue(iter.hasNext());
control.add(iter.next());
}
m.clear();
while(iter.hasNext()){
control.add(iter.next());
}
}
/*
Hi jan,
Today i found another problem.
my code is
HTreeMap<Object, Object> map = db.createHashMap("cache").expireMaxSize(MAX_ITEM_SIZE).counterEnable()
.expireAfterWrite(EXPIRE_TIME, TimeUnit.SECONDS).expireStoreSize(MAX_GB_SIZE).make();
i set EXPIRE_TIME = 216000
but the data was expired right now,the expire time is not 216000s, it seems there is a bug for expireAfterWrite.
if i call expireAfterAccess ,everything seems ok.
*/
@Test (timeout=100000)
public void expireAfterWrite() throws InterruptedException {
if(TT.scale()==0)
return;
//NOTE this test has race condition and may fail under heavy load.
//TODO increase timeout and move into integration tests.
DB db = DBMaker.memoryDB().transactionDisable().make();
int MAX_ITEM_SIZE = (int) 1e7;
int EXPIRE_TIME = 3;
double MAX_GB_SIZE = 1e7;
Map m = db.hashMapCreate("cache").expireMaxSize(MAX_ITEM_SIZE).counterEnable()
.expireAfterWrite(EXPIRE_TIME, TimeUnit.SECONDS).expireStoreSize(MAX_GB_SIZE).make();
for(int i=0;i<1000;i++){
m.put(i,i);
}
Thread.sleep(2000);
for(int i=0;i<500;i++){
m.put(i,i+1);
}
//wait until size is 1000
while(m.size()!=1000){
m.get("aa"); //so internal tasks have change to run
Thread.sleep(10);
}
Thread.sleep(2000);
//wait until size is 1000
while(m.size()!=500){
m.get("aa"); //so internal tasks have change to run
Thread.sleep(10);
}
}
public static class AA implements Serializable{
final int val;
public AA(int val) {
this.val = val;
}
@Override
public boolean equals(Object obj) {
return obj instanceof AA && ((AA)obj).val == val;
}
}
@Test(expected = IllegalArgumentException.class)
public void inconsistentHash(){
DB db = DBMaker.memoryDB()
.transactionDisable()
.make();
HTreeMap m = db.hashMapCreate("test")
.make();
for(int i=0;i<1e5;i++){
m.put(new AA(i),i);
}
}
@Test
public void test()
{
DB db = DBMaker.memoryDB().transactionDisable().make();
Map<String, Integer> map = db.hashMap("map", null, null, new Fun.Function1<Integer, String>() {
@Override
public Integer run(String s) {
return Integer.MIN_VALUE;
}
});
Integer v1 = map.get("s1");
assertEquals(Integer.valueOf(Integer.MIN_VALUE), v1);
}
@Test public void pump(){
int max = 100+ TT.scale()*1000000;
DB db = DBMaker.memoryDB().transactionDisable().make();
Set<Long> s = new HashSet();
for(long i=0;i<max;i++){
s.add(i);
}
HTreeMap<Long,Long> m = db.hashMapCreate("a")
.pumpSource(s.iterator(), new Fun.Function1<Long,Long>() {
@Override
public Long run(Long l) {
return l*l;
}
})
.keySerializer(Serializer.LONG)
.valueSerializer(Serializer.LONG)
.make();
assertEquals(s.size(),m.size());
assertTrue(m.keySet().containsAll(s));
for(Long o:s){
assertEquals((Long)(o*o),m.get(o));
}
}
@Test public void pump_duplicates(){
DB db = DBMaker.memoryDB().transactionDisable().make();
List<Long> s = new ArrayList();
int max = (int) (TT.scale()*1e6);
for(long i=0;i<max;i++){
s.add(i);
}
s.add(-1L);
s.add(-1L);
HTreeMap<Long,Long> m = db.hashMapCreate("a")
.pumpSource(s.iterator(), new Fun.Function1<Long, Long>() {
@Override
public Long run(Long l) {
return l * l;
}
})
.pumpIgnoreDuplicates()
.keySerializer(Serializer.LONG)
.valueSerializer(Serializer.LONG)
.make();
assertEquals(s.size()-1,m.size());
assertTrue(m.keySet().containsAll(s));
for(Long o:s){
assertEquals((Long)(o*o),m.get(o));
}
}
@Test(expected = IllegalArgumentException.class) //TODO better exception here
public void pump_duplicates_fail(){
DB db = DBMaker.memoryDB().transactionDisable().make();
List<Long> s = new ArrayList();
for(long i=0;i<1e6;i++){
s.add(i);
}
s.add(-1L);
s.add(-1L);
HTreeMap<Long,Long> m = db.hashMapCreate("a")
.pumpSource(s.iterator(), new Fun.Function1<Long,Long>() {
@Override
public Long run(Long l) {
return l*l;
}
})
.keySerializer(Serializer.LONG)
.valueSerializer(Serializer.LONG)
.make();
}
@Test public void pumpset(){
DB db = DBMaker.memoryDB().transactionDisable().make();
Set<Long> s = new HashSet();
int max = 100+(int) (1e6* TT.scale());
for(long i=0;i<max;i++){
s.add(i);
}
Set<Long> m = db.hashSetCreate("a")
.pumpSource(s.iterator())
.serializer(Serializer.LONG)
.make();
assertEquals(s.size(), m.size());
assertTrue(s.containsAll(m));
}
@Test public void pumpset_duplicates() {
DB db = DBMaker.memoryDB().transactionDisable().make();
List<Long> s = new ArrayList();
int max = 100+(int) (1e6* TT.scale());
for (long i = 0; i < max; i++) {
s.add(i);
}
s.add(-1L);
s.add(-1L);
Set<Long> m = db.hashSetCreate("a")
.pumpSource(s.iterator())
.pumpIgnoreDuplicates()
.serializer(Serializer.LONG)
.make();
assertEquals(s.size() - 1, m.size());
assertTrue(m.containsAll(s));
}
@Test(expected = IllegalArgumentException.class) //TODO better exception here
public void pumpset_duplicates_fail(){
int max = 100+ TT.scale()*1000000;
DB db = DBMaker.memoryDB().transactionDisable().make();
List<Long> s = new ArrayList();
for(long i=0;i<max;i++){
s.add(i);
}
s.add(-1L);
s.add(-1L);
db.hashSetCreate("a")
.pumpSource(s.iterator())
.serializer(Serializer.LONG)
.make();
}
@Test public void slot_to_offset_long(){
Random r = new Random();
for(int i=0;i<1000;i++){
//fill array with random bites
long[] l = new long[]{r.nextLong(), r.nextLong()};
//turn bites into array pos
List<Integer> b = new ArrayList();
for(int j=0;j<l.length;j++){
long v = l[j];
for(int k=0;k<64;k++){
b.add((int)v&1);
v>>>=1;
}
}
assertEquals(128,b.size());
//iterate over an array, check if calculated pos equals
int offset = 2;
for(int slot=0;slot<128;slot++){
int current = b.get(slot);
int coffset = HTreeMap.dirOffsetFromSlot(l,slot);
if(current==0)
coffset = -coffset;
assertEquals(offset,coffset);
offset+=current;
}
}
}
@Test public void slot_to_offset_int(){
Random r = new Random();
for(int i=0;i<1000;i++){
//fill array with random bites
int[] l = new int[]{r.nextInt(), r.nextInt(), r.nextInt(), r.nextInt()};
//turn bites into array pos
List<Integer> b = new ArrayList();
for(int j=0;j<l.length;j++){
long v = l[j];
for(int k=0;k<32;k++){
b.add((int)v&1);
v>>>=1;
}
}
assertEquals(128,b.size());
//iterate over an array, check if calculated pos equals
int offset = 4;
for(int slot=0;slot<128;slot++){
int current = b.get(slot);
int coffset = HTreeMap.dirOffsetFromSlot(l,slot);
if(current==0)
coffset = -coffset;
assertEquals(offset,coffset);
offset+=current;
}
}
}
@Test public void dir_put_long(){
if(TT.scale()==0)
return;
for(int a=0;a<100;a++) {
long[] reference = new long[127];
Object dir = new int[4];
Random r = new Random();
for (int i = 0; i < 1e3; i++) {
int slot = r.nextInt(127);
long val = r.nextLong()&0xFFFFFFF;
if (i % 3==0 && reference[slot]!=0){
//delete every 10th element
reference[slot] = 0;
dir = HTreeMap.dirRemove(dir, slot);
}else{
reference[slot] = val;
dir = HTreeMap.dirPut(dir, slot, val);
}
//compare dir and reference
long[] dir2 = new long[127];
for (int j = 0; j < 127; j++) {
int offset = HTreeMap.dirOffsetFromSlot(dir, j);
if (offset > 0)
dir2[j] = HTreeMap.dirGet(dir, offset);
}
assertTrue(Arrays.equals(reference, dir2));
if (dir instanceof int[])
assertTrue(Arrays.equals((int[]) dir, (int[]) TT.clone(dir, HTreeMap.DIR_SERIALIZER)));
else
assertTrue(Arrays.equals((long[]) dir, (long[]) TT.clone(dir, HTreeMap.DIR_SERIALIZER)));
}
}
}
@Test public void dir_put_int(){
if(TT.scale()==0)
return;
for(int a=0;a<100;a++) {
long[] reference = new long[127];
Object dir = new int[4];
Random r = new Random();
for (int i = 0; i < 1e3; i++) {
int slot = r.nextInt(127);
long val = r.nextInt((int) 1e6);
if (i % 3==0 && reference[slot]!=0){
//delete every 10th element
reference[slot] = 0;
dir = HTreeMap.dirRemove(dir, slot);
}else{
reference[slot] = val;
dir = HTreeMap.dirPut(dir, slot, val);
}
//compare dir and reference
long[] dir2 = new long[127];
for (int j = 0; j < 127; j++) {
int offset = HTreeMap.dirOffsetFromSlot(dir, j);
if (offset > 0)
dir2[j] = HTreeMap.dirGet(dir, offset);
}
assertTrue(Arrays.equals(reference, dir2));
if (dir instanceof int[])
assertTrue(Arrays.equals((int[]) dir, (int[]) TT.clone(dir, HTreeMap.DIR_SERIALIZER)));
else
assertTrue(Arrays.equals((long[]) dir, (long[]) TT.clone(dir, HTreeMap.DIR_SERIALIZER)));