@ohos-ports/lmdb 2.8.6-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (125) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +580 -0
  3. package/SECURITY.md +12 -0
  4. package/bin/download-prebuilds.js +15 -0
  5. package/binding.gyp +113 -0
  6. package/build/Release/lmdb.node +0 -0
  7. package/caching.js +212 -0
  8. package/dependencies/lmdb/libraries/liblmdb/COPYRIGHT +20 -0
  9. package/dependencies/lmdb/libraries/liblmdb/Doxyfile +1631 -0
  10. package/dependencies/lmdb/libraries/liblmdb/LICENSE +47 -0
  11. package/dependencies/lmdb/libraries/liblmdb/Makefile +136 -0
  12. package/dependencies/lmdb/libraries/liblmdb/chacha8.c +183 -0
  13. package/dependencies/lmdb/libraries/liblmdb/chacha8.h +14 -0
  14. package/dependencies/lmdb/libraries/liblmdb/crypto.c +121 -0
  15. package/dependencies/lmdb/libraries/liblmdb/intro.doc +192 -0
  16. package/dependencies/lmdb/libraries/liblmdb/lmdb.h +1817 -0
  17. package/dependencies/lmdb/libraries/liblmdb/mdb.c +12555 -0
  18. package/dependencies/lmdb/libraries/liblmdb/mdb_copy.1 +74 -0
  19. package/dependencies/lmdb/libraries/liblmdb/mdb_copy.c +106 -0
  20. package/dependencies/lmdb/libraries/liblmdb/mdb_drop.1 +53 -0
  21. package/dependencies/lmdb/libraries/liblmdb/mdb_drop.c +154 -0
  22. package/dependencies/lmdb/libraries/liblmdb/mdb_dump.1 +94 -0
  23. package/dependencies/lmdb/libraries/liblmdb/mdb_dump.c +333 -0
  24. package/dependencies/lmdb/libraries/liblmdb/mdb_load.1 +97 -0
  25. package/dependencies/lmdb/libraries/liblmdb/mdb_load.c +530 -0
  26. package/dependencies/lmdb/libraries/liblmdb/mdb_stat.1 +83 -0
  27. package/dependencies/lmdb/libraries/liblmdb/mdb_stat.c +276 -0
  28. package/dependencies/lmdb/libraries/liblmdb/midl-sparse.c +452 -0
  29. package/dependencies/lmdb/libraries/liblmdb/midl.c +452 -0
  30. package/dependencies/lmdb/libraries/liblmdb/midl.h +208 -0
  31. package/dependencies/lmdb/libraries/liblmdb/module.c +101 -0
  32. package/dependencies/lmdb/libraries/liblmdb/module.h +16 -0
  33. package/dependencies/lmdb/libraries/liblmdb/mtest.c +178 -0
  34. package/dependencies/lmdb/libraries/liblmdb/mtest2.c +124 -0
  35. package/dependencies/lmdb/libraries/liblmdb/mtest3.c +133 -0
  36. package/dependencies/lmdb/libraries/liblmdb/mtest4.c +168 -0
  37. package/dependencies/lmdb/libraries/liblmdb/mtest5.c +135 -0
  38. package/dependencies/lmdb/libraries/liblmdb/mtest6.c +141 -0
  39. package/dependencies/lmdb/libraries/liblmdb/mtest_enc.c +190 -0
  40. package/dependencies/lmdb/libraries/liblmdb/mtest_enc2.c +189 -0
  41. package/dependencies/lmdb/libraries/liblmdb/mtest_remap.c +177 -0
  42. package/dependencies/lmdb/libraries/liblmdb/sample-bdb.txt +73 -0
  43. package/dependencies/lmdb/libraries/liblmdb/sample-mdb.txt +62 -0
  44. package/dependencies/lmdb/libraries/liblmdb/tooltag +27 -0
  45. package/dependencies/lmdb-data-v1/libraries/liblmdb/CHANGES +266 -0
  46. package/dependencies/lmdb-data-v1/libraries/liblmdb/COPYRIGHT +20 -0
  47. package/dependencies/lmdb-data-v1/libraries/liblmdb/Doxyfile +1631 -0
  48. package/dependencies/lmdb-data-v1/libraries/liblmdb/LICENSE +47 -0
  49. package/dependencies/lmdb-data-v1/libraries/liblmdb/intro.doc +192 -0
  50. package/dependencies/lmdb-data-v1/libraries/liblmdb/lmdb.h +1608 -0
  51. package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb.c +10320 -0
  52. package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_copy.1 +55 -0
  53. package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_copy.c +82 -0
  54. package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_dump.1 +75 -0
  55. package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_dump.c +319 -0
  56. package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_load.1 +84 -0
  57. package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_load.c +496 -0
  58. package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_stat.1 +64 -0
  59. package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_stat.c +263 -0
  60. package/dependencies/lmdb-data-v1/libraries/liblmdb/midl.c +359 -0
  61. package/dependencies/lmdb-data-v1/libraries/liblmdb/midl.h +186 -0
  62. package/dependencies/lmdb-data-v1/libraries/liblmdb/mtest.c +177 -0
  63. package/dependencies/lmdb-data-v1/libraries/liblmdb/mtest2.c +124 -0
  64. package/dependencies/lmdb-data-v1/libraries/liblmdb/mtest3.c +133 -0
  65. package/dependencies/lmdb-data-v1/libraries/liblmdb/mtest4.c +168 -0
  66. package/dependencies/lmdb-data-v1/libraries/liblmdb/mtest5.c +135 -0
  67. package/dependencies/lmdb-data-v1/libraries/liblmdb/mtest6.c +141 -0
  68. package/dependencies/lmdb-data-v1/libraries/liblmdb/sample-bdb.txt +73 -0
  69. package/dependencies/lmdb-data-v1/libraries/liblmdb/sample-mdb.txt +62 -0
  70. package/dependencies/lmdb-data-v1/libraries/liblmdb/tooltag +22 -0
  71. package/dependencies/lz4/LICENSE +11 -0
  72. package/dependencies/lz4/lib/LICENSE +24 -0
  73. package/dependencies/lz4/lib/Makefile +225 -0
  74. package/dependencies/lz4/lib/README.md +169 -0
  75. package/dependencies/lz4/lib/dll/example/Makefile +63 -0
  76. package/dependencies/lz4/lib/dll/example/README.md +69 -0
  77. package/dependencies/lz4/lib/dll/example/fullbench-dll.sln +25 -0
  78. package/dependencies/lz4/lib/dll/example/fullbench-dll.vcxproj +182 -0
  79. package/dependencies/lz4/lib/dll/liblz4.def +62 -0
  80. package/dependencies/lz4/lib/liblz4-dll.rc.in +35 -0
  81. package/dependencies/lz4/lib/liblz4.pc.in +14 -0
  82. package/dependencies/lz4/lib/lz4.c +2722 -0
  83. package/dependencies/lz4/lib/lz4.h +842 -0
  84. package/dependencies/lz4/lib/lz4file.c +311 -0
  85. package/dependencies/lz4/lib/lz4file.h +93 -0
  86. package/dependencies/lz4/lib/lz4frame.c +2078 -0
  87. package/dependencies/lz4/lib/lz4frame.h +692 -0
  88. package/dependencies/lz4/lib/lz4frame_static.h +47 -0
  89. package/dependencies/lz4/lib/lz4hc.c +1631 -0
  90. package/dependencies/lz4/lib/lz4hc.h +413 -0
  91. package/dependencies/lz4/lib/xxhash.c +1030 -0
  92. package/dependencies/lz4/lib/xxhash.h +328 -0
  93. package/dependencies/v8/v8-fast-api-calls-v16.h +791 -0
  94. package/dependencies/v8/v8-fast-api-calls.h +888 -0
  95. package/dict/dict.txt +1 -0
  96. package/dict/dict2.txt +1 -0
  97. package/dist/index.cjs +2950 -0
  98. package/dist/index.cjs.map +1 -0
  99. package/index.d.cts +420 -0
  100. package/index.d.ts +450 -0
  101. package/index.js +38 -0
  102. package/keys.js +116 -0
  103. package/level.js +27 -0
  104. package/native.js +85 -0
  105. package/node-index.js +32 -0
  106. package/open.js +448 -0
  107. package/package.json +124 -0
  108. package/read.js +833 -0
  109. package/rollup.config.js +12 -0
  110. package/src/compression.cpp +225 -0
  111. package/src/cursor.cpp +395 -0
  112. package/src/dbi.cpp +382 -0
  113. package/src/env.cpp +1030 -0
  114. package/src/lmdb-js.cpp +64 -0
  115. package/src/lmdb-js.h +633 -0
  116. package/src/misc.cpp +593 -0
  117. package/src/ordered-binary.cpp +46 -0
  118. package/src/txn.cpp +210 -0
  119. package/src/v8-functions.cpp +170 -0
  120. package/src/windows.c +35 -0
  121. package/src/writer.cpp +537 -0
  122. package/util/RangeIterable.js +236 -0
  123. package/util/set-optional-deps.cjs +14 -0
  124. package/util/when.js +8 -0
  125. package/write.js +912 -0
