@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
@@ -0,0 +1,64 @@
1
+ #include "lmdb-js.h"
2
+
3
+ using namespace Napi;
4
+
5
+ int Logging::initLogging() {
6
+ char* logging = getenv("LMDB_JS_LOGGING");
7
+ if (logging)
8
+ fprintf(stderr, "Start logging for lmdb-js\n");
9
+ return !!logging;
10
+ }
11
+ int Logging::debugLogging = Logging::initLogging();
12
+
13
+ Object InitAll(Napi::Env env, Object exports) {
14
+ if (Logging::debugLogging)
15
+ fprintf(stderr, "Start initialization\n");
16
+ // Initializes the module
17
+ // Export Env as constructor for EnvWrap
18
+ EnvWrap::setupExports(env, exports);
19
+
20
+ // Export Cursor as constructor for CursorWrap
21
+ CursorWrap::setupExports(env, exports);
22
+ TxnWrap::setupExports(env, exports);
23
+ DbiWrap::setupExports(env, exports);
24
+ CursorWrap::setupExports(env, exports);
25
+ Compression::setupExports(env, exports);
26
+
27
+ // Export misc things
28
+ setupExportMisc(env, exports);
29
+ if (Logging::debugLogging)
30
+ fprintf(stderr, "Finished initialization\n");
31
+ return exports;
32
+ }
33
+ NAPI_MODULE_INIT() {
34
+ Value exp = Value::From(env, exports);
35
+ return InitAll(env, exp.As<Object>());
36
+ }
37
+
38
+ #ifndef _WIN32
39
+ extern "C" void node_module_register(void* m) {
40
+ //fprintf(stderr, "This is just a dummy function to be called if node isn't there so deno can load this module\n");
41
+ }
42
+ #endif
43
+
44
+ // This file contains code from the node-lmdb project
45
+ // Copyright (c) 2013-2017 Timur Kristóf
46
+ // Licensed to you under the terms of the MIT license
47
+ //
48
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
49
+ // of this software and associated documentation files (the "Software"), to deal
50
+ // in the Software without restriction, including without limitation the rights
51
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
52
+ // copies of the Software, and to permit persons to whom the Software is
53
+ // furnished to do so, subject to the following conditions:
54
+
55
+ // The above copyright notice and this permission notice shall be included in
56
+ // all copies or substantial portions of the Software.
57
+
58
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
59
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
60
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
61
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
62
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
63
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
64
+ // THE SOFTWARE.
package/src/lmdb-js.h ADDED
@@ -0,0 +1,599 @@
1
+
2
+ // This file is part of lmdb-js
3
+ // Copyright (c) 2013-2017 Timur Kristóf
4
+ // Licensed to you under the terms of the MIT license
5
+ //
6
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ // of this software and associated documentation files (the "Software"), to deal
8
+ // in the Software without restriction, including without limitation the rights
9
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ // copies of the Software, and to permit persons to whom the Software is
11
+ // furnished to do so, subject to the following conditions:
12
+
13
+ // The above copyright notice and this permission notice shall be included in
14
+ // all copies or substantial portions of the Software.
15
+
16
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ // THE SOFTWARE.
23
+
24
+ #ifndef NODE_LMDB_H
25
+ #define NODE_LMDB_H
26
+
27
+ #include <vector>
28
+ #include <algorithm>
29
+ #include <napi.h>
30
+ #include <node_api.h>
31
+
32
+ #include "lmdb.h"
33
+ #include "lz4.h"
34
+ #ifdef MDB_RPAGE_CACHE
35
+ #include "chacha8.h"
36
+ #endif
37
+
38
+ using namespace Napi;
39
+
40
+ #ifndef __CPTHREAD_H__
41
+ #define __CPTHREAD_H__
42
+
43
+ #ifdef _WIN32
44
+ # include <windows.h>
45
+ #else
46
+ # include <pthread.h>
47
+ #endif
48
+
49
+ #ifdef _WIN32
50
+ typedef CRITICAL_SECTION pthread_mutex_t;
51
+ typedef void pthread_mutexattr_t;
52
+ typedef void pthread_condattr_t;
53
+ typedef HANDLE pthread_t;
54
+ typedef CONDITION_VARIABLE pthread_cond_t;
55
+
56
+ #endif
57
+
58
+ #define NAPI_FUNCTION(name) napi_value name(napi_env env, napi_callback_info info)
59
+ #define ARGS(count) napi_value returnValue;\
60
+ size_t argc = count;\
61
+ napi_value args[count];\
62
+ napi_get_cb_info(env, info, &argc, args, NULL, NULL);
63
+ #define GET_UINT32_ARG(target, position) napi_get_value_uint32(env, args[position], (uint32_t*) &target)
64
+ #define GET_INT64_ARG(position)\
65
+ int64_t i64;\
66
+ napi_get_value_int64(env, args[position], &i64);
67
+ #define RETURN_UINT32(value) { napi_create_uint32(env, value, &returnValue); return returnValue; }
68
+ #define RETURN_INT32(value) { napi_create_int32(env, value, &returnValue); return returnValue; }
69
+ #define RETURN_UNDEFINED { napi_get_undefined(env, &returnValue); return returnValue; }
70
+ #define THROW_ERROR(message) { napi_throw_error(env, NULL, message); napi_get_undefined(env, &returnValue); return returnValue; }
71
+ #define EXPORT_NAPI_FUNCTION(name, func) { napi_property_descriptor desc = { name, 0, func, 0, 0, 0, (napi_property_attributes) (napi_writable | napi_configurable), 0 };\
72
+ napi_define_properties(env, exports, 1, &desc); }
73
+ #define EXPORT_FUNCTION_ADDRESS(name, func) { \
74
+ napi_value address;\
75
+ void* f = (void*) func;\
76
+ napi_create_double(env, *((double*) &f), &address);\
77
+ napi_property_descriptor desc = { name, 0, 0, 0, 0, address, (napi_property_attributes) (napi_writable | napi_configurable), 0 };\
78
+ napi_define_properties(env, exports, 1, &desc); }
79
+
80
+ #ifdef _WIN32
81
+
82
+ int pthread_mutex_init(pthread_mutex_t *mutex, pthread_mutexattr_t *attr);
83
+ int pthread_mutex_destroy(pthread_mutex_t *mutex);
84
+ int pthread_mutex_lock(pthread_mutex_t *mutex);
85
+ int pthread_mutex_unlock(pthread_mutex_t *mutex);
86
+
87
+ int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *attr);
88
+ int pthread_cond_destroy(pthread_cond_t *cond);
89
+ int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
90
+ int pthread_cond_signal(pthread_cond_t *cond);
91
+ int pthread_cond_broadcast(pthread_cond_t *cond);
92
+
93
+ #endif
94
+
95
+ int cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, uint64_t ns);
96
+
97
+ #endif /* __CPTHREAD_H__ */
98
+
99
+ class Logging {
100
+ public:
101
+ static int debugLogging;
102
+ static int initLogging();
103
+ };
104
+
105
+ enum class LmdbKeyType {
106
+
107
+ // Invalid key (used internally by lmdb-js)
108
+ InvalidKey = -1,
109
+
110
+ // Default key (used internally by lmdb-js)
111
+ DefaultKey = 0,
112
+
113
+ // UCS-2/UTF-16 with zero terminator - Appears to V8 as string
114
+ StringKey = 1,
115
+
116
+ // LMDB fixed size integer key with 32 bit keys - Appearts to V8 as an Uint32
117
+ Uint32Key = 2,
118
+
119
+ // LMDB default key format - Appears to V8 as node::Buffer
120
+ BinaryKey = 3,
121
+
122
+ };
123
+ enum class KeyCreation {
124
+ Reset = 0,
125
+ Continue = 1,
126
+ InArray = 2,
127
+ };
128
+ const int THEAD_MEMORY_THRESHOLD = 4000;
129
+
130
+ class TxnWrap;
131
+ class DbiWrap;
132
+ class EnvWrap;
133
+ class CursorWrap;
134
+ class Compression;
135
+
136
+ // Exports misc stuff to the module
137
+ void setupExportMisc(Env env, Object exports);
138
+
139
+ // Helper callback
140
+ typedef void (*argtokey_callback_t)(MDB_val &key);
141
+
142
+ void consoleLog(Value val);
143
+ void consoleLog(const char *msg);
144
+ void consoleLogN(int n);
145
+ void setFlagFromValue(int *flags, int flag, const char *name, bool defaultValue, Object options);
146
+ void writeValueToEntry(const Value &str, MDB_val *val);
147
+ LmdbKeyType keyTypeFromOptions(const Value &val, LmdbKeyType defaultKeyType = LmdbKeyType::DefaultKey);
148
+ bool getVersionAndUncompress(MDB_val &data, DbiWrap* dw);
149
+ int compareFast(const MDB_val *a, const MDB_val *b);
150
+ Value setGlobalBuffer(const CallbackInfo& info);
151
+ Value lmdbError(const CallbackInfo& info);
152
+ napi_value createBufferForAddress(napi_env env, napi_callback_info info);
153
+ napi_value getViewAddress(napi_env env, napi_callback_info info);
154
+ napi_value detachBuffer(napi_env env, napi_callback_info info);
155
+ Value getAddress(const CallbackInfo& info);
156
+ Value lmdbNativeFunctions(const CallbackInfo& info);
157
+ Value enableDirectV8(const CallbackInfo& info);
158
+
159
+ #ifndef thread_local
160
+ #ifdef __GNUC__
161
+ # define thread_local __thread
162
+ #elif __STDC_VERSION__ >= 201112L
163
+ # define thread_local _Thread_local
164
+ #elif defined(_MSC_VER)
165
+ # define thread_local __declspec(thread)
166
+ #else
167
+ # define thread_local
168
+ #endif
169
+ #endif
170
+
171
+ bool valToBinaryFast(MDB_val &data, DbiWrap* dw);
172
+ Value valToUtf8(Env env, MDB_val &data);
173
+ Value valToString(MDB_val &data);
174
+ Value valToStringUnsafe(MDB_val &data);
175
+ Value valToBinary(MDB_val &data);
176
+ Value valToBinaryUnsafe(MDB_val &data, DbiWrap* dw, Env env);
177
+
178
+ int putWithVersion(MDB_txn* txn,
179
+ MDB_dbi dbi,
180
+ MDB_val * key,
181
+ MDB_val * data,
182
+ unsigned int flags, double version);
183
+
184
+ Napi::Value throwLmdbError(Napi::Env env, int rc);
185
+ Napi::Value throwError(Napi::Env env, const char* message);
186
+
187
+ class TxnWrap;
188
+ class DbiWrap;
189
+ class EnvWrap;
190
+ class CursorWrap;
191
+ class SharedEnv {
192
+ public:
193
+ MDB_env* env;
194
+ uint64_t dev;
195
+ uint64_t inode;
196
+ int count;
197
+ };
198
+
199
+ const int INTERRUPT_BATCH = 9998;
200
+ const int WORKER_WAITING = 9997;
201
+ const int RESTART_WORKER_TXN = 9999;
202
+ const int RESUME_BATCH = 9996;
203
+ const int USER_HAS_LOCK = 9995;
204
+ const int SEPARATE_FLUSHED = 1;
205
+ const int DELETE_ON_CLOSE = 2;
206
+
207
+ class WriteWorker {
208
+ public:
209
+ WriteWorker(MDB_env* env, EnvWrap* envForTxn, uint32_t* instructions);
210
+ void Write();
211
+ MDB_txn* txn;
212
+ MDB_txn* AcquireTxn(int* flags);
213
+ void UnlockTxn();
214
+ int WaitForCallbacks(MDB_txn** txn, bool allowCommit, uint32_t* target);
215
+ virtual void ReportError(const char* error);
216
+ virtual void SendUpdate();
217
+ int interruptionStatus;
218
+ bool finishedProgress;
219
+ bool hasError;
220
+ EnvWrap* envForTxn;
221
+ virtual ~WriteWorker();
222
+ uint32_t* instructions;
223
+ int progressStatus;
224
+ MDB_env* env;
225
+ static int DoWrites(MDB_txn* txn, EnvWrap* envForTxn, uint32_t* instruction, WriteWorker* worker);
226
+ };
227
+ class AsyncWriteWorker : public WriteWorker, public AsyncProgressWorker<char> {
228
+ public:
229
+ AsyncWriteWorker(MDB_env* env, EnvWrap* envForTxn, uint32_t* instructions, const Function& callback);
230
+ void Execute(const AsyncProgressWorker::ExecutionProgress& execution);
231
+ void OnProgress(const char* data, size_t count);
232
+ void OnOK();
233
+ void ReportError(const char* error);
234
+ void SendUpdate();
235
+ private:
236
+ ExecutionProgress* executionProgress;
237
+ };
238
+ class TxnTracked {
239
+ public:
240
+ TxnTracked(MDB_txn *txn, unsigned int flags);
241
+ ~TxnTracked();
242
+ unsigned int flags;
243
+ MDB_txn *txn;
244
+ TxnTracked *parent;
245
+ };
246
+
247
+ /*
248
+ `Env`
249
+ Represents a database environment.
250
+ (Wrapper for `MDB_env`)
251
+ */
252
+ typedef struct env_tracking_t {
253
+ pthread_mutex_t* envsLock;
254
+ std::vector<SharedEnv> envs;
255
+ } env_tracking_t;
256
+
257
+ class EnvWrap : public ObjectWrap<EnvWrap> {
258
+ private:
259
+ // List of open read transactions
260
+ std::vector<TxnWrap*> readTxns;
261
+ static env_tracking_t* initTracking();
262
+ napi_env napiEnv;
263
+ // compression settings and space
264
+ Compression *compression;
265
+ static thread_local std::vector<EnvWrap*>* openEnvWraps;
266
+
267
+ // Cleans up stray transactions
268
+ void cleanupStrayTxns();
269
+ static void cleanupEnvWraps(void* data);
270
+
271
+ friend class TxnWrap;
272
+ friend class DbiWrap;
273
+
274
+ public:
275
+ EnvWrap(const CallbackInfo&);
276
+ ~EnvWrap();
277
+ // The wrapped object
278
+ MDB_env *env;
279
+ // Current write transaction
280
+ static env_tracking_t* envTracking;
281
+ TxnWrap *currentWriteTxn;
282
+ TxnTracked *writeTxn;
283
+ pthread_mutex_t* writingLock;
284
+ pthread_cond_t* writingCond;
285
+ std::vector<AsyncWorker*> workers;
286
+
287
+ MDB_txn* currentReadTxn;
288
+ WriteWorker* writeWorker;
289
+ bool readTxnRenewed;
290
+ unsigned int jsFlags;
291
+ char* keyBuffer;
292
+ int pageSize;
293
+ MDB_txn* getReadTxn();
294
+
295
+ // Sets up exports for the Env constructor
296
+ static void setupExports(Napi::Env env, Object exports);
297
+ void closeEnv();
298
+ int openEnv(int flags, int jsFlags, const char* path, char* keyBuffer, Compression* compression, int maxDbs,
299
+ int maxReaders, mdb_size_t mapSize, int pageSize, char* encryptionKey);
300
+
301
+ /*
302
+ Gets statistics about the database environment.
303
+ */
304
+ Napi::Value stat(const CallbackInfo& info);
305
+
306
+ /*
307
+ Gets statistics about the free space database
308
+ */
309
+ Napi::Value freeStat(const CallbackInfo& info);
310
+
311
+ /*
312
+ Gets information about the database environment.
313
+ */
314
+ Napi::Value info(const CallbackInfo& info);
315
+ /*
316
+ Check for stale readers
317
+ */
318
+ Napi::Value readerCheck(const CallbackInfo& info);
319
+ /*
320
+ Print a list of readers
321
+ */
322
+ Napi::Value readerList(const CallbackInfo& info);
323
+
324
+ /*
325
+ Opens the database environment with the specified options. The options will be used to configure the environment before opening it.
326
+ (Wrapper for `mdb_env_open`)
327
+
328
+ Parameters:
329
+
330
+ * Options object that contains possible configuration options.
331
+
332
+ Possible options are:
333
+
334
+ * maxDbs: the maximum number of named databases you can have in the environment (default is 1)
335
+ * maxReaders: the maximum number of concurrent readers of the environment (default is 126)
336
+ * mapSize: maximal size of the memory map (the full environment) in bytes (default is 10485760 bytes)
337
+ * path: path to the database environment
338
+ */
339
+ Napi::Value open(const CallbackInfo& info);
340
+ Napi::Value getMaxKeySize(const CallbackInfo& info);
341
+
342
+ /*
343
+ Copies the database environment to a file.
344
+ (Wrapper for `mdb_env_copy2`)
345
+
346
+ Parameters:
347
+
348
+ * path - Path to the target file
349
+ * compact (optional) - Copy using compact setting
350
+ * callback - Callback when finished (this is performed asynchronously)
351
+ */
352
+ Napi::Value copy(const CallbackInfo& info);
353
+
354
+ /*
355
+ Closes the database environment.
356
+ (Wrapper for `mdb_env_close`)
357
+ */
358
+ Napi::Value close(const CallbackInfo& info);
359
+
360
+ /*
361
+ Starts a new transaction in the environment.
362
+ (Wrapper for `mdb_txn_begin`)
363
+
364
+ Parameters:
365
+
366
+ * Options object that contains possible configuration options.
367
+
368
+ Possible options are:
369
+
370
+ * readOnly: if true, the transaction is read-only
371
+ */
372
+ Napi::Value beginTxn(const CallbackInfo& info);
373
+ Napi::Value commitTxn(const CallbackInfo& info);
374
+ Napi::Value abortTxn(const CallbackInfo& info);
375
+
376
+ /*
377
+ Flushes all data to the disk asynchronously.
378
+ (Asynchronous wrapper for `mdb_env_sync`)
379
+
380
+ Parameters:
381
+
382
+ * Callback to be executed after the sync is complete.
383
+ */
384
+ Napi::Value sync(const CallbackInfo& info);
385
+
386
+ /*
387
+ Performs a set of operations asynchronously, automatically wrapping it in its own transaction
388
+
389
+ Parameters:
390
+
391
+ * Callback to be executed after the sync is complete.
392
+ */
393
+ Napi::Value startWriting(const CallbackInfo& info);
394
+ static napi_value compress(napi_env env, napi_callback_info info);
395
+ static napi_value write(napi_env env, napi_callback_info info);
396
+ static napi_value onExit(napi_env env, napi_callback_info info);
397
+ Napi::Value resetCurrentReadTxn(const CallbackInfo& info);
398
+ };
399
+
400
+ const int TXN_ABORTABLE = 1;
401
+ const int TXN_SYNCHRONOUS_COMMIT = 2;
402
+ const int TXN_FROM_WORKER = 4;
403
+
404
+ /*
405
+ `Txn`
406
+ Represents a transaction running on a database environment.
407
+ (Wrapper for `MDB_txn`)
408
+ */
409
+ class TxnWrap : public ObjectWrap<TxnWrap> {
410
+ private:
411
+
412
+ // Reference to the MDB_env of the wrapped MDB_txn
413
+ MDB_env *env;
414
+
415
+ // Environment wrapper of the current transaction
416
+ EnvWrap *ew;
417
+ // parent TW, if it is exists
418
+ TxnWrap *parentTw;
419
+
420
+ // Flags used with mdb_txn_begin
421
+ unsigned int flags;
422
+
423
+ friend class CursorWrap;
424
+ friend class DbiWrap;
425
+ friend class EnvWrap;
426
+
427
+ public:
428
+ TxnWrap(const CallbackInfo& info);
429
+ ~TxnWrap();
430
+
431
+ // The wrapped object
432
+ MDB_txn *txn;
433
+
434
+ // Remove the current TxnWrap from its EnvWrap
435
+ void removeFromEnvWrap();
436
+ int begin(EnvWrap *ew, unsigned int flags);
437
+
438
+ /*
439
+ Commits the transaction.
440
+ (Wrapper for `mdb_txn_commit`)
441
+ */
442
+ Napi::Value commit(const CallbackInfo& info);
443
+
444
+ /*
445
+ Aborts the transaction.
446
+ (Wrapper for `mdb_txn_abort`)
447
+ */
448
+ Napi::Value abort(const CallbackInfo& info);
449
+
450
+ /*
451
+ Aborts a read-only transaction but makes it renewable with `renew`.
452
+ (Wrapper for `mdb_txn_reset`)
453
+ */
454
+ void reset();
455
+ /*
456
+ Renews a read-only transaction after it has been reset.
457
+ (Wrapper for `mdb_txn_renew`)
458
+ */
459
+ Napi::Value renew(const CallbackInfo& info);
460
+ static void setupExports(Napi::Env env, Object exports);
461
+ };
462
+
463
+ const int HAS_VERSIONS = 0x1000;
464
+ /*
465
+ `Dbi`
466
+ Represents a database instance in an environment.
467
+ (Wrapper for `MDB_dbi`)
468
+ */
469
+ class DbiWrap : public ObjectWrap<DbiWrap> {
470
+ public:
471
+ // Tells how keys should be treated
472
+ LmdbKeyType keyType;
473
+ // Stores flags set when opened
474
+ int flags;
475
+ // The wrapped object
476
+ MDB_dbi dbi;
477
+ // Reference to the MDB_env of the wrapped MDB_dbi
478
+ MDB_env *env;
479
+ // The EnvWrap object of the current Dbi
480
+ EnvWrap *ew;
481
+ // Whether the Dbi was opened successfully
482
+ bool isOpen;
483
+ // compression settings and space
484
+ Compression* compression;
485
+ // versions stored in data
486
+ bool hasVersions;
487
+ // current unsafe buffer for this db
488
+ bool getFast;
489
+
490
+ friend class TxnWrap;
491
+ friend class CursorWrap;
492
+ friend class EnvWrap;
493
+
494
+ DbiWrap(const CallbackInfo& info);
495
+ ~DbiWrap();
496
+
497
+ /*
498
+ Closes the database instance.
499
+ Wrapper for `mdb_dbi_close`)
500
+ */
501
+ Napi::Value close(const CallbackInfo& info);
502
+
503
+ /*
504
+ Drops the database instance, either deleting it completely (default) or just freeing its pages.
505
+
506
+ Parameters:
507
+
508
+ * Options object that contains possible configuration options.
509
+
510
+ Possible options are:
511
+
512
+ * justFreePages - indicates that the database pages need to be freed but the database shouldn't be deleted
513
+
514
+ */
515
+ Napi::Value drop(const CallbackInfo& info);
516
+
517
+ Napi::Value stat(const CallbackInfo& info);
518
+ int prefetch(uint32_t* keys);
519
+ int open(int flags, char* name, bool hasVersions, LmdbKeyType keyType, Compression* compression);
520
+ int32_t doGetByBinary(uint32_t keySize);
521
+ static void setupExports(Napi::Env env, Object exports);
522
+ };
523
+
524
+ class Compression : public ObjectWrap<Compression> {
525
+ public:
526
+ char* dictionary; // dictionary to use to decompress
527
+ char* compressDictionary; // separate dictionary to use to compress since the decompression dictionary can move around in the main thread
528
+ unsigned int dictionarySize;
529
+ char* decompressTarget;
530
+ unsigned int decompressSize;
531
+ unsigned int compressionThreshold;
532
+ // compression acceleration (defaults to 1)
533
+ int acceleration;
534
+ static thread_local LZ4_stream_t* stream;
535
+ void decompress(MDB_val& data, bool &isValid, bool canAllocate);
536
+ argtokey_callback_t compress(MDB_val* value, argtokey_callback_t freeValue);
537
+ int compressInstruction(EnvWrap* env, double* compressionAddress);
538
+ Napi::Value ctor(const CallbackInfo& info);
539
+ Napi::Value setBuffer(const CallbackInfo& info);
540
+ Compression(const CallbackInfo& info);
541
+ friend class EnvWrap;
542
+ friend class DbiWrap;
543
+ //NAN_METHOD(Compression::startCompressing);
544
+ static void setupExports(Napi::Env env, Object exports);
545
+ };
546
+
547
+ /*
548
+ `Cursor`
549
+ Represents a cursor instance that is assigned to a transaction and a database instance
550
+ (Wrapper for `MDB_cursor`)
551
+ */
552
+ class CursorWrap : public ObjectWrap<CursorWrap> {
553
+
554
+ private:
555
+
556
+ // Key/data pair where the cursor is at, and ending key
557
+ MDB_val key, data, endKey;
558
+ // Free function for the current key
559
+ argtokey_callback_t freeKey;
560
+
561
+ public:
562
+ MDB_cursor_op iteratingOp;
563
+ MDB_cursor *cursor;
564
+ // Stores how key is represented
565
+ LmdbKeyType keyType;
566
+ int flags;
567
+ DbiWrap *dw;
568
+ MDB_txn *txn;
569
+
570
+ // The wrapped object
571
+ CursorWrap(MDB_cursor* cursor);
572
+ CursorWrap(const CallbackInfo& info);
573
+ ~CursorWrap();
574
+
575
+ // Sets up exports for the Cursor constructor
576
+ static void setupExports(Napi::Env env, Object exports);
577
+
578
+ /*
579
+ Closes the cursor.
580
+ (Wrapper for `mdb_cursor_close`)
581
+
582
+ Parameters:
583
+
584
+ * Transaction object
585
+ * Database instance object
586
+ */
587
+ Napi::Value close(const CallbackInfo& info);
588
+ /*
589
+ Deletes the key/data pair to which the cursor refers.
590
+ (Wrapper for `mdb_cursor_del`)
591
+ */
592
+ Napi::Value del(const CallbackInfo& info);
593
+
594
+ int returnEntry(int lastRC, MDB_val &key, MDB_val &data);
595
+ int32_t doPosition(uint32_t offset, uint32_t keySize, uint64_t endKeyAddress);
596
+ //Value getStringByBinary(const CallbackInfo& info);
597
+ };
598
+
599
+ #endif // NODE_LMDB_H