-
Notifications
You must be signed in to change notification settings - Fork 39
/
index.test.js
1153 lines (1124 loc) · 38.3 KB
/
index.test.js
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
import path from 'path';
import rimraf from 'rimraf';
import chai from 'chai';
let should = chai.should();
let expect = chai.expect;
import { spawn } from 'child_process';
import { unlinkSync } from 'fs'
import { fileURLToPath } from 'url'
import { dirname } from 'path'
import { encoder as orderedBinaryEncoder } from 'ordered-binary/index.js'
import inspector from 'inspector'
//inspector.open(9330, null, true); debugger
let nativeMethods, dirName = dirname(fileURLToPath(import.meta.url))
import { open, levelup, bufferToKeyValue, keyValueToBuffer, asBinary, ABORT, IF_EXISTS } from '../index.js';
import { createBufferForAddress } from '../native.js'
import { RangeIterable } from '../util/RangeIterable.js'
import { assert } from 'console';
import { openAsClass } from '../open.js';
describe('lmdb-js', function() {
let testDirPath = path.resolve(dirName, './testdata-ls');
// just to make a reasonable sized chunk of data...
function expand(str) {
str = '(' + str + ')';
str = str + str;
str = str + str;
str = str + str;
str = str + str;
str = str + str;
return str;
}
before(function(done) {
// cleanup previous test directory
rimraf(testDirPath, function(err) {
if (err) {
return done(err);
}
done();
});
});
let testIteration = 0
describe('Basic use', basicTests({ }));
describe('Basic use with overlapping sync', basicTests({ overlappingSync: true }));
describe('Basic use with encryption', basicTests({ compression: false, encryptionKey: 'Use this key to encrypt the data' }));
//describe('Check encrypted data', basicTests({ compression: false, encryptionKey: 'Use this key to encrypt the data', checkLast: true }));
describe('Basic use with JSON', basicTests({ encoding: 'json' }));
describe('Basic use with ordered-binary', basicTests({ encoding: 'ordered-binary' }));
if (typeof WeakRef != 'undefined')
describe('Basic use with caching', basicTests({ cache: true }));
function basicTests(options) { return function() {
this.timeout(1000000);
let db, db2, db3;
before(function() {
if (!options.checkLast)
testIteration++;
db = open(Object.assign({
name: 'mydb1',
create: true,
useVersions: true,
batchStartThreshold: 10,
//asyncTransactionOrder: 'strict',
//useWritemap: true,
//noSync: true,
//overlappingSync: true,
maxReaders: 100,
keyEncoder: orderedBinaryEncoder,
compression: {
threshold: 256,
},
}, options));
if (!options.checkLast)
db.clearSync();
db2 = db.openDB(Object.assign({
name: 'mydb2',
create: true,
dupSort: true,
}));
if (!options.checkLast)
db2.clearSync();
db3 = db.openDB({
name: 'mydb3',
create: true,
dupSort: true,
encoding: 'ordered-binary',
});
if (!options.checkLast)
db3.clearSync();
});
if (options.checkLast) {
it('encrypted data can not be accessed', function() {
let data = db.get('key1');
data.should.deep.equal('test')
})
return
}
it('zero length values', async function() {
await db.committed // should be able to await db even if nothing has happened
db.put(5, asBinary(Buffer.from([])));
db.put(5, asBinary(createBufferForAddress(16, 0)));// externally allocated buffers of zero-length with the same non-null-pointer can crash node, #161
db.put(5, asBinary(createBufferForAddress(16, 0)));
await db2.put('key1', asBinary(Buffer.from([])));
should.equal(db.getBinary(5).length, 0);
should.equal(db2.getBinary('key1').length, 0);
db.put(5, asBinary(Buffer.from([4])));
db2.remove('key1');
await db2.put('key1', asBinary(Buffer.from([4])));
should.equal(db.getBinary(5).length, 1);
should.equal(db2.getBinary('key1').length, 1);
db.put(5, asBinary(Buffer.from([])));
db2.remove('key1');
await db2.put('key1', asBinary(Buffer.from([])));
should.equal(db.getBinary(5).length, 0);
should.equal(db2.getBinary('key1').length, 0);
await db2.remove('key1');
});
it('query of keys', async function() {
let keys = [
Symbol.for('test'),
false,
true,
-33,
-1.1,
3.3,
5,
[5,4],
[5,55],
[5, 'words after number'],
[6, 'abc'],
[ 'Test', null, 1 ],
[ 'Test', Symbol.for('test'), 2 ],
[ 'Test', 'not null', 3 ],
'hello',
['hello', 3],
['hello', 'world'],
[ 'uid', 'I-7l9ySkD-wAOULIjOEnb', 'Rwsu6gqOw8cqdCZG5_YNF' ],
'z'
]
for (let key of keys) {
await db.put(key, 3);
}
let returnedKeys = []
for (let { key, value } of db.getRange({
start: Symbol.for('A')
})) {
returnedKeys.push(key)
value.should.equal(db.get(key))
}
keys.should.deep.equal(returnedKeys)
returnedKeys = []
for (let { key, value } of db.getRange({
reverse: true,
})) {
returnedKeys.unshift(key)
value.should.equal(db.get(key))
}
keys.should.deep.equal(returnedKeys)
});
it('reverse query range', async function() {
const keys = [
[ 'Test', 100, 1 ],
[ 'Test', 10010, 2 ],
[ 'Test', 10010, 3 ]
]
for (let key of keys)
db.put(key, 3);
await db.committed
for (let { key, value } of db.getRange({
start: ['Test', null],
end: ['Test', null],
reverse: true
})) {
throw new Error('Should not return any results')
}
})
it('more reverse query range', async function() {
db.putSync('0Sdts8FwTqt2Hv5j9KE7ebjsQcFbYDdL/0Sdtsud6g8YGhPwUK04fRVKhuTywhnx8', 1, 1, null);
db.putSync('0Sdts8FwTqt2Hv5j9KE7ebjsQcFbYDdL/0Sdu0mnkm8lS38yIZa4Xte3Q3JUoD84V', 1, 1, null);
const options =
{
start: '0Sdts8FwTqt2Hv5j9KE7ebjsQcFbYDdL/0SdvKaMkMNPoydWV6HxZbFtKeQm5sqz3',
end: '0Sdts8FwTqt2Hv5j9KE7ebjsQcFbYDdL/00000000dKZzSn03pte5dWbaYfrZl4hG',
reverse: true
};
let returnedKeys = Array.from(db.getKeys(options))
returnedKeys.should.deep.equal(['0Sdts8FwTqt2Hv5j9KE7ebjsQcFbYDdL/0Sdu0mnkm8lS38yIZa4Xte3Q3JUoD84V', '0Sdts8FwTqt2Hv5j9KE7ebjsQcFbYDdL/0Sdtsud6g8YGhPwUK04fRVKhuTywhnx8'])
});
it('clear between puts', async function() {
db.put('key0', 'zero')
db.clearAsync()
await db.put('key1', 'one')
should.equal(db.get('key0'), undefined)
should.equal(db.get('hello'), undefined)
should.equal(db.get('key1'), 'one')
})
it('string', async function() {
await db.put('key1', 'Hello world!');
let data = db.get('key1');
data.should.equal('Hello world!');
await db.remove('key1')
let data2 = db.get('key1');
should.equal(data2, undefined);
});
it('string with version', async function() {
await db.put('key1', 'Hello world!', 53252);
let entry = db.getEntry('key1');
entry.value.should.equal('Hello world!');
entry.version.should.equal(53252);
(await db.remove('key1', 33)).should.equal(false);
entry = db.getEntry('key1');
entry.value.should.equal('Hello world!');
entry.version.should.equal(53252);
(await db.remove('key1', 53252)).should.equal(true);
entry = db.getEntry('key1');
should.equal(entry, undefined);
});
it('string with version branching', async function() {
await db.put('key1', 'Hello world!', 53252);
let entry = db.getEntry('key1');
entry.value.should.equal('Hello world!');
entry.version.should.equal(53252);
(await db.ifVersion('key1', 777, () => {
db.put('newKey', 'test', 6);
db2.put('keyB', 'test', 6);
})).should.equal(false);
should.equal(db.get('newKey'), undefined);
should.equal(db2.get('keyB'), undefined);
let result = (await db.ifVersion('key1', 53252, () => {
db.put('newKey', 'test', 6);
db2.put('keyB', 'test', 6);
}))
should.equal(db.get('newKey'), 'test')
should.equal(db2.get('keyB'), 'test')
should.equal(result, true);
result = await db.ifNoExists('key1', () => {
db.put('newKey', 'changed', 7);
})
should.equal(db.get('newKey'), 'test');
should.equal(result, false);
result = await db.ifNoExists('key-no-exist', () => {
db.put('newKey', 'changed', 7);
})
should.equal(db.get('newKey'), 'changed')
should.equal(result, true);
result = await db2.ifVersion('key-no-exist', IF_EXISTS, () => {
db.put('newKey', 'changed again', 7);
})
should.equal(db.get('newKey'), 'changed')
should.equal(result, false);
result = await db2.ifVersion('keyB', IF_EXISTS, () => {
db.put('newKey', 'changed again', 7);
})
should.equal(db.get('newKey'), 'changed again')
should.equal(result, true);
result = await db2.remove('key-no-exists');
should.equal(result, true);
result = await db2.remove('key-no-exists', IF_EXISTS);
should.equal(result, false);
});
it('repeated ifNoExists', async function() {
let keyBase = 'c333f4e0-f692-4bca-ad45-f805923f974f-c333f4e0-f692-4bca-ad45-f805923f974f-c333f4e0-f692-4bca-ad45-f805923f974f'
let result;
for (let i = 0; i < 500; i++) {
let key = keyBase + (i % 100);
result = db.ifNoExists(keyBase + i, () => {
db.put(keyBase + i, 'changed', 7);
});
if (i % 100 == 0) {
await result;
}
}
await result;
});
it('string with compression and versions', async function() {
let str = expand('Hello world!')
await db.put('key1', str, 53252);
let entry = db.getEntry('key1');
entry.value.should.equal(str);
entry.version.should.equal(53252);
(await db.remove('key1', 33)).should.equal(false);
let data = db.get('key1');
data.should.equal(str);
(await db.remove('key1', 53252)).should.equal(true);
data = db.get('key1');
should.equal(data, undefined);
});
it('repeated compressions', async function() {
let str = expand('Hello world!')
db.put('key1', str, 53252);
db.put('key1', str, 53253);
db.put('key1', str, 53254);
await db.put('key1', str, 53255);
let entry = db.getEntry('key1');
entry.value.should.equal(str);
entry.version.should.equal(53255);
(await db.remove('key1')).should.equal(true);
});
it('forced compression due to starting with 255', async function() {
await db.put('key1', asBinary(Buffer.from([255])));
let entry = db.getBinary('key1');
entry.length.should.equal(1);
entry[0].should.equal(255);
(await db.remove('key1')).should.equal(true);
});
if (options.encoding == 'ordered-binary')
return // no more tests need to be applied for this
it('store objects', async function() {
let dataIn = {foo: 3, bar: true}
await db.put('key1', dataIn);
let dataOut = db.get('key1');
dataOut.should.deep.equal(dataIn);
db.removeSync('not-there').should.equal(false);
});
it('store binary', async function() {
let dataIn = {foo: 4, bar: true}
let buffer = db.encoder.encode(dataIn);
if (typeof buffer == 'string')
return
await db.put('key1', asBinary(buffer));
let dataOut = db.get('key1');
dataOut.should.deep.equal(dataIn);
});
it('writes batch with callback', async function() {
let dataIn = {name: 'for batch 1'}
await db.batch(() => {
db.put('key1', dataIn);
db.put('key2', dataIn);
})
})
it('conditional put', async function() {
if (db.encoding == 'ordered-binary')
return;
const key = 'test';
await db.put(key, { a: 1, b: 2 }, 1);
db.getEntry(key);
await db.put(key, { a: 2, b: 3 }, 2, 1);
const entry2 = db.get(key);
should.equal(entry2.a, 2);
})
it.skip('trigger sync commit', async function() {
let dataIn = {foo: 4, bar: false}
db.immediateBatchThreshold = 1
db.syncBatchThreshold = 1
await db.put('key1', dataIn);
await db.put('key2', dataIn);
db.immediateBatchThreshold = 100000
db.syncBatchThreshold = 1000000
let dataOut = db.get('key1');
dataOut.should.deep.equal(dataIn);
});
function iterateQuery(acrossTransactions) { return async () => {
let data1 = {foo: 1, bar: true}
let data2 = {foo: 2, bar: false}
db.put('key1', data1);
db.put('key2', data2);
await db.committed
let count = 0
for (let { key, value } of db.getRange({start:'key', end:'keyz', snapshot: !acrossTransactions})) {
if (acrossTransactions)
await delay(10)
count++
switch(key) {
case 'key1': data1.should.deep.equal(value); break;
case 'key2': data2.should.deep.equal(value); break;
}
}
should.equal(count >= 2, true);
should.equal(db.getCount({start:'key', end:'keyz'}) >= 2, true);
}}
it('should iterate over query', iterateQuery(false));
it('should iterate over query, across transactions', iterateQuery(true));
it('should break out of query', async function() {
let data1 = {foo: 1, bar: true}
let data2 = {foo: 2, bar: false}
db.put('key1', data1);
db.put('key2', data2);
await db.committed
let count = 0;
for (let { key, value } of db.getRange({start:'key', end:'keyz'})) {
if (count > 0)
break;
count++;
data1.should.deep.equal(value);
'key1'.should.equal(key);
}
count.should.equal(1);
});
it('getRange with arrays', async function() {
const keys = [
[ 'foo', 0 ],
[ 'foo', 1 ],
[ 'foo', 2 ],
]
let promise
keys.forEach((key, i) => {
promise = db.put(key, i)
})
await promise
let result = Array.from(db.getRange({
start: [ 'foo'],
end: [ 'foo', 1 ],
}))
result.should.deep.equal([ { key: [ 'foo', 0 ], value: 0 } ])
result = Array.from(db.getRange({
start: [ 'foo', 0 ],
end: [ 'foo', 1 ],
}))
result.should.deep.equal([ { key: [ 'foo', 0 ], value: 0 } ])
result = Array.from(db.getRange({
start: [ 'foo', 2 ],
end: [ 'foo', [2, null] ],
}))
result.should.deep.equal([ { key: [ 'foo', 2 ], value: 2 } ])
})
it('should iterate over query with offset/limit', async function() {
let data1 = {foo: 1, bar: true}
let data2 = {foo: 2, bar: false}
let data3 = {foo: 3, bar: false}
db.put('key1', data1);
db.put('key2', data2);
await db.put('key3', data3);
let count = 0
for (let { key, value } of db.getRange({start:'key', end:'keyz', offset: 1, limit: 1})) {
count++
switch(key) {
case 'key2': data2.should.deep.equal(value); break;
}
}
count.should.equal(1)
count = 0
for (let { key, value } of db.getRange({start:'key', end:'keyz', offset: 3, limit: 3})) {
count++
}
count.should.equal(0)
for (let { key, value } of db.getRange({start:'key', end:'keyz', offset: 10, limit: 3})) {
count++
}
count.should.equal(0)
for (let { key, value } of db.getRange({start:'key', end:'keyz', offset: 2, limit: 3})) {
count++
switch(key) {
case 'key3': data3.should.deep.equal(value); break;
}
}
count.should.equal(1)
});
it('should handle open iterators and cursor renewal', async function() {
let data1 = {foo: 1, bar: true};
let data2 = {foo: 2, bar: false};
let data3 = {foo: 3, bar: false};
db2.put('key1', data1);
db.put('key1', data1);
db.put('key2', data2);
await db.put('key3', data3);
let it1 = db.getRange({start:'key', end:'keyz'})[Symbol.iterator]();
let it2 = db2.getRange({start:'key', end:'keyz'})[Symbol.iterator]();
let it3 = db.getRange({start:'key', end:'keyz'})[Symbol.iterator]();
it1.return();
it2.return();
await new Promise(resolve => setTimeout(resolve, 10));
it1 = db.getRange({start:'key', end:'keyz'})[Symbol.iterator]();
it2 = db2.getRange({start:'key', end:'keyz'})[Symbol.iterator]();
let it4 = db.getRange({start:'key', end:'keyz'})[Symbol.iterator]();
let it5 = db2.getRange({start:'key', end:'keyz'})[Symbol.iterator]();
await new Promise(resolve => setTimeout(resolve, 20));
it4.return()
it5.return()
it1.return()
it2.return()
it3.return()
});
it('should iterate over dupsort query, with removal', async function() {
let data1 = {foo: 1, bar: true}
let data2 = {foo: 2, bar: false}
let data3 = {foo: 3, bar: true}
db2.put('key1', data1);
db2.put('key1', data2);
db2.put('key1', data3);
await db2.put('key2', data3);
let count = 0;
for (let value of db2.getValues('key1')) {
count++
switch(count) {
case 1: data1.should.deep.equal(value); break;
case 2: data2.should.deep.equal(value); break;
case 3: data3.should.deep.equal(value); break;
}
}
count.should.equal(3);
db2.getValuesCount('key1').should.equal(3);
await db2.remove('key1', data2);
count = 0;
for (let value of db2.getValues('key1')) {
count++;
switch(count) {
case 1: data1.should.deep.equal(value); break;
case 2: data3.should.deep.equal(value); break;
}
}
count.should.equal(2)
db2.getValuesCount('key1').should.equal(2);
count = 0;
for (let value of db2.getValues('key1', { reverse: true })) {
count++;
switch(count) {
case 1: data3.should.deep.equal(value); break;
case 2: data1.should.deep.equal(value); break;
}
}
count.should.equal(2);
db2.getValuesCount('key1').should.equal(2);
count = 0;
for (let value of db2.getValues('key0')) {
count++;
}
count.should.equal(0);
db2.getValuesCount('key0').should.equal(0);
db2.getCount({start: 'key1', end: 'key3'}).should.equal(3);
});
it('should iterate over ordered-binary dupsort query with start/end', async function() {
db3.put('key1', 1);
db3.put('key1', 2);
db3.put('key1', 3);
await db3.put('key2', 3);
let count = 0;
for (let value of db3.getValues('key1', { start: 1 })) {
count++
value.should.equal(count)
}
count.should.equal(3);
count = 0;
for (let value of db3.getValues('key1', { end: 3 })) {
count++
value.should.equal(count)
}
count.should.equal(2);
});
it('should count ordered-binary dupsort query with start/end', async function() {
db3.put('key1', 1);
db3.put('key1', 2);
db3.put('key1', 3);
await db3.put('key2', 3);
db3.getValuesCount('key1').should.equal(3);
db3.getValuesCount('key1', { start: 1, end: 3 }).should.equal(2);
db3.getValuesCount('key1', { start: 2, end: 3 }).should.equal(1);
db3.getValuesCount('key1', { start: 2 }).should.equal(2);
db3.getValuesCount('key1', { end: 2 }).should.equal(1);
db3.getValuesCount('key1', { start: 1, end: 2 }).should.equal(1);
db3.getValuesCount('key1', { start: 2, end: 2 }).should.equal(0);
db3.getValuesCount('key1').should.equal(3);
});
it('should reverse iterate ordered-binary dupsort query with start/end', async function() {
db3.put('key1', 1);
db3.put('key1', 2);
db3.put('key1', 3);
await db3.put('key2', 3);
let count = 0;
for (let value of db3.getValues('key1', { reverse: true, start: 2 })) {
count++;
value.should.equal(3 - count);
}
count.should.equal(2);
count = 0;
for (let value of db3.getValues('key1', { reverse: true, start: 2.5 })) {
count++;
value.should.equal(3 - count);
}
count.should.equal(2);
count = 0;
for (let value of db3.getValues('key1', { reverse: true, start: 50 })) {
count++;
value.should.equal(4 - count);
}
count.should.equal(3);
count = 0;
for (let value of db3.getValues('key1', { reverse: true, start: 2, end: 1 })) {
count++;
value.should.equal(3 - count);
}
count.should.equal(1);
count = 0;
for (let value of db3.getValues('key1', { reverse: true, end: 1 })) {
count++;
value.should.equal(4 - count);
}
count.should.equal(2);
count = 0;
for (let value of db3.getValues('key1', { reverse: true, start: 0.5 })) {
count++;
}
count.should.equal(0);
});
it('doesExist', async function() {
let data1 = {foo: 1, bar: true}
let data2 = {foo: 2, bar: false}
let data3 = {foo: 3, bar: true}
db2.put('key1', data1);
db2.put('key1', data3);
db2.put(false, 3);
await db2.put('key2', data3);
should.equal(db2.doesExist('key1'), true);
should.equal(db2.doesExist('key1', data1), true);
should.equal(db2.doesExist('key1', data2), false);
should.equal(db2.doesExist('key1', data3), true);
should.equal(db2.doesExist(false), true);
should.equal(db2.doesExist(false, 3), true);
should.equal(db2.doesExist(false, 4), false);
})
it('should iterate over keys without duplicates', async function() {
let lastKey
for (let key of db2.getKeys({ start: 'k' })) {
if (key == lastKey)
throw new Error('duplicate key returned')
lastKey = key
}
});
it('big keys', async function() {
let keyBase = ''
for (let i = 0; i < 1900; i++) {
keyBase += 'A'
}
let keys = []
let promise
for (let i = 40; i < 120; i++) {
let key = String.fromCharCode(i) + keyBase
keys.push(key)
promise = db.put(key, i)
}
await promise
let returnedKeys = []
for (let { key, value } of db.getRange({})) {
if (key.length > 1000) {
returnedKeys.push(key)
should.equal(key.charCodeAt(0), value)
should.equal(db.get(key), value)
promise = db.remove(key)
}
}
returnedKeys.should.deep.equal(keys)
await promise
should.equal(db.get(returnedKeys[0]), undefined)
});
it('prefetch', async function() {
await new Promise(resolve => db.prefetch(['key1', 'key2'], resolve));
let key = ''
for (let i = 0; i < 1900; i++) {
key += 'A'
}
let keys = []
for (let i = 0; i < 20; i++) {
keys.push(key)
}
let value = keys.join(',')
await db.put(key, value);
await db.prefetch(keys);
let values = await db.getMany(keys);
should.equal(values.length, 20);
should.equal(values[3], value);
values = await db.getMany([]);
should.equal(values.length, 0);
});
it('invalid key', async function() {
expect(() => db.get(Buffer.from([]))).to.throw();
expect(() => db.put(Buffer.from([]), 'test')).to.throw();
expect(() => db.get({ foo: 'bar' })).to.throw();
expect(() => db.put({ foo: 'bar' }, 'hello')).to.throw();
expect(() => db.put('x'.repeat(4027), 'hello')).to.throw();
expect(() => db2.put('x', 'x'.repeat(4027))).to.throw();
Array.from(db.getRange({ start: 'x', end: Buffer.from([])}))
//expect(() => Array.from(db.getRange({ start: 'x'.repeat(4027)}))).to.throw();
});
it('put options (sync)', function() {
db.putSync('zkey6', 'test', { append: true, version: 33 });
let entry = db.getEntry('zkey6');
entry.value.should.equal('test');
entry.version.should.equal(33);
should.equal(db.putSync('zkey7', 'test', { append: true, noOverwrite: true }), true);
should.equal(db2.putSync('zkey6', 'test1', { appendDup: true }), true);
should.equal(db2.putSync('zkey6', 'test2', { appendDup: true }), true);
should.equal(db.putSync('zkey5', 'test', { append: true, version: 44 }), false);
should.equal(db.putSync('zkey7', 'test', { noOverwrite: true }), false);
should.equal(db2.putSync('zkey6', 'test1', { noDupData: true }), false);
});
it('async transactions', async function() {
let ranTransaction
db.put('key1', 'async initial value'); // should be queued for async write, but should put before queued transaction
let errorHandled
db.childTransaction(() => {
db.put('key1', 'should be rolled back');
throw new Error('Make sure this is properly propagated without interfering with next transaction')
}).catch(error => {
if (error)
errorHandled = true
})
await db.childTransaction(() => {
should.equal(db.get('key1'), 'async initial value');
db.put('key-a', 'async test a');
should.equal(db.get('key-a'), 'async test a');
})
should.equal(errorHandled, true);
await db.transactionAsync(() => {
ranTransaction = true;
should.equal(db.get('key1'), 'async initial value');
db.put('key1', 'async test 1');
should.equal(db.get('key1'), 'async test 1');
for (let { key, value } of db.getRange({start: 'key1', end: 'key1z' })) {
should.equal(value, 'async test 1');
}
db2.put('key2-async', 'async test 2');
should.equal(db2.get('key2-async'), 'async test 2');
});
should.equal(db.get('key1'), 'async test 1');
should.equal(db2.get('key2-async'), 'async test 2');
should.equal(ranTransaction, true);
});
it('child transaction in sync transaction', async function() {
if (db.cache)
return
await db.transactionSync(async () => {
db.put('key3', 'test-sync-txn');
db.childTransaction(() => {
db.put('key3', 'test-child-txn');
return ABORT;
})
should.equal(db.get('key3'), 'test-sync-txn');
db.childTransaction(() => {
db.put('key3', 'test-child-txn');
})
should.equal(db.get('key3'), 'test-child-txn');
await db.childTransaction(async () => {
await new Promise(resolve => setTimeout(resolve, 1))
db.put('key3', 'test-async-child-txn');
})
should.equal(db.get('key3'), 'test-async-child-txn');
})
});
it('async transaction with interrupting sync transaction default order', async function() {
for (let i =0; i< 10;i++) {
db.strictAsyncOrder = true
let order = []
let ranSyncTxn
db.transactionAsync(() => {
order.push('a1');
db.put('async1', 'test');
if (!ranSyncTxn) {
ranSyncTxn = true;
setImmediate(() => {
db.transactionSync(() => {
order.push('s1');
db.put('inside-sync', 'test');
});
});
}
});
db.put('outside-txn', 'test');
await db.transactionAsync(() => {
order.push('a2');
db.put('async2', 'test');
});
order[0].should.equal('a1')
order.should.include('s1')
order.should.include('a2')
should.equal(db.get('async1'), 'test');
should.equal(db.get('outside-txn'), 'test');
should.equal(db.get('inside-sync'), 'test');
should.equal(db.get('async2'), 'test');
}
});
it('multiple async mixed', async function() {
let result
for (let i = 0; i < 100; i++) {
if (i%4 < 3) {
if (i%8 == 1) {
let sync = () => db.transactionSync(() => {
db.put('foo' + i, i)
})
if (i%16 == 1)
setImmediate(sync)
else
sync()
continue
}
db.strictAsyncOrder = i%4 == 2
result = db.transaction(() => {
db.put('foo' + i, i)
})
} else {
result = db.put('foo' + i, i)
}
}
await result
for (let i = 0; i < 100; i++) {
should.equal(db.get('foo' + i), i)
}
})
it('big child transactions', async function() {
let ranTransaction
db.put('key1', 'async initial value'); // should be queued for async write, but should put before queued transaction
let errorHandled
if (!db.cache) {
db.childTransaction(() => {
let value
for (let i = 0; i < 5000; i++) {
db.put('key' + i, 'test')
}
})
await db.put('key1', 'test');
should.equal(db.get('key1'), 'test');
}
});
it('handle write transaction with hanging cursors', async function() {
db.put('c1', 'value1');
db.put('c2', 'value2');
db.put('c3', 'value3');
await db.committed
let iterator
db.transactionSync(() => {
if (db.cache) {
iterator = db.getRange({ start: 'c1' })[Symbol.iterator]();
should.equal(iterator.next().value.value, 'value1');
} else {
db.childTransaction(() => {
iterator = db.getRange({ start: 'c1' })[Symbol.iterator]();
should.equal(iterator.next().value.value, 'value1');
});
}
should.equal(iterator.next().value.value, 'value2');
});
should.equal(iterator.next().value.value, 'value3');
});
it('mixed batches', async function() {
let promise
for (let i = 0; i < 20; i++) {
db.put(i, 'test')
promise = db.batch(() => {
for (let j = 0; j < 20; j++) {
db.put('test:' + i + '/' + j, i + j)
}
})
}
await promise
for (let i = 0; i < 20; i++) {
should.equal(db.get(i), 'test');
for (let j = 0; j < 20; j++) {
should.equal(db.get('test:' + i + '/' + j), i + j)
}
}
});
it('levelup style callback', function(done) {
should.equal(db.isOperational(), true)
should.equal(db.status, 'open')
should.equal(db.supports.permanence, true)
db.put('key1', '1', (error, result) => {
should.equal(error, null)
'1'.should.equal(db.get('key1'))
db.del('key1', (error, result) => {
should.equal(error, null)
let leveldb = levelup(db)
leveldb.get('key1', (error, value) => {
should.equal(error.name, 'NotFoundError')
leveldb.put('key1', 'test', (error, value) => {
leveldb.getMany(['key1'], (error, values) => {
should.equal('test', values[0])
done();
})
})
})
})
})
});
it('batch operations', async function() {
let batch = db.batch()
batch.put('test:z', 'z')
batch.clear()
batch.put('test:a', 'a')
batch.put('test:b', 'b')
batch.put('test:c', 'c')
batch.del('test:c')
let callbacked
await batch.write(() => { callbacked = true })
should.equal(callbacked, true)
should.equal(db.get('test:a'), 'a')
should.equal(db.get('test:b'), 'b')
should.equal(db.get('test:c'), undefined)
should.equal(db.get('test:d'), undefined)
});
it('batch array', async function() {
await db.batch([
{type: 'put', key: 'test:a', value: 1 },
{type: 'put', key: 'test:b', value: 2 },
{type: 'put', key: 'test:c', value: 3 },
{type: 'del', key: 'test:c' },
])
should.equal(db.get('test:a'), 1)
should.equal(db.get('test:b'), 2)
should.equal(db.get('test:c'), undefined)
});
it('read and write with binary encoding', async function() {
let dbBinary = db.openDB(Object.assign({
name: 'mydb5',
encoding: 'binary'
}));
dbBinary.put('buffer', Buffer.from('hello'));
dbBinary.put('empty', Buffer.from([]));
let big = new Uint8Array(0x21000);
big.fill(3);
dbBinary.put('big', big);
let promise = dbBinary.put('Uint8Array', new Uint8Array([1,2,3]));
await promise
await promise.flushed
dbBinary.get('big')[3].should.equal(3);
dbBinary.get('buffer').toString().should.equal('hello');
dbBinary.get('big')[3].should.equal(3);
dbBinary.get('Uint8Array')[1].should.equal(2);
dbBinary.get('empty').length.should.equal(0);
dbBinary.get('big')[3].should.equal(3);
dbBinary.get('big')[3].should.equal(3);
dbBinary.getBinaryFast('big')[3].should.equal(3);
dbBinary.getBinaryFast('big')[3].should.equal(3); // do it twice to test detach the previous one
dbBinary.get('Uint8Array')[1].should.equal(2);
Array.from(dbBinary.getRange({ start: 'big' }))[0].value[3].should.equal(3);
Array.from(dbBinary.getRange({ start: 'big' }))[0].value[3].should.equal(3);
});
it('read and write with binary encoding of key and value', async function() {
let dbBinary = db.openDB({
name: 'mydb-binary',
encoding: 'binary',
keyEncoding: 'binary'
});
let k = Buffer.from("key");
let v = Buffer.from("value");
await dbBinary.put(k, v);
let count = 0;
for (let { key, value } of dbBinary.getRange({})) {
should.equal(key.constructor, Buffer);
should.equal(key.length, 3);
should.equal(value.constructor, Buffer);
should.equal(value.length, 5);
count++;
}
should.equal(count, 1);
});
it.skip('large txn', async function(){
while(true) {
for (let i = 0; i < 5000000; i++) {
db.put(i, i);
}
await db.committed;
await db.clearAsync();
}
})
it('open and close with compression', async function() {
let data = ''
for (let i = 0; i < 100; i++) {
data += Math.random()
}
for (let i = 0; i < 10; i++) {
let db = open(testDirPath + '/təst-close.mdb', {
// name: 'test-close',
compression: true,
overlappingSync: true,
});
let v = db.get('key')
v = db.get('key1')
v = db.get('key2')
v = db.get('key3')
db.put('key', data);
let promise = db.close();
expect(() => db.put('key1', data)).to.throw();
await promise;
}
})
after(function(done) {
db.get('key1');
let iterator = db.getRange({})[Symbol.iterator]()
setTimeout(async () => {
db.get('key1');