@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/index.d.ts ADDED
@@ -0,0 +1,385 @@
1
+ declare namespace lmdb {
2
+ export function open<V = any, K extends Key = Key>(path: string, options: RootDatabaseOptions): RootDatabase<V, K>
3
+ export function open<V = any, K extends Key = Key>(options: RootDatabaseOptionsWithPath): RootDatabase<V, K>
4
+ export function openAsClass<V = any, K extends Key = Key>(options: RootDatabaseOptionsWithPath): DatabaseClass<V, K>
5
+
6
+ class Database<V = any, K extends Key = Key> {
7
+ /**
8
+ * Get the value stored by given id/key
9
+ * @param id The key for the entry
10
+ **/
11
+ get(id: K): V | undefined
12
+ /**
13
+ * Get the entry stored by given id/key, which includes both the value and the version number (if available)
14
+ * @param id The key for the entry
15
+ **/
16
+ getEntry(id: K): {
17
+ value: V
18
+ version?: number
19
+ } | undefined
20
+
21
+ /**
22
+ * Get the value stored by given id/key in binary format, as a Buffer
23
+ * @param id The key for the entry
24
+ **/
25
+ getBinary(id: K): Buffer | undefined
26
+
27
+ /**
28
+ * Get the value stored by given id/key in binary format, as a temporary Buffer.
29
+ * This is faster, but the data is only valid until the next get operation (then it will be overwritten).
30
+ * @param id The key for the entry
31
+ **/
32
+ getBinaryFast(id: K): Buffer | undefined
33
+
34
+ /**
35
+ * Asynchronously fetch the values stored by the given ids and accesses all
36
+ * pages to ensure that any hard page faults and disk I/O are performed
37
+ * asynchronously in a separate thread. Once completed, synchronous
38
+ * gets to the same entries will most likely be in memory and fast.
39
+ * @param ids The keys for the entries to prefetch
40
+ **/
41
+ prefetch(ids: K[], callback?: Function): Promise<void>
42
+
43
+ /**
44
+ * Asynchronously get the values stored by the given ids and return the
45
+ * values in array corresponding to the array of ids.
46
+ * @param ids The keys for the entries to get
47
+ **/
48
+ getMany(ids: K[], callback?: (error: any, values: V[]) => any): Promise<(V | undefined)[]>
49
+
50
+ /**
51
+ * Store the provided value, using the provided id/key
52
+ * @param id The key for the entry
53
+ * @param value The value to store
54
+ **/
55
+ put(id: K, value: V): Promise<boolean>
56
+ /**
57
+ * Store the provided value, using the provided id/key and version number, and optionally the required
58
+ * existing version
59
+ * @param id The key for the entry
60
+ * @param value The value to store
61
+ * @param version The version number to assign to this entry
62
+ * @param ifVersion If provided the put will only succeed if the previous version number matches this (atomically checked)
63
+ **/
64
+ put(id: K, value: V, version: number, ifVersion?: number): Promise<boolean>
65
+ /**
66
+ * Remove the entry with the provided id/key
67
+ * @param id The key for the entry to remove
68
+ **/
69
+ remove(id: K): Promise<boolean>
70
+ /**
71
+ * Remove the entry with the provided id/key, conditionally based on the provided existing version number
72
+ * @param id The key for the entry to remove
73
+ * @param ifVersion If provided the remove will only succeed if the previous version number matches this (atomically checked)
74
+ **/
75
+ remove(id: K, ifVersion: number): Promise<boolean>
76
+ /**
77
+ * Remove the entry with the provided id/key and value (mainly used for dupsort databases) and optionally the required
78
+ * existing version
79
+ * @param id The key for the entry to remove
80
+ * @param valueToRemove The value for the entry to remove
81
+ **/
82
+ remove(id: K, valueToRemove: V): Promise<boolean>
83
+ /**
84
+ * Syncronously store the provided value, using the provided id/key, will return after the data has been written.
85
+ * @param id The key for the entry
86
+ * @param value The value to store
87
+ **/
88
+ putSync(id: K, value: V): void
89
+ /**
90
+ * Syncronously store the provided value, using the provided id/key and version number
91
+ * @param id The key for the entry
92
+ * @param value The value to store
93
+ * @param version The version number to assign to this entry
94
+ **/
95
+ putSync(id: K, value: V, version: number): void
96
+ /**
97
+ * Syncronously store the provided value, using the provided id/key and options
98
+ * @param id The key for the entry
99
+ * @param value The value to store
100
+ * @param options The version number to assign to this entry
101
+ **/
102
+ putSync(id: K, value: V, options: PutOptions): void
103
+ /**
104
+ * Syncronously remove the entry with the provided id/key
105
+ * existing version
106
+ * @param id The key for the entry to remove
107
+ **/
108
+ removeSync(id: K): boolean
109
+ /**
110
+ * Synchronously remove the entry with the provided id/key and value (mainly used for dupsort databases)
111
+ * existing version
112
+ * @param id The key for the entry to remove
113
+ * @param valueToRemove The value for the entry to remove
114
+ **/
115
+ removeSync(id: K, valueToRemove: V): boolean
116
+ /**
117
+ * Get all the values for the given key (for dupsort databases)
118
+ * existing version
119
+ * @param key The key for the entry to remove
120
+ * @param options The options for the iterator
121
+ **/
122
+ getValues(key: K, options?: RangeOptions): ArrayLikeIterable<V>
123
+ /**
124
+ * Get the count of all the values for the given key (for dupsort databases)
125
+ * existing version
126
+ * @param options The options for the range/iterator
127
+ **/
128
+ getValuesCount(key: K, options?: RangeOptions): number
129
+ /**
130
+ * Get all the unique keys for the given range
131
+ * existing version
132
+ * @param options The options for the range/iterator
133
+ **/
134
+ getKeys(options?: RangeOptions): ArrayLikeIterable<K>
135
+ /**
136
+ * Get the count of all the unique keys for the given range
137
+ * existing version
138
+ * @param options The options for the range/iterator
139
+ **/
140
+ getKeysCount(options?: RangeOptions): number
141
+ /**
142
+ * Get all the entries for the given range
143
+ * existing version
144
+ * @param options The options for the range/iterator
145
+ **/
146
+ getRange(options?: RangeOptions): ArrayLikeIterable<{ key: K, value: V, version?: number }>
147
+ /**
148
+ * Get the count of all the entries for the given range
149
+ * existing version
150
+ * @param options The options for the range/iterator
151
+ **/
152
+ getCount(options?: RangeOptions): number
153
+ /**
154
+ * @deprecated since version 2.0, use transaction() instead
155
+ */
156
+ transactionAsync<T>(action: () => T): T
157
+ /**
158
+ * Execute a transaction asyncronously, running all the actions within the action callback in the transaction,
159
+ * and committing the transaction after the action callback completes.
160
+ * existing version
161
+ * @param action The function to execute within the transaction
162
+ **/
163
+ transaction<T>(action: () => T): Promise<T>
164
+ /**
165
+ * Execute a transaction syncronously, running all the actions within the action callback in the transaction,
166
+ * and committing the transaction after the action callback completes.
167
+ * existing version
168
+ * @param action The function to execute within the transaction
169
+ * @params flags Additional flags specifying transaction behavior, this is optional and defaults to abortable, synchronous commits that are flushed to disk before returning
170
+ **/
171
+ transactionSync<T>(action: () => T, flags?: TransactionFlags): T
172
+ /**
173
+ * Execute a transaction asyncronously, running all the actions within the action callback in the transaction,
174
+ * and committing the transaction after the action callback completes.
175
+ * existing version
176
+ * @param action The function to execute within the transaction
177
+ **/
178
+ childTransaction<T>(action: () => T): Promise<T>
179
+ /**
180
+ * Execute a set of write operations that will all be batched together in next queued asynchronous transaction.
181
+ * @param action The function to execute with a set of write operations.
182
+ **/
183
+ batch<T>(action: () => any): Promise<boolean>
184
+ /**
185
+ * Execute writes actions that are all conditionally dependent on the entry with the provided key having the provided
186
+ * version number (checked atomically).
187
+ * @param id Key of the entry to check
188
+ * @param ifVersion The require version number of the entry for all actions to succeed
189
+ * @param action The function to execute with actions that will be dependent on this condition
190
+ **/
191
+ ifVersion(id: K, ifVersion: number, action: () => any): Promise<boolean>
192
+ /**
193
+ * Execute writes actions that are all conditionally dependent on the entry with the provided key
194
+ * not existing (checked atomically).
195
+ * @param id Key of the entry to check
196
+ * @param action The function to execute with actions that will be dependent on this condition
197
+ **/
198
+ ifNoExists(id: K, action: () => any): Promise<boolean>
199
+ /**
200
+ * Check if an entry for the provided key exists
201
+ * @param id Key of the entry to check
202
+ */
203
+ doesExist(key: K): boolean
204
+ /**
205
+ * Check if an entry for the provided key/value exists
206
+ * @param id Key of the entry to check
207
+ * @param value Value of the entry to check
208
+ */
209
+ doesExist(key: K, value: V): boolean
210
+ /**
211
+ * Check if an entry for the provided key exists with the expected version
212
+ * @param id Key of the entry to check
213
+ * @param version Expected version
214
+ */
215
+ doesExist(key: K, version: number): boolean
216
+ /**
217
+ * @deprecated since version 2.0, use drop() or dropSync() instead
218
+ */
219
+ deleteDB(): Promise<void>
220
+ /**
221
+ * Delete this database/store (asynchronously).
222
+ **/
223
+ drop(): Promise<void>
224
+ /**
225
+ * Synchronously delete this database/store.
226
+ **/
227
+ dropSync(): void
228
+ /**
229
+ * @deprecated since version 2.0, use clearAsync() or clearSync() instead
230
+ */
231
+ clear(): Promise<void>
232
+ /**
233
+ * Asynchronously clear all the entries from this database/store.
234
+ **/
235
+ clearAsync(): Promise<void>
236
+ /**
237
+ * Synchronously clear all the entries from this database/store.
238
+ **/
239
+ clearSync(): void
240
+ /** A promise-like object that resolves when previous writes have been committed. */
241
+ committed: Promise<boolean>
242
+ /** A promise-like object that resolves when previous writes have been committed and fully flushed/synced to disk/storage. */
243
+ flushed: Promise<boolean>
244
+ /**
245
+ * Check the reader locks and remove any stale reader locks. Returns the number of stale locks that were removed.
246
+ **/
247
+ readerCheck(): number
248
+ /**
249
+ * Returns a string that describes all the current reader locks, useful for debugging if reader locks aren't being removed.
250
+ **/
251
+ readerList(): string
252
+ /**
253
+ * Returns statistics about the current database
254
+ **/
255
+ getStats(): {}
256
+ /**
257
+ * Explicitly force the read transaction to reset to the latest snapshot/version of the database
258
+ **/
259
+ resetReadTxn(): void
260
+ /**
261
+ * Make a snapshot copy of the current database at the indicated path
262
+ **/
263
+ backup(path: string): Promise<void>
264
+ /**
265
+ * Close the current database.
266
+ **/
267
+ close(): Promise<void>
268
+ /**
269
+ * Add event listener
270
+ */
271
+ on(event: 'beforecommit' | 'aftercommit', callback: (event: any) => void): void
272
+ }
273
+ /* A constant that can be returned from a transaction to indicate that the transaction should be aborted */
274
+ export const ABORT: {};
275
+ /* A constant that can be used as a conditional versions for put and ifVersion to indicate that the write should conditional on the key/entry existing */
276
+ export const IF_EXISTS: number;
277
+ class RootDatabase<V = any, K extends Key = Key> extends Database<V, K> {
278
+ /**
279
+ * Open a database store using the provided options.
280
+ **/
281
+ openDB<OV = V, OK extends Key = K>(options: DatabaseOptions & { name: string }): Database<OV, OK>
282
+ /**
283
+ * Open a database store using the provided options.
284
+ **/
285
+ openDB<OV = V, OK extends Key = K>(dbName: string, dbOptions: DatabaseOptions): Database<OV, OK>
286
+ }
287
+ class DatabaseClass<V = any, K extends Key = Key> {
288
+ new(name: string | null, options: DatabaseOptions): Database<V, K>
289
+ }
290
+
291
+ type Key = Key[] | string | symbol | number | boolean | Uint8Array;
292
+
293
+ interface DatabaseOptions {
294
+ name?: string
295
+ cache?: boolean | {}
296
+ compression?: boolean | CompressionOptions
297
+ encoding?: 'msgpack' | 'json' | 'string' | 'binary' | 'ordered-binary'
298
+ sharedStructuresKey?: Key
299
+ useVersions?: boolean
300
+ keyEncoding?: 'uint32' | 'binary' | 'ordered-binary'
301
+ dupSort?: boolean
302
+ strictAsyncOrder?: boolean
303
+ }
304
+ interface RootDatabaseOptions extends DatabaseOptions {
305
+ /** The maximum number of databases to be able to open (there is some extra overhead if this is set very high).*/
306
+ maxDbs?: number
307
+ /** Set a longer delay (in milliseconds) to wait longer before committing writes to increase the number of writes per transaction (higher latency, but more efficient) **/
308
+ commitDelay?: number
309
+ mapSize?: number
310
+ pageSize?: number
311
+ overlappingSync?: boolean
312
+ separateFlushed?: boolean
313
+ remapChunks?: boolean
314
+ /** This provides a small performance boost (when not using useWritemap) for writes, by skipping zero'ing out malloc'ed data, but can leave application data in unused portions of the database. This is recommended unless there are concerns of database files being accessible. */
315
+ noMemInit?: boolean
316
+ /** Use writemaps, discouraged at this. This improves performance by reducing malloc calls, but it is possible for a stray pointer to corrupt data. */
317
+ useWritemap?: boolean
318
+ noSubdir?: boolean
319
+ noSync?: boolean
320
+ noMetaSync?: boolean
321
+ readOnly?: boolean
322
+ maxReaders?: number
323
+ }
324
+ interface RootDatabaseOptionsWithPath extends RootDatabaseOptions {
325
+ path: string
326
+ }
327
+ interface CompressionOptions {
328
+ threshold?: number
329
+ dictionary?: Buffer
330
+ }
331
+ interface RangeOptions {
332
+ /** Starting key for a range **/
333
+ start?: Key
334
+ /** Ending key for a range **/
335
+ end?: Key
336
+ /** Iterate through the entries in reverse order **/
337
+ reverse?: boolean
338
+ /** Include version numbers in each entry returned **/
339
+ versions?: boolean
340
+ /** The maximum number of entries to return **/
341
+ limit?: number
342
+ /** The number of entries to skip **/
343
+ offset?: number
344
+ /** Use a snapshot of the database from when the iterator started **/
345
+ snapshot?: boolean
346
+ }
347
+ interface PutOptions {
348
+ /* Append to the database using MDB_APPEND, which can be faster */
349
+ append?: boolean
350
+ /* Append to a dupsort database using MDB_APPENDDUP, which can be faster */
351
+ appendDup?: boolean
352
+ /* Perform put with MDB_NOOVERWRITE which will fail if the entry for the key already exists */
353
+ noOverwrite?: boolean
354
+ /* Perform put with MDB_NODUPDATA which will fail if the entry for the key/value already exists */
355
+ noDupData?: boolean
356
+ /* The version of the entry to set */
357
+ version?: number
358
+ }
359
+ export enum TransactionFlags {
360
+ /* Indicates that the transaction needs to be abortable */
361
+ ABORTABLE = 1,
362
+ /* Indicates that the transaction needs to be committed before returning */
363
+ SYNCHRONOUS_COMMIT = 2,
364
+ /* Indicates that the function can return before the transaction has been flushed to disk */
365
+ NO_SYNC_FLUSH = 0x10000
366
+ }
367
+ class ArrayLikeIterable<T> implements Iterable<T> {
368
+ map<U>(callback: (entry: T) => U): ArrayLikeIterable<U>
369
+ filter(callback: (entry: T) => any): ArrayLikeIterable<T>
370
+ [Symbol.iterator]() : Iterator<T>
371
+ forEach(callback: (entry: T) => any): void
372
+ asArray: T[]
373
+ }
374
+ export function getLastVersion(): number
375
+ export function compareKeys(a: Key, b: Key): number
376
+ class Binary {}
377
+ /* Wrap a Buffer/Uint8Array for direct assignment as a value bypassing any encoding, for put (and doesExist) operations.
378
+ */
379
+ export function asBinary(buffer: Uint8Array): Binary
380
+ /* Indicates if V8 accelerated functions are enabled. If this is false, some functions will be a little slower, and you may want to npm install --build-from-source to enable maximum performance */
381
+ export let v8AccelerationEnabled: boolean
382
+ /* Return database augmented with methods to better conform to levelup */
383
+ export function levelup(database: Database): Database
384
+ }
385
+ export = lmdb
package/index.js ADDED
@@ -0,0 +1,37 @@
1
+ import { EventEmitter } from 'events';
2
+ import { setExternals, setNativeFunctions, Dbi } from './native.js';
3
+ import { arch, tmpdir, platform } from 'os';
4
+ import fs from 'fs';
5
+ import { Encoder as MsgpackrEncoder } from 'msgpackr';
6
+ import { WeakLRUCache } from 'weak-lru-cache';
7
+ import * as orderedBinary from 'ordered-binary';
8
+
9
+
10
+ orderedBinary.enableNullTermination();
11
+ setExternals({
12
+ arch, fs, tmpdir, MsgpackrEncoder, WeakLRUCache, orderedBinary,
13
+ EventEmitter, os: platform(), onExit(callback) {
14
+ if (process.getMaxListeners() < process.listenerCount('exit') + 8)
15
+ process.setMaxListeners(process.listenerCount('exit') + 8);
16
+ process.on('exit', callback);
17
+ },
18
+ });
19
+ export { toBufferKey as keyValueToBuffer, compareKeys, compareKeys as compareKey, fromBufferKey as bufferToKeyValue } from 'ordered-binary';
20
+ export { ABORT, IF_EXISTS, asBinary } from './write.js';
21
+ import { ABORT, IF_EXISTS, asBinary } from './write.js';
22
+ export { levelup } from './level.js';
23
+ import { levelup } from './level.js';
24
+ export { clearKeptObjects } from './native.js';
25
+ import { nativeAddon } from './native.js';
26
+ export let { noop } = nativeAddon;
27
+ export { open, openAsClass, getLastVersion, allDbs } from './open.js';
28
+ import { toBufferKey as keyValueToBuffer, compareKeys as compareKey, fromBufferKey as bufferToKeyValue } from 'ordered-binary';
29
+ import { open, openAsClass, getLastVersion } from './open.js';
30
+ export const TransactionFlags = {
31
+ ABORTABLE: 1,
32
+ SYNCHRONOUS_COMMIT: 2,
33
+ NO_SYNC_FLUSH: 0x10000,
34
+ };
35
+ export default {
36
+ open, openAsClass, getLastVersion, compareKey, keyValueToBuffer, bufferToKeyValue, ABORT, IF_EXISTS, asBinary, levelup, TransactionFlags
37
+ };
package/keys.js ADDED
@@ -0,0 +1,102 @@
1
+ import { getAddress, orderedBinary } from './native.js';
2
+
3
+ const writeUint32Key = (key, target, start) => {
4
+ (target.dataView || (target.dataView = new DataView(target.buffer, 0, target.length))).setUint32(start, key, true);
5
+ return start + 4;
6
+ };
7
+ const readUint32Key = (target, start) => {
8
+ return (target.dataView || (target.dataView = new DataView(target.buffer, 0, target.length))).getUint32(start, true);
9
+ };
10
+ const writeBufferKey = (key, target, start) => {
11
+ target.set(key, start);
12
+ return key.length + start;
13
+ };
14
+ const Uint8ArraySlice = Uint8Array.prototype.slice;
15
+ const readBufferKey = (target, start, end) => {
16
+ return Uint8ArraySlice.call(target, start, end);
17
+ };
18
+
19
+ export function applyKeyHandling(store) {
20
+ if (store.encoding == 'ordered-binary') {
21
+ store.encoder = store.decoder = {
22
+ writeKey: orderedBinary.writeKey,
23
+ readKey: orderedBinary.readKey,
24
+ };
25
+ }
26
+ if (store.encoder && store.encoder.writeKey && !store.encoder.encode) {
27
+ store.encoder.encode = function(value) {
28
+ return saveKey(value, this.writeKey, false, store.maxKeySize);
29
+ };
30
+ }
31
+ if (store.decoder && store.decoder.readKey && !store.decoder.decode) {
32
+ store.decoder.decode = function(buffer) { return this.readKey(buffer, 0, buffer.length); };
33
+ store.decoderCopies = true;
34
+ }
35
+ if (store.keyIsUint32 || store.keyEncoding == 'uint32') {
36
+ store.writeKey = writeUint32Key;
37
+ store.readKey = readUint32Key;
38
+ } else if (store.keyIsBuffer || store.keyEncoding == 'binary') {
39
+ store.writeKey = writeBufferKey;
40
+ store.readKey = readBufferKey;
41
+ } else if (store.keyEncoder) {
42
+ store.writeKey = store.keyEncoder.writeKey;
43
+ store.readKey = store.keyEncoder.readKey;
44
+ } else {
45
+ store.writeKey = orderedBinary.writeKey;
46
+ store.readKey = orderedBinary.readKey;
47
+ }
48
+ }
49
+
50
+ let saveBuffer, saveDataView = { setFloat64() {}, setUint32() {} }, saveDataAddress;
51
+ let savePosition = 8000;
52
+ let DYNAMIC_KEY_BUFFER_SIZE = 8192;
53
+ function allocateSaveBuffer() {
54
+ saveBuffer = typeof Buffer != 'undefined' ? Buffer.alloc(DYNAMIC_KEY_BUFFER_SIZE) : new Uint8Array(DYNAMIC_KEY_BUFFER_SIZE);
55
+ saveBuffer.buffer.address = getAddress(saveBuffer);
56
+ saveDataAddress = saveBuffer.buffer.address;
57
+ // TODO: Conditionally only do this for key sequences?
58
+ saveDataView.setUint32(savePosition, 0xffffffff);
59
+ saveDataView.setFloat64(savePosition + 4, saveDataAddress, true); // save a pointer from the old buffer to the new address for the sake of the prefetch sequences
60
+ saveBuffer.dataView = saveDataView = new DataView(saveBuffer.buffer, saveBuffer.byteOffset, saveBuffer.byteLength);
61
+ savePosition = 0;
62
+ }
63
+ export function saveKey(key, writeKey, saveTo, maxKeySize) {
64
+ if (savePosition > 7800) {
65
+ allocateSaveBuffer();
66
+ }
67
+ let start = savePosition;
68
+ try {
69
+ savePosition = key === undefined ? start + 4 :
70
+ writeKey(key, saveBuffer, start + 4);
71
+ } catch (error) {
72
+ saveBuffer.fill(0, start + 4); // restore zeros
73
+ if (error.name == 'RangeError') {
74
+ if (8180 - start < maxKeySize) {
75
+ allocateSaveBuffer(); // try again:
76
+ return saveKey(key, writeKey, saveTo, maxKeySize);
77
+ }
78
+ throw new Error('Key was too large, max key size is ' + maxKeySize);
79
+ } else
80
+ throw error;
81
+ }
82
+ let length = savePosition - start - 4;
83
+ if (length > maxKeySize) {
84
+ throw new Error('Key of size ' + length + ' was too large, max key size is ' + maxKeySize);
85
+ }
86
+ if (savePosition >= 8160) { // need to reserve enough room at the end for pointers
87
+ savePosition = start // reset position
88
+ allocateSaveBuffer(); // try again:
89
+ return saveKey(key, writeKey, saveTo, maxKeySize);
90
+ }
91
+ if (saveTo) {
92
+ saveDataView.setUint32(start, length, true); // save the length
93
+ saveTo.saveBuffer = saveBuffer;
94
+ savePosition = (savePosition + 12) & 0xfffffc;
95
+ return start + saveDataAddress;
96
+ } else {
97
+ saveBuffer.start = start + 4;
98
+ saveBuffer.end = savePosition;
99
+ savePosition = (savePosition + 7) & 0xfffff8; // full 64-bit word alignment since these are usually copied
100
+ return saveBuffer;
101
+ }
102
+ }
package/level.js ADDED
@@ -0,0 +1,27 @@
1
+ export function levelup(store) {
2
+ return Object.assign(Object.create(store), {
3
+ get(key, options, callback) {
4
+ let result = store.get(key);
5
+ if (typeof options == 'function')
6
+ callback = options;
7
+ if (callback) {
8
+ if (result === undefined)
9
+ callback(new NotFoundError());
10
+ else
11
+ callback(null, result);
12
+ } else {
13
+ if (result === undefined)
14
+ return Promise.reject(new NotFoundError());
15
+ else
16
+ return Promise.resolve(result);
17
+ }
18
+ },
19
+ });
20
+ }
21
+ class NotFoundError extends Error {
22
+ constructor(message) {
23
+ super(message);
24
+ this.name = 'NotFoundError';
25
+ this.notFound = true;
26
+ }
27
+ }
package/native.js ADDED
@@ -0,0 +1,82 @@
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, createBufferForAddress, clearKeptObjects, setGlobalBuffer, arch, fs, os, onExit, tmpdir, lmdbError, path, EventEmitter, orderedBinary, MsgpackrEncoder, WeakLRUCache, setEnvMap, getEnvMap, getByBinary, detachBuffer, write, position, iterate, prefetch, resetTxn, getCurrentValue, getCurrentShared, getStringByBinary, getSharedByBinary, compress;
5
+
6
+ path = pathModule;
7
+ let dirName = (typeof __dirname == 'string' ? __dirname : // for bun, which doesn't have fileURLToPath
8
+ dirname(fileURLToPath(import.meta.url))).replace(/dist$/, ''); // for node, which doesn't have __dirname in ESM
9
+ export let nativeAddon = loadNAPI(dirName);
10
+
11
+ if (process.isBun) {
12
+ const { linkSymbols, FFIType } = require('bun:ffi');
13
+ let lmdbLib = linkSymbols({
14
+ getByBinary: {
15
+ args: [FFIType.f64, FFIType.u32],
16
+ returns: FFIType.u32,
17
+ ptr: nativeAddon.getByBinaryPtr
18
+ },
19
+ iterate: {
20
+ args: [FFIType.f64],
21
+ returns: FFIType.i32,
22
+ ptr: nativeAddon.iteratePtr,
23
+ },
24
+ position: {
25
+ args: [FFIType.f64, FFIType.u32, FFIType.u32, FFIType.u32, FFIType.f64],
26
+ returns: FFIType.i32,
27
+ ptr: nativeAddon.positionPtr,
28
+ },
29
+ write: {
30
+ args: [FFIType.f64, FFIType.f64],
31
+ returns: FFIType.i32,
32
+ ptr: nativeAddon.writePtr,
33
+ },
34
+ resetTxn: {
35
+ args: [FFIType.f64],
36
+ returns: FFIType.void,
37
+ ptr: nativeAddon.resetTxnPtr,
38
+ }
39
+ });
40
+ for (let key in lmdbLib.symbols) {
41
+ nativeAddon[key] = lmdbLib.symbols[key].native;
42
+ }
43
+ }
44
+ setNativeFunctions(nativeAddon);
45
+
46
+ export function setNativeFunctions(externals) {
47
+ Env = externals.Env;
48
+ Txn = externals.Txn;
49
+ Dbi = externals.Dbi;
50
+ Compression = externals.Compression;
51
+ getAddress = externals.getAddress;
52
+ createBufferForAddress = externals.createBufferForAddress;
53
+ clearKeptObjects = externals.clearKeptObjects || function() {};
54
+ getByBinary = externals.getByBinary;
55
+ detachBuffer = externals.detachBuffer;
56
+ setGlobalBuffer = externals.setGlobalBuffer;
57
+ prefetch = externals.prefetch;
58
+ iterate = externals.iterate;
59
+ position = externals.position;
60
+ resetTxn = externals.resetTxn;
61
+ getCurrentValue = externals.getCurrentValue;
62
+ getCurrentShared = externals.getCurrentShared;
63
+ getStringByBinary = externals.getStringByBinary;
64
+ getSharedByBinary = externals.getSharedByBinary;
65
+ write = externals.write;
66
+ compress = externals.compress;
67
+ Cursor = externals.Cursor;
68
+ lmdbError = externals.lmdbError;
69
+ if (externals.tmpdir)
70
+ tmpdir = externals.tmpdir
71
+ }
72
+ export function setExternals(externals) {
73
+ arch = externals.arch;
74
+ fs = externals.fs;
75
+ EventEmitter = externals.EventEmitter;
76
+ orderedBinary = externals.orderedBinary;
77
+ MsgpackrEncoder = externals.MsgpackrEncoder;
78
+ WeakLRUCache = externals.WeakLRUCache;
79
+ tmpdir = externals.tmpdir;
80
+ os = externals.os;
81
+ onExit = externals.onExit;
82
+ }
package/node-index.js ADDED
@@ -0,0 +1,28 @@
1
+ import { createRequire } from 'module';
2
+ import { setRequire } from './open.js';
3
+ import { nativeAddon, setNativeFunctions } from './native.js';
4
+ import { setFlagsFromString } from 'v8';
5
+ setRequire(createRequire(import.meta.url));
6
+ export let v8AccelerationEnabled = false
7
+
8
+ let versions = process.versions;
9
+ let [ majorVersion, minorVersion ] = versions.node.split('.')
10
+
11
+ if (versions.v8 && +majorVersion == nativeAddon.version.nodeCompiledVersion) {
12
+ let v8Funcs = {};
13
+ let fastApiCalls = (majorVersion == 17 || majorVersion == 18 || majorVersion == 16 && minorVersion > 6) && !process.env.DISABLE_TURBO_CALLS;
14
+ if (fastApiCalls)
15
+ setFlagsFromString('--turbo-fast-api-calls')
16
+ nativeAddon.enableDirectV8(v8Funcs, fastApiCalls);
17
+ Object.assign(nativeAddon, v8Funcs);
18
+ v8AccelerationEnabled = true;
19
+ } else if (majorVersion == 14) {
20
+ // node v14 only has ABI compatibility with node v16 for zero-arg clearKeptObjects
21
+ let v8Funcs = {};
22
+ nativeAddon.enableDirectV8(v8Funcs, false);
23
+ nativeAddon.clearKeptObjects = v8Funcs.clearKeptObjects;
24
+ }
25
+ setNativeFunctions(nativeAddon);
26
+
27
+ export * from './index.js'
28
+ export { default } from './index.js'