-
Notifications
You must be signed in to change notification settings - Fork 39
/
lmdb-js.h
590 lines (488 loc) · 16.5 KB
/
lmdb-js.h
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
// This file is part of lmdb-js
// Copyright (c) 2013-2017 Timur Kristóf
// Licensed to you under the terms of the MIT license
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#ifndef NODE_LMDB_H
#define NODE_LMDB_H
#include <vector>
#include <algorithm>
#include <napi.h>
#include <node_api.h>
#include "lmdb.h"
#include "lz4.h"
#ifdef MDB_RPAGE_CACHE
#include "chacha8.h"
#endif
using namespace Napi;
#ifndef __CPTHREAD_H__
#define __CPTHREAD_H__
#ifdef _WIN32
# include <windows.h>
#else
# include <pthread.h>
#endif
#ifdef _WIN32
typedef CRITICAL_SECTION pthread_mutex_t;
typedef void pthread_mutexattr_t;
typedef void pthread_condattr_t;
typedef HANDLE pthread_t;
typedef CONDITION_VARIABLE pthread_cond_t;
#endif
#define NAPI_FUNCTION(name) napi_value name(napi_env env, napi_callback_info info)
#define ARGS(count) napi_value returnValue;\
size_t argc = count;\
napi_value args[count];\
napi_get_cb_info(env, info, &argc, args, NULL, NULL);
#define GET_UINT32_ARG(target, position) napi_get_value_uint32(env, args[position], (uint32_t*) &target)
#define GET_INT64_ARG(target, position) napi_get_value_int64(env, args[position], (int64_t*) &target)
#define RETURN_UINT32(value) { napi_create_uint32(env, value, &returnValue); return returnValue; }
#define RETURN_INT32(value) { napi_create_int32(env, value, &returnValue); return returnValue; }
#define RETURN_INT64(value) { napi_create_int64(env, value, &returnValue); return returnValue; }
#define RETURN_UNDEFINED { napi_get_undefined(env, &returnValue); return returnValue; }
#define THROW_ERROR(message) { napi_throw_error(env, NULL, message); napi_get_undefined(env, &returnValue); return returnValue; }
#define EXPORT_NAPI_FUNCTION(name, func) { napi_property_descriptor desc = { name, 0, func, 0, 0, 0, (napi_property_attributes) (napi_writable | napi_configurable), 0 }; napi_define_properties(env, exports, 1, &desc); }
#ifdef _WIN32
int pthread_mutex_init(pthread_mutex_t *mutex, pthread_mutexattr_t *attr);
int pthread_mutex_destroy(pthread_mutex_t *mutex);
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_unlock(pthread_mutex_t *mutex);
int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *attr);
int pthread_cond_destroy(pthread_cond_t *cond);
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
int pthread_cond_signal(pthread_cond_t *cond);
int pthread_cond_broadcast(pthread_cond_t *cond);
#endif
int cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, uint64_t ns);
#endif /* __CPTHREAD_H__ */
class Logging {
public:
static int debugLogging;
static int initLogging();
};
enum class LmdbKeyType {
// Invalid key (used internally by lmdb-js)
InvalidKey = -1,
// Default key (used internally by lmdb-js)
DefaultKey = 0,
// UCS-2/UTF-16 with zero terminator - Appears to V8 as string
StringKey = 1,
// LMDB fixed size integer key with 32 bit keys - Appearts to V8 as an Uint32
Uint32Key = 2,
// LMDB default key format - Appears to V8 as node::Buffer
BinaryKey = 3,
};
enum class KeyCreation {
Reset = 0,
Continue = 1,
InArray = 2,
};
const int THEAD_MEMORY_THRESHOLD = 4000;
class TxnWrap;
class DbiWrap;
class EnvWrap;
class CursorWrap;
class Compression;
// Exports misc stuff to the module
void setupExportMisc(Env env, Object exports);
// Helper callback
typedef void (*argtokey_callback_t)(MDB_val &key);
void consoleLog(Value val);
void consoleLog(const char *msg);
void consoleLogN(int n);
void setFlagFromValue(int *flags, int flag, const char *name, bool defaultValue, Object options);
void writeValueToEntry(const Value &str, MDB_val *val);
LmdbKeyType keyTypeFromOptions(const Value &val, LmdbKeyType defaultKeyType = LmdbKeyType::DefaultKey);
bool getVersionAndUncompress(MDB_val &data, DbiWrap* dw);
int compareFast(const MDB_val *a, const MDB_val *b);
Value setGlobalBuffer(const CallbackInfo& info);
Value lmdbError(const CallbackInfo& info);
napi_value createBufferForAddress(napi_env env, napi_callback_info info);
napi_value getViewAddress(napi_env env, napi_callback_info info);
napi_value detachBuffer(napi_env env, napi_callback_info info);
Value getAddress(const CallbackInfo& info);
Value lmdbNativeFunctions(const CallbackInfo& info);
Value enableDirectV8(const CallbackInfo& info);
#ifndef thread_local
#ifdef __GNUC__
# define thread_local __thread
#elif __STDC_VERSION__ >= 201112L
# define thread_local _Thread_local
#elif defined(_MSC_VER)
# define thread_local __declspec(thread)
#else
# define thread_local
#endif
#endif
bool valToBinaryFast(MDB_val &data, DbiWrap* dw);
Value valToUtf8(Env env, MDB_val &data);
Value valToString(MDB_val &data);
Value valToStringUnsafe(MDB_val &data);
Value valToBinary(MDB_val &data);
Value valToBinaryUnsafe(MDB_val &data, DbiWrap* dw, Env env);
int putWithVersion(MDB_txn* txn,
MDB_dbi dbi,
MDB_val * key,
MDB_val * data,
unsigned int flags, double version);
Napi::Value throwLmdbError(Napi::Env env, int rc);
Napi::Value throwError(Napi::Env env, const char* message);
class TxnWrap;
class DbiWrap;
class EnvWrap;
class CursorWrap;
class SharedEnv {
public:
MDB_env* env;
uint64_t dev;
uint64_t inode;
int count;
};
const int INTERRUPT_BATCH = 9998;
const int ALLOW_COMMIT = 9997;
const int RESTART_WORKER_TXN = 9999;
const int RESUME_BATCH = 9996;
const int USER_HAS_LOCK = 9995;
const int SEPARATE_FLUSHED = 1;
const int DELETE_ON_CLOSE = 2;
class WriteWorker {
public:
WriteWorker(MDB_env* env, EnvWrap* envForTxn, uint32_t* instructions);
void Write();
MDB_txn* txn;
MDB_txn* AcquireTxn(int* flags);
void UnlockTxn();
int WaitForCallbacks(MDB_txn** txn, bool allowCommit, uint32_t* target);
virtual void ReportError(const char* error);
virtual void SendUpdate();
int interruptionStatus;
bool finishedProgress;
bool hasError;
EnvWrap* envForTxn;
virtual ~WriteWorker();
uint32_t* instructions;
int progressStatus;
MDB_env* env;
static int DoWrites(MDB_txn* txn, EnvWrap* envForTxn, uint32_t* instruction, WriteWorker* worker);
};
class AsyncWriteWorker : public WriteWorker, public AsyncProgressWorker<char> {
public:
AsyncWriteWorker(MDB_env* env, EnvWrap* envForTxn, uint32_t* instructions, const Function& callback);
void Execute(const AsyncProgressWorker::ExecutionProgress& execution);
void OnProgress(const char* data, size_t count);
void OnOK();
void ReportError(const char* error);
void SendUpdate();
private:
ExecutionProgress* executionProgress;
};
class TxnTracked {
public:
TxnTracked(MDB_txn *txn, unsigned int flags);
~TxnTracked();
unsigned int flags;
MDB_txn *txn;
TxnTracked *parent;
};
/*
`Env`
Represents a database environment.
(Wrapper for `MDB_env`)
*/
typedef struct env_tracking_t {
pthread_mutex_t* envsLock;
std::vector<SharedEnv> envs;
} env_tracking_t;
class EnvWrap : public ObjectWrap<EnvWrap> {
private:
// List of open read transactions
std::vector<TxnWrap*> readTxns;
static env_tracking_t* initTracking();
napi_env napiEnv;
// compression settings and space
Compression *compression;
static thread_local std::vector<EnvWrap*> openEnvWraps;
// Cleans up stray transactions
void cleanupStrayTxns();
friend class TxnWrap;
friend class DbiWrap;
public:
EnvWrap(const CallbackInfo&);
~EnvWrap();
// The wrapped object
MDB_env *env;
// Current write transaction
static env_tracking_t* envTracking;
TxnWrap *currentWriteTxn;
TxnTracked *writeTxn;
pthread_mutex_t* writingLock;
pthread_cond_t* writingCond;
std::vector<AsyncWorker*> workers;
MDB_txn* currentReadTxn;
WriteWorker* writeWorker;
bool readTxnRenewed;
unsigned int jsFlags;
char* keyBuffer;
int pageSize;
MDB_txn* getReadTxn();
// Sets up exports for the Env constructor
static void setupExports(Napi::Env env, Object exports);
void closeEnv();
int openEnv(int flags, int jsFlags, const char* path, char* keyBuffer, Compression* compression, int maxDbs,
int maxReaders, mdb_size_t mapSize, int pageSize, char* encryptionKey);
/*
Gets statistics about the database environment.
*/
Napi::Value stat(const CallbackInfo& info);
/*
Gets statistics about the free space database
*/
Napi::Value freeStat(const CallbackInfo& info);
/*
Gets information about the database environment.
*/
Napi::Value info(const CallbackInfo& info);
/*
Check for stale readers
*/
Napi::Value readerCheck(const CallbackInfo& info);
/*
Print a list of readers
*/
Napi::Value readerList(const CallbackInfo& info);
/*
Opens the database environment with the specified options. The options will be used to configure the environment before opening it.
(Wrapper for `mdb_env_open`)
Parameters:
* Options object that contains possible configuration options.
Possible options are:
* maxDbs: the maximum number of named databases you can have in the environment (default is 1)
* maxReaders: the maximum number of concurrent readers of the environment (default is 126)
* mapSize: maximal size of the memory map (the full environment) in bytes (default is 10485760 bytes)
* path: path to the database environment
*/
Napi::Value open(const CallbackInfo& info);
Napi::Value getMaxKeySize(const CallbackInfo& info);
/*
Copies the database environment to a file.
(Wrapper for `mdb_env_copy2`)
Parameters:
* path - Path to the target file
* compact (optional) - Copy using compact setting
* callback - Callback when finished (this is performed asynchronously)
*/
Napi::Value copy(const CallbackInfo& info);
/*
Closes the database environment.
(Wrapper for `mdb_env_close`)
*/
Napi::Value close(const CallbackInfo& info);
/*
Starts a new transaction in the environment.
(Wrapper for `mdb_txn_begin`)
Parameters:
* Options object that contains possible configuration options.
Possible options are:
* readOnly: if true, the transaction is read-only
*/
Napi::Value beginTxn(const CallbackInfo& info);
Napi::Value commitTxn(const CallbackInfo& info);
Napi::Value abortTxn(const CallbackInfo& info);
/*
Flushes all data to the disk asynchronously.
(Asynchronous wrapper for `mdb_env_sync`)
Parameters:
* Callback to be executed after the sync is complete.
*/
Napi::Value sync(const CallbackInfo& info);
/*
Performs a set of operations asynchronously, automatically wrapping it in its own transaction
Parameters:
* Callback to be executed after the sync is complete.
*/
Napi::Value startWriting(const CallbackInfo& info);
static napi_value compress(napi_env env, napi_callback_info info);
static napi_value write(napi_env env, napi_callback_info info);
static napi_value onExit(napi_env env, napi_callback_info info);
Napi::Value resetCurrentReadTxn(const CallbackInfo& info);
};
const int TXN_ABORTABLE = 1;
const int TXN_SYNCHRONOUS_COMMIT = 2;
const int TXN_FROM_WORKER = 4;
/*
`Txn`
Represents a transaction running on a database environment.
(Wrapper for `MDB_txn`)
*/
class TxnWrap : public ObjectWrap<TxnWrap> {
private:
// Reference to the MDB_env of the wrapped MDB_txn
MDB_env *env;
// Environment wrapper of the current transaction
EnvWrap *ew;
// parent TW, if it is exists
TxnWrap *parentTw;
// Flags used with mdb_txn_begin
unsigned int flags;
friend class CursorWrap;
friend class DbiWrap;
friend class EnvWrap;
public:
TxnWrap(const CallbackInfo& info);
~TxnWrap();
// The wrapped object
MDB_txn *txn;
// Remove the current TxnWrap from its EnvWrap
void removeFromEnvWrap();
int begin(EnvWrap *ew, unsigned int flags);
/*
Commits the transaction.
(Wrapper for `mdb_txn_commit`)
*/
Napi::Value commit(const CallbackInfo& info);
/*
Aborts the transaction.
(Wrapper for `mdb_txn_abort`)
*/
Napi::Value abort(const CallbackInfo& info);
/*
Aborts a read-only transaction but makes it renewable with `renew`.
(Wrapper for `mdb_txn_reset`)
*/
void reset();
/*
Renews a read-only transaction after it has been reset.
(Wrapper for `mdb_txn_renew`)
*/
Napi::Value renew(const CallbackInfo& info);
static void setupExports(Napi::Env env, Object exports);
};
const int HAS_VERSIONS = 0x1000;
/*
`Dbi`
Represents a database instance in an environment.
(Wrapper for `MDB_dbi`)
*/
class DbiWrap : public ObjectWrap<DbiWrap> {
public:
// Tells how keys should be treated
LmdbKeyType keyType;
// Stores flags set when opened
int flags;
// The wrapped object
MDB_dbi dbi;
// Reference to the MDB_env of the wrapped MDB_dbi
MDB_env *env;
// The EnvWrap object of the current Dbi
EnvWrap *ew;
// Whether the Dbi was opened successfully
bool isOpen;
// compression settings and space
Compression* compression;
// versions stored in data
bool hasVersions;
// current unsafe buffer for this db
bool getFast;
friend class TxnWrap;
friend class CursorWrap;
friend class EnvWrap;
DbiWrap(const CallbackInfo& info);
~DbiWrap();
/*
Closes the database instance.
Wrapper for `mdb_dbi_close`)
*/
Napi::Value close(const CallbackInfo& info);
/*
Drops the database instance, either deleting it completely (default) or just freeing its pages.
Parameters:
* Options object that contains possible configuration options.
Possible options are:
* justFreePages - indicates that the database pages need to be freed but the database shouldn't be deleted
*/
Napi::Value drop(const CallbackInfo& info);
Napi::Value stat(const CallbackInfo& info);
int prefetch(uint32_t* keys);
int open(int flags, char* name, bool hasVersions, LmdbKeyType keyType, Compression* compression);
int32_t doGetByBinary(uint32_t keySize);
static void setupExports(Napi::Env env, Object exports);
};
class Compression : public ObjectWrap<Compression> {
public:
char* dictionary; // dictionary to use to decompress
char* compressDictionary; // separate dictionary to use to compress since the decompression dictionary can move around in the main thread
unsigned int dictionarySize;
char* decompressTarget;
unsigned int decompressSize;
unsigned int compressionThreshold;
// compression acceleration (defaults to 1)
int acceleration;
static thread_local LZ4_stream_t* stream;
void decompress(MDB_val& data, bool &isValid, bool canAllocate);
argtokey_callback_t compress(MDB_val* value, argtokey_callback_t freeValue);
int compressInstruction(EnvWrap* env, double* compressionAddress);
Napi::Value ctor(const CallbackInfo& info);
Napi::Value setBuffer(const CallbackInfo& info);
Compression(const CallbackInfo& info);
friend class EnvWrap;
friend class DbiWrap;
//NAN_METHOD(Compression::startCompressing);
static void setupExports(Napi::Env env, Object exports);
};
/*
`Cursor`
Represents a cursor instance that is assigned to a transaction and a database instance
(Wrapper for `MDB_cursor`)
*/
class CursorWrap : public ObjectWrap<CursorWrap> {
private:
// Key/data pair where the cursor is at, and ending key
MDB_val key, data, endKey;
// Free function for the current key
argtokey_callback_t freeKey;
public:
MDB_cursor_op iteratingOp;
MDB_cursor *cursor;
// Stores how key is represented
LmdbKeyType keyType;
int flags;
DbiWrap *dw;
MDB_txn *txn;
// The wrapped object
CursorWrap(MDB_cursor* cursor);
CursorWrap(const CallbackInfo& info);
~CursorWrap();
// Sets up exports for the Cursor constructor
static void setupExports(Napi::Env env, Object exports);
/*
Closes the cursor.
(Wrapper for `mdb_cursor_close`)
Parameters:
* Transaction object
* Database instance object
*/
Napi::Value close(const CallbackInfo& info);
/*
Deletes the key/data pair to which the cursor refers.
(Wrapper for `mdb_cursor_del`)
*/
Napi::Value del(const CallbackInfo& info);
int returnEntry(int lastRC, MDB_val &key, MDB_val &data);
int32_t doPosition(uint32_t offset, uint32_t keySize, uint64_t endKeyAddress);
//Value getStringByBinary(const CallbackInfo& info);
};
#endif // NODE_LMDB_H