package/native.js ADDED
@@ -0,0 +1,85 @@
1
+ import { dirname, join, default as pathModule } from 'path';
2
+ import { fileURLToPath } from 'url';
3
+ import loadNAPI from 'node-gyp-build-optional-packages';
4
+ export let Env, Txn, Dbi, Compression, Cursor, getAddress, getBufferAddress, createBufferForAddress, clearKeptObjects, globalBuffer, setGlobalBuffer, arch, fs, os, onExit, tmpdir, lmdbError, path, EventEmitter, orderedBinary, MsgpackrEncoder, WeakLRUCache, setEnvMap, getEnvMap, getByBinary, detachBuffer, startRead, setReadCallback, write, position, iterate, prefetch, resetTxn, getCurrentValue, getCurrentShared, getStringByBinary, getSharedByBinary, getSharedBuffer, compress;
5
+ path = pathModule;
6
+ let dirName = dirname(fileURLToPath(import.meta.url)).replace(/dist$/, '');
7
+ export let nativeAddon = loadNAPI(dirName);
8
+
9
+ if (process.isBun && false) {
10
+ const { linkSymbols, FFIType } = require('bun:ffi');
11
+ let lmdbLib = linkSymbols({
12
+ getByBinary: {
13
+ args: [FFIType.f64, FFIType.u32],
14
+ returns: FFIType.u32,
15
+ ptr: nativeAddon.getByBinaryPtr
16
+ },
17
+ iterate: {
18
+ args: [FFIType.f64],
19
+ returns: FFIType.i32,
20
+ ptr: nativeAddon.iteratePtr,
21
+ },
22
+ position: {
23
+ args: [FFIType.f64, FFIType.u32, FFIType.u32, FFIType.u32, FFIType.f64],
24
+ returns: FFIType.i32,
25
+ ptr: nativeAddon.positionPtr,
26
+ },
27
+ write: {
28
+ args: [FFIType.f64, FFIType.f64],
29
+ returns: FFIType.i32,
30
+ ptr: nativeAddon.writePtr,
31
+ },
32
+ resetTxn: {
33
+ args: [FFIType.f64],
34
+ returns: FFIType.void,
35
+ ptr: nativeAddon.resetTxnPtr,
36
+ }
37
+ });
38
+ for (let key in lmdbLib.symbols) {
39
+ nativeAddon[key] = lmdbLib.symbols[key].native;
40
+ }
41
+ }
42
+ setNativeFunctions(nativeAddon);
43
+
44
+ export function setNativeFunctions(externals) {
45
+ Env = externals.Env;
46
+ Txn = externals.Txn;
47
+ Dbi = externals.Dbi;
48
+ Compression = externals.Compression;
49
+ getAddress = externals.getAddress;
50
+ getBufferAddress = externals.getBufferAddress;
51
+ createBufferForAddress = externals.createBufferForAddress;
52
+ clearKeptObjects = externals.clearKeptObjects || function() {};
53
+ getByBinary = externals.getByBinary;
54
+ detachBuffer = externals.detachBuffer;
55
+ startRead = externals.startRead;
56
+ setReadCallback = externals.setReadCallback;
57
+ setGlobalBuffer = externals.setGlobalBuffer;
58
+ globalBuffer = externals.globalBuffer;
59
+ getSharedBuffer = externals.getSharedBuffer;
60
+ prefetch = externals.prefetch;
61
+ iterate = externals.iterate;
62
+ position = externals.position;
63
+ resetTxn = externals.resetTxn;
64
+ getCurrentValue = externals.getCurrentValue;
65
+ getCurrentShared = externals.getCurrentShared;
66
+ getStringByBinary = externals.getStringByBinary;
67
+ getSharedByBinary = externals.getSharedByBinary;
68
+ write = externals.write;
69
+ compress = externals.compress;
70
+ Cursor = externals.Cursor;
71
+ lmdbError = externals.lmdbError;
72
+ if (externals.tmpdir)
73
+ tmpdir = externals.tmpdir
74
+ }
75
+ export function setExternals(externals) {
76
+ arch = externals.arch;
77
+ fs = externals.fs;
78
+ EventEmitter = externals.EventEmitter;
79
+ orderedBinary = externals.orderedBinary;
80
+ MsgpackrEncoder = externals.MsgpackrEncoder;
81
+ WeakLRUCache = externals.WeakLRUCache;
82
+ tmpdir = externals.tmpdir;
83
+ os = externals.os;
84
+ onExit = externals.onExit;
85
+ }
package/node-index.js ADDED
@@ -0,0 +1,32 @@
1
+ import { createRequire } from 'module';
2
+ import { setRequire } from './open.js';
3
+ import { nativeAddon, setNativeFunctions } from './native.js';
4
+ export { nativeAddon } from './native.js'
5
+ let require = createRequire(import.meta.url);
6
+ setRequire(require);
7
+ export let v8AccelerationEnabled = false
8
+
9
+ let versions = process.versions;
10
+ if (!versions.deno && !process.isBun) {
11
+ let [ majorVersion, minorVersion ] = versions.node.split('.');
12
+ if (versions.v8 && +majorVersion == nativeAddon.version.nodeCompiledVersion) {
13
+ let v8Funcs = {};
14
+ let fastApiCalls = (majorVersion == 17 || majorVersion == 18 || majorVersion == 16 && minorVersion > 8) && !process.env.DISABLE_TURBO_CALLS;
15
+ if (fastApiCalls) {
16
+ require('v8').setFlagsFromString('--turbo-fast-api-calls');
17
+ }
18
+ nativeAddon.enableDirectV8(v8Funcs, fastApiCalls);
19
+ Object.assign(nativeAddon, v8Funcs);
20
+ v8AccelerationEnabled = true;
21
+ } else if (majorVersion == 14) {
22
+ // node v14 only has ABI compatibility with node v16 for zero-arg clearKeptObjects
23
+ let v8Funcs = {};
24
+ nativeAddon.enableDirectV8(v8Funcs, false);
25
+ nativeAddon.clearKeptObjects = v8Funcs.clearKeptObjects;
26
+ }
27
+ nativeAddon.enableThreadSafeCalls();
28
+ }
29
+ setNativeFunctions(nativeAddon);
30
+
31
+ export * from './index.js'
32
+ export { default } from './index.js'
package/open.js ADDED
@@ -0,0 +1,448 @@
1
+ import { Compression, getAddress, arch, fs, path as pathModule, lmdbError, EventEmitter, MsgpackrEncoder, Env,
2
+ Dbi, tmpdir, os, nativeAddon } from './native.js';
3
+ import { CachingStore, setGetLastVersion } from './caching.js';
4
+ import { addReadMethods, makeReusableBuffer } from './read.js';
5
+ import { addWriteMethods } from './write.js';
6
+ import { applyKeyHandling } from './keys.js';
7
+ let moduleRequire = typeof require == 'function' && require;
8
+ export function setRequire(require) {
9
+ moduleRequire = require;
10
+ }
11
+
12
+ setGetLastVersion(getLastVersion, getLastTxnId);
13
+ let keyBytes, keyBytesView;
14
+ const buffers = [];
15
+ const { onExit, getEnvsPointer, setEnvsPointer, getEnvFlags, setJSFlags } = nativeAddon;
16
+ if (globalThis.__lmdb_envs__)
17
+ setEnvsPointer(globalThis.__lmdb_envs__);
18
+ else
19
+ globalThis.__lmdb_envs__ = getEnvsPointer();
20
+
21
+ // this is hard coded as an upper limit because it is important assumption of the fixed buffers in writing instructions
22
+ // this corresponds to the max key size for 8KB pages
23
+ const MAX_KEY_SIZE = 4026;
24
+ // this is used as the key size by default because default page size is OS page size, which is usually
25
+ // 4KB (but is 16KB on M-series MacOS), and this keeps a consistent max key size when no page size specified.
26
+ const DEFAULT_MAX_KEY_SIZE = 1978;
27
+ const DEFAULT_COMMIT_DELAY = 0;
28
+
29
+ export const allDbs = new Map();
30
+ let defaultCompression;
31
+ let lastSize;
32
+ let hasRegisteredOnExit;
33
+ export function open(path, options) {
34
+ if (nativeAddon.open) {
35
+ if (nativeAddon.open !== open) {
36
+ // this is the case when lmdb-js has been opened in both ESM and CJS mode, which means that there are two
37
+ // separate JS modules, but they are both using the same native module.
38
+ getLastVersion = nativeAddon.getLastVersion;
39
+ getLastTxnId = nativeAddon.getLastTxnId;
40
+ setGetLastVersion(getLastVersion, getLastTxnId);
41
+ return nativeAddon.open(path, options);
42
+ }
43
+ } else {
44
+ nativeAddon.open = open;
45
+ nativeAddon.getLastVersion = getLastVersion;
46
+ nativeAddon.getLastTxnId = getLastTxnId;
47
+ }
48
+ if (!keyBytes) // TODO: Consolidate get buffer and key buffer (don't think we need both)
49
+ allocateFixedBuffer();
50
+ if (typeof path == 'object' && !options) {
51
+ options = path;
52
+ path = options.path;
53
+ }
54
+ options = options || {};
55
+ let noFSAccess = options.noFSAccess; // this can only be configured on open, can't let users change it
56
+ let userOptions = options;
57
+ if (path == null) {
58
+ options = Object.assign({
59
+ deleteOnClose: true,
60
+ noSync: true,
61
+ }, options);
62
+ path = tmpdir() + '/' + Math.floor(Math.random() * 2821109907455).toString(36) + '.mdb'
63
+ } else if (!options)
64
+ options = {};
65
+ let extension = pathModule.extname(path);
66
+ let name = pathModule.basename(path, extension);
67
+ let is32Bit = arch().endsWith('32');
68
+ let remapChunks = options.remapChunks || options.encryptionKey || (options.mapSize ?
69
+ (is32Bit && options.mapSize > 0x100000000) : // larger than fits in address space, must use dynamic maps
70
+ is32Bit); // without a known map size, we default to being able to handle large data correctly/well*/
71
+ let userMapSize = options.mapSize;
72
+ options = Object.assign({
73
+ noSubdir: Boolean(extension),
74
+ isRoot: true,
75
+ maxDbs: 12,
76
+ remapChunks,
77
+ keyBytes,
78
+ overlappingSync: (options.noSync || options.readOnly) ? false : (os != 'win32'),
79
+ // default map size limit of 4 exabytes when using remapChunks, since it is not preallocated and we can
80
+ // make it super huge.
81
+ mapSize: remapChunks ? 0x10000000000000 :
82
+ 0x20000, // Otherwise we start small with 128KB
83
+ safeRestore: process.env.LMDB_RESTORE == 'safe',
84
+ }, options);
85
+ options.path = path;
86
+ if (options.asyncTransactionOrder == 'strict') {
87
+ options.strictAsyncOrder = true;
88
+ }
89
+ if (nativeAddon.version.major + nativeAddon.version.minor / 100 + nativeAddon.version.patch / 10000 < 0.0980) {
90
+ options.overlappingSync = false; // not support on older versions
91
+ options.trackMetrics = false;
92
+ options.usePreviousSnapshot = false;
93
+ options.safeRestore = false;
94
+ options.remapChunks = false;
95
+ if (!userMapSize) options.mapSize = 0x40000000; // 1 GB
96
+ }
97
+
98
+ if (!exists(options.noSubdir ? pathModule.dirname(path) : path))
99
+ fs.mkdirSync(options.noSubdir ? pathModule.dirname(path) : path, { recursive: true }
100
+ );
101
+ function makeCompression(compressionOptions) {
102
+ if (compressionOptions instanceof Compression)
103
+ return compressionOptions;
104
+ let useDefault = typeof compressionOptions != 'object';
105
+ if (useDefault && defaultCompression)
106
+ return defaultCompression;
107
+ compressionOptions = Object.assign({
108
+ threshold: 1000,
109
+ dictionary: fs.readFileSync(new URL('./dict/dict.txt', import.meta.url.replace(/dist[\\\/]index.cjs$/, ''))),
110
+ getValueBytes: makeReusableBuffer(0),
111
+ }, compressionOptions);
112
+ let compression = Object.assign(new Compression(compressionOptions), compressionOptions);
113
+ if (useDefault)
114
+ defaultCompression = compression;
115
+ return compression;
116
+ }
117
+
118
+ if (options.compression)
119
+ options.compression = makeCompression(options.compression);
120
+ let flags =
121
+ (options.overlappingSync ? 0x1000 : 0) |
122
+ (options.noSubdir ? 0x4000 : 0) |
123
+ (options.noSync ? 0x10000 : 0) |
124
+ (options.readOnly ? 0x20000 : 0) |
125
+ (options.noMetaSync ? 0x40000 : 0) |
126
+ (options.useWritemap ? 0x80000 : 0) |
127
+ (options.mapAsync ? 0x100000 : 0) |
128
+ (options.noReadAhead ? 0x800000 : 0) |
129
+ (options.noMemInit ? 0x1000000 : 0) |
130
+ (options.usePreviousSnapshot ? 0x2000000 : 0) |
131
+ (options.remapChunks ? 0x4000000 : 0) |
132
+ (options.safeRestore ? 0x800 : 0) |
133
+ (options.trackMetrics ? 0x400 : 0);
134
+
135
+ let env = new Env();
136
+ let jsFlags = (options.overlappingSync ? 0x1000 : 0) |
137
+ (options.separateFlushed ? 1 : 0) |
138
+ (options.deleteOnClose ? 2 : 0);
139
+ let rc = env.open(options, flags, jsFlags);
140
+ env.path = path;
141
+ if (rc)
142
+ lmdbError(rc);
143
+ delete options.keyBytes // no longer needed, don't copy to stores
144
+ let maxKeySize = env.getMaxKeySize();
145
+ maxKeySize = Math.min(maxKeySize, options.pageSize ? MAX_KEY_SIZE : DEFAULT_MAX_KEY_SIZE);
146
+ flags = getEnvFlags(env.address); // re-retrieve them, they are not necessarily the same if we are connecting to an existing env
147
+ if (flags & 0x1000) {
148
+ if (userOptions.noSync) {
149
+ env.close();
150
+ throw new Error('Can not set noSync on a database that was opened with overlappingSync');
151
+ }
152
+ } else if (options.overlappingSync) {
153
+ if (userOptions.overlappingSync) {
154
+ env.close();
155
+ throw new Error('Can not enable overlappingSync on a database that was opened without this flag');
156
+ }
157
+ options.overlappingSync = false;
158
+ jsFlags = jsFlags & 0xff; // clear overlapping sync
159
+ setJSFlags(env.address, jsFlags);
160
+ }
161
+
162
+ env.readerCheck(); // clear out any stale entries
163
+ if ((options.overlappingSync || options.deleteOnClose) && !hasRegisteredOnExit && process.on) {
164
+ hasRegisteredOnExit = true;
165
+ process.on('exit', onExit);
166
+ }
167
+
168
+ class LMDBStore extends EventEmitter {
169
+ constructor(dbName, dbOptions) {
170
+ super();
171
+ if (dbName === undefined)
172
+ throw new Error('Database name must be supplied in name property (may be null for root database)');
173
+
174
+ if (options.compression && dbOptions.compression !== false && typeof dbOptions.compression != 'object')
175
+ dbOptions.compression = options.compression; // use the parent compression if available
176
+ else if (dbOptions.compression)
177
+ dbOptions.compression = makeCompression(dbOptions.compression);
178
+
179
+ if (dbOptions.dupSort && (dbOptions.useVersions || dbOptions.cache)) {
180
+ throw new Error('The dupSort flag can not be combined with versions or caching');
181
+ }
182
+ let keyIsBuffer = dbOptions.keyIsBuffer
183
+ if (dbOptions.keyEncoding == 'uint32') {
184
+ dbOptions.keyIsUint32 = true;
185
+ } else if (dbOptions.keyEncoder) {
186
+ if (dbOptions.keyEncoder.enableNullTermination) {
187
+ dbOptions.keyEncoder.enableNullTermination()
188
+ } else
189
+ keyIsBuffer = true;
190
+ } else if (dbOptions.keyEncoding == 'binary') {
191
+ keyIsBuffer = true;
192
+ }
193
+ let flags = (dbOptions.reverseKey ? 0x02 : 0) |
194
+ (dbOptions.dupSort ? 0x04 : 0) |
195
+ (dbOptions.dupFixed ? 0x10 : 0) |
196
+ (dbOptions.integerDup ? 0x20 : 0) |
197
+ (dbOptions.reverseDup ? 0x40 : 0) |
198
+ (!options.readOnly && dbOptions.create !== false ? 0x40000 : 0) |
199
+ (dbOptions.useVersions ? 0x100 : 0);
200
+ let keyType = (dbOptions.keyIsUint32 || dbOptions.keyEncoding == 'uint32') ? 2 : keyIsBuffer ? 3 : 0;
201
+ if (keyType == 2)
202
+ flags |= 0x08; // integer key
203
+
204
+ if (options.readOnly) {
205
+ // in read-only mode we use a read-only txn to open the database
206
+ // TODO: LMDB is actually not entirely thread-safe when it comes to opening databases with
207
+ // read-only transactions since there is a race condition on setting the update dbis that
208
+ // occurs outside the lock
209
+ // make sure we are using a fresh read txn, so we don't want to share with a cursor txn
210
+ this.resetReadTxn();
211
+ this.ensureReadTxn();
212
+ this.db = new Dbi(env, flags, dbName, keyType, dbOptions.compression);
213
+ } else {
214
+ this.transactionSync(() => {
215
+ this.db = new Dbi(env, flags, dbName, keyType, dbOptions.compression);
216
+ }, options.overlappingSync ? 0x10002 : 2); // no flush-sync, but synchronously commit
217
+ }
218
+ this._commitReadTxn(); // current read transaction becomes invalid after opening another db
219
+ if (!this.db || this.db.dbi == 0xffffffff) {// not found
220
+ throw new Error('Database not found')
221
+ }
222
+ this.dbAddress = this.db.address
223
+ this.db.name = dbName || null;
224
+ this.name = dbName;
225
+ this.status = 'open';
226
+ this.env = env;
227
+ this.reads = 0;
228
+ this.writes = 0;
229
+ this.transactions = 0;
230
+ this.averageTransactionTime = 5;
231
+ if (dbOptions.syncBatchThreshold)
232
+ console.warn('syncBatchThreshold is no longer supported');
233
+ if (dbOptions.immediateBatchThreshold)
234
+ console.warn('immediateBatchThreshold is no longer supported');
235
+ this.commitDelay = DEFAULT_COMMIT_DELAY;
236
+ Object.assign(this, { // these are the options that are inherited
237
+ path: options.path,
238
+ encoding: options.encoding,
239
+ strictAsyncOrder: options.strictAsyncOrder,
240
+ }, dbOptions);
241
+ let Encoder;
242
+ if (this.encoder && this.encoder.Encoder) {
243
+ Encoder = this.encoder.Encoder;
244
+ this.encoder = null; // don't copy everything from the module
245
+ }
246
+ if (!Encoder && !(this.encoder && this.encoder.encode) && (!this.encoding || this.encoding == 'msgpack' || this.encoding == 'cbor')) {
247
+ Encoder = (this.encoding == 'cbor' ? moduleRequire('cbor-x').Encoder : MsgpackrEncoder);
248
+ }
249
+ if (Encoder) {
250
+ this.encoder = new Encoder(Object.assign(
251
+ assignConstrainedProperties(['copyBuffers', 'getStructures', 'saveStructures', 'useFloat32', 'useRecords', 'structuredClone', 'variableMapSize', 'useTimestamp32', 'largeBigIntToFloat', 'encodeUndefinedAsNil', 'int64AsNumber', 'onInvalidDate', 'mapsAsObjects', 'useTag259ForMaps', 'pack', 'maxSharedStructures', 'shouldShareStructure', 'randomAccessStructure', 'freezeData'],
252
+ this.sharedStructuresKey ? this.setupSharedStructures() : {
253
+ copyBuffers: true, // need to copy any embedded buffers that are found since we use unsafe buffers
254
+ }, options, dbOptions), this.encoder));
255
+ }
256
+ if (this.encoding == 'json') {
257
+ this.encoder = {
258
+ encode: JSON.stringify,
259
+ };
260
+ } else if (this.encoder) {
261
+ this.decoder = this.encoder;
262
+ this.decoderCopies = !this.encoder.needsStableBuffer
263
+ }
264
+ this.maxKeySize = maxKeySize;
265
+ applyKeyHandling(this);
266
+ allDbs.set(dbName ? name + '-' + dbName : name, this);
267
+ }
268
+ openDB(dbName, dbOptions) {
269
+ if (this.dupSort && this.name == null)
270
+ throw new Error('Can not open named databases if the main database is dupSort')
271
+ if (typeof dbName == 'object' && !dbOptions) {
272
+ dbOptions = dbName;
273
+ dbName = dbOptions.name;
274
+ } else
275
+ dbOptions = dbOptions || {};
276
+ try {
277
+ return dbOptions.cache ?
278
+ new (CachingStore(LMDBStore, env))(dbName, dbOptions) :
279
+ new LMDBStore(dbName, dbOptions);
280
+ } catch(error) {
281
+ if (error.message == 'Database not found')
282
+ return; // return undefined to indicate db not found
283
+ if (error.message.indexOf('MDB_DBS_FULL') > -1) {
284
+ error.message += ' (increase your maxDbs option)';
285
+ }
286
+ throw error;
287
+ }
288
+ }
289
+ open(dbOptions, callback) {
290
+ let db = this.openDB(dbOptions);
291
+ if (callback)
292
+ callback(null, db);
293
+ return db;
294
+ }
295
+ backup(path, compact) {
296
+ if (noFSAccess)
297
+ return;
298
+ fs.mkdirSync(pathModule.dirname(path), { recursive: true });
299
+ return new Promise((resolve, reject) => env.copy(path, false, (error) => {
300
+ if (error) {
301
+ reject(error);
302
+ } else {
303
+ resolve();
304
+ }
305
+ }));
306
+ }
307
+ isOperational() {
308
+ return this.status == 'open';
309
+ }
310
+ sync(callback) {
311
+ return env.sync(callback || function(error) {
312
+ if (error) {
313
+ console.error(error);
314
+ }
315
+ });
316
+ }
317
+ deleteDB() {
318
+ console.warn('deleteDB() is deprecated, use drop or dropSync instead');
319
+ return this.dropSync();
320
+ }
321
+ dropSync() {
322
+ this.transactionSync(() =>
323
+ this.db.drop({
324
+ justFreePages: false
325
+ }), options.overlappingSync ? 0x10002 : 2);
326
+ }
327
+ clear(callback) {
328
+ if (typeof callback == 'function')
329
+ return this.clearAsync(callback);
330
+ console.warn('clear() is deprecated, use clearAsync or clearSync instead');
331
+ this.clearSync();
332
+ }
333
+ clearSync() {
334
+ if (this.encoder) {
335
+ if (this.encoder.clearSharedData)
336
+ this.encoder.clearSharedData()
337
+ else if (this.encoder.structures)
338
+ this.encoder.structures = []
339
+ }
340
+ this.transactionSync(() =>
341
+ this.db.drop({
342
+ justFreePages: true
343
+ }), options.overlappingSync ? 0x10002 : 2);
344
+ }
345
+ readerCheck() {
346
+ return env.readerCheck();
347
+ }
348
+ readerList() {
349
+ return env.readerList().join('');
350
+ }
351
+ setupSharedStructures() {
352
+ const getStructures = () => {
353
+ let lastVersion; // because we are doing a read here, we may need to save and restore the lastVersion from the last read
354
+ if (this.useVersions)
355
+ lastVersion = getLastVersion();
356
+ let buffer = this.getBinary(this.sharedStructuresKey);
357
+ if (this.useVersions)
358
+ setLastVersion(lastVersion);
359
+ return buffer && this.decoder.decode(buffer);
360
+ };
361
+ return {
362
+ saveStructures: (structures, isCompatible) => {
363
+ return this.transactionSync(() => {
364
+ let existingStructuresBuffer = this.getBinary(this.sharedStructuresKey);
365
+ let existingStructures = existingStructuresBuffer && this.decoder.decode(existingStructuresBuffer);
366
+ if (typeof isCompatible == 'function' ?
367
+ !isCompatible(existingStructures) :
368
+ (existingStructures && existingStructures.length != isCompatible))
369
+ return false; // it changed, we need to indicate that we couldn't update
370
+ this.put(this.sharedStructuresKey, structures);
371
+ }, options.overlappingSync ? 0x10000 : 0);
372
+ },
373
+ getStructures,
374
+ copyBuffers: true, // need to copy any embedded buffers that are found since we use unsafe buffers
375
+ };
376
+ }
377
+ }
378
+ // if caching class overrides putSync, don't want to double call the caching code
379
+ const putSync = LMDBStore.prototype.putSync;
380
+ const removeSync = LMDBStore.prototype.removeSync;
381
+ addReadMethods(LMDBStore, { env, maxKeySize, keyBytes, keyBytesView, getLastVersion });
382
+ if (!options.readOnly)
383
+ addWriteMethods(LMDBStore, { env, maxKeySize, fixedBuffer: keyBytes,
384
+ resetReadTxn: LMDBStore.prototype.resetReadTxn, ...options });
385
+ LMDBStore.prototype.supports = {
386
+ permanence: true,
387
+ bufferKeys: true,
388
+ promises: true,
389
+ snapshots: true,
390
+ clear: true,
391
+ status: true,
392
+ deferredOpen: true,
393
+ openCallback: true,
394
+ };
395
+ let Class = options.cache ? CachingStore(LMDBStore, env) : LMDBStore;
396
+ return options.asClass ? Class : new Class(options.name || null, options);
397
+ }
398
+ export function openAsClass(path, options) {
399
+ if (typeof path == 'object' && !options) {
400
+ options = path;
401
+ path = options.path;
402
+ }
403
+ options = options || {};
404
+ options.asClass = true;
405
+ return open(path, options);
406
+ }
407
+
408
+ export function getLastVersion() {
409
+ return keyBytesView.getFloat64(16, true);
410
+ }
411
+ export function setLastVersion(version) {
412
+ return keyBytesView.setFloat64(16, version, true);
413
+ }
414
+
415
+ export function getLastTxnId() {
416
+ return keyBytesView.getUint32(32, true);
417
+ }
418
+
419
+ const KEY_BUFFER_SIZE = 4096;
420
+ function allocateFixedBuffer() {
421
+ keyBytes = typeof Buffer != 'undefined' ? Buffer.allocUnsafeSlow(KEY_BUFFER_SIZE) : new Uint8Array(KEY_BUFFER_SIZE);
422
+ const keyBuffer = keyBytes.buffer;
423
+ keyBytesView = keyBytes.dataView || (keyBytes.dataView = new DataView(keyBytes.buffer, 0, KEY_BUFFER_SIZE)); // max key size is actually 4026
424
+ keyBytes.uint32 = new Uint32Array(keyBuffer, 0, KEY_BUFFER_SIZE >> 2);
425
+ keyBytes.float64 = new Float64Array(keyBuffer, 0, KEY_BUFFER_SIZE >> 3);
426
+ keyBytes.uint32.address = keyBytes.address = keyBuffer.address = getAddress(keyBuffer);
427
+ }
428
+
429
+ function exists(path) {
430
+ if (fs.existsSync)
431
+ return fs.existsSync(path);
432
+ try {
433
+ return fs.statSync(path);
434
+ } catch (error) {
435
+ return false
436
+ }
437
+ }
438
+
439
+ function assignConstrainedProperties(allowedProperties, target) {
440
+ for (let i = 2; i < arguments.length; i++) {
441
+ let source = arguments[i];
442
+ for (let key in source) {
443
+ if (allowedProperties.includes(key))
444
+ target[key] = source[key];
445
+ }
446
+ }
447
+ return target;
448
+ }
package/package.json ADDED
@@ -0,0 +1,124 @@
1
+ {
2
+ "name": "@ohos-ports/lmdb",
3
+ "author": "Kris Zyp",
4
+ "version": "2.8.6-beta.0",
5
+ "description": "Simple, efficient, scalable, high-performance LMDB interface",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+ssh://git@github.com/kriszyp/lmdb-js.git"
10
+ },
11
+ "keywords": [
12
+ "lmdb",
13
+ "database",
14
+ "mdb",
15
+ "lightning",
16
+ "key-value store",
17
+ "storage",
18
+ "adapter",
19
+ "performance"
20
+ ],
21
+ "publishConfig": {
22
+ "registry": "https://npm.cnb.cool/OpenHarmonyPCDeveloper/npm/-/packages/"
23
+ },
24
+ "type": "module",
25
+ "main": "dist/index.cjs",
26
+ "module": "index.js",
27
+ "exports": {
28
+ ".": {
29
+ "types": {
30
+ "require": "./index.d.cts",
31
+ "import": "./index.d.ts"
32
+ },
33
+ "node": {
34
+ "require": "./dist/index.cjs",
35
+ "import": "./node-index.js"
36
+ },
37
+ "default": "./index.js"
38
+ }
39
+ },
40
+ "files": [
41
+ "/dist",
42
+ "/util",
43
+ "/dict",
44
+ "/dependencies",
45
+ "/src",
46
+ "*.md",
47
+ "/*.js",
48
+ "index.d.ts",
49
+ "index.d.cts",
50
+ "/*.ts",
51
+ "/*.gyp",
52
+ "/bin",
53
+ "/build/Release/*.node"
54
+ ],
55
+ "types": "./index.d.ts",
56
+ "tsd": {
57
+ "directory": "test/types"
58
+ },
59
+ "bin": {
60
+ "download-lmdb-prebuilds": "./bin/download-prebuilds.js"
61
+ },
62
+ "scripts": {
63
+ "install": "node-gyp-build-optional-packages",
64
+ "build": "node-gyp --debug configure && node-gyp --debug build && rollup -c",
65
+ "rebuild": "node-gyp build && rollup -c && cpy index.d.ts . --rename=index.d.cts",
66
+ "build-js": "rollup -c",
67
+ "prepare": "rollup -c",
68
+ "before-publish": "rollup -c && prebuildify-ci download && node util/set-optional-deps.cjs && npm run test",
69
+ "prebuild-libc-musl": "ENABLE_V8_FUNCTIONS=false prebuildify-platform-packages --tag-libc --napi --platform-packages --target 16.18.0",
70
+ "prebuild-libc": "prebuildify-platform-packages --tag-libc --target 20.0.0 || true && prebuildify-platform-packages --tag-libc --target 18.15.0 && prebuildify-platform-packages --platform-packages --tag-libc --target 16.18.0 && ENABLE_V8_FUNCTIONS=false prebuildify-platform-packages --napi --platform-packages --tag-libc --target 16.18.0",
71
+ "prebuild-macos": "prebuildify-platform-packages --target 20.0.0 && prebuildify-platform-packages --target 18.15.0 && prebuildify-platform-packages --platform-packages --target 16.18.0 && ENABLE_V8_FUNCTIONS=false prebuildify-platform-packages --napi --platform-packages --target 16.18.0",
72
+ "prebuild-win32": "prebuildify-platform-packages --target 20.0.0 && prebuildify-platform-packages --target 18.15.0 && prebuildify-platform-packages --target 16.18.0 && set ENABLE_V8_FUNCTIONS=false&& prebuildify-platform-packages --napi --platform-packages --target 16.18.0",
73
+ "prebuild-libc-arm7": "ENABLE_V8_FUNCTIONS=false prebuildify-platform-packages --napi --platform-packages --tag-libc --target 16.18.0",
74
+ "prebuildify": "prebuildify-platform-packages --napi --target 18.15.0",
75
+ "full-publish": "cd prebuilds/win32-x64 && npm publish --access public && cd ../darwin-x64 && npm publish --access public && cd ../darwin-arm64 && npm publish --access public && cd ../linux-x64 && npm publish --access public && cd ../linux-arm64 && npm publish --access public && cd ../linux-arm && npm publish --access public && cd ../.. && npm publish",
76
+ "recompile": "node-gyp clean && node-gyp configure && node-gyp build",
77
+ "test": "mocha test/**.test.js --expose-gc --recursive",
78
+ "deno-test": "deno run --allow-ffi --allow-write --allow-read --allow-env --allow-net --unstable test/deno.ts",
79
+ "test2": "mocha test/performance.js -u tdd",
80
+ "test:types": "tsd",
81
+ "benchmark": "node ./benchmark/index.js"
82
+ },
83
+ "gypfile": false,
84
+ "dependencies": {
85
+ "msgpackr": "^1.9.5",
86
+ "node-addon-api": "^6.1.0",
87
+ "node-gyp-build-optional-packages": "5.1.1",
88
+ "ordered-binary": "^1.4.1",
89
+ "weak-lru-cache": "^1.2.2"
90
+ },
91
+ "devDependencies": {
92
+ "@types/node": "^16.7.10",
93
+ "benchmark": "^2.1.4",
94
+ "chai": "^4.3.4",
95
+ "cpy-cli": "^4.1.0",
96
+ "fs-extra": "^9.0.1",
97
+ "mocha": "^10.1.0",
98
+ "prebuildify-ci": "^1.0.5",
99
+ "prebuildify-platform-packages": "5.0.4",
100
+ "prettier": "^3.0.2",
101
+ "rimraf": "^3.0.2",
102
+ "@ohos-ports/rollup": "^2.61.1",
103
+ "tsd": "^0.14.0"
104
+ },
105
+ "bugs": {
106
+ "url": "https://github.com/kriszyp/lmdb-js/issues"
107
+ },
108
+ "homepage": "https://github.com/kriszyp/lmdb-js#readme",
109
+ "directories": {
110
+ "test": "tests"
111
+ },
112
+ "prettier": {
113
+ "useTabs": true,
114
+ "singleQuote": true
115
+ },
116
+ "optionalDependencies": {
117
+ "@lmdb/lmdb-darwin-arm64": "2.8.5",
118
+ "@lmdb/lmdb-darwin-x64": "2.8.5",
119
+ "@lmdb/lmdb-linux-arm": "2.8.5",
120
+ "@lmdb/lmdb-linux-arm64": "2.8.5",
121
+ "@lmdb/lmdb-linux-x64": "2.8.5",
122
+ "@lmdb/lmdb-win32-x64": "2.8.5"
123
+ }
124
+ }