@ohos-ports/lmdb 2.8.6-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (125) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +580 -0
  3. package/SECURITY.md +12 -0
  4. package/bin/download-prebuilds.js +15 -0
  5. package/binding.gyp +113 -0
  6. package/build/Release/lmdb.node +0 -0
  7. package/caching.js +212 -0
  8. package/dependencies/lmdb/libraries/liblmdb/COPYRIGHT +20 -0
  9. package/dependencies/lmdb/libraries/liblmdb/Doxyfile +1631 -0
  10. package/dependencies/lmdb/libraries/liblmdb/LICENSE +47 -0
  11. package/dependencies/lmdb/libraries/liblmdb/Makefile +136 -0
  12. package/dependencies/lmdb/libraries/liblmdb/chacha8.c +183 -0
  13. package/dependencies/lmdb/libraries/liblmdb/chacha8.h +14 -0
  14. package/dependencies/lmdb/libraries/liblmdb/crypto.c +121 -0
  15. package/dependencies/lmdb/libraries/liblmdb/intro.doc +192 -0
  16. package/dependencies/lmdb/libraries/liblmdb/lmdb.h +1817 -0
  17. package/dependencies/lmdb/libraries/liblmdb/mdb.c +12555 -0
  18. package/dependencies/lmdb/libraries/liblmdb/mdb_copy.1 +74 -0
  19. package/dependencies/lmdb/libraries/liblmdb/mdb_copy.c +106 -0
  20. package/dependencies/lmdb/libraries/liblmdb/mdb_drop.1 +53 -0
  21. package/dependencies/lmdb/libraries/liblmdb/mdb_drop.c +154 -0
  22. package/dependencies/lmdb/libraries/liblmdb/mdb_dump.1 +94 -0
  23. package/dependencies/lmdb/libraries/liblmdb/mdb_dump.c +333 -0
  24. package/dependencies/lmdb/libraries/liblmdb/mdb_load.1 +97 -0
  25. package/dependencies/lmdb/libraries/liblmdb/mdb_load.c +530 -0
  26. package/dependencies/lmdb/libraries/liblmdb/mdb_stat.1 +83 -0
  27. package/dependencies/lmdb/libraries/liblmdb/mdb_stat.c +276 -0
  28. package/dependencies/lmdb/libraries/liblmdb/midl-sparse.c +452 -0
  29. package/dependencies/lmdb/libraries/liblmdb/midl.c +452 -0
  30. package/dependencies/lmdb/libraries/liblmdb/midl.h +208 -0
  31. package/dependencies/lmdb/libraries/liblmdb/module.c +101 -0
  32. package/dependencies/lmdb/libraries/liblmdb/module.h +16 -0
  33. package/dependencies/lmdb/libraries/liblmdb/mtest.c +178 -0
  34. package/dependencies/lmdb/libraries/liblmdb/mtest2.c +124 -0
  35. package/dependencies/lmdb/libraries/liblmdb/mtest3.c +133 -0
  36. package/dependencies/lmdb/libraries/liblmdb/mtest4.c +168 -0
  37. package/dependencies/lmdb/libraries/liblmdb/mtest5.c +135 -0
  38. package/dependencies/lmdb/libraries/liblmdb/mtest6.c +141 -0
  39. package/dependencies/lmdb/libraries/liblmdb/mtest_enc.c +190 -0
  40. package/dependencies/lmdb/libraries/liblmdb/mtest_enc2.c +189 -0
  41. package/dependencies/lmdb/libraries/liblmdb/mtest_remap.c +177 -0
  42. package/dependencies/lmdb/libraries/liblmdb/sample-bdb.txt +73 -0
  43. package/dependencies/lmdb/libraries/liblmdb/sample-mdb.txt +62 -0
  44. package/dependencies/lmdb/libraries/liblmdb/tooltag +27 -0
  45. package/dependencies/lmdb-data-v1/libraries/liblmdb/CHANGES +266 -0
  46. package/dependencies/lmdb-data-v1/libraries/liblmdb/COPYRIGHT +20 -0
  47. package/dependencies/lmdb-data-v1/libraries/liblmdb/Doxyfile +1631 -0
  48. package/dependencies/lmdb-data-v1/libraries/liblmdb/LICENSE +47 -0
  49. package/dependencies/lmdb-data-v1/libraries/liblmdb/intro.doc +192 -0
  50. package/dependencies/lmdb-data-v1/libraries/liblmdb/lmdb.h +1608 -0
  51. package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb.c +10320 -0
  52. package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_copy.1 +55 -0
  53. package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_copy.c +82 -0
  54. package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_dump.1 +75 -0
  55. package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_dump.c +319 -0
  56. package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_load.1 +84 -0
  57. package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_load.c +496 -0
  58. package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_stat.1 +64 -0
  59. package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_stat.c +263 -0
  60. package/dependencies/lmdb-data-v1/libraries/liblmdb/midl.c +359 -0
  61. package/dependencies/lmdb-data-v1/libraries/liblmdb/midl.h +186 -0
  62. package/dependencies/lmdb-data-v1/libraries/liblmdb/mtest.c +177 -0
  63. package/dependencies/lmdb-data-v1/libraries/liblmdb/mtest2.c +124 -0
  64. package/dependencies/lmdb-data-v1/libraries/liblmdb/mtest3.c +133 -0
  65. package/dependencies/lmdb-data-v1/libraries/liblmdb/mtest4.c +168 -0
  66. package/dependencies/lmdb-data-v1/libraries/liblmdb/mtest5.c +135 -0
  67. package/dependencies/lmdb-data-v1/libraries/liblmdb/mtest6.c +141 -0
  68. package/dependencies/lmdb-data-v1/libraries/liblmdb/sample-bdb.txt +73 -0
  69. package/dependencies/lmdb-data-v1/libraries/liblmdb/sample-mdb.txt +62 -0
  70. package/dependencies/lmdb-data-v1/libraries/liblmdb/tooltag +22 -0
  71. package/dependencies/lz4/LICENSE +11 -0
  72. package/dependencies/lz4/lib/LICENSE +24 -0
  73. package/dependencies/lz4/lib/Makefile +225 -0
  74. package/dependencies/lz4/lib/README.md +169 -0
  75. package/dependencies/lz4/lib/dll/example/Makefile +63 -0
  76. package/dependencies/lz4/lib/dll/example/README.md +69 -0
  77. package/dependencies/lz4/lib/dll/example/fullbench-dll.sln +25 -0
  78. package/dependencies/lz4/lib/dll/example/fullbench-dll.vcxproj +182 -0
  79. package/dependencies/lz4/lib/dll/liblz4.def +62 -0
  80. package/dependencies/lz4/lib/liblz4-dll.rc.in +35 -0
  81. package/dependencies/lz4/lib/liblz4.pc.in +14 -0
  82. package/dependencies/lz4/lib/lz4.c +2722 -0
  83. package/dependencies/lz4/lib/lz4.h +842 -0
  84. package/dependencies/lz4/lib/lz4file.c +311 -0
  85. package/dependencies/lz4/lib/lz4file.h +93 -0
  86. package/dependencies/lz4/lib/lz4frame.c +2078 -0
  87. package/dependencies/lz4/lib/lz4frame.h +692 -0
  88. package/dependencies/lz4/lib/lz4frame_static.h +47 -0
  89. package/dependencies/lz4/lib/lz4hc.c +1631 -0
  90. package/dependencies/lz4/lib/lz4hc.h +413 -0
  91. package/dependencies/lz4/lib/xxhash.c +1030 -0
  92. package/dependencies/lz4/lib/xxhash.h +328 -0
  93. package/dependencies/v8/v8-fast-api-calls-v16.h +791 -0
  94. package/dependencies/v8/v8-fast-api-calls.h +888 -0
  95. package/dict/dict.txt +1 -0
  96. package/dict/dict2.txt +1 -0
  97. package/dist/index.cjs +2950 -0
  98. package/dist/index.cjs.map +1 -0
  99. package/index.d.cts +420 -0
  100. package/index.d.ts +450 -0
  101. package/index.js +38 -0
  102. package/keys.js +116 -0
  103. package/level.js +27 -0
  104. package/native.js +85 -0
  105. package/node-index.js +32 -0
  106. package/open.js +448 -0
  107. package/package.json +124 -0
  108. package/read.js +833 -0
  109. package/rollup.config.js +12 -0
  110. package/src/compression.cpp +225 -0
  111. package/src/cursor.cpp +395 -0
  112. package/src/dbi.cpp +382 -0
  113. package/src/env.cpp +1030 -0
  114. package/src/lmdb-js.cpp +64 -0
  115. package/src/lmdb-js.h +633 -0
  116. package/src/misc.cpp +593 -0
  117. package/src/ordered-binary.cpp +46 -0
  118. package/src/txn.cpp +210 -0
  119. package/src/v8-functions.cpp +170 -0
  120. package/src/windows.c +35 -0
  121. package/src/writer.cpp +537 -0
  122. package/util/RangeIterable.js +236 -0
  123. package/util/set-optional-deps.cjs +14 -0
  124. package/util/when.js +8 -0
  125. package/write.js +912 -0
