-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.d.ts
363 lines (355 loc) · 13.4 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
declare namespace lmdb {
export function open<V = any, K extends Key = Key>(path: string, options: RootDatabaseOptions): RootDatabase<V, K>
export function open<V = any, K extends Key = Key>(options: RootDatabaseOptionsWithPath): RootDatabase<V, K>
class Database<V = any, K extends Key = Key> {
/**
* Get the value stored by given id/key
* @param id The key for the entry
**/
get(id: K): V | undefined
/**
* Get the entry stored by given id/key, which includes both the value and the version number (if available)
* @param id The key for the entry
**/
getEntry(id: K): {
value: V
version?: number
} | undefined
/**
* Get the value stored by given id/key in binary format, as a Buffer
* @param id The key for the entry
**/
getBinary(id: K): Buffer | undefined
/**
* Get the value stored by given id/key in binary format, as a temporary Buffer.
* This is faster, but the data is only valid until the next get operation (then it will be overwritten).
* @param id The key for the entry
**/
getBinaryFast(id: K): Buffer | undefined
/**
* Asynchronously fetch the values stored by the given ids and accesses all
* pages to ensure that any hard page faults and disk I/O are performed
* asynchronously in a separate thread. Once completed, synchronous
* gets to the same entries will most likely be in memory and fast.
* @param ids The keys for the entries to prefetch
**/
prefetch(ids: K[], callback?: Function): Promise<void>
/**
* Asynchronously get the values stored by the given ids and return the
* values in array corresponding to the array of ids.
* @param ids The keys for the entries to get
**/
getMany(ids: K[], callback?: (error: any, values: V[]) => any): Promise<(V | undefined)[]>
/**
* Store the provided value, using the provided id/key
* @param id The key for the entry
* @param value The value to store
**/
put(id: K, value: V): Promise<boolean>
/**
* Store the provided value, using the provided id/key and version number, and optionally the required
* existing version
* @param id The key for the entry
* @param value The value to store
* @param version The version number to assign to this entry
* @param ifVersion If provided the put will only succeed if the previous version number matches this (atomically checked)
**/
put(id: K, value: V, version: number, ifVersion?: number): Promise<boolean>
/**
* Remove the entry with the provided id/key
* @param id The key for the entry to remove
**/
remove(id: K): Promise<boolean>
/**
* Remove the entry with the provided id/key, conditionally based on the provided existing version number
* @param id The key for the entry to remove
* @param ifVersion If provided the remove will only succeed if the previous version number matches this (atomically checked)
**/
remove(id: K, ifVersion: number): Promise<boolean>
/**
* Remove the entry with the provided id/key and value (mainly used for dupsort databases) and optionally the required
* existing version
* @param id The key for the entry to remove
* @param valueToRemove The value for the entry to remove
**/
remove(id: K, valueToRemove: V): Promise<boolean>
/**
* Syncronously store the provided value, using the provided id/key, will return after the data has been written.
* @param id The key for the entry
* @param value The value to store
**/
putSync(id: K, value: V): void
/**
* Syncronously store the provided value, using the provided id/key and version number
* @param id The key for the entry
* @param value The value to store
* @param version The version number to assign to this entry
**/
putSync(id: K, value: V, version: number): void
/**
* Syncronously store the provided value, using the provided id/key and options
* @param id The key for the entry
* @param value The value to store
* @param options The version number to assign to this entry
**/
putSync(id: K, value: V, options: PutOptions): void
/**
* Syncronously remove the entry with the provided id/key
* existing version
* @param id The key for the entry to remove
**/
removeSync(id: K): boolean
/**
* Synchronously remove the entry with the provided id/key and value (mainly used for dupsort databases)
* existing version
* @param id The key for the entry to remove
* @param valueToRemove The value for the entry to remove
**/
removeSync(id: K, valueToRemove: V): boolean
/**
* Get all the values for the given key (for dupsort databases)
* existing version
* @param key The key for the entry to remove
* @param options The options for the iterator
**/
getValues(key: K, options?: RangeOptions): ArrayLikeIterable<V>
/**
* Get the count of all the values for the given key (for dupsort databases)
* existing version
* @param options The options for the range/iterator
**/
getValuesCount(key: K, options?: RangeOptions): number
/**
* Get all the unique keys for the given range
* existing version
* @param options The options for the range/iterator
**/
getKeys(options: RangeOptions): ArrayLikeIterable<K>
/**
* Get the count of all the unique keys for the given range
* existing version
* @param options The options for the range/iterator
**/
getKeysCount(options: RangeOptions): number
/**
* Get all the entries for the given range
* existing version
* @param options The options for the range/iterator
**/
getRange(options: RangeOptions): ArrayLikeIterable<{ key: K, value: V, version?: number }>
/**
* Get the count of all the entries for the given range
* existing version
* @param options The options for the range/iterator
**/
getCount(options: RangeOptions): number
/**
* @deprecated since version 2.0, use transaction() instead
*/
transactionAsync<T>(action: () => T): T
/**
* Execute a transaction asyncronously, running all the actions within the action callback in the transaction,
* and committing the transaction after the action callback completes.
* existing version
* @param action The function to execute within the transaction
**/
transaction<T>(action: () => T): Promise<T>
/**
* Execute a transaction syncronously, running all the actions within the action callback in the transaction,
* and committing the transaction after the action callback completes.
* existing version
* @param action The function to execute within the transaction
**/
transactionSync<T>(action: () => T): T
/**
* Execute a transaction asyncronously, running all the actions within the action callback in the transaction,
* and committing the transaction after the action callback completes.
* existing version
* @param action The function to execute within the transaction
**/
childTransaction<T>(action: () => T): Promise<T>
/**
* Execute a set of write operations that will all be batched together in next queued asynchronous transaction.
* @param action The function to execute with a set of write operations.
**/
batch<T>(action: () => any): Promise<boolean>
/**
* Execute writes actions that are all conditionally dependent on the entry with the provided key having the provided
* version number (checked atomically).
* @param id Key of the entry to check
* @param ifVersion The require version number of the entry for all actions to succeed
* @param action The function to execute with actions that will be dependent on this condition
**/
ifVersion(id: K, ifVersion: number, action: () => any): Promise<boolean>
/**
* Execute writes actions that are all conditionally dependent on the entry with the provided key
* not existing (checked atomically).
* @param id Key of the entry to check
* @param action The function to execute with actions that will be dependent on this condition
**/
ifNoExists(id: K, action: () => any): Promise<boolean>
/**
* Check if an entry for the provided key exists
* @param id Key of the entry to check
*/
doesExist(key: K): boolean
/**
* Check if an entry for the provided key/value exists
* @param id Key of the entry to check
* @param value Value of the entry to check
*/
doesExist(key: K, value: V): boolean
/**
* Check if an entry for the provided key exists with the expected version
* @param id Key of the entry to check
* @param version Expected version
*/
doesExist(key: K, version: number): boolean
/**
* @deprecated since version 2.0, use drop() or dropSync() instead
*/
deleteDB(): Promise<void>
/**
* Delete this database/store (asynchronously).
**/
drop(): Promise<void>
/**
* Synchronously delete this database/store.
**/
dropSync(): void
/**
* @deprecated since version 2.0, use clearAsync() or clearSync() instead
*/
clear(): Promise<void>
/**
* Asynchronously clear all the entries from this database/store.
**/
clearAsync(): Promise<void>
/**
* Synchronously clear all the entries from this database/store.
**/
clearSync(): void
/**
* Check the reader locks and remove any stale reader locks. Returns the number of stale locks that were removed.
**/
readerCheck(): number
/**
* Returns a string that describes all the current reader locks, useful for debugging if reader locks aren't being removed.
**/
readerList(): string
/**
* Returns statistics about the current database
**/
getStats(): {}
/**
* Explicitly force the read transaction to reset to the latest snapshot/version of the database
**/
resetReadTxn(): void
/**
* Make a snapshot copy of the current database at the indicated path
**/
backup(path: string): Promise<void>
/**
* Close the current database.
**/
close(): Promise<void>
/**
* Add event listener
*/
on(event: 'beforecommit' | 'aftercommit', callback: (event: any) => void): void
}
/* A special value that can be returned from a transaction to indicate that the transaction should be aborted */
export const ABORT = 10000000000000
class RootDatabase<V = any, K extends Key = Key> extends Database<V, K> {
/**
* Open a database store using the provided options.
**/
openDB<OV = V, OK extends Key = K>(options: DatabaseOptions & { name: string }): Database<OV, OK>
/**
* Open a database store using the provided options.
**/
openDB<OV = V, OK extends Key = K>(dbName: string, dbOptions: DatabaseOptions): Database<OV, OK>
}
type Key = Key[] | string | symbol | number | boolean | Buffer;
interface DatabaseOptions {
name?: string
cache?: boolean
compression?: boolean | CompressionOptions
encoding?: 'msgpack' | 'json' | 'string' | 'binary' | 'ordered-binary'
sharedStructuresKey?: Key
useVersions?: boolean
keyEncoding?: 'uint32' | 'binary' | 'ordered-binary'
dupSort?: boolean
strictAsyncOrder?: boolean
}
interface RootDatabaseOptions extends DatabaseOptions {
/** The maximum number of databases to be able to open (there is some extra overhead if this is set very high).*/
maxDbs?: number
/** 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) **/
commitDelay?: number
asyncTransactionOrder?: 'after' | 'before' | 'strict'
mapSize?: number
pageSize?: number
remapChunks?: boolean
/** 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. */
noMemInit?: boolean
/** Use writemaps, discouraged at this. This improves performance by reducing malloc calls, but it is possible for a stray pointer to corrupt data. */
useWritemap?: boolean
noSubdir?: boolean
noSync?: boolean
noMetaSync?: boolean
readOnly?: boolean
mapAsync?: boolean
maxReaders?: number
winMemoryPriority?: 1 | 2 | 3 | 4 | 5
}
interface RootDatabaseOptionsWithPath extends RootDatabaseOptions {
path: string
}
interface CompressionOptions {
threshold?: number
dictionary?: Buffer
}
interface RangeOptions {
/** Starting key for a range **/
start?: Key
/** Ending key for a range **/
end?: Key
/** Iterate through the entries in reverse order **/
reverse?: boolean
/** Include version numbers in each entry returned **/
versions?: boolean
/** The maximum number of entries to return **/
limit?: number
/** The number of entries to skip **/
offset?: number
/** Use a snapshot of the database from when the iterator started **/
snapshot?: boolean
}
interface PutOptions {
/* Append to the database using MDB_APPEND, which can be faster */
append?: boolean
/* Append to a dupsort database using MDB_APPENDDUP, which can be faster */
appendDup?: boolean
/* Perform put with MDB_NOOVERWRITE which will fail if the entry for the key already exists */
noOverwrite?: boolean
/* Perform put with MDB_NODUPDATA which will fail if the entry for the key/value already exists */
noDupData?: boolean
/* The version of the entry to set */
version?: number
}
class ArrayLikeIterable<T> implements Iterable<T> {
map<U>(callback: (entry: T) => U): ArrayLikeIterable<U>
filter(callback: (entry: T) => any): ArrayLikeIterable<T>
[Symbol.iterator]() : Iterator<T>
forEach(callback: (entry: T) => any): void
asArray: T[]
}
export function getLastVersion(): number
export function compareKeys(a: Key, b: Key): number
class Binary {}
/* Wrap a Buffer/Uint8Array for direct assignment as a value bypassing any encoding, for put (and doesExist) operations.
*/
export function asBinary(buffer: Uint8Array): Binary
}
export = lmdb