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