package/dist/index.cjs ADDED
@@ -0,0 +1,2950 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var module$1 = require('module');
6
+ var pathModule = require('path');
7
+ var url = require('url');
8
+ var loadNAPI = require('node-gyp-build-optional-packages');
9
+ var events = require('events');
10
+ var os$1 = require('os');
11
+ var fs$1 = require('fs');
12
+ var msgpackr = require('msgpackr');
13
+ var weakLruCache = require('weak-lru-cache');
14
+ var orderedBinary$1 = require('ordered-binary');
15
+
16
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
17
+
18
+ function _interopNamespace(e) {
19
+ if (e && e.__esModule) return e;
20
+ var n = Object.create(null);
21
+ if (e) {
22
+ Object.keys(e).forEach(function (k) {
23
+ if (k !== 'default') {
24
+ var d = Object.getOwnPropertyDescriptor(e, k);
25
+ Object.defineProperty(n, k, d.get ? d : {
26
+ enumerable: true,
27
+ get: function () { return e[k]; }
28
+ });
29
+ }
30
+ });
31
+ }
32
+ n["default"] = e;
33
+ return Object.freeze(n);
34
+ }
35
+
36
+ var pathModule__default = /*#__PURE__*/_interopDefaultLegacy(pathModule);
37
+ var loadNAPI__default = /*#__PURE__*/_interopDefaultLegacy(loadNAPI);
38
+ var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs$1);
39
+ var orderedBinary__namespace = /*#__PURE__*/_interopNamespace(orderedBinary$1);
40
+
41
+ let Env, Txn, Dbi, Compression, Cursor, getAddress, getBufferAddress; exports.clearKeptObjects = void 0; let globalBuffer, setGlobalBuffer, arch, fs, os, tmpdir, lmdbError, path, EventEmitter, orderedBinary, MsgpackrEncoder, WeakLRUCache, getByBinary, startRead, setReadCallback, write, position, iterate, prefetch, resetTxn, getCurrentValue, getStringByBinary, getSharedBuffer, compress;
42
+ path = pathModule__default["default"];
43
+ let dirName = pathModule.dirname(url.fileURLToPath((typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('index.cjs', document.baseURI).href)))).replace(/dist$/, '');
44
+ let nativeAddon = loadNAPI__default["default"](dirName);
45
+
46
+ if (process.isBun && false) {
47
+ const { linkSymbols, FFIType } = require('bun:ffi');
48
+ let lmdbLib = linkSymbols({
49
+ getByBinary: {
50
+ args: [FFIType.f64, FFIType.u32],
51
+ returns: FFIType.u32,
52
+ ptr: nativeAddon.getByBinaryPtr
53
+ },
54
+ iterate: {
55
+ args: [FFIType.f64],
56
+ returns: FFIType.i32,
57
+ ptr: nativeAddon.iteratePtr,
58
+ },
59
+ position: {
60
+ args: [FFIType.f64, FFIType.u32, FFIType.u32, FFIType.u32, FFIType.f64],
61
+ returns: FFIType.i32,
62
+ ptr: nativeAddon.positionPtr,
63
+ },
64
+ write: {
65
+ args: [FFIType.f64, FFIType.f64],
66
+ returns: FFIType.i32,
67
+ ptr: nativeAddon.writePtr,
68
+ },
69
+ resetTxn: {
70
+ args: [FFIType.f64],
71
+ returns: FFIType.void,
72
+ ptr: nativeAddon.resetTxnPtr,
73
+ }
74
+ });
75
+ for (let key in lmdbLib.symbols) {
76
+ nativeAddon[key] = lmdbLib.symbols[key].native;
77
+ }
78
+ }
79
+ setNativeFunctions(nativeAddon);
80
+
81
+ function setNativeFunctions(externals) {
82
+ Env = externals.Env;
83
+ Txn = externals.Txn;
84
+ Dbi = externals.Dbi;
85
+ Compression = externals.Compression;
86
+ getAddress = externals.getAddress;
87
+ getBufferAddress = externals.getBufferAddress;
88
+ externals.createBufferForAddress;
89
+ exports.clearKeptObjects = externals.clearKeptObjects || function() {};
90
+ getByBinary = externals.getByBinary;
91
+ externals.detachBuffer;
92
+ startRead = externals.startRead;
93
+ setReadCallback = externals.setReadCallback;
94
+ setGlobalBuffer = externals.setGlobalBuffer;
95
+ globalBuffer = externals.globalBuffer;
96
+ getSharedBuffer = externals.getSharedBuffer;
97
+ prefetch = externals.prefetch;
98
+ iterate = externals.iterate;
99
+ position = externals.position;
100
+ resetTxn = externals.resetTxn;
101
+ getCurrentValue = externals.getCurrentValue;
102
+ externals.getCurrentShared;
103
+ getStringByBinary = externals.getStringByBinary;
104
+ externals.getSharedByBinary;
105
+ write = externals.write;
106
+ compress = externals.compress;
107
+ Cursor = externals.Cursor;
108
+ lmdbError = externals.lmdbError;
109
+ if (externals.tmpdir)
110
+ tmpdir = externals.tmpdir;
111
+ }
112
+ function setExternals(externals) {
113
+ arch = externals.arch;
114
+ fs = externals.fs;
115
+ EventEmitter = externals.EventEmitter;
116
+ orderedBinary = externals.orderedBinary;
117
+ MsgpackrEncoder = externals.MsgpackrEncoder;
118
+ WeakLRUCache = externals.WeakLRUCache;
119
+ tmpdir = externals.tmpdir;
120
+ os = externals.os;
121
+ externals.onExit;
122
+ }
123
+
124
+ function when(promise, callback, errback) {
125
+ if (promise && promise.then) {
126
+ return errback ?
127
+ promise.then(callback, errback) :
128
+ promise.then(callback);
129
+ }
130
+ return callback(promise);
131
+ }
132
+
133
+ var backpressureArray;
134
+
135
+ const WAITING_OPERATION = 0x2000000;
136
+ const BACKPRESSURE_THRESHOLD = 300000;
137
+ const TXN_DELIMITER = 0x8000000;
138
+ const TXN_COMMITTED = 0x10000000;
139
+ const TXN_FLUSHED = 0x20000000;
140
+ const TXN_FAILED = 0x40000000;
141
+ const FAILED_CONDITION = 0x4000000;
142
+ const REUSE_BUFFER_MODE$1 = 512;
143
+ const RESET_BUFFER_MODE = 1024;
144
+ const NO_RESOLVE = 16;
145
+ const HAS_TXN = 8;
146
+ const CONDITIONAL_VERSION_LESS_THAN = 0x800;
147
+ const CONDITIONAL_ALLOW_NOTFOUND = 0x800;
148
+
149
+ const SYNC_PROMISE_SUCCESS = Promise.resolve(true);
150
+ const SYNC_PROMISE_FAIL = Promise.resolve(false);
151
+ SYNC_PROMISE_SUCCESS.isSync = true;
152
+ SYNC_PROMISE_FAIL.isSync = true;
153
+ const PROMISE_SUCCESS = Promise.resolve(true);
154
+ const ABORT = 4.452694326329068e-106; // random/unguessable numbers, which work across module/versions and native
155
+ const IF_EXISTS$1 = 3.542694326329068e-103;
156
+ const CALLBACK_THREW = {};
157
+ const LocalSharedArrayBuffer = typeof Deno != 'undefined' ? ArrayBuffer : SharedArrayBuffer; // Deno can't handle SharedArrayBuffer as an FFI argument due to https://github.com/denoland/deno/issues/12678
158
+ const ByteArray = typeof Buffer != 'undefined' ? function(buffer) { return Buffer.from(buffer) } : Uint8Array;
159
+ const queueTask = typeof setImmediate != 'undefined' ? setImmediate : setTimeout; // TODO: Or queueMicrotask?
160
+ //let debugLog = []
161
+ const WRITE_BUFFER_SIZE = 0x10000;
162
+ function addWriteMethods(LMDBStore, { env, fixedBuffer, resetReadTxn, useWritemap, maxKeySize,
163
+ eventTurnBatching, txnStartThreshold, batchStartThreshold, overlappingSync, commitDelay, separateFlushed, maxFlushDelay }) {
164
+ // stands for write instructions
165
+ var dynamicBytes;
166
+ function allocateInstructionBuffer(lastPosition) {
167
+ // Must use a shared buffer on older node in order to use Atomics, and it is also more correct since we are
168
+ // indeed accessing and modifying it from another thread (in C). However, Deno can't handle it for
169
+ // FFI so aliased above
170
+ let buffer = new LocalSharedArrayBuffer(WRITE_BUFFER_SIZE);
171
+ let lastBytes = dynamicBytes;
172
+ dynamicBytes = new ByteArray(buffer);
173
+ let uint32 = dynamicBytes.uint32 = new Uint32Array(buffer, 0, WRITE_BUFFER_SIZE >> 2);
174
+ uint32[2] = 0;
175
+ dynamicBytes.float64 = new Float64Array(buffer, 0, WRITE_BUFFER_SIZE >> 3);
176
+ buffer.address = getBufferAddress(dynamicBytes);
177
+ uint32.address = buffer.address + uint32.byteOffset;
178
+ dynamicBytes.position = 1; // we start at position 1 to save space for writing the txn id before the txn delimiter
179
+ if (lastPosition) {
180
+ lastBytes.float64[lastPosition + 1] = dynamicBytes.uint32.address + (dynamicBytes.position << 3);
181
+ lastBytes.uint32[lastPosition << 1] = 3; // pointer instruction
182
+ }
183
+ return dynamicBytes;
184
+ }
185
+ var newBufferThreshold = (WRITE_BUFFER_SIZE - maxKeySize - 64) >> 3; // need to reserve more room if we do inline values
186
+ var outstandingWriteCount = 0;
187
+ var startAddress = 0;
188
+ var writeTxn = null;
189
+ var committed;
190
+ var abortedNonChildTransactionWarn;
191
+ var nextTxnCallbacks = [];
192
+ var commitPromise, flushPromise, flushResolvers = [], batchFlushResolvers = [];
193
+ commitDelay = commitDelay || 0;
194
+ eventTurnBatching = eventTurnBatching === false ? false : true;
195
+ var enqueuedCommit;
196
+ var afterCommitCallbacks = [];
197
+ var beforeCommitCallbacks = [];
198
+ var enqueuedEventTurnBatch;
199
+ var batchDepth = 0;
200
+ var lastWritePromise;
201
+ var writeBatchStart, outstandingBatchCount, lastSyncTxnFlush;
202
+ var hasUnresolvedTxns;
203
+ txnStartThreshold = txnStartThreshold || 5;
204
+ batchStartThreshold = batchStartThreshold || 1000;
205
+ maxFlushDelay = maxFlushDelay || 500;
206
+
207
+ allocateInstructionBuffer();
208
+ dynamicBytes.uint32[2] = TXN_DELIMITER | TXN_COMMITTED | TXN_FLUSHED;
209
+ var txnResolution, nextResolution = {
210
+ uint32: dynamicBytes.uint32, flagPosition: 2, flag: 0, valueBuffer: null, next: null, meta: null };
211
+ var uncommittedResolution = {
212
+ uint32: null, flagPosition: 2, flag: 0, valueBuffer: null, next: nextResolution, meta: null };
213
+ var unwrittenResolution = nextResolution;
214
+ var lastPromisedResolution = uncommittedResolution;
215
+ var lastQueuedResolution = uncommittedResolution;
216
+ function writeInstructions(flags, store, key, value, version, ifVersion) {
217
+ let writeStatus;
218
+ let targetBytes, position, encoder;
219
+ let valueSize, valueBuffer, valueBufferStart;
220
+ if (flags & 2) {
221
+ // encode first in case we have to write a shared structure
222
+ encoder = store.encoder;
223
+ if (value && value['\x10binary-data\x02'])
224
+ valueBuffer = value['\x10binary-data\x02'];
225
+ else if (encoder) {
226
+ if (encoder.copyBuffers) // use this as indicator for support buffer reuse for now
227
+ valueBuffer = encoder.encode(value, REUSE_BUFFER_MODE$1 | (writeTxn ? RESET_BUFFER_MODE : 0)); // in addition, if we are writing sync, after using, we can immediately reset the encoder's position to reuse that space, which can improve performance
228
+ else { // various other encoders, including JSON.stringify, that might serialize to a string
229
+ valueBuffer = encoder.encode(value);
230
+ if (typeof valueBuffer == 'string')
231
+ valueBuffer = Buffer.from(valueBuffer); // TODO: Would be nice to write strings inline in the instructions
232
+ }
233
+ } else if (typeof value == 'string') {
234
+ valueBuffer = Buffer.from(value); // TODO: Would be nice to write strings inline in the instructions
235
+ } else if (value instanceof Uint8Array)
236
+ valueBuffer = value;
237
+ else
238
+ throw new Error('Invalid value to put in database ' + value + ' (' + (typeof value) +'), consider using encoder');
239
+ valueBufferStart = valueBuffer.start;
240
+ if (valueBufferStart > -1) // if we have buffers with start/end position
241
+ valueSize = valueBuffer.end - valueBufferStart; // size
242
+ else
243
+ valueSize = valueBuffer.length;
244
+ if (store.dupSort && valueSize > maxKeySize)
245
+ throw new Error('The value is larger than the maximum size (' + maxKeySize + ') for a value in a dupSort database');
246
+ } else
247
+ valueSize = 0;
248
+ if (writeTxn) {
249
+ targetBytes = fixedBuffer;
250
+ position = 0;
251
+ } else {
252
+ if (eventTurnBatching && !enqueuedEventTurnBatch && batchDepth == 0) {
253
+ enqueuedEventTurnBatch = queueTask(() => {
254
+ try {
255
+ for (let i = 0, l = beforeCommitCallbacks.length; i < l; i++) {
256
+ try {
257
+ beforeCommitCallbacks[i]();
258
+ } catch(error) {
259
+ console.error('In beforecommit callback', error);
260
+ }
261
+ }
262
+ } catch(error) {
263
+ console.error(error);
264
+ }
265
+ enqueuedEventTurnBatch = null;
266
+ batchDepth--;
267
+ finishBatch();
268
+ if (writeBatchStart)
269
+ writeBatchStart(); // TODO: When we support delay start of batch, optionally don't delay this
270
+ });
271
+ commitPromise = null; // reset the commit promise, can't know if it is really a new transaction prior to finishWrite being called
272
+ flushPromise = null;
273
+ writeBatchStart = writeInstructions(1, store);
274
+ outstandingBatchCount = 0;
275
+ batchDepth++;
276
+ }
277
+ targetBytes = dynamicBytes;
278
+ position = targetBytes.position;
279
+ }
280
+ let uint32 = targetBytes.uint32, float64 = targetBytes.float64;
281
+ let flagPosition = position << 1; // flagPosition is the 32-bit word starting position
282
+
283
+ // don't increment position until we are sure we don't have any key writing errors
284
+ if (!uint32) {
285
+ throw new Error('Internal buffers have been corrupted');
286
+ }
287
+ uint32[flagPosition + 1] = store.db.dbi;
288
+ if (flags & 4) {
289
+ let keyStartPosition = (position << 3) + 12;
290
+ let endPosition;
291
+ try {
292
+ endPosition = store.writeKey(key, targetBytes, keyStartPosition);
293
+ if (!(keyStartPosition < endPosition) && (flags & 0xf) != 12)
294
+ throw new Error('Invalid key or zero length key is not allowed in LMDB ' + key)
295
+ } catch(error) {
296
+ targetBytes.fill(0, keyStartPosition);
297
+ if (error.name == 'RangeError')
298
+ error = new Error('Key size is larger than the maximum key size (' + maxKeySize + ')');
299
+ throw error;
300
+ }
301
+ let keySize = endPosition - keyStartPosition;
302
+ if (keySize > maxKeySize) {
303
+ targetBytes.fill(0, keyStartPosition); // restore zeros
304
+ throw new Error('Key size is larger than the maximum key size (' + maxKeySize + ')');
305
+ }
306
+ uint32[flagPosition + 2] = keySize;
307
+ position = (endPosition + 16) >> 3;
308
+ if (flags & 2) {
309
+ let mustCompress;
310
+ if (valueBufferStart > -1) { // if we have buffers with start/end position
311
+ // record pointer to value buffer
312
+ float64[position] = (valueBuffer.address ||
313
+ (valueBuffer.address = getAddress(valueBuffer.buffer))) + valueBufferStart;
314
+ mustCompress = valueBuffer[valueBufferStart] >= 250; // this is the compression indicator, so we must compress
315
+ } else {
316
+ let valueArrayBuffer = valueBuffer.buffer;
317
+ // record pointer to value buffer
318
+ let address = (valueArrayBuffer.address ||
319
+ (valueBuffer.length === 0 ? 0 : // externally allocated buffers of zero-length with the same non-null-pointer can crash node, #161
320
+ valueArrayBuffer.address = getAddress(valueArrayBuffer)))
321
+ + valueBuffer.byteOffset;
322
+ if (address <= 0 && valueBuffer.length > 0)
323
+ console.error('Supplied buffer had an invalid address', address);
324
+ float64[position] = address;
325
+ mustCompress = valueBuffer[0] >= 250; // this is the compression indicator, so we must compress
326
+ }
327
+ uint32[(position++ << 1) - 1] = valueSize;
328
+ if (store.compression && (valueSize >= store.compression.threshold || mustCompress)) {
329
+ flags |= 0x100000;
330
+ float64[position] = store.compression.address;
331
+ if (!writeTxn)
332
+ compress(env.address, uint32.address + (position << 3), () => {
333
+ // this is never actually called in NodeJS, just use to pin the buffer in memory until it is finished
334
+ // and is a no-op in Deno
335
+ if (!float64)
336
+ throw new Error('No float64 available');
337
+ });
338
+ position++;
339
+ }
340
+ }
341
+ if (ifVersion !== undefined) {
342
+ if (ifVersion === null)
343
+ flags |= 0x10; // if it does not exist, MDB_NOOVERWRITE
344
+ else {
345
+ flags |= 0x100;
346
+ float64[position++] = ifVersion;
347
+ }
348
+ }
349
+ if (version !== undefined) {
350
+ flags |= 0x200;
351
+ float64[position++] = version || 0;
352
+ }
353
+ } else
354
+ position++;
355
+ targetBytes.position = position;
356
+ if (writeTxn) {
357
+ uint32[0] = flags;
358
+ write(env.address, uint32.address);
359
+ return () => (uint32[0] & FAILED_CONDITION) ? SYNC_PROMISE_FAIL : SYNC_PROMISE_SUCCESS;
360
+ }
361
+ // if we ever use buffers that haven't been zero'ed, need to clear out the next slot like this:
362
+ // uint32[position << 1] = 0 // clear out the next slot
363
+ let nextUint32;
364
+ if (position > newBufferThreshold) {
365
+ targetBytes = allocateInstructionBuffer(position);
366
+ position = targetBytes.position;
367
+ nextUint32 = targetBytes.uint32;
368
+ } else
369
+ nextUint32 = uint32;
370
+ let resolution = nextResolution;
371
+ // create the placeholder next resolution
372
+ nextResolution = resolution.next = { // we try keep resolutions exactly the same object type
373
+ uint32: nextUint32,
374
+ flagPosition: position << 1,
375
+ flag: 0, // TODO: eventually eliminate this, as we can probably signify HAS_TXN/NO_RESOLVE/FAILED_CONDITION in upper bits
376
+ valueBuffer: fixedBuffer, // these are all just placeholders so that we have the right hidden class initially allocated
377
+ next: null,
378
+ meta: null,
379
+ };
380
+ lastQueuedResolution = resolution;
381
+
382
+ let writtenBatchDepth = batchDepth;
383
+
384
+ return (callback) => {
385
+ if (writtenBatchDepth) {
386
+ // if we are in a batch, the transaction can't close, so we do the faster,
387
+ // but non-deterministic updates, knowing that the write thread can
388
+ // just poll for the status change if we miss a status update
389
+ writeStatus = uint32[flagPosition];
390
+ uint32[flagPosition] = flags;
391
+ //writeStatus = Atomics.or(uint32, flagPosition, flags)
392
+ if (writeBatchStart && !writeStatus) {
393
+ outstandingBatchCount += 1 + (valueSize >> 12);
394
+ if (outstandingBatchCount > batchStartThreshold) {
395
+ outstandingBatchCount = 0;
396
+ writeBatchStart();
397
+ writeBatchStart = null;
398
+ }
399
+ }
400
+ } else // otherwise the transaction could end at any time and we need to know the
401
+ // deterministically if it is ending, so we can reset the commit promise
402
+ // so we use the slower atomic operation
403
+ writeStatus = Atomics.or(uint32, flagPosition, flags);
404
+
405
+ outstandingWriteCount++;
406
+ if (writeStatus & TXN_DELIMITER) {
407
+ commitPromise = null; // TODO: Don't reset these if this comes from the batch start operation on an event turn batch
408
+ flushPromise = null;
409
+ flushResolvers = [];
410
+ queueCommitResolution(resolution);
411
+ if (!startAddress) {
412
+ startAddress = uint32.address + (flagPosition << 2);
413
+ }
414
+ }
415
+ if (!writtenBatchDepth && batchFlushResolvers.length > 0) {
416
+ flushResolvers.push(...batchFlushResolvers);
417
+ batchFlushResolvers = [];
418
+ }
419
+ if (!flushPromise && overlappingSync) {
420
+ flushPromise = new Promise(resolve => {
421
+ if (writtenBatchDepth) {
422
+ batchFlushResolvers.push(resolve);
423
+ } else {
424
+ flushResolvers.push(resolve);
425
+ }
426
+ });
427
+ }
428
+ if (writeStatus & WAITING_OPERATION) { // write thread is waiting
429
+ write(env.address, 0);
430
+ }
431
+ if (outstandingWriteCount > BACKPRESSURE_THRESHOLD && !writeBatchStart) {
432
+ if (!backpressureArray)
433
+ backpressureArray = new Int32Array(new SharedArrayBuffer(4), 0, 1);
434
+ Atomics.wait(backpressureArray, 0, 0, Math.round(outstandingWriteCount / BACKPRESSURE_THRESHOLD));
435
+ }
436
+ if (startAddress) {
437
+ if (eventTurnBatching)
438
+ startWriting(); // start writing immediately because this has already been batched/queued
439
+ else if (!enqueuedCommit && txnStartThreshold) {
440
+ enqueuedCommit = (commitDelay == 0 && typeof setImmediate != 'undefined') ? setImmediate(() => startWriting()) : setTimeout(() => startWriting(), commitDelay);
441
+ } else if (outstandingWriteCount > txnStartThreshold)
442
+ startWriting();
443
+ }
444
+
445
+ if ((outstandingWriteCount & 7) === 0)
446
+ resolveWrites();
447
+
448
+ if (store.cache) {
449
+ resolution.meta = {
450
+ key,
451
+ store,
452
+ valueSize: valueBuffer ? valueBuffer.length : 0,
453
+ };
454
+ }
455
+ resolution.valueBuffer = valueBuffer;
456
+
457
+ if (callback) {
458
+ if (callback === IF_EXISTS$1)
459
+ ifVersion = IF_EXISTS$1;
460
+ else {
461
+ let meta = resolution.meta || (resolution.meta = {});
462
+ meta.reject = callback;
463
+ meta.resolve = (value) => callback(null, value);
464
+ return;
465
+ }
466
+ }
467
+ if (ifVersion === undefined) {
468
+ if (writtenBatchDepth > 1) {
469
+ if (!resolution.flag && !store.cache)
470
+ resolution.flag = NO_RESOLVE;
471
+ return PROMISE_SUCCESS; // or return undefined?
472
+ }
473
+ if (commitPromise) {
474
+ if (!resolution.flag)
475
+ resolution.flag = NO_RESOLVE;
476
+ } else {
477
+ commitPromise = new Promise((resolve, reject) => {
478
+ let meta = resolution.meta || (resolution.meta = {});
479
+ meta.resolve = resolve;
480
+ resolve.unconditional = true;
481
+ meta.reject = reject;
482
+ });
483
+ if (separateFlushed)
484
+ commitPromise.flushed = overlappingSync ? flushPromise : commitPromise;
485
+ }
486
+ return commitPromise;
487
+ }
488
+ lastWritePromise = new Promise((resolve, reject) => {
489
+ let meta = resolution.meta || (resolution.meta = {});
490
+ meta.resolve = resolve;
491
+ meta.reject = reject;
492
+ });
493
+ if (separateFlushed)
494
+ lastWritePromise.flushed = overlappingSync ? flushPromise : lastWritePromise;
495
+ return lastWritePromise;
496
+ };
497
+ }
498
+ Promise.resolve();
499
+ function startWriting() {
500
+ if (enqueuedCommit) {
501
+ clearImmediate(enqueuedCommit);
502
+ enqueuedCommit = null;
503
+ }
504
+ let resolvers = flushResolvers;
505
+ env.startWriting(startAddress, (status) => {
506
+ if (dynamicBytes.uint32[dynamicBytes.position << 1] & TXN_DELIMITER)
507
+ queueCommitResolution(nextResolution);
508
+
509
+ resolveWrites(true);
510
+ switch (status) {
511
+ case 0:
512
+ for (let resolver of resolvers) {
513
+ resolver();
514
+ }
515
+ break;
516
+ case 1:
517
+ break;
518
+ case 2:
519
+ hasUnresolvedTxns = false;
520
+ executeTxnCallbacks();
521
+ return hasUnresolvedTxns;
522
+ default:
523
+ try {
524
+ lmdbError(status);
525
+ } catch(error) {
526
+ console.error(error);
527
+ if (commitRejectPromise) {
528
+ commitRejectPromise.reject(error);
529
+ commitRejectPromise = null;
530
+ }
531
+ }
532
+ }
533
+ });
534
+ startAddress = 0;
535
+ }
536
+
537
+ function queueCommitResolution(resolution) {
538
+ if (!(resolution.flag & HAS_TXN)) {
539
+ resolution.flag = HAS_TXN;
540
+ if (txnResolution) {
541
+ txnResolution.nextTxn = resolution;
542
+ //outstandingWriteCount = 0
543
+ }
544
+ else
545
+ txnResolution = resolution;
546
+ }
547
+ }
548
+ var TXN_DONE = TXN_COMMITTED | TXN_FAILED;
549
+ function resolveWrites(async) {
550
+ // clean up finished instructions
551
+ let instructionStatus;
552
+ while ((instructionStatus = unwrittenResolution.uint32[unwrittenResolution.flagPosition])
553
+ & 0x1000000) {
554
+ if (unwrittenResolution.callbacks) {
555
+ nextTxnCallbacks.push(unwrittenResolution.callbacks);
556
+ unwrittenResolution.callbacks = null;
557
+ }
558
+ outstandingWriteCount--;
559
+ if (unwrittenResolution.flag !== HAS_TXN) {
560
+ if (unwrittenResolution.flag === NO_RESOLVE && !unwrittenResolution.meta) {
561
+ // in this case we can completely remove from the linked list, clearing more memory
562
+ lastPromisedResolution.next = unwrittenResolution = unwrittenResolution.next;
563
+ continue;
564
+ }
565
+ unwrittenResolution.uint32 = null;
566
+ }
567
+ unwrittenResolution.valueBuffer = null;
568
+ unwrittenResolution.flag = instructionStatus;
569
+ lastPromisedResolution = unwrittenResolution;
570
+ unwrittenResolution = unwrittenResolution.next;
571
+ }
572
+ while (txnResolution &&
573
+ (instructionStatus = txnResolution.uint32[txnResolution.flagPosition] & TXN_DONE)) {
574
+ if (instructionStatus & TXN_FAILED)
575
+ rejectCommit();
576
+ else
577
+ resolveCommit(async);
578
+ }
579
+ }
580
+
581
+ function resolveCommit(async) {
582
+ afterCommit(txnResolution.uint32[txnResolution.flagPosition - 1]);
583
+ if (async)
584
+ resetReadTxn();
585
+ else
586
+ queueMicrotask(resetReadTxn); // TODO: only do this if there are actually committed writes?
587
+ do {
588
+ if (uncommittedResolution.meta && uncommittedResolution.meta.resolve) {
589
+ let resolve = uncommittedResolution.meta.resolve;
590
+ if (uncommittedResolution.flag & FAILED_CONDITION && !resolve.unconditional)
591
+ resolve(false);
592
+ else
593
+ resolve(true);
594
+ }
595
+ } while((uncommittedResolution = uncommittedResolution.next) && uncommittedResolution != txnResolution)
596
+ txnResolution = txnResolution.nextTxn;
597
+ }
598
+ var commitRejectPromise;
599
+ function rejectCommit() {
600
+ afterCommit();
601
+ if (!commitRejectPromise) {
602
+ let rejectFunction;
603
+ commitRejectPromise = new Promise((resolve, reject) => rejectFunction = reject);
604
+ commitRejectPromise.reject = rejectFunction;
605
+ }
606
+ do {
607
+ if (uncommittedResolution.meta && uncommittedResolution.meta.reject) {
608
+ uncommittedResolution.flag & 0xf;
609
+ let error = new Error("Commit failed (see commitError for details)");
610
+ error.commitError = commitRejectPromise;
611
+ uncommittedResolution.meta.reject(error);
612
+ }
613
+ } while((uncommittedResolution = uncommittedResolution.next) && uncommittedResolution != txnResolution)
614
+ txnResolution = txnResolution.nextTxn;
615
+ }
616
+ function atomicStatus(uint32, flagPosition, newStatus) {
617
+ if (batchDepth) {
618
+ // if we are in a batch, the transaction can't close, so we do the faster,
619
+ // but non-deterministic updates, knowing that the write thread can
620
+ // just poll for the status change if we miss a status update
621
+ let writeStatus = uint32[flagPosition];
622
+ uint32[flagPosition] = newStatus;
623
+ return writeStatus;
624
+ //return Atomics.or(uint32, flagPosition, newStatus)
625
+ } else // otherwise the transaction could end at any time and we need to know the
626
+ // deterministically if it is ending, so we can reset the commit promise
627
+ // so we use the slower atomic operation
628
+ try {
629
+ return Atomics.or(uint32, flagPosition, newStatus);
630
+ } catch(error) {
631
+ console.error(error);
632
+ return;
633
+ }
634
+ }
635
+ function afterCommit(txnId) {
636
+ for (let i = 0, l = afterCommitCallbacks.length; i < l; i++) {
637
+ try {
638
+ afterCommitCallbacks[i]({next: uncommittedResolution, last: txnResolution, txnId});
639
+ } catch(error) {
640
+ console.error('In aftercommit callback', error);
641
+ }
642
+ }
643
+ }
644
+ async function executeTxnCallbacks() {
645
+ env.writeTxn = writeTxn = { write: true };
646
+ nextTxnCallbacks.isExecuting = true;
647
+ for (let i = 0; i < nextTxnCallbacks.length; i++) {
648
+ let txnCallbacks = nextTxnCallbacks[i];
649
+ for (let j = 0, l = txnCallbacks.length; j < l; j++) {
650
+ let userTxnCallback = txnCallbacks[j];
651
+ let asChild = userTxnCallback.asChild;
652
+ if (asChild) {
653
+ env.beginTxn(1); // abortable
654
+ let parentTxn = writeTxn;
655
+ env.writeTxn = writeTxn = { write: true };
656
+ try {
657
+ let result = userTxnCallback.callback();
658
+ if (result && result.then) {
659
+ hasUnresolvedTxns = true;
660
+ await result;
661
+ }
662
+ if (result === ABORT)
663
+ env.abortTxn();
664
+ else
665
+ env.commitTxn();
666
+ clearWriteTxn(parentTxn);
667
+ txnCallbacks[j] = result;
668
+ } catch(error) {
669
+ clearWriteTxn(parentTxn);
670
+ env.abortTxn();
671
+ txnError(error, txnCallbacks, j);
672
+ }
673
+ } else {
674
+ try {
675
+ let result = userTxnCallback();
676
+ txnCallbacks[j] = result;
677
+ if (result && result.then) {
678
+ hasUnresolvedTxns = true;
679
+ await result;
680
+ }
681
+ } catch(error) {
682
+ txnError(error, txnCallbacks, j);
683
+ }
684
+ }
685
+ }
686
+ }
687
+ nextTxnCallbacks = [];
688
+ clearWriteTxn(null);
689
+ if (hasUnresolvedTxns) {
690
+ env.resumeWriting();
691
+ }
692
+ function txnError(error, txnCallbacks, i) {
693
+ (txnCallbacks.errors || (txnCallbacks.errors = []))[i] = error;
694
+ txnCallbacks[i] = CALLBACK_THREW;
695
+ }
696
+ }
697
+ function finishBatch() {
698
+ dynamicBytes.uint32[(dynamicBytes.position + 1) << 1] = 0; // clear out the next slot
699
+ let writeStatus = atomicStatus(dynamicBytes.uint32, (dynamicBytes.position++) << 1, 2); // atomically write the end block
700
+ nextResolution.flagPosition += 2;
701
+ if (writeStatus & WAITING_OPERATION) {
702
+ write(env.address, 0);
703
+ }
704
+ if (dynamicBytes.position > newBufferThreshold) {
705
+ allocateInstructionBuffer(dynamicBytes.position);
706
+ nextResolution.flagPosition = dynamicBytes.position << 1;
707
+ nextResolution.uint32 = dynamicBytes.uint32;
708
+ }
709
+ }
710
+ function clearWriteTxn(parentTxn) {
711
+ // TODO: We might actually want to track cursors in a write txn and manually
712
+ // close them.
713
+ if (writeTxn && writeTxn.refCount > 0)
714
+ writeTxn.isDone = true;
715
+ env.writeTxn = writeTxn = parentTxn || null;
716
+ }
717
+ Object.assign(LMDBStore.prototype, {
718
+ put(key, value, versionOrOptions, ifVersion) {
719
+ let callback, flags = 15, type = typeof versionOrOptions;
720
+ if (type == 'object' && versionOrOptions) {
721
+ if (versionOrOptions.noOverwrite)
722
+ flags |= 0x10;
723
+ if (versionOrOptions.noDupData)
724
+ flags |= 0x20;
725
+ if (versionOrOptions.append)
726
+ flags |= 0x20000;
727
+ if (versionOrOptions.ifVersion != undefined)
728
+ ifVersion = versionOrOptions.ifVersion;
729
+ versionOrOptions = versionOrOptions.version;
730
+ if (typeof ifVersion == 'function')
731
+ callback = ifVersion;
732
+ } else if (type == 'function') {
733
+ callback = versionOrOptions;
734
+ }
735
+ return writeInstructions(flags, this, key, value, this.useVersions ? versionOrOptions || 0 : undefined, ifVersion)(callback);
736
+ },
737
+ remove(key, ifVersionOrValue, callback) {
738
+ let flags = 13;
739
+ let ifVersion, value;
740
+ if (ifVersionOrValue !== undefined) {
741
+ if (typeof ifVersionOrValue == 'function')
742
+ callback = ifVersionOrValue;
743
+ else if (ifVersionOrValue === IF_EXISTS$1 && !callback)
744
+ // we have a handler for IF_EXISTS in the callback handler for remove
745
+ callback = ifVersionOrValue;
746
+ else if (this.useVersions)
747
+ ifVersion = ifVersionOrValue;
748
+ else {
749
+ flags = 14;
750
+ value = ifVersionOrValue;
751
+ }
752
+ }
753
+ return writeInstructions(flags, this, key, value, undefined, ifVersion)(callback);
754
+ },
755
+ del(key, options, callback) {
756
+ return this.remove(key, options, callback);
757
+ },
758
+ ifNoExists(key, callback) {
759
+ return this.ifVersion(key, null, callback);
760
+ },
761
+ ifVersion(key, version, callback, options) {
762
+ if (!callback) {
763
+ return new Batch((operations, callback) => {
764
+ let promise = this.ifVersion(key, version, operations, options);
765
+ if (callback)
766
+ promise.then(callback);
767
+ return promise;
768
+ });
769
+ }
770
+ if (writeTxn) {
771
+ if (version === undefined || this.doesExist(key, version)) {
772
+ callback();
773
+ return SYNC_PROMISE_SUCCESS;
774
+ }
775
+ return SYNC_PROMISE_FAIL;
776
+ }
777
+ let flags = key === undefined || version === undefined ? 1 : 4;
778
+ if (options?.ifLessThan)
779
+ flags |= CONDITIONAL_VERSION_LESS_THAN;
780
+ if (options?.allowNotFound)
781
+ flags |= CONDITIONAL_ALLOW_NOTFOUND;
782
+ let finishStartWrite = writeInstructions(flags, this, key, undefined, undefined, version);
783
+ let promise;
784
+ batchDepth += 2;
785
+ if (batchDepth > 2)
786
+ promise = finishStartWrite();
787
+ else {
788
+ writeBatchStart = () => {
789
+ promise = finishStartWrite();
790
+ };
791
+ outstandingBatchCount = 0;
792
+ }
793
+ try {
794
+ if (typeof callback === 'function') {
795
+ callback();
796
+ } else {
797
+ for (let i = 0, l = callback.length; i < l; i++) {
798
+ let operation = callback[i];
799
+ this[operation.type](operation.key, operation.value);
800
+ }
801
+ }
802
+ } finally {
803
+ if (!promise) {
804
+ finishBatch();
805
+ batchDepth -= 2;
806
+ promise = finishStartWrite(); // finish write once all the operations have been written (and it hasn't been written prematurely)
807
+ writeBatchStart = null;
808
+ } else {
809
+ batchDepth -= 2;
810
+ finishBatch();
811
+ }
812
+ }
813
+ return promise;
814
+ },
815
+ batch(callbackOrOperations) {
816
+ return this.ifVersion(undefined, undefined, callbackOrOperations);
817
+ },
818
+ drop(callback) {
819
+ return writeInstructions(1024 + 12, this, Buffer.from([]), undefined, undefined, undefined)(callback);
820
+ },
821
+ clearAsync(callback) {
822
+ if (this.encoder) {
823
+ if (this.encoder.clearSharedData)
824
+ this.encoder.clearSharedData();
825
+ else if (this.encoder.structures)
826
+ this.encoder.structures = [];
827
+ }
828
+ return writeInstructions(12, this, Buffer.from([]), undefined, undefined, undefined)(callback);
829
+ },
830
+ _triggerError() {
831
+ finishBatch();
832
+ },
833
+
834
+ putSync(key, value, versionOrOptions, ifVersion) {
835
+ if (writeTxn)
836
+ return this.put(key, value, versionOrOptions, ifVersion) === SYNC_PROMISE_SUCCESS;
837
+ else
838
+ return this.transactionSync(() =>
839
+ this.put(key, value, versionOrOptions, ifVersion) === SYNC_PROMISE_SUCCESS, overlappingSync? 0x10002 : 2); // non-abortable, async flush
840
+ },
841
+ removeSync(key, ifVersionOrValue) {
842
+ if (writeTxn)
843
+ return this.remove(key, ifVersionOrValue) === SYNC_PROMISE_SUCCESS;
844
+ else
845
+ return this.transactionSync(() =>
846
+ this.remove(key, ifVersionOrValue) === SYNC_PROMISE_SUCCESS, overlappingSync? 0x10002 : 2); // non-abortable, async flush
847
+ },
848
+ transaction(callback) {
849
+ if (writeTxn && !nextTxnCallbacks.isExecuting) {
850
+ if (!useWritemap)
851
+ return this.childTransaction(callback);
852
+ let result = callback();
853
+ if (result == ABORT && !abortedNonChildTransactionWarn) {
854
+ console.warn('Can not abort a transaction inside another transaction with ' + (this.cache ? 'caching enabled' : 'useWritemap enabled'));
855
+ abortedNonChildTransactionWarn = true;
856
+ }
857
+ return result;
858
+ }
859
+ return this.transactionAsync(callback, true);
860
+ },
861
+ childTransaction(callback) {
862
+ if (useWritemap)
863
+ throw new Error('Child transactions are not supported in writemap mode');
864
+ if (writeTxn) {
865
+ let parentTxn = writeTxn;
866
+ let thisTxn = env.writeTxn = writeTxn = { write: true };
867
+ env.beginTxn(1); // abortable
868
+ let callbackDone, finishTxn;
869
+ try {
870
+ return writeTxn.childResults = when(callback(), finishTxn = (result) => {
871
+ if (writeTxn !== thisTxn) // need to wait for child txn to finish asynchronously
872
+ return writeTxn.childResults.then(() => finishTxn(result));
873
+ callbackDone = true;
874
+ if (result === ABORT)
875
+ env.abortTxn();
876
+ else
877
+ env.commitTxn();
878
+ clearWriteTxn(parentTxn);
879
+ return result;
880
+ }, (error) => {
881
+ env.abortTxn();
882
+ clearWriteTxn(parentTxn);
883
+ throw error;
884
+ });
885
+ } catch(error) {
886
+ if (!callbackDone)
887
+ env.abortTxn();
888
+ clearWriteTxn(parentTxn);
889
+ throw error;
890
+ }
891
+ }
892
+ return this.transactionAsync(callback, true);
893
+ },
894
+ transactionAsync(callback, asChild) {
895
+ let txnIndex;
896
+ let txnCallbacks;
897
+ if (lastQueuedResolution.callbacks) {
898
+ txnCallbacks = lastQueuedResolution.callbacks;
899
+ txnIndex = txnCallbacks.push(asChild ? { callback, asChild } : callback) - 1;
900
+ } else if (nextTxnCallbacks.isExecuting) {
901
+ txnCallbacks = [asChild ? { callback, asChild } : callback];
902
+ txnCallbacks.results = commitPromise;
903
+ nextTxnCallbacks.push(txnCallbacks);
904
+ txnIndex = 0;
905
+ } else {
906
+ if (writeTxn)
907
+ throw new Error('Can not enqueue transaction during write txn');
908
+ let finishWrite = writeInstructions(8 | (this.strictAsyncOrder ? 0x100000 : 0), this);
909
+ txnCallbacks = [asChild ? { callback, asChild } : callback];
910
+ lastQueuedResolution.callbacks = txnCallbacks;
911
+ lastQueuedResolution.id = Math.random();
912
+ txnCallbacks.results = finishWrite();
913
+ txnIndex = 0;
914
+ }
915
+ return txnCallbacks.results.then((results) => {
916
+ let result = txnCallbacks[txnIndex];
917
+ if (result === CALLBACK_THREW)
918
+ throw txnCallbacks.errors[txnIndex];
919
+ return result;
920
+ });
921
+ },
922
+ transactionSync(callback, flags) {
923
+ if (writeTxn) {
924
+ if (!useWritemap && (flags == undefined || (flags & 1))) // can't use child transactions in write maps
925
+ // already nested in a transaction, execute as child transaction (if possible) and return
926
+ return this.childTransaction(callback);
927
+ let result = callback(); // else just run in current transaction
928
+ if (result == ABORT && !abortedNonChildTransactionWarn) {
929
+ console.warn('Can not abort a transaction inside another transaction with ' + (this.cache ? 'caching enabled' : 'useWritemap enabled'));
930
+ abortedNonChildTransactionWarn = true;
931
+ }
932
+ return result;
933
+ }
934
+ let callbackDone, finishTxn;
935
+ this.transactions++;
936
+ if (!env.address)
937
+ throw new Error('The database has been closed and you can not transact on it');
938
+ env.beginTxn(flags == undefined ? 3 : flags);
939
+ let thisTxn = writeTxn = env.writeTxn = { write: true };
940
+ try {
941
+ this.emit('begin-transaction');
942
+ return writeTxn.childResults = when(callback(), finishTxn = (result) => {
943
+ if (writeTxn !== thisTxn) // need to wait for child txn to finish asynchronously
944
+ return writeTxn.childResults.then(() => finishTxn(result));
945
+ try {
946
+ callbackDone = true;
947
+ if (result === ABORT)
948
+ env.abortTxn();
949
+ else {
950
+ env.commitTxn();
951
+ resetReadTxn();
952
+ }
953
+ return result;
954
+ } finally {
955
+ clearWriteTxn(null);
956
+ }
957
+ }, (error) => {
958
+ try { env.abortTxn(); } catch(e) {}
959
+ clearWriteTxn(null);
960
+ throw error;
961
+ });
962
+ } catch(error) {
963
+ if (!callbackDone)
964
+ try { env.abortTxn(); } catch(e) {}
965
+ clearWriteTxn(null);
966
+ throw error;
967
+ }
968
+ },
969
+ transactionSyncStart(callback) {
970
+ return this.transactionSync(callback, 0);
971
+ },
972
+ // make the db a thenable/promise-like for when the last commit is committed
973
+ committed: committed = {
974
+ then(onfulfilled, onrejected) {
975
+ if (commitPromise)
976
+ return commitPromise.then(onfulfilled, onrejected);
977
+ if (lastWritePromise) // always resolve to true
978
+ return lastWritePromise.then(() => onfulfilled(true), onrejected);
979
+ return SYNC_PROMISE_SUCCESS.then(onfulfilled, onrejected);
980
+ }
981
+ },
982
+ flushed: {
983
+ // make this a thenable for when the commit is flushed to disk
984
+ then(onfulfilled, onrejected) {
985
+ if (flushPromise)
986
+ flushPromise.hasCallbacks = true;
987
+ return Promise.all([flushPromise || committed, lastSyncTxnFlush]).then(onfulfilled, onrejected);
988
+ }
989
+ },
990
+ _endWrites(resolvedPromise, resolvedSyncPromise) {
991
+ this.put = this.remove = this.del = this.batch = this.removeSync = this.putSync = this.transactionAsync = this.drop = this.clearAsync = () => { throw new Error('Database is closed') };
992
+ // wait for all txns to finish, checking again after the current txn is done
993
+ let finalPromise = flushPromise || commitPromise || lastWritePromise;
994
+ if (flushPromise)
995
+ flushPromise.hasCallbacks = true;
996
+ let finalSyncPromise = lastSyncTxnFlush;
997
+ if (finalPromise && resolvedPromise != finalPromise ||
998
+ finalSyncPromise ) {
999
+ return Promise.all([finalPromise, finalSyncPromise]).then(() => this._endWrites(finalPromise, finalSyncPromise), () => this._endWrites(finalPromise, finalSyncPromise));
1000
+ }
1001
+ Object.defineProperty(env, 'sync', { value: null });
1002
+ },
1003
+ on(event, callback) {
1004
+ if (event == 'beforecommit') {
1005
+ eventTurnBatching = true;
1006
+ beforeCommitCallbacks.push(callback);
1007
+ } else if (event == 'aftercommit')
1008
+ afterCommitCallbacks.push(callback);
1009
+ else
1010
+ super.on(event, callback);
1011
+ }
1012
+ });
1013
+ }
1014
+
1015
+ class Batch extends Array {
1016
+ constructor(callback) {
1017
+ super();
1018
+ this.callback = callback;
1019
+ }
1020
+ put(key, value) {
1021
+ this.push({ type: 'put', key, value });
1022
+ }
1023
+ del(key) {
1024
+ this.push({ type: 'del', key });
1025
+ }
1026
+ clear() {
1027
+ this.splice(0, this.length);
1028
+ }
1029
+ write(callback) {
1030
+ return this.callback(this, callback);
1031
+ }
1032
+ }
1033
+ function asBinary(buffer) {
1034
+ return {
1035
+ ['\x10binary-data\x02']: buffer
1036
+ };
1037
+ }
1038
+
1039
+ const SKIP = {};
1040
+ const DONE = {
1041
+ value: null,
1042
+ done: true,
1043
+ };
1044
+ if (!Symbol.asyncIterator) {
1045
+ Symbol.asyncIterator = Symbol.for('Symbol.asyncIterator');
1046
+ }
1047
+
1048
+ class RangeIterable {
1049
+ constructor(sourceArray) {
1050
+ if (sourceArray) {
1051
+ this.iterate = sourceArray[Symbol.iterator].bind(sourceArray);
1052
+ }
1053
+ }
1054
+ map(func) {
1055
+ let source = this;
1056
+ let iterable = new RangeIterable();
1057
+ iterable.iterate = (async) => {
1058
+ let iterator = source[Symbol.iterator](async);
1059
+ let i = 0;
1060
+ return {
1061
+ next(resolvedResult) {
1062
+ let result;
1063
+ do {
1064
+ let iteratorResult;
1065
+ if (resolvedResult) {
1066
+ iteratorResult = resolvedResult;
1067
+ resolvedResult = null; // don't go in this branch on next iteration
1068
+ } else {
1069
+ iteratorResult = iterator.next();
1070
+ if (iteratorResult.then) {
1071
+ return iteratorResult.then(iteratorResult => this.next(iteratorResult));
1072
+ }
1073
+ }
1074
+ if (iteratorResult.done === true) {
1075
+ this.done = true;
1076
+ if (iterable.onDone) iterable.onDone();
1077
+ return iteratorResult;
1078
+ }
1079
+ result = func(iteratorResult.value, i++);
1080
+ if (result && result.then) {
1081
+ return result.then(result =>
1082
+ result === SKIP ?
1083
+ this.next() :
1084
+ {
1085
+ value: result
1086
+ });
1087
+ }
1088
+ } while(result === SKIP);
1089
+ if (result === DONE) {
1090
+ if (iterable.onDone) iterable.onDone();
1091
+ return result;
1092
+ }
1093
+ return {
1094
+ value: result
1095
+ };
1096
+ },
1097
+ return() {
1098
+ if (iterable.onDone) iterable.onDone();
1099
+ return iterator.return();
1100
+ },
1101
+ throw() {
1102
+ if (iterable.onDone) iterable.onDone();
1103
+ return iterator.throw();
1104
+ }
1105
+ };
1106
+ };
1107
+ return iterable;
1108
+ }
1109
+ [Symbol.asyncIterator]() {
1110
+ return this.iterator = this.iterate();
1111
+ }
1112
+ [Symbol.iterator]() {
1113
+ return this.iterator = this.iterate();
1114
+ }
1115
+ filter(func) {
1116
+ return this.map(element => {
1117
+ let result = func(element);
1118
+ // handle promise
1119
+ if (result?.then) return result.then((result) => result ? element : SKIP);
1120
+ else return result ? element : SKIP;
1121
+ });
1122
+ }
1123
+
1124
+ forEach(callback) {
1125
+ let iterator = this.iterator = this.iterate();
1126
+ let result;
1127
+ while ((result = iterator.next()).done !== true) {
1128
+ callback(result.value);
1129
+ }
1130
+ }
1131
+ concat(secondIterable) {
1132
+ let concatIterable = new RangeIterable();
1133
+ concatIterable.iterate = (async) => {
1134
+ let iterator = this.iterator = this.iterate();
1135
+ let isFirst = true;
1136
+ function iteratorDone(result) {
1137
+ if (isFirst) {
1138
+ isFirst = false;
1139
+ iterator = secondIterable[Symbol.iterator](async);
1140
+ result = iterator.next();
1141
+ if (concatIterable.onDone) {
1142
+ if (result.then)
1143
+ result.then((result) => {
1144
+ if (result.done()) concatIterable.onDone();
1145
+ });
1146
+ else if (result.done) concatIterable.onDone();
1147
+ }
1148
+ } else {
1149
+ if (concatIterable.onDone) concatIterable.onDone();
1150
+ }
1151
+ return result;
1152
+ }
1153
+ return {
1154
+ next() {
1155
+ let result = iterator.next();
1156
+ if (result.then)
1157
+ return result.then((result) => {
1158
+ if (result.done) return iteratorDone(result);
1159
+ return result;
1160
+ });
1161
+ if (result.done) return iteratorDone(result);
1162
+ return result;
1163
+ },
1164
+ return() {
1165
+ if (concatIterable.onDone) concatIterable.onDone();
1166
+ return iterator.return();
1167
+ },
1168
+ throw() {
1169
+ if (concatIterable.onDone) concatIterable.onDone();
1170
+ return iterator.throw();
1171
+ }
1172
+ };
1173
+ };
1174
+ return concatIterable;
1175
+ }
1176
+
1177
+ flatMap(callback) {
1178
+ let mappedIterable = new RangeIterable();
1179
+ mappedIterable.iterate = (async) => {
1180
+ let iterator = this.iterator = this.iterate(async);
1181
+ let currentSubIterator;
1182
+ return {
1183
+ next() {
1184
+ do {
1185
+ if (currentSubIterator) {
1186
+ let result = currentSubIterator.next();
1187
+ if (!result.done) {
1188
+ return result;
1189
+ }
1190
+ }
1191
+ let result = iterator.next();
1192
+ if (result.done) {
1193
+ if (mappedIterable.onDone) mappedIterable.onDone();
1194
+ return result;
1195
+ }
1196
+ let value = callback(result.value);
1197
+ if (Array.isArray(value) || value instanceof RangeIterable)
1198
+ currentSubIterator = value[Symbol.iterator]();
1199
+ else {
1200
+ currentSubIterator = null;
1201
+ return { value };
1202
+ }
1203
+ } while(true);
1204
+ },
1205
+ return() {
1206
+ if (mappedIterable.onDone) mappedIterable.onDone();
1207
+ if (currentSubIterator)
1208
+ currentSubIterator.return();
1209
+ return iterator.return();
1210
+ },
1211
+ throw() {
1212
+ if (mappedIterable.onDone) mappedIterable.onDone();
1213
+ if (currentSubIterator)
1214
+ currentSubIterator.throw();
1215
+ return iterator.throw();
1216
+ }
1217
+ };
1218
+ };
1219
+ return mappedIterable;
1220
+ }
1221
+
1222
+ slice(start, end) {
1223
+ return this.map((element, i) => {
1224
+ if (i < start)
1225
+ return SKIP;
1226
+ if (i >= end) {
1227
+ DONE.value = element;
1228
+ return DONE;
1229
+ }
1230
+ return element;
1231
+ });
1232
+ }
1233
+ next() {
1234
+ if (!this.iterator)
1235
+ this.iterator = this.iterate();
1236
+ return this.iterator.next();
1237
+ }
1238
+ toJSON() {
1239
+ if (this.asArray && this.asArray.forEach) {
1240
+ return this.asArray;
1241
+ }
1242
+ throw new Error('Can not serialize async iterables without first calling resolveJSON');
1243
+ //return Array.from(this)
1244
+ }
1245
+ get asArray() {
1246
+ if (this._asArray)
1247
+ return this._asArray;
1248
+ let promise = new Promise((resolve, reject) => {
1249
+ let iterator = this.iterate();
1250
+ let array = [];
1251
+ let iterable = this;
1252
+ Object.defineProperty(array, 'iterable', { value: iterable });
1253
+ function next(result) {
1254
+ while (result.done !== true) {
1255
+ if (result.then) {
1256
+ return result.then(next);
1257
+ } else {
1258
+ array.push(result.value);
1259
+ }
1260
+ result = iterator.next();
1261
+ }
1262
+ resolve(iterable._asArray = array);
1263
+ }
1264
+ next(iterator.next());
1265
+ });
1266
+ promise.iterable = this;
1267
+ return this._asArray || (this._asArray = promise);
1268
+ }
1269
+ resolveData() {
1270
+ return this.asArray;
1271
+ }
1272
+ }
1273
+ RangeIterable.prototype.DONE = DONE;
1274
+
1275
+ const REUSE_BUFFER_MODE = 512;
1276
+ const writeUint32Key = (key, target, start) => {
1277
+ (target.dataView || (target.dataView = new DataView(target.buffer, 0, target.length))).setUint32(start, key, true);
1278
+ return start + 4;
1279
+ };
1280
+ const readUint32Key = (target, start) => {
1281
+ return (target.dataView || (target.dataView = new DataView(target.buffer, 0, target.length))).getUint32(start, true);
1282
+ };
1283
+ const writeBufferKey = (key, target, start) => {
1284
+ target.set(key, start);
1285
+ return key.length + start;
1286
+ };
1287
+ const Uint8ArraySlice$1 = Uint8Array.prototype.slice;
1288
+ const readBufferKey = (target, start, end) => {
1289
+ return Uint8ArraySlice$1.call(target, start, end);
1290
+ };
1291
+
1292
+ let lastEncodedValue, bytes;
1293
+ function applyKeyHandling(store) {
1294
+ if (store.encoding == 'ordered-binary') {
1295
+ store.encoder = store.decoder = {
1296
+ writeKey: orderedBinary.writeKey,
1297
+ readKey: orderedBinary.readKey,
1298
+ };
1299
+ }
1300
+ if (store.encoder && store.encoder.writeKey && !store.encoder.encode) {
1301
+ store.encoder.encode = function(value, mode) {
1302
+ if (typeof value !== 'object' && value && value === lastEncodedValue) ; else {
1303
+ lastEncodedValue = value;
1304
+ bytes = saveKey(value, this.writeKey, false, store.maxKeySize);
1305
+ }
1306
+ if (bytes.end > 0 && !(REUSE_BUFFER_MODE & mode)) {
1307
+ return bytes.subarray(bytes.start, bytes.end);
1308
+ }
1309
+ return bytes;
1310
+ };
1311
+ store.encoder.copyBuffers = true; // just an indicator for the buffer reuse in write.js
1312
+ }
1313
+ if (store.decoder && store.decoder.readKey && !store.decoder.decode) {
1314
+ store.decoder.decode = function(buffer) { return this.readKey(buffer, 0, buffer.length); };
1315
+ store.decoderCopies = true;
1316
+ }
1317
+ if (store.keyIsUint32 || store.keyEncoding == 'uint32') {
1318
+ store.writeKey = writeUint32Key;
1319
+ store.readKey = readUint32Key;
1320
+ } else if (store.keyIsBuffer || store.keyEncoding == 'binary') {
1321
+ store.writeKey = writeBufferKey;
1322
+ store.readKey = readBufferKey;
1323
+ } else if (store.keyEncoder) {
1324
+ store.writeKey = store.keyEncoder.writeKey;
1325
+ store.readKey = store.keyEncoder.readKey;
1326
+ } else {
1327
+ store.writeKey = orderedBinary.writeKey;
1328
+ store.readKey = orderedBinary.readKey;
1329
+ }
1330
+ }
1331
+
1332
+ let saveBuffer, saveDataView = { setFloat64() {}, setUint32() {} }, saveDataAddress;
1333
+ let savePosition$1 = 8000;
1334
+ let DYNAMIC_KEY_BUFFER_SIZE$1 = 8192;
1335
+ function allocateSaveBuffer() {
1336
+ saveBuffer = typeof Buffer != 'undefined' ? Buffer.alloc(DYNAMIC_KEY_BUFFER_SIZE$1) : new Uint8Array(DYNAMIC_KEY_BUFFER_SIZE$1);
1337
+ saveBuffer.buffer.address = getAddress(saveBuffer.buffer);
1338
+ saveDataAddress = saveBuffer.buffer.address;
1339
+ // TODO: Conditionally only do this for key sequences?
1340
+ saveDataView.setUint32(savePosition$1, 0xffffffff);
1341
+ saveDataView.setFloat64(savePosition$1 + 4, saveDataAddress, true); // save a pointer from the old buffer to the new address for the sake of the prefetch sequences
1342
+ saveDataView = saveBuffer.dataView || (saveBuffer.dataView = new DataView(saveBuffer.buffer, saveBuffer.byteOffset, saveBuffer.byteLength));
1343
+ savePosition$1 = 0;
1344
+ }
1345
+ function saveKey(key, writeKey, saveTo, maxKeySize, flags) {
1346
+ if (savePosition$1 > 7800) {
1347
+ allocateSaveBuffer();
1348
+ }
1349
+ let start = savePosition$1;
1350
+ try {
1351
+ savePosition$1 = key === undefined ? start + 4 :
1352
+ writeKey(key, saveBuffer, start + 4);
1353
+ } catch (error) {
1354
+ saveBuffer.fill(0, start + 4); // restore zeros
1355
+ if (error.name == 'RangeError') {
1356
+ if (8180 - start < maxKeySize) {
1357
+ allocateSaveBuffer(); // try again:
1358
+ return saveKey(key, writeKey, saveTo, maxKeySize);
1359
+ }
1360
+ throw new Error('Key was too large, max key size is ' + maxKeySize);
1361
+ } else
1362
+ throw error;
1363
+ }
1364
+ let length = savePosition$1 - start - 4;
1365
+ if (length > maxKeySize) {
1366
+ throw new Error('Key of size ' + length + ' was too large, max key size is ' + maxKeySize);
1367
+ }
1368
+ if (savePosition$1 >= 8160) { // need to reserve enough room at the end for pointers
1369
+ savePosition$1 = start; // reset position
1370
+ allocateSaveBuffer(); // try again:
1371
+ return saveKey(key, writeKey, saveTo, maxKeySize);
1372
+ }
1373
+ if (saveTo) {
1374
+ saveDataView.setUint32(start, flags ? length | flags : length, true); // save the length
1375
+ saveTo.saveBuffer = saveBuffer;
1376
+ savePosition$1 = (savePosition$1 + 12) & 0xfffffc;
1377
+ return start + saveDataAddress;
1378
+ } else {
1379
+ saveBuffer.start = start + 4;
1380
+ saveBuffer.end = savePosition$1;
1381
+ savePosition$1 = (savePosition$1 + 7) & 0xfffff8; // full 64-bit word alignment since these are usually copied
1382
+ return saveBuffer;
1383
+ }
1384
+ }
1385
+
1386
+ const IF_EXISTS = 3.542694326329068e-103;
1387
+ const ITERATOR_DONE = { done: true, value: undefined };
1388
+ const Uint8ArraySlice = Uint8Array.prototype.slice;
1389
+ let getValueBytes = globalBuffer;
1390
+ if (!getValueBytes.maxLength) {
1391
+ getValueBytes.maxLength = getValueBytes.length;
1392
+ getValueBytes.isGlobal = true;
1393
+ Object.defineProperty(getValueBytes, 'length', { value: getValueBytes.length, writable: true, configurable: true });
1394
+ }
1395
+ const START_ADDRESS_POSITION = 4064;
1396
+ const NEW_BUFFER_THRESHOLD = 0x8000;
1397
+ const SOURCE_SYMBOL = Symbol.for('source');
1398
+ const UNMODIFIED = {};
1399
+ let mmaps = [];
1400
+
1401
+ function addReadMethods(LMDBStore, {
1402
+ maxKeySize, env, keyBytes, keyBytesView, getLastVersion, getLastTxnId
1403
+ }) {
1404
+ let readTxn, readTxnRenewed, asSafeBuffer = false;
1405
+ let renewId = 1;
1406
+ let outstandingReads = 0;
1407
+ Object.assign(LMDBStore.prototype, {
1408
+ getString(id, options) {
1409
+ let txn = env.writeTxn || (options && options.transaction) || (readTxnRenewed ? readTxn : renewReadTxn(this));
1410
+ let string = getStringByBinary(this.dbAddress, this.writeKey(id, keyBytes, 0), txn.address || 0);
1411
+ if (typeof string === 'number') { // indicates the buffer wasn't large enough
1412
+ this._allocateGetBuffer(string);
1413
+ // and then try again
1414
+ string = getStringByBinary(this.dbAddress, this.writeKey(id, keyBytes, 0), txn.address || 0);
1415
+ }
1416
+ if (string)
1417
+ this.lastSize = string.length;
1418
+ return string;
1419
+ },
1420
+ getBinaryFast(id, options) {
1421
+ let rc;
1422
+ let txn = env.writeTxn || (options && options.transaction) || (readTxnRenewed ? readTxn : renewReadTxn(this));
1423
+ rc = this.lastSize = getByBinary(this.dbAddress, this.writeKey(id, keyBytes, 0), (options && options.ifNotTxnId) || 0, txn.address || 0);
1424
+ if (rc < 0) {
1425
+ if (rc == -30798) // MDB_NOTFOUND
1426
+ return; // undefined
1427
+ if (rc == -30004) // txn id matched
1428
+ return UNMODIFIED;
1429
+ if (rc == -30781 /*MDB_BAD_VALSIZE*/ && this.writeKey(id, keyBytes, 0) == 0)
1430
+ throw new Error(id === undefined ?
1431
+ 'A key is required for get, but is undefined' :
1432
+ 'Zero length key is not allowed in LMDB');
1433
+ if (rc == -30000) // int32 overflow, read uint32
1434
+ rc = this.lastSize = keyBytesView.getUint32(0, true);
1435
+ else if (rc == -30001) {// shared buffer
1436
+ this.lastSize = keyBytesView.getUint32(0, true);
1437
+ let bufferId = keyBytesView.getUint32(4, true);
1438
+ return getMMapBuffer(bufferId, this.lastSize);
1439
+ } else
1440
+ throw lmdbError(rc);
1441
+ }
1442
+ let compression = this.compression;
1443
+ let bytes = compression ? compression.getValueBytes : getValueBytes;
1444
+ if (rc > bytes.maxLength) {
1445
+ // 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
1446
+ return this._returnLargeBuffer(
1447
+ () => getByBinary(this.dbAddress, this.writeKey(id, keyBytes, 0), 0, txn.address || 0));
1448
+ }
1449
+ bytes.length = this.lastSize;
1450
+ return bytes;
1451
+ },
1452
+ getBFAsync(id, options, callback) {
1453
+ let txn = env.writeTxn || (options && options.transaction) || (readTxnRenewed ? readTxn : renewReadTxn(this));
1454
+ txn.refCount = (txn.refCount || 0) + 1;
1455
+ outstandingReads++;
1456
+ let address = recordReadInstruction(txn.address, this.db.dbi, id, this.writeKey, maxKeySize, ( rc, bufferId, offset, size ) => {
1457
+ if (rc && rc !== 1)
1458
+ callback(lmdbError(rc));
1459
+ outstandingReads--;
1460
+ let buffer = mmaps[bufferId];
1461
+ if (!buffer) {
1462
+ buffer = mmaps[bufferId] = getSharedBuffer(bufferId, env.address);
1463
+ }
1464
+ //console.log({bufferId, offset, size})
1465
+ if (buffer.isSharedMap) {
1466
+ // using LMDB shared memory
1467
+ // TODO: We may want explicit support for clearing aborting the transaction on the next event turn,
1468
+ // but for now we are relying on the GC to cleanup transaction for larger blocks of memory
1469
+ let bytes = new Uint8Array(buffer, offset, size);
1470
+ bytes.txn = txn;
1471
+ callback(bytes, 0, size);
1472
+ } else {
1473
+ // using copied memory
1474
+ txn.done(); // decrement and possibly abort
1475
+ callback(buffer, offset, size);
1476
+ }
1477
+ });
1478
+ if (address) {
1479
+ startRead(address, () => {
1480
+ resolveReads();
1481
+ });
1482
+ }
1483
+ },
1484
+ getAsync(id, options, callback) {
1485
+ let promise;
1486
+ if (!callback)
1487
+ promise = new Promise(resolve => callback = resolve);
1488
+ this.getBFAsync(id, options, (buffer, offset, size) => {
1489
+ if (this.useVersions) {
1490
+ // TODO: And get the version
1491
+ offset += 8;
1492
+ size -= 8;
1493
+ }
1494
+ let bytes = new Uint8Array(buffer, offset, size);
1495
+ let value;
1496
+ if (this.decoder) {
1497
+ // the decoder potentially uses the data from the buffer in the future and needs a stable buffer
1498
+ value = bytes && this.decoder.decode(bytes);
1499
+ } else if (this.encoding == 'binary') {
1500
+ value = bytes;
1501
+ } else {
1502
+ value = Buffer.prototype.utf8Slice.call(bytes, 0, size);
1503
+ if (this.encoding == 'json' && value)
1504
+ value = JSON.parse(value);
1505
+ }
1506
+ callback(value);
1507
+ });
1508
+ return promise;
1509
+ },
1510
+ retain(data, options) {
1511
+ if (!data)
1512
+ return
1513
+ let source = data[SOURCE_SYMBOL];
1514
+ let buffer = source ? source.bytes : data;
1515
+ if (!buffer.isGlobal && !env.writeTxn) {
1516
+ let txn = options?.transaction || (readTxnRenewed ? readTxn : renewReadTxn(this));
1517
+ buffer.txn = txn;
1518
+ txn.refCount = (txn.refCount || 0) + 1;
1519
+ return data;
1520
+ } else {
1521
+ buffer = Uint8ArraySlice.call(buffer, 0, this.lastSize);
1522
+ if (source) {
1523
+ source.bytes = buffer;
1524
+ return data;
1525
+ } else
1526
+ return buffer;
1527
+ }
1528
+ },
1529
+ _returnLargeBuffer(getFast) {
1530
+ let bytes;
1531
+ let compression = this.compression;
1532
+ if (asSafeBuffer && this.lastSize > NEW_BUFFER_THRESHOLD) {
1533
+ // used by getBinary to indicate it should create a dedicated buffer to receive this
1534
+ let bytesToRestore;
1535
+ try {
1536
+ if (compression) {
1537
+ bytesToRestore = compression.getValueBytes;
1538
+ let dictionary = compression.dictionary || [];
1539
+ let dictLength = (dictionary.length >> 3) << 3;// make sure it is word-aligned
1540
+ bytes = makeReusableBuffer(this.lastSize);
1541
+ compression.setBuffer(bytes.buffer, bytes.byteOffset, this.lastSize, dictionary, dictLength);
1542
+ compression.getValueBytes = bytes;
1543
+ } else {
1544
+ bytesToRestore = getValueBytes;
1545
+ setGlobalBuffer(bytes = getValueBytes = makeReusableBuffer(this.lastSize));
1546
+ }
1547
+ getFast();
1548
+ } finally {
1549
+ if (compression) {
1550
+ let dictLength = (compression.dictionary.length >> 3) << 3;
1551
+ compression.setBuffer(bytesToRestore.buffer, bytesToRestore.byteOffset, bytesToRestore.maxLength, compression.dictionary, dictLength);
1552
+ compression.getValueBytes = bytesToRestore;
1553
+ } else {
1554
+ setGlobalBuffer(bytesToRestore);
1555
+ getValueBytes = bytesToRestore;
1556
+ }
1557
+ }
1558
+ return bytes;
1559
+ }
1560
+ // grow our shared/static buffer to accomodate the size of the data
1561
+ bytes = this._allocateGetBuffer(this.lastSize);
1562
+ // and try again
1563
+ getFast();
1564
+ bytes.length = this.lastSize;
1565
+ return bytes;
1566
+ },
1567
+ _allocateGetBuffer(lastSize) {
1568
+ let newLength = Math.min(Math.max(lastSize * 2, 0x1000), 0xfffffff8);
1569
+ let bytes;
1570
+ if (this.compression) {
1571
+ let dictionary = this.compression.dictionary || Buffer.allocUnsafeSlow(0);
1572
+ let dictLength = (dictionary.length >> 3) << 3;// make sure it is word-aligned
1573
+ bytes = Buffer.allocUnsafeSlow(newLength + dictLength);
1574
+ bytes.set(dictionary); // copy dictionary into start
1575
+ // the section after the dictionary is the target area for get values
1576
+ bytes = bytes.subarray(dictLength);
1577
+ this.compression.setBuffer(bytes.buffer, bytes.byteOffset, newLength, dictionary, dictLength);
1578
+ bytes.maxLength = newLength;
1579
+ Object.defineProperty(bytes, 'length', { value: newLength, writable: true, configurable: true });
1580
+ this.compression.getValueBytes = bytes;
1581
+ } else {
1582
+ bytes = makeReusableBuffer(newLength);
1583
+ setGlobalBuffer(getValueBytes = bytes);
1584
+ }
1585
+ bytes.isGlobal = true;
1586
+ return bytes;
1587
+ },
1588
+ getBinary(id, options) {
1589
+ try {
1590
+ asSafeBuffer = true;
1591
+ let fastBuffer = this.getBinaryFast(id, options);
1592
+ return fastBuffer && (fastBuffer.isGlobal ? Uint8ArraySlice.call(fastBuffer, 0, this.lastSize) : fastBuffer);
1593
+ } finally {
1594
+ asSafeBuffer = false;
1595
+ }
1596
+ },
1597
+ getSharedBinary(id, options) {
1598
+ let fastBuffer = this.getBinaryFast(id, options);
1599
+ if (fastBuffer) {
1600
+ if (fastBuffer.isGlobal || writeTxn)
1601
+ return Uint8ArraySlice.call(fastBuffer, 0, this.lastSize)
1602
+ fastBuffer.txn = (options && options.transaction);
1603
+ options.transaction.refCount = (options.transaction.refCount || 0) + 1;
1604
+ return fastBuffer;
1605
+ }
1606
+ },
1607
+ get(id, options) {
1608
+ if (this.decoderCopies) {
1609
+ // the decoder copies any data, so we can use the fast binary retrieval that overwrites the same buffer space
1610
+ let bytes = this.getBinaryFast(id, options);
1611
+ return bytes && (bytes == UNMODIFIED ? UNMODIFIED : this.decoder.decode(bytes, options));
1612
+ }
1613
+ if (this.encoding == 'binary')
1614
+ return this.getBinary(id, options);
1615
+ if (this.decoder) {
1616
+ // the decoder potentially uses the data from the buffer in the future and needs a stable buffer
1617
+ let bytes = this.getBinary(id, options);
1618
+ return bytes && (bytes == UNMODIFIED ? UNMODIFIED : this.decoder.decode(bytes));
1619
+ }
1620
+
1621
+ let result = this.getString(id, options);
1622
+ if (result) {
1623
+ if (this.encoding == 'json')
1624
+ return JSON.parse(result);
1625
+ }
1626
+ return result;
1627
+ },
1628
+ getEntry(id, options) {
1629
+ let value = this.get(id, options);
1630
+ if (value !== undefined) {
1631
+ if (this.useVersions)
1632
+ return {
1633
+ value,
1634
+ version: getLastVersion(),
1635
+ //size: this.lastSize
1636
+ };
1637
+ else
1638
+ return {
1639
+ value,
1640
+ //size: this.lastSize
1641
+ };
1642
+ }
1643
+ },
1644
+ resetReadTxn() {
1645
+ resetReadTxn();
1646
+ },
1647
+ _commitReadTxn() {
1648
+ if (readTxn) {
1649
+ readTxn.isCommitted = true;
1650
+ readTxn.commit();
1651
+ }
1652
+ lastReadTxnRef = null;
1653
+ readTxnRenewed = null;
1654
+ readTxn = null;
1655
+ },
1656
+ ensureReadTxn() {
1657
+ if (!env.writeTxn && !readTxnRenewed)
1658
+ renewReadTxn(this);
1659
+ },
1660
+ doesExist(key, versionOrValue) {
1661
+ if (versionOrValue == null) {
1662
+ // undefined means the entry exists, null is used specifically to check for the entry *not* existing
1663
+ return (this.getBinaryFast(key) === undefined) == (versionOrValue === null);
1664
+ }
1665
+ else if (this.useVersions) {
1666
+ return this.getBinaryFast(key) !== undefined && (versionOrValue === IF_EXISTS || getLastVersion() === versionOrValue);
1667
+ }
1668
+ else {
1669
+ if (versionOrValue && versionOrValue['\x10binary-data\x02'])
1670
+ versionOrValue = versionOrValue['\x10binary-data\x02'];
1671
+ else if (this.encoder)
1672
+ versionOrValue = this.encoder.encode(versionOrValue);
1673
+ if (typeof versionOrValue == 'string')
1674
+ versionOrValue = Buffer.from(versionOrValue);
1675
+ return this.getValuesCount(key, { start: versionOrValue, exactMatch: true}) > 0;
1676
+ }
1677
+ },
1678
+ getValues(key, options) {
1679
+ let defaultOptions = {
1680
+ key,
1681
+ valuesForKey: true
1682
+ };
1683
+ if (options && options.snapshot === false)
1684
+ throw new Error('Can not disable snapshots for getValues');
1685
+ return this.getRange(options ? Object.assign(defaultOptions, options) : defaultOptions);
1686
+ },
1687
+ getKeys(options) {
1688
+ if (!options)
1689
+ options = {};
1690
+ options.values = false;
1691
+ return this.getRange(options);
1692
+ },
1693
+ getCount(options) {
1694
+ if (!options)
1695
+ options = {};
1696
+ options.onlyCount = true;
1697
+ return this.getRange(options).iterate();
1698
+ },
1699
+ getKeysCount(options) {
1700
+ if (!options)
1701
+ options = {};
1702
+ options.onlyCount = true;
1703
+ options.values = false;
1704
+ return this.getRange(options).iterate();
1705
+ },
1706
+ getValuesCount(key, options) {
1707
+ if (!options)
1708
+ options = {};
1709
+ options.key = key;
1710
+ options.valuesForKey = true;
1711
+ options.onlyCount = true;
1712
+ return this.getRange(options).iterate();
1713
+ },
1714
+ getRange(options) {
1715
+ let iterable = new RangeIterable();
1716
+ if (!options)
1717
+ options = {};
1718
+ let includeValues = options.values !== false;
1719
+ let includeVersions = options.versions;
1720
+ let valuesForKey = options.valuesForKey;
1721
+ let limit = options.limit;
1722
+ let db = this.db;
1723
+ let snapshot = options.snapshot;
1724
+ let compression = this.compression;
1725
+ iterable.iterate = () => {
1726
+ let currentKey = valuesForKey ? options.key : options.start;
1727
+ const reverse = options.reverse;
1728
+ let count = 0;
1729
+ let cursor, cursorRenewId, cursorAddress;
1730
+ let txn;
1731
+ let flags = (includeValues ? 0x100 : 0) | (reverse ? 0x400 : 0) |
1732
+ (valuesForKey ? 0x800 : 0) | (options.exactMatch ? 0x4000 : 0) |
1733
+ (options.inclusiveEnd ? 0x8000 : 0) |
1734
+ (options.exclusiveStart ? 0x10000 : 0);
1735
+ let store = this;
1736
+ function resetCursor() {
1737
+ try {
1738
+ if (cursor)
1739
+ finishCursor();
1740
+ let txnAddress;
1741
+ txn = options.transaction;
1742
+ if (txn) {
1743
+ if (txn.isDone) throw new Error('Can not iterate on range with transaction that is already' +
1744
+ ' done');
1745
+ txnAddress = txn.address;
1746
+ cursor = null;
1747
+ } else {
1748
+ let writeTxn = env.writeTxn;
1749
+ if (writeTxn)
1750
+ snapshot = false;
1751
+ txn = env.writeTxn || options.transaction || (readTxnRenewed ? readTxn : renewReadTxn(store));
1752
+ cursor = !writeTxn && db.availableCursor;
1753
+ }
1754
+ if (cursor) {
1755
+ db.availableCursor = null;
1756
+ flags |= 0x2000;
1757
+ } else {
1758
+ cursor = new Cursor(db, txnAddress || 0);
1759
+ }
1760
+ cursorAddress = cursor.address;
1761
+ txn.refCount = (txn.refCount || 0) + 1; // track transaction so we always use the same one
1762
+ if (snapshot === false) {
1763
+ cursorRenewId = renewId; // use shared read transaction
1764
+ txn.renewingRefCount = (txn.renewingRefCount || 0) + 1; // need to know how many are renewing cursors
1765
+ }
1766
+ } catch(error) {
1767
+ if (cursor) {
1768
+ try {
1769
+ cursor.close();
1770
+ } catch(error) { }
1771
+ }
1772
+ throw error;
1773
+ }
1774
+ }
1775
+ resetCursor();
1776
+ if (options.onlyCount) {
1777
+ flags |= 0x1000;
1778
+ let count = position$1(options.offset);
1779
+ if (count < 0)
1780
+ lmdbError(count);
1781
+ finishCursor();
1782
+ return count;
1783
+ }
1784
+ function position$1(offset) {
1785
+ if (!env.address) {
1786
+ throw new Error('Can not iterate on a closed database');
1787
+ }
1788
+ let keySize = currentKey === undefined ? 0 : store.writeKey(currentKey, keyBytes, 0);
1789
+ let endAddress;
1790
+ if (valuesForKey) {
1791
+ if (options.start === undefined && options.end === undefined)
1792
+ endAddress = 0;
1793
+ else {
1794
+ let startAddress;
1795
+ if (store.encoder.writeKey) {
1796
+ startAddress = saveKey(options.start, store.encoder.writeKey, iterable, maxKeySize);
1797
+ keyBytesView.setFloat64(START_ADDRESS_POSITION, startAddress, true);
1798
+ endAddress = saveKey(options.end, store.encoder.writeKey, iterable, maxKeySize);
1799
+ } else if ((!options.start || options.start instanceof Uint8Array) && (!options.end || options.end instanceof Uint8Array)) {
1800
+ startAddress = saveKey(options.start, orderedBinary.writeKey, iterable, maxKeySize);
1801
+ keyBytesView.setFloat64(START_ADDRESS_POSITION, startAddress, true);
1802
+ endAddress = saveKey(options.end, orderedBinary.writeKey, iterable, maxKeySize);
1803
+ } else {
1804
+ throw new Error('Only key-based encoding is supported for start/end values');
1805
+ }
1806
+ }
1807
+ } else
1808
+ endAddress = saveKey(options.end, store.writeKey, iterable, maxKeySize);
1809
+ return position(cursorAddress, flags, offset || 0, keySize, endAddress);
1810
+ }
1811
+
1812
+ function finishCursor() {
1813
+ if (txn.isDone)
1814
+ return;
1815
+ if (iterable.onDone)
1816
+ iterable.onDone();
1817
+ if (cursorRenewId)
1818
+ txn.renewingRefCount--;
1819
+ if (--txn.refCount <= 0 && txn.notCurrent) {
1820
+ cursor.close();
1821
+ txn.abort(); // this is no longer main read txn, abort it now that we are done
1822
+ txn.isDone = true;
1823
+ } else {
1824
+ if (db.availableCursor || txn != readTxn) {
1825
+ cursor.close();
1826
+ } else { // try to reuse it
1827
+ db.availableCursor = cursor;
1828
+ db.cursorTxn = txn;
1829
+ }
1830
+ }
1831
+ }
1832
+ return {
1833
+ next() {
1834
+ let keySize, lastSize;
1835
+ if (cursorRenewId && (cursorRenewId != renewId || txn.isDone)) {
1836
+ resetCursor();
1837
+ keySize = position$1(0);
1838
+ }
1839
+ if (count === 0) { // && includeValues) // on first entry, get current value if we need to
1840
+ keySize = position$1(options.offset);
1841
+ } else
1842
+ keySize = iterate(cursorAddress);
1843
+ if (keySize <= 0 ||
1844
+ (count++ >= limit)) {
1845
+ if (count < 0)
1846
+ lmdbError(count);
1847
+ finishCursor();
1848
+ return ITERATOR_DONE;
1849
+ }
1850
+ if (!valuesForKey || snapshot === false) {
1851
+ if (keySize > 20000) {
1852
+ if (keySize > 0x1000000)
1853
+ lmdbError(keySize - 0x100000000);
1854
+ throw new Error('Invalid key size ' + keySize.toString(16))
1855
+ }
1856
+ currentKey = store.readKey(keyBytes, 32, keySize + 32);
1857
+ }
1858
+ if (includeValues) {
1859
+ let value;
1860
+ lastSize = keyBytesView.getUint32(0, true);
1861
+ let bufferId = keyBytesView.getUint32(4, true);
1862
+ let bytes;
1863
+ if (bufferId) {
1864
+ bytes = getMMapBuffer(bufferId, lastSize);
1865
+ } else {
1866
+ bytes = compression ? compression.getValueBytes : getValueBytes;
1867
+ if (lastSize > bytes.maxLength) {
1868
+ store.lastSize = lastSize;
1869
+ asSafeBuffer = store.encoding == 'binary';
1870
+ try {
1871
+ bytes = store._returnLargeBuffer(() => getCurrentValue(cursorAddress));
1872
+ } finally {
1873
+ asSafeBuffer = false;
1874
+ }
1875
+ } else
1876
+ bytes.length = lastSize;
1877
+ }
1878
+ if (store.decoder) {
1879
+ value = store.decoder.decode(bytes, lastSize);
1880
+ } else if (store.encoding == 'binary')
1881
+ value = bytes.isGlobal ? Uint8ArraySlice.call(bytes, 0, lastSize) : bytes;
1882
+ else {
1883
+ value = bytes.toString('utf8', 0, lastSize);
1884
+ if (store.encoding == 'json' && value)
1885
+ value = JSON.parse(value);
1886
+ }
1887
+ if (includeVersions)
1888
+ return {
1889
+ value: {
1890
+ key: currentKey,
1891
+ value,
1892
+ version: getLastVersion()
1893
+ }
1894
+ };
1895
+ else if (valuesForKey)
1896
+ return {
1897
+ value
1898
+ };
1899
+ else
1900
+ return {
1901
+ value: {
1902
+ key: currentKey,
1903
+ value,
1904
+ }
1905
+ };
1906
+ } else if (includeVersions) {
1907
+ return {
1908
+ value: {
1909
+ key: currentKey,
1910
+ version: getLastVersion()
1911
+ }
1912
+ };
1913
+ } else {
1914
+ return {
1915
+ value: currentKey
1916
+ };
1917
+ }
1918
+ },
1919
+ return() {
1920
+ finishCursor();
1921
+ return ITERATOR_DONE;
1922
+ },
1923
+ throw() {
1924
+ finishCursor();
1925
+ return ITERATOR_DONE;
1926
+ }
1927
+ };
1928
+ };
1929
+ return iterable;
1930
+ },
1931
+
1932
+ getMany(keys, callback) {
1933
+ // this is an asynchronous get for multiple keys. It actually works by prefetching asynchronously,
1934
+ // allowing a separate to absorb the potentially largest cost: hard page faults (and disk I/O).
1935
+ // And then we just do standard sync gets (to deserialized data) to fulfil the callback/promise
1936
+ // once the prefetch occurs
1937
+ let promise = callback ? undefined : new Promise(resolve => callback = (error, results) => resolve(results));
1938
+ this.prefetch(keys, () => {
1939
+ let results = new Array(keys.length);
1940
+ for (let i = 0, l = keys.length; i < l; i++) {
1941
+ results[i] = get.call(this, keys[i]);
1942
+ }
1943
+ callback(null, results);
1944
+ });
1945
+ return promise;
1946
+ },
1947
+ getSharedBufferForGet(id, options) {
1948
+ let txn = env.writeTxn || (options && options.transaction) || (readTxnRenewed ? readTxn : renewReadTxn(this));
1949
+ this.lastSize = this.keyIsCompatibility ? txn.getBinaryShared(id) : this.db.get(this.writeKey(id, keyBytes, 0));
1950
+ if (this.lastSize === -30798) { // not found code
1951
+ return; //undefined
1952
+ }
1953
+ return this.lastSize;
1954
+ },
1955
+ prefetch(keys, callback) {
1956
+ if (!keys)
1957
+ throw new Error('An array of keys must be provided');
1958
+ if (!keys.length) {
1959
+ if (callback) {
1960
+ callback(null);
1961
+ return;
1962
+ } else
1963
+ return Promise.resolve();
1964
+ }
1965
+ let buffers = [];
1966
+ let startPosition;
1967
+ let bufferHolder = {};
1968
+ let lastBuffer;
1969
+ for (let key of keys) {
1970
+ let position;
1971
+ if (key && key.key !== undefined && key.value !== undefined) {
1972
+ position = saveKey(key.value, this.writeKey, bufferHolder, maxKeySize, 0x80000000);
1973
+ saveKey(key.key, this.writeKey, bufferHolder, maxKeySize);
1974
+ } else {
1975
+ position = saveKey(key, this.writeKey, bufferHolder, maxKeySize);
1976
+ }
1977
+ if (!startPosition)
1978
+ startPosition = position;
1979
+ if (bufferHolder.saveBuffer != lastBuffer) {
1980
+ buffers.push(bufferHolder);
1981
+ lastBuffer = bufferHolder.saveBuffer;
1982
+ bufferHolder = { saveBuffer: lastBuffer };
1983
+ }
1984
+ }
1985
+ saveKey(undefined, this.writeKey, bufferHolder, maxKeySize);
1986
+ outstandingReads++;
1987
+ prefetch(this.dbAddress, startPosition, (error) => {
1988
+ outstandingReads--;
1989
+ if (error)
1990
+ console.error('Error with prefetch', buffers, bufferHolder); // partly exists to keep the buffers pinned in memory
1991
+ else
1992
+ callback(null);
1993
+ });
1994
+ if (!callback)
1995
+ return new Promise(resolve => callback = resolve);
1996
+ },
1997
+ useReadTransaction() {
1998
+ let txn = readTxnRenewed ? readTxn : renewReadTxn(this);
1999
+ if (!txn.use) {
2000
+ throw new Error('Can not use read transaction from a closed database');
2001
+ }
2002
+ txn.use();
2003
+ return txn;
2004
+ },
2005
+ close(callback) {
2006
+ this.status = 'closing';
2007
+ let txnPromise;
2008
+ if (this.isRoot) {
2009
+ // if it is root, we need to abort and/or wait for transactions to finish
2010
+ if (readTxn) readTxn.abort();
2011
+ else readTxn = {};
2012
+ readTxn.isDone = true;
2013
+ Object.defineProperty(readTxn,'renew', {
2014
+ value: () => {
2015
+ throw new Error('Can not read from a closed database');
2016
+ }, configurable: true
2017
+ });
2018
+ Object.defineProperty(readTxn,'use', {
2019
+ value: () => {
2020
+ throw new Error('Can not read from a closed database');
2021
+ }, configurable: true
2022
+ });
2023
+ readTxnRenewed = null;
2024
+ txnPromise = this._endWrites && this._endWrites();
2025
+ }
2026
+ const doClose = () => {
2027
+ if (this.isRoot) {
2028
+ if (outstandingReads > 0) {
2029
+ return new Promise(resolve => setTimeout(() => resolve(doClose()), 1));
2030
+ }
2031
+ env.address = 0;
2032
+ env.close();
2033
+ } else
2034
+ this.db.close();
2035
+ this.status = 'closed';
2036
+ if (callback)
2037
+ callback();
2038
+ };
2039
+ if (txnPromise)
2040
+ return txnPromise.then(doClose);
2041
+ else {
2042
+ doClose();
2043
+ return Promise.resolve();
2044
+ }
2045
+ },
2046
+ getStats() {
2047
+ env.writeTxn || (readTxnRenewed ? readTxn : renewReadTxn(this));
2048
+ let dbStats = this.db.stat();
2049
+ dbStats.root = env.stat();
2050
+ Object.assign(dbStats, env.info());
2051
+ dbStats.free = env.freeStat();
2052
+ return dbStats;
2053
+ },
2054
+ });
2055
+ let get = LMDBStore.prototype.get;
2056
+ let lastReadTxnRef;
2057
+ function getMMapBuffer(bufferId, size) {
2058
+ let buffer = mmaps[bufferId];
2059
+ if (!buffer) {
2060
+ buffer = mmaps[bufferId] = getSharedBuffer(bufferId, env.address);
2061
+ }
2062
+ let offset = keyBytesView.getUint32(8, true);
2063
+ return new Uint8Array(buffer, offset, size);
2064
+ }
2065
+ function renewReadTxn(store) {
2066
+ if (!env.address) {
2067
+ throw new Error('Can not renew a transaction from a closed database');
2068
+ }
2069
+ if (!readTxn) {
2070
+ let retries = 0;
2071
+ let waitArray;
2072
+ do {
2073
+ try {
2074
+ let lastReadTxn = lastReadTxnRef && lastReadTxnRef.deref();
2075
+ readTxn = new Txn(env, 0x20000, lastReadTxn && !lastReadTxn.isDone && lastReadTxn);
2076
+ if (readTxn.address == 0) {
2077
+ readTxn = lastReadTxn;
2078
+ if (readTxn.notCurrent)
2079
+ readTxn.notCurrent = false;
2080
+ }
2081
+ break;
2082
+ } catch (error) {
2083
+ if (error.message.includes('temporarily')) {
2084
+ if (!waitArray)
2085
+ waitArray = new Int32Array(new SharedArrayBuffer(4), 0, 1);
2086
+ Atomics.wait(waitArray, 0, 0, retries * 2);
2087
+ } else
2088
+ throw error;
2089
+ }
2090
+ } while (retries++ < 100);
2091
+ }
2092
+ // we actually don't renew here, we let the renew take place in the next
2093
+ // lmdb native read/call so as to avoid an extra native call
2094
+ readTxnRenewed = setTimeout(resetReadTxn, 0);
2095
+ store.emit('begin-transaction');
2096
+ return readTxn;
2097
+ }
2098
+ function resetReadTxn() {
2099
+ renewId++;
2100
+ if (readTxnRenewed) {
2101
+ readTxnRenewed = null;
2102
+ if (readTxn.refCount - (readTxn.renewingRefCount || 0) > 0) {
2103
+ readTxn.notCurrent = true;
2104
+ lastReadTxnRef = new WeakRef(readTxn);
2105
+ readTxn = null;
2106
+ } else if (readTxn.address && !readTxn.isDone) {
2107
+ resetTxn(readTxn.address);
2108
+ } else {
2109
+ console.warn('Attempt to reset an invalid read txn', readTxn);
2110
+ throw new Error('Attempt to reset an invalid read txn');
2111
+ }
2112
+ }
2113
+ }
2114
+ }
2115
+ function makeReusableBuffer(size) {
2116
+ let bytes = typeof Buffer != 'undefined' ? Buffer.alloc(size) : new Uint8Array(size);
2117
+ bytes.maxLength = size;
2118
+ Object.defineProperty(bytes, 'length', { value: size, writable: true, configurable: true });
2119
+ return bytes;
2120
+ }
2121
+
2122
+ Txn.prototype.done = function() {
2123
+ this.refCount--;
2124
+ if (this.refCount === 0 && this.notCurrent) {
2125
+ this.abort();
2126
+ this.isDone = true;
2127
+ }
2128
+ };
2129
+ Txn.prototype.use = function() {
2130
+ this.refCount = (this.refCount || 0) + 1;
2131
+ };
2132
+
2133
+
2134
+ let readInstructions, readCallbacks = new Map(), uint32Instructions, instructionsDataView = { setFloat64() {}, setUint32() {} }, instructionsAddress;
2135
+ let savePosition = 8000;
2136
+ let DYNAMIC_KEY_BUFFER_SIZE = 8192;
2137
+ function allocateInstructionsBuffer() {
2138
+ readInstructions = typeof Buffer != 'undefined' ? Buffer.alloc(DYNAMIC_KEY_BUFFER_SIZE) : new Uint8Array(DYNAMIC_KEY_BUFFER_SIZE);
2139
+ uint32Instructions = new Int32Array(readInstructions.buffer, 0, readInstructions.buffer.byteLength >> 2);
2140
+ uint32Instructions[2] = 0xf0000000; // indicates a new read task must be started
2141
+ instructionsAddress = readInstructions.buffer.address = getAddress(readInstructions.buffer);
2142
+ readInstructions.dataView = instructionsDataView = new DataView(readInstructions.buffer, readInstructions.byteOffset, readInstructions.byteLength);
2143
+ savePosition = 0;
2144
+ }
2145
+ function recordReadInstruction(txnAddress, dbi, key, writeKey, maxKeySize, callback) {
2146
+ if (savePosition > 7800) {
2147
+ allocateInstructionsBuffer();
2148
+ }
2149
+ let start = savePosition;
2150
+ let keyPosition = savePosition + 16;
2151
+ try {
2152
+ savePosition = key === undefined ? keyPosition :
2153
+ writeKey(key, readInstructions, keyPosition);
2154
+ } catch (error) {
2155
+ if (error.name == 'RangeError') {
2156
+ if (8180 - start < maxKeySize) {
2157
+ allocateInstructionsBuffer(); // try again:
2158
+ return recordReadInstruction(txnAddress, dbi, key, writeKey, maxKeySize, callback);
2159
+ }
2160
+ throw new Error('Key was too large, max key size is ' + maxKeySize);
2161
+ } else
2162
+ throw error;
2163
+ }
2164
+ let length = savePosition - keyPosition;
2165
+ if (length > maxKeySize) {
2166
+ savePosition = start;
2167
+ throw new Error('Key of size ' + length + ' was too large, max key size is ' + maxKeySize);
2168
+ }
2169
+ uint32Instructions[(start >> 2) + 3] = length; // save the length
2170
+ uint32Instructions[(start >> 2) + 2] = dbi;
2171
+ savePosition = (savePosition + 12) & 0xfffffc;
2172
+ instructionsDataView.setFloat64(start, txnAddress, true);
2173
+ let callbackId = addReadCallback(() => {
2174
+ let position = start >> 2;
2175
+ let rc = thisInstructions[position];
2176
+ callback(rc, thisInstructions[position + 1], thisInstructions[position + 2], thisInstructions[position + 3]);
2177
+ });
2178
+ let thisInstructions = uint32Instructions;
2179
+ //if (start === 0)
2180
+ return startRead(instructionsAddress + start, callbackId, {}, 'read');
2181
+ //else
2182
+ //nextRead(start);
2183
+ }
2184
+ let nextCallbackId = 0;
2185
+ let addReadCallback = globalThis.__lmdb_read_callback;
2186
+ if (!addReadCallback) {
2187
+ addReadCallback = globalThis.__lmdb_read_callback = function(callback) {
2188
+ let callbackId = nextCallbackId++;
2189
+ readCallbacks.set(callbackId, callback);
2190
+ return callbackId;
2191
+ };
2192
+ setReadCallback(function(callbackId) {
2193
+ readCallbacks.get(callbackId)();
2194
+ readCallbacks.delete(callbackId);
2195
+ });
2196
+ }
2197
+
2198
+ let getLastVersion$1, getLastTxnId$1;
2199
+ const mapGet = Map.prototype.get;
2200
+ const CachingStore = (Store, env) => {
2201
+ let childTxnChanges;
2202
+ return class LMDBStore extends Store {
2203
+ constructor(dbName, options) {
2204
+ super(dbName, options);
2205
+ if (!env.cacheCommitter) {
2206
+ env.cacheCommitter = true;
2207
+ this.on('aftercommit', ({ next, last, txnId }) => {
2208
+ do {
2209
+ let meta = next.meta;
2210
+ let store = meta && meta.store;
2211
+ if (store) {
2212
+ if (next.flag & FAILED_CONDITION)
2213
+ store.cache.delete(meta.key); // just delete it from the map
2214
+ else {
2215
+ let expirationPriority = meta.valueSize >> 10;
2216
+ let cache = store.cache;
2217
+ let entry = mapGet.call(cache, meta.key);
2218
+ if (entry) {
2219
+ entry.txnId = txnId;
2220
+ cache.used(entry, expirationPriority + 4); // this will enter it into the LRFU (with a little lower priority than a read)
2221
+ }
2222
+ }
2223
+ }
2224
+ } while (next != last && (next = next.next))
2225
+ });
2226
+ }
2227
+ this.db.cachingDb = this;
2228
+ if (options.cache.clearKeptInterval)
2229
+ options.cache.clearKeptObjects = exports.clearKeptObjects;
2230
+ this.cache = new WeakLRUCache(options.cache);
2231
+ if (options.cache.validated)
2232
+ this.cache.validated = true;
2233
+ }
2234
+ get isCaching() {
2235
+ return true
2236
+ }
2237
+ get(id, options) {
2238
+ let value;
2239
+ if (this.cache.validated) {
2240
+ let entry = this.cache.get(id);
2241
+ if (entry) {
2242
+ let cachedValue = entry.value;
2243
+ if (entry.txnId != null) {
2244
+ value = super.get(id, { ifNotTxnId: entry.txnId, transaction: options && options.transaction });
2245
+ if (value === UNMODIFIED)
2246
+ return cachedValue;
2247
+ } else // with no txn id we do not validate; this is the state of a cached value after a write before it transacts
2248
+ return cachedValue;
2249
+ } else
2250
+ value = super.get(id, options);
2251
+ } else if (options && options.transaction) {
2252
+ return super.get(id, options);
2253
+ } else {
2254
+ value = this.cache.getValue(id);
2255
+ if (value !== undefined) {
2256
+ return value;
2257
+ }
2258
+ value = super.get(id);
2259
+ }
2260
+ if (value && typeof value === 'object' && !options && typeof id !== 'object') {
2261
+ let entry = this.cache.setValue(id, value, this.lastSize >> 10);
2262
+ if (this.useVersions) {
2263
+ entry.version = getLastVersion$1();
2264
+ }
2265
+ if (this.cache.validated)
2266
+ entry.txnId = getLastTxnId$1();
2267
+ }
2268
+ return value;
2269
+ }
2270
+ getEntry(id, options) {
2271
+ let entry, value;
2272
+ if (this.cache.validated) {
2273
+ entry = this.cache.get(id);
2274
+ if (entry) {
2275
+ if (entry.txnId != null) {
2276
+ value = super.get(id, { ifNotTxnId: entry.txnId, transaction: options && options.transaction });
2277
+ if (value === UNMODIFIED)
2278
+ return entry;
2279
+ } else // with no txn id we do not validate; this is the state of a cached value after a write before it transacts
2280
+ return entry;
2281
+ } else
2282
+ value = super.get(id, options);
2283
+ } else if (options && options.transaction) {
2284
+ return super.getEntry(id, options);
2285
+ } else {
2286
+ entry = this.cache.get(id);
2287
+ if (entry !== undefined) {
2288
+ return entry;
2289
+ }
2290
+ value = super.get(id);
2291
+ }
2292
+ if (value === undefined)
2293
+ return;
2294
+ if (value && typeof value === 'object' && !options && typeof id !== 'object') {
2295
+ entry = this.cache.setValue(id, value, this.lastSize >> 10);
2296
+ } else
2297
+ entry = { value };
2298
+ if (this.useVersions)
2299
+ entry.version = getLastVersion$1();
2300
+ if (this.cache.validated)
2301
+ entry.txnId = getLastTxnId$1();
2302
+ return entry;
2303
+ }
2304
+ putEntry(id, entry, ifVersion) {
2305
+ let result = super.put(id, entry.value, entry.version, ifVersion);
2306
+ if (typeof id === 'object')
2307
+ return result;
2308
+ if (result && result.then)
2309
+ this.cache.setManually(id, entry); // set manually so we can keep it pinned in memory until it is committed
2310
+ else // sync operation, immediately add to cache
2311
+ this.cache.set(id, entry);
2312
+ }
2313
+ put(id, value, version, ifVersion) {
2314
+ let result = super.put(id, value, version, ifVersion);
2315
+ if (typeof id !== 'object') {
2316
+ if (value && value['\x10binary-data\x02']) {
2317
+ // don't cache binary data, since it will be decoded on get
2318
+ this.cache.delete(id);
2319
+ return result;
2320
+ }
2321
+ // sync operation, immediately add to cache, otherwise keep it pinned in memory until it is committed
2322
+ let entry = this.cache.setValue(id, value, !result || result.isSync ? 0 : -1);
2323
+ if (childTxnChanges)
2324
+ childTxnChanges.add(id);
2325
+ if (version !== undefined)
2326
+ entry.version = typeof version === 'object' ? version.version : version;
2327
+ }
2328
+ return result;
2329
+ }
2330
+ putSync(id, value, version, ifVersion) {
2331
+ if (id !== 'object') {
2332
+ // sync operation, immediately add to cache, otherwise keep it pinned in memory until it is committed
2333
+ if (value && typeof value === 'object') {
2334
+ let entry = this.cache.setValue(id, value);
2335
+ if (childTxnChanges)
2336
+ childTxnChanges.add(id);
2337
+ if (version !== undefined) {
2338
+ entry.version = typeof version === 'object' ? version.version : version;
2339
+ }
2340
+ } else // it is possible that a value used to exist here
2341
+ this.cache.delete(id);
2342
+ }
2343
+ return super.putSync(id, value, version, ifVersion);
2344
+ }
2345
+ remove(id, ifVersion) {
2346
+ this.cache.delete(id);
2347
+ return super.remove(id, ifVersion);
2348
+ }
2349
+ removeSync(id, ifVersion) {
2350
+ this.cache.delete(id);
2351
+ return super.removeSync(id, ifVersion);
2352
+ }
2353
+ clearAsync(callback) {
2354
+ this.cache.clear();
2355
+ return super.clearAsync(callback);
2356
+ }
2357
+ clearSync() {
2358
+ this.cache.clear();
2359
+ super.clearSync();
2360
+ }
2361
+ childTransaction(callback) {
2362
+ return super.childTransaction(() => {
2363
+ let cache = this.cache;
2364
+ let previousChanges = childTxnChanges;
2365
+ try {
2366
+ childTxnChanges = new Set();
2367
+ return when(callback(), (result) => {
2368
+ if (result === ABORT)
2369
+ return abort();
2370
+ childTxnChanges = previousChanges;
2371
+ return result;
2372
+ }, abort);
2373
+ } catch(error) {
2374
+ abort(error);
2375
+ }
2376
+ function abort(error) {
2377
+ // if the transaction was aborted, remove all affected entries from cache
2378
+ for (let id of childTxnChanges)
2379
+ cache.delete(id);
2380
+ childTxnChanges = previousChanges;
2381
+ if (error)
2382
+ throw error;
2383
+ else
2384
+ return ABORT;
2385
+ }
2386
+ });
2387
+ }
2388
+ doesExist(key, versionOrValue) {
2389
+ let entry = this.cache.get(key);
2390
+ if (entry) {
2391
+ if (versionOrValue == null) {
2392
+ return versionOrValue !== null;
2393
+ } else if (this.useVersions) {
2394
+ return versionOrValue === IF_EXISTS$1 || entry.version === versionOrValue;
2395
+ }
2396
+ }
2397
+ return super.doesExist(key, versionOrValue);
2398
+ }
2399
+ };
2400
+ };
2401
+ function setGetLastVersion(get, getTxnId) {
2402
+ getLastVersion$1 = get;
2403
+ getLastTxnId$1 = getTxnId;
2404
+ }
2405
+
2406
+ let moduleRequire = typeof require == 'function' && require;
2407
+ function setRequire(require) {
2408
+ moduleRequire = require;
2409
+ }
2410
+
2411
+ setGetLastVersion(getLastVersion, getLastTxnId);
2412
+ let keyBytes, keyBytesView;
2413
+ const { onExit, getEnvsPointer, setEnvsPointer, getEnvFlags, setJSFlags } = nativeAddon;
2414
+ if (globalThis.__lmdb_envs__)
2415
+ setEnvsPointer(globalThis.__lmdb_envs__);
2416
+ else
2417
+ globalThis.__lmdb_envs__ = getEnvsPointer();
2418
+
2419
+ // this is hard coded as an upper limit because it is important assumption of the fixed buffers in writing instructions
2420
+ // this corresponds to the max key size for 8KB pages
2421
+ const MAX_KEY_SIZE = 4026;
2422
+ // this is used as the key size by default because default page size is OS page size, which is usually
2423
+ // 4KB (but is 16KB on M-series MacOS), and this keeps a consistent max key size when no page size specified.
2424
+ const DEFAULT_MAX_KEY_SIZE = 1978;
2425
+ const DEFAULT_COMMIT_DELAY = 0;
2426
+
2427
+ const allDbs = new Map();
2428
+ let defaultCompression;
2429
+ let hasRegisteredOnExit;
2430
+ function open(path$1, options) {
2431
+ if (nativeAddon.open) {
2432
+ if (nativeAddon.open !== open) {
2433
+ // this is the case when lmdb-js has been opened in both ESM and CJS mode, which means that there are two
2434
+ // separate JS modules, but they are both using the same native module.
2435
+ getLastVersion = nativeAddon.getLastVersion;
2436
+ getLastTxnId = nativeAddon.getLastTxnId;
2437
+ setGetLastVersion(getLastVersion, getLastTxnId);
2438
+ return nativeAddon.open(path$1, options);
2439
+ }
2440
+ } else {
2441
+ nativeAddon.open = open;
2442
+ nativeAddon.getLastVersion = getLastVersion;
2443
+ nativeAddon.getLastTxnId = getLastTxnId;
2444
+ }
2445
+ if (!keyBytes) // TODO: Consolidate get buffer and key buffer (don't think we need both)
2446
+ allocateFixedBuffer();
2447
+ if (typeof path$1 == 'object' && !options) {
2448
+ options = path$1;
2449
+ path$1 = options.path;
2450
+ }
2451
+ options = options || {};
2452
+ let noFSAccess = options.noFSAccess; // this can only be configured on open, can't let users change it
2453
+ let userOptions = options;
2454
+ if (path$1 == null) {
2455
+ options = Object.assign({
2456
+ deleteOnClose: true,
2457
+ noSync: true,
2458
+ }, options);
2459
+ path$1 = tmpdir() + '/' + Math.floor(Math.random() * 2821109907455).toString(36) + '.mdb';
2460
+ } else if (!options)
2461
+ options = {};
2462
+ let extension = path.extname(path$1);
2463
+ let name = path.basename(path$1, extension);
2464
+ let is32Bit = arch().endsWith('32');
2465
+ let remapChunks = options.remapChunks || options.encryptionKey || (options.mapSize ?
2466
+ (is32Bit && options.mapSize > 0x100000000) : // larger than fits in address space, must use dynamic maps
2467
+ is32Bit); // without a known map size, we default to being able to handle large data correctly/well*/
2468
+ let userMapSize = options.mapSize;
2469
+ options = Object.assign({
2470
+ noSubdir: Boolean(extension),
2471
+ isRoot: true,
2472
+ maxDbs: 12,
2473
+ remapChunks,
2474
+ keyBytes,
2475
+ overlappingSync: (options.noSync || options.readOnly) ? false : (os != 'win32'),
2476
+ // default map size limit of 4 exabytes when using remapChunks, since it is not preallocated and we can
2477
+ // make it super huge.
2478
+ mapSize: remapChunks ? 0x10000000000000 :
2479
+ 0x20000, // Otherwise we start small with 128KB
2480
+ safeRestore: process.env.LMDB_RESTORE == 'safe',
2481
+ }, options);
2482
+ options.path = path$1;
2483
+ if (options.asyncTransactionOrder == 'strict') {
2484
+ options.strictAsyncOrder = true;
2485
+ }
2486
+ if (nativeAddon.version.major + nativeAddon.version.minor / 100 + nativeAddon.version.patch / 10000 < 0.0980) {
2487
+ options.overlappingSync = false; // not support on older versions
2488
+ options.trackMetrics = false;
2489
+ options.usePreviousSnapshot = false;
2490
+ options.safeRestore = false;
2491
+ options.remapChunks = false;
2492
+ if (!userMapSize) options.mapSize = 0x40000000; // 1 GB
2493
+ }
2494
+
2495
+ if (!exists(options.noSubdir ? path.dirname(path$1) : path$1))
2496
+ fs.mkdirSync(options.noSubdir ? path.dirname(path$1) : path$1, { recursive: true }
2497
+ );
2498
+ function makeCompression(compressionOptions) {
2499
+ if (compressionOptions instanceof Compression)
2500
+ return compressionOptions;
2501
+ let useDefault = typeof compressionOptions != 'object';
2502
+ if (useDefault && defaultCompression)
2503
+ return defaultCompression;
2504
+ compressionOptions = Object.assign({
2505
+ threshold: 1000,
2506
+ dictionary: fs.readFileSync(new URL('./dict/dict.txt', (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('index.cjs', document.baseURI).href)).replace(/dist[\\\/]index.cjs$/, ''))),
2507
+ getValueBytes: makeReusableBuffer(0),
2508
+ }, compressionOptions);
2509
+ let compression = Object.assign(new Compression(compressionOptions), compressionOptions);
2510
+ if (useDefault)
2511
+ defaultCompression = compression;
2512
+ return compression;
2513
+ }
2514
+
2515
+ if (options.compression)
2516
+ options.compression = makeCompression(options.compression);
2517
+ let flags =
2518
+ (options.overlappingSync ? 0x1000 : 0) |
2519
+ (options.noSubdir ? 0x4000 : 0) |
2520
+ (options.noSync ? 0x10000 : 0) |
2521
+ (options.readOnly ? 0x20000 : 0) |
2522
+ (options.noMetaSync ? 0x40000 : 0) |
2523
+ (options.useWritemap ? 0x80000 : 0) |
2524
+ (options.mapAsync ? 0x100000 : 0) |
2525
+ (options.noReadAhead ? 0x800000 : 0) |
2526
+ (options.noMemInit ? 0x1000000 : 0) |
2527
+ (options.usePreviousSnapshot ? 0x2000000 : 0) |
2528
+ (options.remapChunks ? 0x4000000 : 0) |
2529
+ (options.safeRestore ? 0x800 : 0) |
2530
+ (options.trackMetrics ? 0x400 : 0);
2531
+
2532
+ let env = new Env();
2533
+ let jsFlags = (options.overlappingSync ? 0x1000 : 0) |
2534
+ (options.separateFlushed ? 1 : 0) |
2535
+ (options.deleteOnClose ? 2 : 0);
2536
+ let rc = env.open(options, flags, jsFlags);
2537
+ env.path = path$1;
2538
+ if (rc)
2539
+ lmdbError(rc);
2540
+ delete options.keyBytes; // no longer needed, don't copy to stores
2541
+ let maxKeySize = env.getMaxKeySize();
2542
+ maxKeySize = Math.min(maxKeySize, options.pageSize ? MAX_KEY_SIZE : DEFAULT_MAX_KEY_SIZE);
2543
+ flags = getEnvFlags(env.address); // re-retrieve them, they are not necessarily the same if we are connecting to an existing env
2544
+ if (flags & 0x1000) {
2545
+ if (userOptions.noSync) {
2546
+ env.close();
2547
+ throw new Error('Can not set noSync on a database that was opened with overlappingSync');
2548
+ }
2549
+ } else if (options.overlappingSync) {
2550
+ if (userOptions.overlappingSync) {
2551
+ env.close();
2552
+ throw new Error('Can not enable overlappingSync on a database that was opened without this flag');
2553
+ }
2554
+ options.overlappingSync = false;
2555
+ jsFlags = jsFlags & 0xff; // clear overlapping sync
2556
+ setJSFlags(env.address, jsFlags);
2557
+ }
2558
+
2559
+ env.readerCheck(); // clear out any stale entries
2560
+ if ((options.overlappingSync || options.deleteOnClose) && !hasRegisteredOnExit && process.on) {
2561
+ hasRegisteredOnExit = true;
2562
+ process.on('exit', onExit);
2563
+ }
2564
+
2565
+ class LMDBStore extends EventEmitter {
2566
+ constructor(dbName, dbOptions) {
2567
+ super();
2568
+ if (dbName === undefined)
2569
+ throw new Error('Database name must be supplied in name property (may be null for root database)');
2570
+
2571
+ if (options.compression && dbOptions.compression !== false && typeof dbOptions.compression != 'object')
2572
+ dbOptions.compression = options.compression; // use the parent compression if available
2573
+ else if (dbOptions.compression)
2574
+ dbOptions.compression = makeCompression(dbOptions.compression);
2575
+
2576
+ if (dbOptions.dupSort && (dbOptions.useVersions || dbOptions.cache)) {
2577
+ throw new Error('The dupSort flag can not be combined with versions or caching');
2578
+ }
2579
+ let keyIsBuffer = dbOptions.keyIsBuffer;
2580
+ if (dbOptions.keyEncoding == 'uint32') {
2581
+ dbOptions.keyIsUint32 = true;
2582
+ } else if (dbOptions.keyEncoder) {
2583
+ if (dbOptions.keyEncoder.enableNullTermination) {
2584
+ dbOptions.keyEncoder.enableNullTermination();
2585
+ } else
2586
+ keyIsBuffer = true;
2587
+ } else if (dbOptions.keyEncoding == 'binary') {
2588
+ keyIsBuffer = true;
2589
+ }
2590
+ let flags = (dbOptions.reverseKey ? 0x02 : 0) |
2591
+ (dbOptions.dupSort ? 0x04 : 0) |
2592
+ (dbOptions.dupFixed ? 0x10 : 0) |
2593
+ (dbOptions.integerDup ? 0x20 : 0) |
2594
+ (dbOptions.reverseDup ? 0x40 : 0) |
2595
+ (!options.readOnly && dbOptions.create !== false ? 0x40000 : 0) |
2596
+ (dbOptions.useVersions ? 0x100 : 0);
2597
+ let keyType = (dbOptions.keyIsUint32 || dbOptions.keyEncoding == 'uint32') ? 2 : keyIsBuffer ? 3 : 0;
2598
+ if (keyType == 2)
2599
+ flags |= 0x08; // integer key
2600
+
2601
+ if (options.readOnly) {
2602
+ // in read-only mode we use a read-only txn to open the database
2603
+ // TODO: LMDB is actually not entirely thread-safe when it comes to opening databases with
2604
+ // read-only transactions since there is a race condition on setting the update dbis that
2605
+ // occurs outside the lock
2606
+ // make sure we are using a fresh read txn, so we don't want to share with a cursor txn
2607
+ this.resetReadTxn();
2608
+ this.ensureReadTxn();
2609
+ this.db = new Dbi(env, flags, dbName, keyType, dbOptions.compression);
2610
+ } else {
2611
+ this.transactionSync(() => {
2612
+ this.db = new Dbi(env, flags, dbName, keyType, dbOptions.compression);
2613
+ }, options.overlappingSync ? 0x10002 : 2); // no flush-sync, but synchronously commit
2614
+ }
2615
+ this._commitReadTxn(); // current read transaction becomes invalid after opening another db
2616
+ if (!this.db || this.db.dbi == 0xffffffff) {// not found
2617
+ throw new Error('Database not found')
2618
+ }
2619
+ this.dbAddress = this.db.address;
2620
+ this.db.name = dbName || null;
2621
+ this.name = dbName;
2622
+ this.status = 'open';
2623
+ this.env = env;
2624
+ this.reads = 0;
2625
+ this.writes = 0;
2626
+ this.transactions = 0;
2627
+ this.averageTransactionTime = 5;
2628
+ if (dbOptions.syncBatchThreshold)
2629
+ console.warn('syncBatchThreshold is no longer supported');
2630
+ if (dbOptions.immediateBatchThreshold)
2631
+ console.warn('immediateBatchThreshold is no longer supported');
2632
+ this.commitDelay = DEFAULT_COMMIT_DELAY;
2633
+ Object.assign(this, { // these are the options that are inherited
2634
+ path: options.path,
2635
+ encoding: options.encoding,
2636
+ strictAsyncOrder: options.strictAsyncOrder,
2637
+ }, dbOptions);
2638
+ let Encoder;
2639
+ if (this.encoder && this.encoder.Encoder) {
2640
+ Encoder = this.encoder.Encoder;
2641
+ this.encoder = null; // don't copy everything from the module
2642
+ }
2643
+ if (!Encoder && !(this.encoder && this.encoder.encode) && (!this.encoding || this.encoding == 'msgpack' || this.encoding == 'cbor')) {
2644
+ Encoder = (this.encoding == 'cbor' ? moduleRequire('cbor-x').Encoder : MsgpackrEncoder);
2645
+ }
2646
+ if (Encoder) {
2647
+ this.encoder = new Encoder(Object.assign(
2648
+ assignConstrainedProperties(['copyBuffers', 'getStructures', 'saveStructures', 'useFloat32', 'useRecords', 'structuredClone', 'variableMapSize', 'useTimestamp32', 'largeBigIntToFloat', 'encodeUndefinedAsNil', 'int64AsNumber', 'onInvalidDate', 'mapsAsObjects', 'useTag259ForMaps', 'pack', 'maxSharedStructures', 'shouldShareStructure', 'randomAccessStructure', 'freezeData'],
2649
+ this.sharedStructuresKey ? this.setupSharedStructures() : {
2650
+ copyBuffers: true, // need to copy any embedded buffers that are found since we use unsafe buffers
2651
+ }, options, dbOptions), this.encoder));
2652
+ }
2653
+ if (this.encoding == 'json') {
2654
+ this.encoder = {
2655
+ encode: JSON.stringify,
2656
+ };
2657
+ } else if (this.encoder) {
2658
+ this.decoder = this.encoder;
2659
+ this.decoderCopies = !this.encoder.needsStableBuffer;
2660
+ }
2661
+ this.maxKeySize = maxKeySize;
2662
+ applyKeyHandling(this);
2663
+ allDbs.set(dbName ? name + '-' + dbName : name, this);
2664
+ }
2665
+ openDB(dbName, dbOptions) {
2666
+ if (this.dupSort && this.name == null)
2667
+ throw new Error('Can not open named databases if the main database is dupSort')
2668
+ if (typeof dbName == 'object' && !dbOptions) {
2669
+ dbOptions = dbName;
2670
+ dbName = dbOptions.name;
2671
+ } else
2672
+ dbOptions = dbOptions || {};
2673
+ try {
2674
+ return dbOptions.cache ?
2675
+ new (CachingStore(LMDBStore, env))(dbName, dbOptions) :
2676
+ new LMDBStore(dbName, dbOptions);
2677
+ } catch(error) {
2678
+ if (error.message == 'Database not found')
2679
+ return; // return undefined to indicate db not found
2680
+ if (error.message.indexOf('MDB_DBS_FULL') > -1) {
2681
+ error.message += ' (increase your maxDbs option)';
2682
+ }
2683
+ throw error;
2684
+ }
2685
+ }
2686
+ open(dbOptions, callback) {
2687
+ let db = this.openDB(dbOptions);
2688
+ if (callback)
2689
+ callback(null, db);
2690
+ return db;
2691
+ }
2692
+ backup(path$1, compact) {
2693
+ if (noFSAccess)
2694
+ return;
2695
+ fs.mkdirSync(path.dirname(path$1), { recursive: true });
2696
+ return new Promise((resolve, reject) => env.copy(path$1, false, (error) => {
2697
+ if (error) {
2698
+ reject(error);
2699
+ } else {
2700
+ resolve();
2701
+ }
2702
+ }));
2703
+ }
2704
+ isOperational() {
2705
+ return this.status == 'open';
2706
+ }
2707
+ sync(callback) {
2708
+ return env.sync(callback || function(error) {
2709
+ if (error) {
2710
+ console.error(error);
2711
+ }
2712
+ });
2713
+ }
2714
+ deleteDB() {
2715
+ console.warn('deleteDB() is deprecated, use drop or dropSync instead');
2716
+ return this.dropSync();
2717
+ }
2718
+ dropSync() {
2719
+ this.transactionSync(() =>
2720
+ this.db.drop({
2721
+ justFreePages: false
2722
+ }), options.overlappingSync ? 0x10002 : 2);
2723
+ }
2724
+ clear(callback) {
2725
+ if (typeof callback == 'function')
2726
+ return this.clearAsync(callback);
2727
+ console.warn('clear() is deprecated, use clearAsync or clearSync instead');
2728
+ this.clearSync();
2729
+ }
2730
+ clearSync() {
2731
+ if (this.encoder) {
2732
+ if (this.encoder.clearSharedData)
2733
+ this.encoder.clearSharedData();
2734
+ else if (this.encoder.structures)
2735
+ this.encoder.structures = [];
2736
+ }
2737
+ this.transactionSync(() =>
2738
+ this.db.drop({
2739
+ justFreePages: true
2740
+ }), options.overlappingSync ? 0x10002 : 2);
2741
+ }
2742
+ readerCheck() {
2743
+ return env.readerCheck();
2744
+ }
2745
+ readerList() {
2746
+ return env.readerList().join('');
2747
+ }
2748
+ setupSharedStructures() {
2749
+ const getStructures = () => {
2750
+ let lastVersion; // because we are doing a read here, we may need to save and restore the lastVersion from the last read
2751
+ if (this.useVersions)
2752
+ lastVersion = getLastVersion();
2753
+ let buffer = this.getBinary(this.sharedStructuresKey);
2754
+ if (this.useVersions)
2755
+ setLastVersion(lastVersion);
2756
+ return buffer && this.decoder.decode(buffer);
2757
+ };
2758
+ return {
2759
+ saveStructures: (structures, isCompatible) => {
2760
+ return this.transactionSync(() => {
2761
+ let existingStructuresBuffer = this.getBinary(this.sharedStructuresKey);
2762
+ let existingStructures = existingStructuresBuffer && this.decoder.decode(existingStructuresBuffer);
2763
+ if (typeof isCompatible == 'function' ?
2764
+ !isCompatible(existingStructures) :
2765
+ (existingStructures && existingStructures.length != isCompatible))
2766
+ return false; // it changed, we need to indicate that we couldn't update
2767
+ this.put(this.sharedStructuresKey, structures);
2768
+ }, options.overlappingSync ? 0x10000 : 0);
2769
+ },
2770
+ getStructures,
2771
+ copyBuffers: true, // need to copy any embedded buffers that are found since we use unsafe buffers
2772
+ };
2773
+ }
2774
+ }
2775
+ // if caching class overrides putSync, don't want to double call the caching code
2776
+ LMDBStore.prototype.putSync;
2777
+ LMDBStore.prototype.removeSync;
2778
+ addReadMethods(LMDBStore, { env, maxKeySize, keyBytes, keyBytesView, getLastVersion });
2779
+ if (!options.readOnly)
2780
+ addWriteMethods(LMDBStore, { env, maxKeySize, fixedBuffer: keyBytes,
2781
+ resetReadTxn: LMDBStore.prototype.resetReadTxn, ...options });
2782
+ LMDBStore.prototype.supports = {
2783
+ permanence: true,
2784
+ bufferKeys: true,
2785
+ promises: true,
2786
+ snapshots: true,
2787
+ clear: true,
2788
+ status: true,
2789
+ deferredOpen: true,
2790
+ openCallback: true,
2791
+ };
2792
+ let Class = options.cache ? CachingStore(LMDBStore, env) : LMDBStore;
2793
+ return options.asClass ? Class : new Class(options.name || null, options);
2794
+ }
2795
+ function openAsClass(path, options) {
2796
+ if (typeof path == 'object' && !options) {
2797
+ options = path;
2798
+ path = options.path;
2799
+ }
2800
+ options = options || {};
2801
+ options.asClass = true;
2802
+ return open(path, options);
2803
+ }
2804
+
2805
+ function getLastVersion() {
2806
+ return keyBytesView.getFloat64(16, true);
2807
+ }
2808
+ function setLastVersion(version) {
2809
+ return keyBytesView.setFloat64(16, version, true);
2810
+ }
2811
+
2812
+ function getLastTxnId() {
2813
+ return keyBytesView.getUint32(32, true);
2814
+ }
2815
+
2816
+ const KEY_BUFFER_SIZE = 4096;
2817
+ function allocateFixedBuffer() {
2818
+ keyBytes = typeof Buffer != 'undefined' ? Buffer.allocUnsafeSlow(KEY_BUFFER_SIZE) : new Uint8Array(KEY_BUFFER_SIZE);
2819
+ const keyBuffer = keyBytes.buffer;
2820
+ keyBytesView = keyBytes.dataView || (keyBytes.dataView = new DataView(keyBytes.buffer, 0, KEY_BUFFER_SIZE)); // max key size is actually 4026
2821
+ keyBytes.uint32 = new Uint32Array(keyBuffer, 0, KEY_BUFFER_SIZE >> 2);
2822
+ keyBytes.float64 = new Float64Array(keyBuffer, 0, KEY_BUFFER_SIZE >> 3);
2823
+ keyBytes.uint32.address = keyBytes.address = keyBuffer.address = getAddress(keyBuffer);
2824
+ }
2825
+
2826
+ function exists(path) {
2827
+ if (fs.existsSync)
2828
+ return fs.existsSync(path);
2829
+ try {
2830
+ return fs.statSync(path);
2831
+ } catch (error) {
2832
+ return false
2833
+ }
2834
+ }
2835
+
2836
+ function assignConstrainedProperties(allowedProperties, target) {
2837
+ for (let i = 2; i < arguments.length; i++) {
2838
+ let source = arguments[i];
2839
+ for (let key in source) {
2840
+ if (allowedProperties.includes(key))
2841
+ target[key] = source[key];
2842
+ }
2843
+ }
2844
+ return target;
2845
+ }
2846
+
2847
+ function levelup(store) {
2848
+ return Object.assign(Object.create(store), {
2849
+ get(key, options, callback) {
2850
+ let result = store.get(key);
2851
+ if (typeof options == 'function')
2852
+ callback = options;
2853
+ if (callback) {
2854
+ if (result === undefined)
2855
+ callback(new NotFoundError());
2856
+ else
2857
+ callback(null, result);
2858
+ } else {
2859
+ if (result === undefined)
2860
+ return Promise.reject(new NotFoundError());
2861
+ else
2862
+ return Promise.resolve(result);
2863
+ }
2864
+ },
2865
+ });
2866
+ }
2867
+ class NotFoundError extends Error {
2868
+ constructor(message) {
2869
+ super(message);
2870
+ this.name = 'NotFoundError';
2871
+ this.notFound = true;
2872
+ }
2873
+ }
2874
+
2875
+ orderedBinary__namespace.enableNullTermination();
2876
+ setExternals({
2877
+ arch: os$1.arch, fs: fs__default["default"], tmpdir: os$1.tmpdir, MsgpackrEncoder: msgpackr.Encoder, WeakLRUCache: weakLruCache.WeakLRUCache, orderedBinary: orderedBinary__namespace,
2878
+ EventEmitter: events.EventEmitter, os: os$1.platform(), onExit(callback) {
2879
+ if (process.getMaxListeners() < process.listenerCount('exit') + 8)
2880
+ process.setMaxListeners(process.listenerCount('exit') + 8);
2881
+ process.on('exit', callback);
2882
+ },
2883
+ });
2884
+ let { noop } = nativeAddon;
2885
+ const TransactionFlags = {
2886
+ ABORTABLE: 1,
2887
+ SYNCHRONOUS_COMMIT: 2,
2888
+ NO_SYNC_FLUSH: 0x10000,
2889
+ };
2890
+ var index = {
2891
+ open, openAsClass, getLastVersion, compareKey: orderedBinary$1.compareKeys, keyValueToBuffer: orderedBinary$1.toBufferKey, bufferToKeyValue: orderedBinary$1.fromBufferKey, ABORT, IF_EXISTS: IF_EXISTS$1, asBinary, levelup, TransactionFlags
2892
+ };
2893
+
2894
+ let require$1 = module$1.createRequire((typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('index.cjs', document.baseURI).href)));
2895
+ setRequire(require$1);
2896
+ exports.v8AccelerationEnabled = false;
2897
+
2898
+ let versions = process.versions;
2899
+ if (!versions.deno && !process.isBun) {
2900
+ let [ majorVersion, minorVersion ] = versions.node.split('.');
2901
+ if (versions.v8 && +majorVersion == nativeAddon.version.nodeCompiledVersion) {
2902
+ let v8Funcs = {};
2903
+ let fastApiCalls = (majorVersion == 17 || majorVersion == 18 || majorVersion == 16 && minorVersion > 8) && !process.env.DISABLE_TURBO_CALLS;
2904
+ if (fastApiCalls) {
2905
+ require$1('v8').setFlagsFromString('--turbo-fast-api-calls');
2906
+ }
2907
+ nativeAddon.enableDirectV8(v8Funcs, fastApiCalls);
2908
+ Object.assign(nativeAddon, v8Funcs);
2909
+ exports.v8AccelerationEnabled = true;
2910
+ } else if (majorVersion == 14) {
2911
+ // node v14 only has ABI compatibility with node v16 for zero-arg clearKeptObjects
2912
+ let v8Funcs = {};
2913
+ nativeAddon.enableDirectV8(v8Funcs, false);
2914
+ nativeAddon.clearKeptObjects = v8Funcs.clearKeptObjects;
2915
+ }
2916
+ nativeAddon.enableThreadSafeCalls();
2917
+ }
2918
+ setNativeFunctions(nativeAddon);
2919
+
2920
+ Object.defineProperty(exports, 'bufferToKeyValue', {
2921
+ enumerable: true,
2922
+ get: function () { return orderedBinary$1.fromBufferKey; }
2923
+ });
2924
+ Object.defineProperty(exports, 'compareKey', {
2925
+ enumerable: true,
2926
+ get: function () { return orderedBinary$1.compareKeys; }
2927
+ });
2928
+ Object.defineProperty(exports, 'compareKeys', {
2929
+ enumerable: true,
2930
+ get: function () { return orderedBinary$1.compareKeys; }
2931
+ });
2932
+ Object.defineProperty(exports, 'keyValueToBuffer', {
2933
+ enumerable: true,
2934
+ get: function () { return orderedBinary$1.toBufferKey; }
2935
+ });
2936
+ exports.ABORT = ABORT;
2937
+ exports.IF_EXISTS = IF_EXISTS$1;
2938
+ exports.SKIP = SKIP;
2939
+ exports.TransactionFlags = TransactionFlags;
2940
+ exports.allDbs = allDbs;
2941
+ exports.asBinary = asBinary;
2942
+ exports["default"] = index;
2943
+ exports.getLastTxnId = getLastTxnId;
2944
+ exports.getLastVersion = getLastVersion;
2945
+ exports.levelup = levelup;
2946
+ exports.nativeAddon = nativeAddon;
2947
+ exports.noop = noop;
2948
+ exports.open = open;
2949
+ exports.openAsClass = openAsClass;
2950
+ //# sourceMappingURL=index.cjs.map