@ohos-ports/lmdb 2.8.6-beta.0

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