forked from Venemo/node-lmdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.js
1391 lines (1319 loc) · 39.6 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
'use strict';
var path = require('path');
var mkdirp = require('mkdirp');
var rimraf = require('rimraf');
var chai = require('chai');
var should = chai.should();
var spawn = require('child_process').spawn;
var lmdb = require('..');
const MAX_DB_SIZE = 256 * 1024 * 1024;
describe('Node.js LMDB Bindings', function() {
var testDirPath = path.resolve(__dirname, './testdata');
var testBackupDirPath = path.resolve(__dirname, './testdata/backup');
// 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);
}
// setup clean directory
mkdirp(testBackupDirPath).then(function() {
done();
}, function(err) {
done(err);
});
});
});
it('will construct, open and close an environment', function() {
this.timeout(10000);
var env = new lmdb.Env();
env.open({
path: testDirPath,
maxDbs: 10
});
env.close.should.be.a('function');
env.beginTxn.should.be.a('function');
env.openDbi.should.be.a('function');
env.sync.should.be.a('function');
env.resize.should.be.a('function');
env.stat.should.be.a('function');
env.info.should.be.a('function');
env.close();
});
describe('Basics', function() {
this.timeout(10000);
var env;
before(function() {
env = new lmdb.Env();
env.open({
path: testDirPath,
maxDbs: 10,
maxReaders: 422,
mapSize: MAX_DB_SIZE
});
});
after(function() {
env.close();
});
it('will attempt to create two write transactions', function () {
var wtxn1 = env.beginTxn();
(function() {
var wtxn2 = env.beginTxn();
}).should.throw("You have already opened a write transaction in the current process, can't open a second one.");
wtxn1.abort();
});
it('will open a database, begin a transaction and get/put/delete data', function() {
var dbi = env.openDbi({
name: 'mydb1',
create: true
});
var txn = env.beginTxn();
var data = txn.getString(dbi, 'hello');
should.equal(data, null);
txn.putString(dbi, 'hello', 'Hello world!');
var data2 = txn.getString(dbi, 'hello');
data2.should.equal('Hello world!');
txn.del(dbi, 'hello');
var data3 = txn.getString(dbi, 'hello');
should.equal(data3, null);
txn.commit();
dbi.close();
});
it('env.openDbi should throw an error when invalid parameters are passed', function() {
chai.assert.throw(function () {
env.openDbi();
});
chai.assert.throw(function () {
env.openDbi(null);
});
chai.assert.throw(function () {
env.openDbi(1);
});
});
it('will open a database and empty the database without closing it', function() {
var dbi = env.openDbi({
name: 'mydb1',
create: true
});
dbi.drop({
justFreePages: true
});
dbi.close();
});
it.skip('will open a database, begin a transaction and get/put/delete string data containing zeros', function() {
var dbi = env.openDbi({
name: 'mydb1x',
create: true
});
// dbi.close();
var txn = env.beginTxn({readOnly: true});
/* var dbi = env.openDbi({
name: 'mydb1x',
//txn
});*/
var data = txn.getString(dbi, 'hello');
txn.reset();
var txn = env.beginTxn();
should.equal(data, null);
txn.putString(dbi, 'hello', 'Hello \0 world!');
var data2 = txn.getString(dbi, 'hello');
data2.should.equal('Hello \0 world!');
txn.del(dbi, 'hello');
var data3 = txn.getString(dbi, 'hello');
should.equal(data3, null);
txn.commit();
dbi.close();
});
it('will check if UTF-16 Buffers can be read as strings', function() {
// The string we want to store using a buffer
var expectedString = 'Hello \0 world!';
// node-lmdb internally stores a terminating zero, so we need to manually emulate that here
// NOTE: this would NEVER work without 'utf16le'!
var buf = Buffer.from(expectedString + '\0', 'utf16le');
var key = 'hello';
// Open dbi and create cursor
var dbi = env.openDbi({
name: 'mydb1xx',
create: true
});
var txn = env.beginTxn();
// Check non-existence of the key
var data1 = txn.getBinary(dbi, key);
should.equal(data1, null);
// Store data as binary
txn.putBinary(dbi, key, buf);
// Retrieve data as binary and check
var data2 = txn.getBinary(dbi, key);
should.equal(buf.compare(data2), 0);
// Retrieve same data as string and check
var data3 = txn.getString(dbi, key);
should.equal(data3, expectedString);
// Delete data
txn.del(dbi, key);
// Put new binary data without zero termination
txn.putBinary(dbi, key, Buffer.from(expectedString));
// Verify that you can't read it back as a string
(function () {
var data = txn.getString(dbi, key);
}).should.throw('Invalid zero-terminated UTF-16 string');
// Delete data
txn.del(dbi, key);
// Verify non-existence of data
var data3 = txn.getBinary(dbi, key);
should.equal(data3, null);
txn.commit();
dbi.close();
});
it('will throw Javascript error if named database cannot be found', function () {
try {
env.openDbi({
name: 'does-not-exist',
create: false
});
} catch (err) {
err.should.be.an.instanceof(Error);
}
});
it('will get information about an environment', function() {
var info = env.info();
info.mapAddress.should.be.a('number');
info.mapSize.should.be.a('number');
info.lastPageNumber.should.be.a('number');
info.lastTxnId.should.be.a('number');
info.maxReaders.should.be.a('number');
info.numReaders.should.be.a('number');
should.equal(info.mapSize, MAX_DB_SIZE);
should.equal(info.maxReaders, 422);
});
it('will check for open transactions before resizing the mapSize', function() {
var dbi = env.openDbi({
name: 'mydb1',
create: true
});
var info = env.info();
should.equal(info.mapSize, MAX_DB_SIZE);
// Open write transaction
var txn = env.beginTxn();
try {
env.resize(info.mapSize * 2);
} catch (err) {
err.should.be.an.instanceof(Error);
}
txn.abort();
info = env.info();
should.equal(info.mapSize, MAX_DB_SIZE);
// Open readOnly transaction
txn = env.beginTxn({ readOnly: true });
try {
env.resize(info.mapSize * 2);
} catch (err) {
err.should.be.an.instanceof(Error);
}
txn.abort();
info = env.info();
should.equal(info.mapSize, MAX_DB_SIZE);
dbi.close();
});
it('will resize the mapSize', function() {
var dbi = env.openDbi({
name: 'mydb1',
create: true
});
var info = env.info();
should.equal(info.mapSize, MAX_DB_SIZE);
env.resize(info.mapSize * 2);
info = env.info();
should.equal(info.mapSize, 2 * MAX_DB_SIZE);
dbi.close();
});
it('will get statistics about an environment', function() {
var stat = env.stat();
stat.pageSize.should.be.a('number');
stat.treeDepth.should.be.a('number');
stat.treeBranchPageCount.should.be.a('number');
stat.treeLeafPageCount.should.be.a('number');
stat.entryCount.should.be.a('number');
});
it('will get statistics about a database', function() {
var dbi = env.openDbi({
name: 'mydb2',
create: true
});
var txn = env.beginTxn();
var stat = dbi.stat(txn);
stat.pageSize.should.be.a('number');
stat.treeDepth.should.be.a('number');
stat.treeBranchPageCount.should.be.a('number');
stat.treeLeafPageCount.should.be.a('number');
stat.entryCount.should.be.a('number');
txn.abort();
dbi.close();
});
it('will create a database with a user-supplied transaction', function () {
var txn = env.beginTxn();
var dbi = env.openDbi({
name: 'dbUsingUserSuppliedTxn',
create: true,
txn: txn
});
txn.putString(dbi, 'hello', 'world');
txn.commit();
var txn = env.beginTxn({ readOnly: true });
var str = txn.getString(dbi, 'hello');
should.equal(str, 'world');
txn.abort();
dbi.close();
});
it('will create a database and back it up', function (done) {
var txn = env.beginTxn();
var dbi = env.openDbi({
name: 'backup',
create: true,
txn: txn
});
txn.putString(dbi, 'hello', 'world');
txn.commit();
env.copy(testBackupDirPath, (error) => {
done(error)
});
// console.log('sent copy')
});
});
describe('Data types', function() {
this.timeout(10000);
var env;
var dbi;
var txn;
before(function() {
env = new lmdb.Env();
env.open({
path: testDirPath,
maxDbs: 10
});
dbi = env.openDbi({
name: 'mydb3',
create: true
});
txn = env.beginTxn();
});
after(function() {
txn.commit();
dbi.close();
env.close();
});
it('string', function() {
txn.putString(dbi, 'key1', 'Hello world!');
var data = txn.getString(dbi, 'key1');
data.should.equal('Hello world!');
txn.del(dbi, 'key1');
var data2 = txn.getString(dbi, 'key1');
should.equal(data2, null);
});
it('string (zero copy)', function() {
txn.putString(dbi, 'key1', 'Hello world!');
var data = txn.getStringUnsafe(dbi, 'key1');
data.should.equal('Hello world!');
txn.del(dbi, 'key1');
var data2 = txn.getStringUnsafe(dbi, 'key1');
should.equal(data2, null);
});
it('binary', function() {
var buffer = new Buffer('48656c6c6f2c20776f726c6421', 'hex');
txn.putBinary(dbi, 'key2', buffer);
var data = txn.getBinary(dbi, 'key2');
data.should.deep.equal(buffer);
txn.del(dbi, 'key2');
var data2 = txn.getBinary(dbi, 'key2');
should.equal(data2, null);
});
it('binary (zero copy)', function() {
var buffer = new Buffer('48656c6c6f2c20776f726c6421', 'hex');
txn.putBinary(dbi, 'key2', buffer);
var data = txn.getBinaryUnsafe(dbi, 'key2');
var byte = data[0]; // make sure we can access it
env.detachBuffer(data.buffer);
var data = txn.getBinaryUnsafe(dbi, 'key2');
var byte = data[0]; // make sure we can access it
data.should.deep.equal(buffer);
env.detachBuffer(data.buffer);
txn.del(dbi, 'key2');
var data2 = txn.getBinaryUnsafe(dbi, 'key2');
should.equal(data2, null);
});
it('binary key', function() {
var buffer = new Buffer('48656c6c6f2c20776f726c6421', 'hex');
var key = new Buffer('key2');
txn.putBinary(dbi, key, buffer);
var data = txn.getBinary(dbi, key);
data.should.deep.equal(buffer);
txn.del(dbi, key, { keyIsBuffer: true });
var data2 = txn.getBinary(dbi, key);
should.equal(data2, null);
});
it('number', function() {
txn.putNumber(dbi, 'key3', 9007199254740991);
var data = txn.getNumber(dbi, 'key3');
data.should.equal(Math.pow(2, 53) - 1);
txn.del(dbi, 'key3');
var data2 = txn.getNumber(dbi, 'key3');
should.equal(data2, null);
});
it('boolean', function() {
txn.putBoolean(dbi, 'key4', true);
var data = txn.getBoolean(dbi, 'key4');
data.should.equal(true);
txn.putBoolean(dbi, 'key5', false);
var data2 = txn.getBoolean(dbi, 'key5');
data2.should.equal(false);
txn.del(dbi, 'key4');
txn.del(dbi, 'key5');
var data3 = txn.getBoolean(dbi, 'key4');
var data4 = txn.getBoolean(dbi, 'key5');
should.equal(data3, null);
should.equal(data4, null);
});
});
describe('Multiple transactions', function() {
this.timeout(10000);
var env;
var dbi;
before(function() {
env = new lmdb.Env();
env.open({
path: testDirPath,
maxDbs: 10
});
dbi = env.openDbi({
name: 'mydb4',
create: true,
keyIsUint32: true
});
var txn = env.beginTxn();
txn.putString(dbi, 1, 'Hello1');
txn.putString(dbi, 2, 'Hello2');
txn.commit();
});
after(function() {
dbi.close();
env.close();
});
it('readonly transaction should not see uncommited changes', function() {
var readTxn = env.beginTxn({readOnly: true});
var data = readTxn.getString(dbi, 1);
should.equal(data, 'Hello1');
var writeTxn = env.beginTxn();
writeTxn.putString(dbi, 1, 'Ha ha ha');
var data2 = writeTxn.getString(dbi, 1);
data2.should.equal('Ha ha ha');
var data3 = readTxn.getString(dbi, 1);
should.equal(data3, 'Hello1');
writeTxn.commit();
var data4 = readTxn.getString(dbi, 1);
should.equal(data4, 'Hello1');
readTxn.reset();
readTxn.renew();
var data5 = readTxn.getString(dbi, 1);
should.equal(data5, 'Ha ha ha');
readTxn.abort();
});
it('readonly transaction will throw if tries to write', function() {
var readTxn = env.beginTxn({readOnly: true});
(function() {
readTxn.putString(dbi, 2, 'hööhh');
}).should.throw('Permission denied');
readTxn.abort();
});
});
describe('Cursors, basic operation', function() {
this.timeout(10000);
var env;
var dbi;
var total = 50;
before(function() {
env = new lmdb.Env();
env.open({
path: testDirPath,
maxDbs: 10,
mapSize: MAX_DB_SIZE
});
dbi = env.openDbi({
name: 'cursor_verybasic',
create: true
});
const txn = env.beginTxn();
let count = 0;
while (count < total) {
let key = "hello_" + count.toString(16);
let data = key + "_data";
txn.putString(dbi, key, data);
count++;
}
txn.commit();
});
it('will move cursor over values, expects to get correct key', function (done) {
var txn = env.beginTxn({ readOnly: true });
var cursor = new lmdb.Cursor(txn, dbi);
var count;
for (count = 0; count < total; count ++) {
var expectedKey = "hello_" + count.toString(16);
var key = cursor.goToKey(expectedKey);
should.equal(expectedKey, key);
}
should.equal(count, total);
count = 0;
for (var key = cursor.goToFirst(); key; key = cursor.goToNext()) {
var key2 = cursor.goToKey(key);
should.equal(key, key2);
count ++;
}
should.equal(count, total);
cursor.close();
txn.abort();
done();
});
it('will move cursor over values, expects to get correct key even if key is binary', function (done) {
var txn = env.beginTxn({ readOnly: true });
var cursor = new lmdb.Cursor(txn, dbi);
var count;
for (count = 0; count < total; count ++) {
var expectedKey = "hello_" + count.toString(16);
var binaryKey = new Buffer(expectedKey + "\0", "utf16le");
var key = cursor.goToKey(binaryKey);
should.equal(expectedKey, key);
should.equal(binaryKey.toString("utf16le"), key + "\0");
}
should.equal(count, total);
count = 0;
for (var key = cursor.goToFirst(); key; key = cursor.goToNext()) {
var key2 = cursor.goToKey(new Buffer(key + "\0", "utf16le"), { keyIsBuffer: true });
should.equal(key, key2);
count ++;
}
should.equal(count, total);
cursor.close();
txn.abort();
done();
});
after(function () {
dbi.close();
env.close();
});
});
describe('Cursors', function() {
this.timeout(10000);
var env;
var dbi;
var total = 1000;
before(function() {
env = new lmdb.Env();
env.open({
path: testDirPath,
maxDbs: 10,
mapSize: MAX_DB_SIZE
});
dbi = env.openDbi({
name: 'mydb5',
create: true,
dupSort: false,
keyIsUint32: true
});
var txn = env.beginTxn();
var c = 0;
while(c < total) {
var buffer = new Buffer(new Array(8));
buffer.writeDoubleBE(c);
txn.putBinary(dbi, c, buffer);
c++;
}
txn.commit();
});
after(function() {
dbi.close();
env.close();
});
it('will move cursor over key/values', function(done) {
var txn = env.beginTxn();
var cursor = new lmdb.Cursor(txn, dbi);
cursor.goToKey(40);
cursor.getCurrentBinary(function(key, value) {
key.should.equal(40);
value.readDoubleBE().should.equal(40);
});
var values = [];
cursor.goToKey(0);
function iterator() {
cursor.getCurrentBinary(function(key, value) {
value.readDoubleBE().should.equal(values.length);
values.push(value);
});
cursor.goToNext();
if (values.length < total) {
// prevent maximum call stack errors
if (values.length % 1000 === 0) {
setImmediate(iterator);
} else {
iterator();
}
} else {
cursor.close();
txn.abort();
done();
}
}
iterator();
});
it('will move cursor over key/values (zero copy)', function(done) {
var txn = env.beginTxn();
var cursor = new lmdb.Cursor(txn, dbi);
cursor.goToKey(40);
cursor.getCurrentBinaryUnsafe(function(key, value) {
key.should.equal(40);
value.readDoubleBE().should.equal(40);
});
var values = [];
cursor.goToKey(0);
function iterator() {
cursor.getCurrentBinaryUnsafe(function(key, value) {
value.readDoubleBE().should.equal(values.length);
values.push(value);
});
cursor.goToNext();
if (values.length < total) {
// prevent maximum call stack errors
if (values.length % 1000 === 0) {
setImmediate(iterator);
} else {
iterator();
}
} else {
cursor.close();
txn.abort();
done();
}
}
iterator();
});
it('will first/last key', function() {
var txn = env.beginTxn();
var cursor = new lmdb.Cursor(txn, dbi);
cursor.goToFirst();
cursor.getCurrentBinary(function(key, value) {
key.should.equal(0);
value.readDoubleBE().should.equal(0);
});
cursor.goToLast();
cursor.getCurrentBinary(function(key, value) {
key.should.equal(total - 1);
value.readDoubleBE().should.equal(total - 1);
});
cursor.close();
txn.abort();
});
});
describe('Cursors, dupsort', function() {
this.timeout(10000);
var env;
var dbi;
var total = 50;
var dataCount = {};
before(function() {
env = new lmdb.Env();
env.open({
path: testDirPath,
maxDbs: 10,
mapSize: MAX_DB_SIZE
});
dbi = env.openDbi({
name: 'cursor_dupsort',
create: true,
dupSort: true
});
const txn = env.beginTxn();
var count;
for (count = 0; count < total; count ++) {
let key = "hello_" + count.toString(16);
let data = key + "_data";
dataCount[key] = (count % 4) + 1;
for (var j = 0; j < dataCount[key]; j++) {
txn.putString(dbi, key, data + String(j));
}
}
txn.commit();
});
it('will move cursor over values, expects to get correct key and data', function (done) {
var txn = env.beginTxn({ readOnly: true });
var cursor = new lmdb.Cursor(txn, dbi);
var count;
for (count = 0; count < total; count ++) {
var expectedKey = "hello_" + count.toString(16);
var expectedDataX = expectedKey + "_data";
var key = cursor.goToRange(expectedKey);
should.equal(expectedKey, key);
var data = cursor.getCurrentString();
should.equal(expectedDataX + "0", data);
var dc;
// Iterate over dup keys
dc = 0;
for (var k = cursor.goToFirstDup(); k; k = cursor.goToNextDup()) {
var data = cursor.getCurrentString();
should.equal(expectedKey, k);
should.equal(expectedDataX + String(dc), data);
dc ++;
}
should.equal(dataCount[key], dc);
// Iterate over dup keys by using goToDup first
dc = 0;
for (var k = cursor.goToDup(expectedKey, expectedDataX + "0"); k; k = cursor.goToNextDup()) {
var data = cursor.getCurrentString();
should.equal(expectedKey, k);
should.equal(expectedDataX + String(dc), data);
dc ++;
}
should.equal(dataCount[key], dc);
}
should.equal(count, total);
count = 0;
cursor.close();
txn.abort();
done();
});
after(function () {
dbi.close();
env.close();
});
});
describe('Cursors (with strings)', function() {
this.timeout(10000);
var env;
var dbi;
var total = 1000;
before(function() {
env = new lmdb.Env();
env.open({
path: testDirPath,
maxDbs: 10,
mapSize: MAX_DB_SIZE
});
dbi = env.openDbi({
name: 'cursorstrings',
create: true,
dupSort: true,
keyIsUint32: true
});
var txn = env.beginTxn();
var c = 0;
while(c < total) {
txn.putString(dbi, c, c.toString());
c++;
}
txn.commit();
});
after(function() {
dbi.close();
env.close();
});
it('will move cursor over key/values (zero copy)', function(done) {
var txn = env.beginTxn();
var cursor = new lmdb.Cursor(txn, dbi);
cursor.goToKey(40);
cursor.getCurrentStringUnsafe(function(key, value) {
key.should.equal(40);
value.should.equal('40');
});
var values = [];
cursor.goToKey(0);
function iterator() {
cursor.getCurrentStringUnsafe(function(key, value) {
value.should.equal(values.length.toString());
values.push(value);
});
cursor.goToNext();
if (values.length < total) {
// prevent maximum call stack errors
if (values.length % 1000 === 0) {
setImmediate(iterator);
} else {
iterator();
}
} else {
cursor.close();
txn.abort();
done();
}
}
iterator();
});
it('will move cursor over key/values', function(done) {
var txn = env.beginTxn();
var cursor = new lmdb.Cursor(txn, dbi);
cursor.goToKey(40);
cursor.getCurrentString(function(key, value) {
key.should.equal(40);
value.should.equal('40');
});
var values = [];
cursor.goToKey(0);
function iterator() {
cursor.getCurrentString(function(key, value) {
value.should.equal(values.length.toString());
values.push(value);
});
cursor.goToNext();
if (values.length < total) {
// prevent maximum call stack errors
if (values.length % 1000 === 0) {
setImmediate(iterator);
} else {
iterator();
}
} else {
cursor.close();
txn.abort();
done();
}
}
iterator();
});
it('will first/last key', function(done) {
var txn = env.beginTxn();
var cursor = new lmdb.Cursor(txn, dbi);
cursor.goToFirst();
cursor.getCurrentString(function(key, value) {
key.should.equal(0);
value.should.equal('0');
});
cursor.goToLast();
cursor.getCurrentString(function(key, value) {
key.should.equal(total - 1);
value.should.equal((total - 1).toString());
});
cursor.close();
txn.abort();
done();
});
});
describe('Cursors with binary key and data', function() {
this.timeout(10000);
var env;
var dbi;
var total = 1000;
let padding = '000000000';
let keyEnc = 'utf8';
let valueEnc = 'utf8';
before(function() {
env = new lmdb.Env();
env.open({
path: testDirPath,
maxDbs: 10,
mapSize: MAX_DB_SIZE
});
dbi = env.openDbi({
name: 'cursorbinkeydata',
create: true,
keyIsBuffer: true
});
const txn = env.beginTxn();
let count = 0;
while (count < total) {
let keyStr = (padding + count).slice(-padding.length);
let dataStr = expand(keyStr);
let key = Buffer.from(keyStr,keyEnc);
let data = Buffer.from(dataStr,valueEnc) // just for kicks.
txn.putBinary(dbi, key, data);
count++;
}
txn.commit();
});
after(function() {
dbi.close();
env.close();
});
it('will move cursor over key/values', function(done) {
var txn = env.beginTxn();
var cursor = new lmdb.Cursor(txn, dbi);
let expectedKey = (padding + 40).slice(-padding.length);
let key = Buffer.from(expectedKey,keyEnc);
cursor.goToKey(key);
cursor.getCurrentBinary(function(key, value) {
let readKey = key.toString(keyEnc);
let readValue = value.toString(valueEnc);
readKey.should.equal(expectedKey);
readValue.should.equal(expand(expectedKey));
});
let count = 0;
key = cursor.goToFirst();
// key is a string... bug...
//(typeof key).should.not.equal('string');
while (key && count < total+1) { //+1 to run off end if fails to return null
let expectedKey = (padding + count).slice(-padding.length);
cursor.getCurrentBinary(function(key, value) {
(typeof key).should.not.equal('string');
let readKey = key.toString(keyEnc);
let readValue = value.toString(valueEnc);
readKey.should.equal(expectedKey);
readValue.should.equal(expand(expectedKey));
});
key = cursor.goToNext();
(typeof key).should.not.equal('string');
count++;
}
cursor.close();
txn.commit();
count.should.equal(total);
done();
});
});
describe('Cursors reading existing binary key and data', function() {
this.timeout(10000);
var env;
var dbi;
var total = 1000;
let padding = '000000000';
let keyEnc = 'utf8';
let valueEnc = 'utf8';
before(function() {
env = new lmdb.Env();
env.open({
path: testDirPath,
maxDbs: 10,
mapSize: MAX_DB_SIZE
});
dbi = env.openDbi({
name: 'cursorbinkeydata',
create: false,
keyIsBuffer: true
});
});
after(function() {
dbi.close();
env.close();
});
it('will move cursor over existing key/values', function(done) {
var txn = env.beginTxn();
var cursor = new lmdb.Cursor(txn, dbi);
let expectedKey = (padding + 40).slice(-padding.length);
let key = Buffer.from(expectedKey,keyEnc);
cursor.goToKey(key);
let rvalue = cursor.getCurrentBinary(function(key, value) {
(typeof key).should.not.equal('string');
let readKey = key.toString(keyEnc);
let readValue = value.toString(valueEnc);
readKey.should.equal(expectedKey);
readValue.should.equal(expand(expectedKey));
});
rvalue.toString(valueEnc).should.equal(expand(expectedKey));
let count = 0;
key = cursor.goToFirst();
(typeof key).should.not.equal('string');
while (key && count < total+1) { //+1 to run off end if fails to return null
let expectedKey = (padding + count).slice(-padding.length);
cursor.getCurrentBinary(function(key, value) {
let readKey = key.toString(keyEnc);
let readValue = value.toString(valueEnc);
readKey.should.equal(expectedKey);
readValue.should.equal(expand(expectedKey));
});
key = cursor.goToNext();
(typeof key).should.not.equal('string');
count++;
}
cursor.close();
txn.commit();
count.should.equal(total);
done();
});
});
describe('Cluster', function() {
this.timeout(10000);
it('will run a cluster of processes with read-only transactions', function(done) {
var child = spawn('node', [path.resolve(__dirname, './cluster')]);
child.stdout.on('data', function(data) {