-
Notifications
You must be signed in to change notification settings - Fork 44
/
read.js
1182 lines (1170 loc) · 33.4 KB
/
read.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 { RangeIterable } from './util/RangeIterable.js';
import {
getAddress,
Cursor,
Txn,
orderedBinary,
lmdbError,
getByBinary,
setGlobalBuffer,
prefetch,
iterate,
position as doPosition,
resetTxn,
getCurrentValue,
getCurrentShared,
getStringByBinary,
globalBuffer,
getSharedBuffer,
startRead,
setReadCallback,
directWrite,
getUserSharedBuffer,
notifyUserCallbacks,
attemptLock,
unlock,
} from './native.js';
import { saveKey } from './keys.js';
const IF_EXISTS = 3.542694326329068e-103;
const DEFAULT_BEGINNING_KEY = Buffer.from([5]); // the default starting key for iteration, which excludes symbols/metadata
const ITERATOR_DONE = { done: true, value: undefined };
const Uint8ArraySlice = Uint8Array.prototype.slice;
let getValueBytes = globalBuffer;
if (!getValueBytes.maxLength) {
getValueBytes.maxLength = getValueBytes.length;
getValueBytes.isGlobal = true;
Object.defineProperty(getValueBytes, 'length', {
value: getValueBytes.length,
writable: true,
configurable: true,
});
}
const START_ADDRESS_POSITION = 4064;
const NEW_BUFFER_THRESHOLD = 0x8000;
const SOURCE_SYMBOL = Symbol.for('source');
export const UNMODIFIED = {};
let mmaps = [];
export function addReadMethods(
LMDBStore,
{ maxKeySize, env, keyBytes, keyBytesView, getLastVersion, getLastTxnId },
) {
let readTxn,
readTxnRenewed,
asSafeBuffer = false;
let renewId = 1;
let outstandingReads = 0;
Object.assign(LMDBStore.prototype, {
getString(id, options) {
let txn =
env.writeTxn ||
(options && options.transaction) ||
(readTxnRenewed ? readTxn : renewReadTxn(this));
let string = getStringByBinary(
this.dbAddress,
this.writeKey(id, keyBytes, 0),
txn.address || 0,
);
if (typeof string === 'number') {
// indicates the buffer wasn't large enough
this._allocateGetBuffer(string);
// and then try again
string = getStringByBinary(
this.dbAddress,
this.writeKey(id, keyBytes, 0),
txn.address || 0,
);
}
if (string) this.lastSize = string.length;
return string;
},
getBinaryFast(id, options) {
let rc;
let txn =
env.writeTxn ||
(options && options.transaction) ||
(readTxnRenewed ? readTxn : renewReadTxn(this));
rc = this.lastSize = getByBinary(
this.dbAddress,
this.writeKey(id, keyBytes, 0),
(options && options.ifNotTxnId) || 0,
txn.address || 0,
);
if (rc < 0) {
if (rc == -30798)
// MDB_NOTFOUND
return; // undefined
if (rc == -30004)
// txn id matched
return UNMODIFIED;
if (
rc == -30781 /*MDB_BAD_VALSIZE*/ &&
this.writeKey(id, keyBytes, 0) == 0
)
throw new Error(
id === undefined
? 'A key is required for get, but is undefined'
: 'Zero length key is not allowed in LMDB',
);
if (rc == -30000)
// int32 overflow, read uint32
rc = this.lastSize = keyBytesView.getUint32(0, true);
else if (rc == -30001) {
// shared buffer
this.lastSize = keyBytesView.getUint32(0, true);
let bufferId = keyBytesView.getUint32(4, true);
let bytes = getMMapBuffer(bufferId, this.lastSize);
return asSafeBuffer ? Buffer.from(bytes) : bytes;
} else throw lmdbError(rc);
}
let compression = this.compression;
let bytes = compression ? compression.getValueBytes : getValueBytes;
if (rc > bytes.maxLength) {
// this means the target buffer wasn't big enough, so the get failed to copy all the data from the database, need to either grow or use special buffer
return this._returnLargeBuffer(() =>
getByBinary(
this.dbAddress,
this.writeKey(id, keyBytes, 0),
0,
txn.address || 0,
),
);
}
bytes.length = this.lastSize;
return bytes;
},
getBFAsync(id, options, callback) {
let txn =
env.writeTxn ||
(options && options.transaction) ||
(readTxnRenewed ? readTxn : renewReadTxn(this));
txn.refCount = (txn.refCount || 0) + 1;
outstandingReads++;
if (!txn.address) {
throw new Error('Invalid transaction, it has no address');
}
let address = recordReadInstruction(
txn.address,
this.db.dbi,
id,
this.writeKey,
maxKeySize,
(rc, bufferId, offset, size) => {
if (rc && rc !== 1) callback(lmdbError(rc));
outstandingReads--;
let buffer = mmaps[bufferId];
if (!buffer) {
buffer = mmaps[bufferId] = getSharedBuffer(bufferId, env.address);
}
//console.log({bufferId, offset, size})
if (buffer.isSharedMap) {
// using LMDB shared memory
// TODO: We may want explicit support for clearing aborting the transaction on the next event turn,
// but for now we are relying on the GC to cleanup transaction for larger blocks of memory
let bytes = new Uint8Array(buffer, offset, size);
bytes.txn = txn;
callback(bytes, 0, size);
} else {
// using copied memory
txn.done(); // decrement and possibly abort
callback(buffer, offset, size);
}
},
);
if (address) {
startRead(address, () => {
resolveReads();
});
}
},
getAsync(id, options, callback) {
let promise;
if (!callback) promise = new Promise((resolve) => (callback = resolve));
this.getBFAsync(id, options, (buffer, offset, size) => {
if (this.useVersions) {
// TODO: And get the version
offset += 8;
size -= 8;
}
let bytes = new Uint8Array(buffer, offset, size);
let value;
if (this.decoder) {
// the decoder potentially uses the data from the buffer in the future and needs a stable buffer
value = bytes && this.decoder.decode(bytes);
} else if (this.encoding == 'binary') {
value = bytes;
} else {
value = Buffer.prototype.utf8Slice.call(bytes, 0, size);
if (this.encoding == 'json' && value) value = JSON.parse(value);
}
callback(value);
});
return promise;
},
retain(data, options) {
if (!data) return;
let source = data[SOURCE_SYMBOL];
let buffer = source ? source.bytes : data;
if (!buffer.isGlobal && !env.writeTxn) {
let txn =
options?.transaction ||
(readTxnRenewed ? readTxn : renewReadTxn(this));
buffer.txn = txn;
txn.refCount = (txn.refCount || 0) + 1;
return data;
} else {
buffer = Uint8ArraySlice.call(buffer, 0, this.lastSize);
if (source) {
source.bytes = buffer;
return data;
} else return buffer;
}
},
_returnLargeBuffer(getFast) {
let bytes;
let compression = this.compression;
if (asSafeBuffer && this.lastSize > NEW_BUFFER_THRESHOLD) {
// used by getBinary to indicate it should create a dedicated buffer to receive this
let bytesToRestore;
try {
if (compression) {
bytesToRestore = compression.getValueBytes;
let dictionary = compression.dictionary || [];
let dictLength = (dictionary.length >> 3) << 3; // make sure it is word-aligned
bytes = makeReusableBuffer(this.lastSize);
compression.setBuffer(
bytes.buffer,
bytes.byteOffset,
this.lastSize,
dictionary,
dictLength,
);
compression.getValueBytes = bytes;
} else {
bytesToRestore = getValueBytes;
setGlobalBuffer(
(bytes = getValueBytes = makeReusableBuffer(this.lastSize)),
);
}
getFast();
} finally {
if (compression) {
let dictLength = (compression.dictionary.length >> 3) << 3;
compression.setBuffer(
bytesToRestore.buffer,
bytesToRestore.byteOffset,
bytesToRestore.maxLength,
compression.dictionary,
dictLength,
);
compression.getValueBytes = bytesToRestore;
} else {
setGlobalBuffer(bytesToRestore);
getValueBytes = bytesToRestore;
}
}
return bytes;
}
// grow our shared/static buffer to accomodate the size of the data
bytes = this._allocateGetBuffer(this.lastSize);
// and try again
getFast();
bytes.length = this.lastSize;
return bytes;
},
_allocateGetBuffer(lastSize) {
let newLength = Math.min(Math.max(lastSize * 2, 0x1000), 0xfffffff8);
let bytes;
if (this.compression) {
let dictionary =
this.compression.dictionary || Buffer.allocUnsafeSlow(0);
let dictLength = (dictionary.length >> 3) << 3; // make sure it is word-aligned
bytes = Buffer.allocUnsafeSlow(newLength + dictLength);
bytes.set(dictionary); // copy dictionary into start
// the section after the dictionary is the target area for get values
bytes = bytes.subarray(dictLength);
this.compression.setBuffer(
bytes.buffer,
bytes.byteOffset,
newLength,
dictionary,
dictLength,
);
bytes.maxLength = newLength;
Object.defineProperty(bytes, 'length', {
value: newLength,
writable: true,
configurable: true,
});
this.compression.getValueBytes = bytes;
} else {
bytes = makeReusableBuffer(newLength);
setGlobalBuffer((getValueBytes = bytes));
}
bytes.isGlobal = true;
return bytes;
},
getBinary(id, options) {
try {
asSafeBuffer = true;
let fastBuffer = this.getBinaryFast(id, options);
return (
fastBuffer &&
(fastBuffer.isGlobal
? Uint8ArraySlice.call(fastBuffer, 0, this.lastSize)
: fastBuffer)
);
} finally {
asSafeBuffer = false;
}
},
getSharedBinary(id, options) {
let fastBuffer = this.getBinaryFast(id, options);
if (fastBuffer) {
if (fastBuffer.isGlobal || writeTxn)
return Uint8ArraySlice.call(fastBuffer, 0, this.lastSize);
fastBuffer.txn = options && options.transaction;
options.transaction.refCount = (options.transaction.refCount || 0) + 1;
return fastBuffer;
}
},
get(id, options) {
if (this.decoderCopies) {
// the decoder copies any data, so we can use the fast binary retrieval that overwrites the same buffer space
let bytes = this.getBinaryFast(id, options);
return (
bytes &&
(bytes == UNMODIFIED
? UNMODIFIED
: this.decoder.decode(bytes, options))
);
}
if (this.encoding == 'binary') return this.getBinary(id, options);
if (this.decoder) {
// the decoder potentially uses the data from the buffer in the future and needs a stable buffer
let bytes = this.getBinary(id, options);
return (
bytes &&
(bytes == UNMODIFIED ? UNMODIFIED : this.decoder.decode(bytes))
);
}
let result = this.getString(id, options);
if (result) {
if (this.encoding == 'json') return JSON.parse(result);
}
return result;
},
getEntry(id, options) {
let value = this.get(id, options);
if (value !== undefined) {
if (this.useVersions)
return {
value,
version: getLastVersion(),
//size: this.lastSize
};
else
return {
value,
//size: this.lastSize
};
}
},
directWrite(id, options) {
let rc;
let txn =
env.writeTxn ||
(options && options.transaction) ||
(readTxnRenewed ? readTxn : renewReadTxn(this));
let keySize = this.writeKey(id, keyBytes, 0);
let dataOffset = ((keySize >> 3) + 1) << 3;
keyBytes.set(options.bytes, dataOffset);
rc = directWrite(
this.dbAddress,
keySize,
options.offset,
options.bytes.length,
txn.address || 0,
);
if (rc < 0) lmdbError(rc);
},
getUserSharedBuffer(id, defaultBuffer, options) {
let keySize;
const setKeyBytes = () => {
if (options?.envKey) keySize = this.writeKey(id, keyBytes, 0);
else {
keyBytes.dataView.setUint32(0, this.db.dbi);
keySize = this.writeKey(id, keyBytes, 4);
}
};
setKeyBytes();
let sharedBuffer = getUserSharedBuffer(
env.address,
keySize,
defaultBuffer,
options?.callback,
);
sharedBuffer.notify = () => {
setKeyBytes();
return notifyUserCallbacks(env.address, keySize);
};
return sharedBuffer;
},
attemptLock(id, version, callback) {
keyBytes.dataView.setUint32(0, this.db.dbi);
keyBytes.dataView.setFloat64(4, version);
let keySize = this.writeKey(id, keyBytes, 12);
return attemptLock(env.address, keySize, callback);
},
unlock(id, version, onlyCheck) {
keyBytes.dataView.setUint32(0, this.db.dbi);
keyBytes.dataView.setFloat64(4, version);
let keySize = this.writeKey(id, keyBytes, 12);
return unlock(env.address, keySize, onlyCheck);
},
hasLock(id, version) {
return this.unlock(id, version, true);
},
resetReadTxn() {
resetReadTxn();
},
_commitReadTxn() {
if (readTxn) {
readTxn.isCommitted = true;
readTxn.commit();
}
lastReadTxnRef = null;
readTxnRenewed = null;
readTxn = null;
},
ensureReadTxn() {
if (!env.writeTxn && !readTxnRenewed) renewReadTxn(this);
},
doesExist(key, versionOrValue, options) {
if (versionOrValue == null) {
// undefined means the entry exists, null is used specifically to check for the entry *not* existing
return (
(this.getBinaryFast(key, options) === undefined) ==
(versionOrValue === null)
);
} else if (this.useVersions) {
return (
this.getBinaryFast(key, options) !== undefined &&
(versionOrValue === IF_EXISTS || getLastVersion() === versionOrValue)
);
} else {
if (versionOrValue && versionOrValue['\x10binary-data\x02'])
versionOrValue = versionOrValue['\x10binary-data\x02'];
else if (this.encoder)
versionOrValue = this.encoder.encode(versionOrValue);
if (typeof versionOrValue == 'string')
versionOrValue = Buffer.from(versionOrValue);
let defaultOptions = { start: versionOrValue, exactMatch: true };
return (
this.getValuesCount(
key,
options ? Object.assign(defaultOptions, options) : defaultOptions,
) > 0
);
}
},
getValues(key, options) {
let defaultOptions = {
key,
valuesForKey: true,
};
if (options && options.snapshot === false)
throw new Error('Can not disable snapshots for getValues');
return this.getRange(
options ? Object.assign(defaultOptions, options) : defaultOptions,
);
},
getKeys(options) {
if (!options) options = {};
options.values = false;
return this.getRange(options);
},
getCount(options) {
if (!options) options = {};
options.onlyCount = true;
return this.getRange(options).iterate();
},
getKeysCount(options) {
if (!options) options = {};
options.onlyCount = true;
options.values = false;
return this.getRange(options).iterate();
},
getValuesCount(key, options) {
if (!options) options = {};
options.key = key;
options.valuesForKey = true;
options.onlyCount = true;
return this.getRange(options).iterate();
},
getRange(options) {
let iterable = new RangeIterable();
let textDecoder = new TextDecoder();
if (!options) options = {};
let includeValues = options.values !== false;
let includeVersions = options.versions;
let valuesForKey = options.valuesForKey;
let limit = options.limit;
let db = this.db;
let snapshot = options.snapshot;
if (snapshot === false && this.dupSort && includeValues)
throw new Error(
'Can not disable snapshot on a' + ' dupSort data store',
);
let compression = this.compression;
iterable.iterate = () => {
const reverse = options.reverse;
let currentKey = valuesForKey
? options.key
: reverse || 'start' in options
? options.start
: DEFAULT_BEGINNING_KEY;
let count = 0;
let cursor, cursorRenewId, cursorAddress;
let txn;
let flags =
(includeValues ? 0x100 : 0) |
(reverse ? 0x400 : 0) |
(valuesForKey ? 0x800 : 0) |
(options.exactMatch ? 0x4000 : 0) |
(options.inclusiveEnd ? 0x8000 : 0) |
(options.exclusiveStart ? 0x10000 : 0);
let store = this;
function resetCursor() {
try {
if (cursor) finishCursor();
let txnAddress;
txn = options.transaction;
if (txn) {
if (txn.isDone)
throw new Error(
'Can not iterate on range with transaction that is already' +
' done',
);
txnAddress = txn.address;
if (!txnAddress) {
throw new Error('Invalid transaction, it has no address');
}
cursor = null;
} else {
let writeTxn = env.writeTxn;
if (writeTxn) snapshot = false;
txn =
env.writeTxn ||
options.transaction ||
(readTxnRenewed ? readTxn : renewReadTxn(store));
cursor = !writeTxn && db.availableCursor;
}
if (cursor) {
db.availableCursor = null;
flags |= 0x2000;
} else {
cursor = new Cursor(db, txnAddress || 0);
}
cursorAddress = cursor.address;
if (txn.use)
txn.use(); // track transaction so we always use the same one
else txn.refCount = (txn.refCount || 0) + 1;
if (snapshot === false) {
cursorRenewId = renewId; // use shared read transaction
txn.renewingRefCount = (txn.renewingRefCount || 0) + 1; // need to know how many are renewing cursors
}
} catch (error) {
if (cursor) {
try {
cursor.close();
} catch (error) {}
}
throw error;
}
}
resetCursor();
if (options.onlyCount) {
flags |= 0x1000;
let count = position(options.offset);
if (count < 0) lmdbError(count);
finishCursor();
return count;
}
function position(offset) {
if (!env.address) {
throw new Error('Can not iterate on a closed database');
}
let keySize =
currentKey === undefined
? 0
: store.writeKey(currentKey, keyBytes, 0);
let endAddress;
if (valuesForKey) {
if (options.start === undefined && options.end === undefined)
endAddress = 0;
else {
let startAddress;
if (store.encoder.writeKey) {
startAddress = saveKey(
options.start,
store.encoder.writeKey,
iterable,
maxKeySize,
);
keyBytesView.setFloat64(
START_ADDRESS_POSITION,
startAddress,
true,
);
endAddress = saveKey(
options.end,
store.encoder.writeKey,
iterable,
maxKeySize,
);
} else if (
(!options.start || options.start instanceof Uint8Array) &&
(!options.end || options.end instanceof Uint8Array)
) {
startAddress = saveKey(
options.start,
orderedBinary.writeKey,
iterable,
maxKeySize,
);
keyBytesView.setFloat64(
START_ADDRESS_POSITION,
startAddress,
true,
);
endAddress = saveKey(
options.end,
orderedBinary.writeKey,
iterable,
maxKeySize,
);
} else {
throw new Error(
'Only key-based encoding is supported for start/end values',
);
let encoded = store.encoder.encode(options.start);
let bufferAddress =
encoded.buffer.address ||
(encoded.buffer.address =
getAddress(encoded.buffer) - encoded.byteOffset);
startAddress = bufferAddress + encoded.byteOffset;
}
}
} else
endAddress = saveKey(
reverse && !('end' in options)
? DEFAULT_BEGINNING_KEY
: options.end,
store.writeKey,
iterable,
maxKeySize,
);
return doPosition(
cursorAddress,
flags,
offset || 0,
keySize,
endAddress,
);
}
function finishCursor() {
if (!cursor || txn.isDone) return;
if (iterable.onDone) iterable.onDone();
if (cursorRenewId) txn.renewingRefCount--;
if (txn.refCount <= 1 && txn.notCurrent) {
cursor.close(); // this must be closed before the transaction is aborted or it can cause a
// segmentation fault
}
if (txn.done) txn.done();
else if (--txn.refCount <= 0 && txn.notCurrent) {
txn.abort();
txn.isDone = true;
}
if (!txn.isDone) {
if (db.availableCursor || txn != readTxn) {
cursor.close();
} else {
// try to reuse it
db.availableCursor = cursor;
db.cursorTxn = txn;
}
}
cursor = null;
}
return {
next() {
let keySize, lastSize;
if (cursorRenewId && (cursorRenewId != renewId || txn.isDone)) {
if (flags & 0x10000) flags = flags & ~0x10000; // turn off exclusive start when repositioning
resetCursor();
keySize = position(0);
}
if (!cursor) {
return ITERATOR_DONE;
}
if (count === 0) {
// && includeValues) // on first entry, get current value if we need to
keySize = position(options.offset);
} else keySize = iterate(cursorAddress);
if (keySize <= 0 || count++ >= limit) {
if (keySize < -30700 && keySize !== -30798) lmdbError(keySize);
finishCursor();
return ITERATOR_DONE;
}
if (!valuesForKey || snapshot === false) {
if (keySize > 20000) {
if (keySize > 0x1000000) lmdbError(keySize - 0x100000000);
throw new Error('Invalid key size ' + keySize.toString(16));
}
currentKey = store.readKey(keyBytes, 32, keySize + 32);
}
if (includeValues) {
let value;
lastSize = keyBytesView.getUint32(0, true);
let bufferId = keyBytesView.getUint32(4, true);
let bytes;
if (bufferId) {
bytes = getMMapBuffer(bufferId, lastSize);
if (store.encoding === 'binary') bytes = Buffer.from(bytes);
} else {
bytes = compression ? compression.getValueBytes : getValueBytes;
if (lastSize > bytes.maxLength) {
store.lastSize = lastSize;
asSafeBuffer = store.encoding === 'binary';
try {
bytes = store._returnLargeBuffer(() =>
getCurrentValue(cursorAddress),
);
} finally {
asSafeBuffer = false;
}
} else bytes.length = lastSize;
}
if (store.decoder) {
value = store.decoder.decode(bytes, lastSize);
} else if (store.encoding == 'binary')
value = bytes.isGlobal
? Uint8ArraySlice.call(bytes, 0, lastSize)
: bytes;
else {
// use the faster utf8Slice if available, otherwise fall back to TextDecoder (a little slower)
// note applying Buffer's utf8Slice to a Uint8Array works in Node, but not in Bun.
value = bytes.utf8Slice
? bytes.utf8Slice(0, lastSize)
: textDecoder.decode(
Uint8ArraySlice.call(bytes, 0, lastSize),
);
if (store.encoding == 'json' && value)
value = JSON.parse(value);
}
if (includeVersions)
return {
value: {
key: currentKey,
value,
version: getLastVersion(),
},
};
else if (valuesForKey)
return {
value,
};
else
return {
value: {
key: currentKey,
value,
},
};
} else if (includeVersions) {
return {
value: {
key: currentKey,
version: getLastVersion(),
},
};
} else {
return {
value: currentKey,
};
}
},
return() {
finishCursor();
return ITERATOR_DONE;
},
throw() {
finishCursor();
return ITERATOR_DONE;
},
};
};
return iterable;
},
getMany(keys, callback) {
// this is an asynchronous get for multiple keys. It actually works by prefetching asynchronously,
// allowing a separate thread/task to absorb the potentially largest cost: hard page faults (and disk I/O).
// And then we just do standard sync gets (to deserialized data) to fulfil the callback/promise
// once the prefetch occurs
let promise = callback
? undefined
: new Promise(
(resolve) => (callback = (error, results) => resolve(results)),
);
this.prefetch(keys, () => {
let results = new Array(keys.length);
for (let i = 0, l = keys.length; i < l; i++) {
results[i] = get.call(this, keys[i]);
}
callback(null, results);
});
return promise;
},
getSharedBufferForGet(id, options) {
let txn =
env.writeTxn ||
(options && options.transaction) ||
(readTxnRenewed ? readTxn : renewReadTxn(this));
this.lastSize = this.keyIsCompatibility
? txn.getBinaryShared(id)
: this.db.get(this.writeKey(id, keyBytes, 0));
if (this.lastSize === -30798) {
// not found code
return; //undefined
}
return this.lastSize;
this.lastSize = keyBytesView.getUint32(0, true);
let bufferIndex = keyBytesView.getUint32(12, true);
lastOffset = keyBytesView.getUint32(8, true);
let buffer = buffers[bufferIndex];
let startOffset;
if (
!buffer ||
lastOffset < (startOffset = buffer.startOffset) ||
lastOffset + this.lastSize > startOffset + 0x100000000
) {
if (buffer) env.detachBuffer(buffer.buffer);
startOffset = (lastOffset >>> 16) * 0x10000;
console.log(
'make buffer for address',
bufferIndex * 0x100000000 + startOffset,
);
buffer = buffers[bufferIndex] = Buffer.from(
getBufferForAddress(bufferIndex * 0x100000000 + startOffset),
);
buffer.startOffset = startOffset;
}
lastOffset -= startOffset;
return buffer;
return buffer.slice(
lastOffset,
lastOffset + this.lastSize,
); /*Uint8ArraySlice.call(buffer, lastOffset, lastOffset + this.lastSize)*/
},
prefetch(keys, callback) {
if (!keys) throw new Error('An array of keys must be provided');
if (!keys.length) {
if (callback) {
callback(null);
return;
} else return Promise.resolve();
}
let buffers = [];
let startPosition;
let bufferHolder = {};
let lastBuffer;
for (let key of keys) {
let position;
if (key && key.key !== undefined && key.value !== undefined) {
position = saveKey(
key.value,
this.writeKey,
bufferHolder,
maxKeySize,
0x80000000,
);
saveReferenceToBuffer();
saveKey(key.key, this.writeKey, bufferHolder, maxKeySize);
} else {
position = saveKey(key, this.writeKey, bufferHolder, maxKeySize);
}
if (!startPosition) startPosition = position;
saveReferenceToBuffer();
}
function saveReferenceToBuffer() {
if (bufferHolder.saveBuffer != lastBuffer) {
buffers.push(bufferHolder.saveBuffer);
lastBuffer = bufferHolder.saveBuffer;
}
}
saveKey(undefined, this.writeKey, bufferHolder, maxKeySize);
saveReferenceToBuffer();
outstandingReads++;
prefetch(this.dbAddress, startPosition, (error) => {
outstandingReads--;
if (error)
console.error('Error with prefetch', buffers); // partly exists to keep the buffers pinned in memory
else callback(null);
});
if (!callback) return new Promise((resolve) => (callback = resolve));
},
useReadTransaction() {
let txn = readTxnRenewed ? readTxn : renewReadTxn(this);
if (!txn.use) {
throw new Error('Can not use read transaction from a closed database');
}
// because the renew actually happens lazily in read operations, renew needs to be explicit
// here in order to actually secure a real read transaction. Try to only do it if necessary;
// once it has a refCount, it should be good to go
if (!(readTxn.refCount - (readTxn.renewingRefCount || 0) > 0))
txn.renew();
txn.use();
return txn;
},
close(callback) {
this.status = 'closing';
let txnPromise;
if (this.isRoot) {
// if it is root, we need to abort and/or wait for transactions to finish
if (readTxn) {
try {
readTxn.abort();
} catch (error) {}
} else readTxn = {};
readTxn.isDone = true;
Object.defineProperty(readTxn, 'renew', {
value: () => {
throw new Error('Can not read from a closed database');
},
configurable: true,
});
Object.defineProperty(readTxn, 'use', {
value: () => {
throw new Error('Can not read from a closed database');
},
configurable: true,
});
readTxnRenewed = null;
txnPromise = this._endWrites && this._endWrites();
}
const doClose = () => {
if (this.isRoot) {
if (outstandingReads > 0) {
return new Promise((resolve) =>
setTimeout(() => resolve(doClose()), 1),
);
}
env.address = 0;
try {
env.close();
} catch (error) {}
} else this.db.close();
this.status = 'closed';
if (callback) callback();
};
if (txnPromise) return txnPromise.then(doClose);
else {
doClose();
return Promise.resolve();
}
},
getStats() {
let txn = env.writeTxn || (readTxnRenewed ? readTxn : renewReadTxn(this));
let dbStats = this.db.stat();
dbStats.root = env.stat();
Object.assign(dbStats, env.info());
dbStats.free = env.freeStat();
return dbStats;
},
});
let get = LMDBStore.prototype.get;
let lastReadTxnRef;
function getMMapBuffer(bufferId, size) {
let buffer = mmaps[bufferId];
if (!buffer) {
buffer = mmaps[bufferId] = getSharedBuffer(bufferId, env.address);