@ohos-ports/lmdb 2.5.3-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (120) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +536 -0
  3. package/SECURITY.md +12 -0
  4. package/binding.gyp +111 -0
  5. package/build/Release/lmdb.node +0 -0
  6. package/caching.js +156 -0
  7. package/dependencies/lmdb/libraries/liblmdb/COPYRIGHT +20 -0
  8. package/dependencies/lmdb/libraries/liblmdb/Doxyfile +1631 -0
  9. package/dependencies/lmdb/libraries/liblmdb/LICENSE +47 -0
  10. package/dependencies/lmdb/libraries/liblmdb/Makefile +136 -0
  11. package/dependencies/lmdb/libraries/liblmdb/chacha8.c +183 -0
  12. package/dependencies/lmdb/libraries/liblmdb/chacha8.h +14 -0
  13. package/dependencies/lmdb/libraries/liblmdb/crypto.c +121 -0
  14. package/dependencies/lmdb/libraries/liblmdb/intro.doc +192 -0
  15. package/dependencies/lmdb/libraries/liblmdb/lmdb.h +1792 -0
  16. package/dependencies/lmdb/libraries/liblmdb/mdb.c +12429 -0
  17. package/dependencies/lmdb/libraries/liblmdb/mdb_copy.1 +74 -0
  18. package/dependencies/lmdb/libraries/liblmdb/mdb_copy.c +106 -0
  19. package/dependencies/lmdb/libraries/liblmdb/mdb_drop.1 +53 -0
  20. package/dependencies/lmdb/libraries/liblmdb/mdb_drop.c +154 -0
  21. package/dependencies/lmdb/libraries/liblmdb/mdb_dump.1 +94 -0
  22. package/dependencies/lmdb/libraries/liblmdb/mdb_dump.c +333 -0
  23. package/dependencies/lmdb/libraries/liblmdb/mdb_load.1 +97 -0
  24. package/dependencies/lmdb/libraries/liblmdb/mdb_load.c +530 -0
  25. package/dependencies/lmdb/libraries/liblmdb/mdb_stat.1 +83 -0
  26. package/dependencies/lmdb/libraries/liblmdb/mdb_stat.c +276 -0
  27. package/dependencies/lmdb/libraries/liblmdb/midl.c +452 -0
  28. package/dependencies/lmdb/libraries/liblmdb/midl.h +208 -0
  29. package/dependencies/lmdb/libraries/liblmdb/module.c +101 -0
  30. package/dependencies/lmdb/libraries/liblmdb/module.h +16 -0
  31. package/dependencies/lmdb/libraries/liblmdb/mtest.c +178 -0
  32. package/dependencies/lmdb/libraries/liblmdb/mtest2.c +124 -0
  33. package/dependencies/lmdb/libraries/liblmdb/mtest3.c +133 -0
  34. package/dependencies/lmdb/libraries/liblmdb/mtest4.c +168 -0
  35. package/dependencies/lmdb/libraries/liblmdb/mtest5.c +135 -0
  36. package/dependencies/lmdb/libraries/liblmdb/mtest6.c +141 -0
  37. package/dependencies/lmdb/libraries/liblmdb/mtest_enc.c +190 -0
  38. package/dependencies/lmdb/libraries/liblmdb/mtest_enc2.c +189 -0
  39. package/dependencies/lmdb/libraries/liblmdb/mtest_remap.c +177 -0
  40. package/dependencies/lmdb/libraries/liblmdb/sample-bdb.txt +73 -0
  41. package/dependencies/lmdb/libraries/liblmdb/sample-mdb.txt +62 -0
  42. package/dependencies/lmdb/libraries/liblmdb/tooltag +27 -0
  43. package/dependencies/lmdb-data-v1/libraries/liblmdb/COPYRIGHT +20 -0
  44. package/dependencies/lmdb-data-v1/libraries/liblmdb/Doxyfile +1631 -0
  45. package/dependencies/lmdb-data-v1/libraries/liblmdb/LICENSE +47 -0
  46. package/dependencies/lmdb-data-v1/libraries/liblmdb/Makefile +118 -0
  47. package/dependencies/lmdb-data-v1/libraries/liblmdb/intro.doc +192 -0
  48. package/dependencies/lmdb-data-v1/libraries/liblmdb/lmdb.h +1653 -0
  49. package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb.c +11359 -0
  50. package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_copy.1 +61 -0
  51. package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_copy.c +84 -0
  52. package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_drop.1 +40 -0
  53. package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_drop.c +135 -0
  54. package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_dump.1 +81 -0
  55. package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_dump.c +319 -0
  56. package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_load.1 +84 -0
  57. package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_load.c +492 -0
  58. package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_stat.1 +70 -0
  59. package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_stat.c +264 -0
  60. package/dependencies/lmdb-data-v1/libraries/liblmdb/midl.c +421 -0
  61. package/dependencies/lmdb-data-v1/libraries/liblmdb/midl.h +200 -0
  62. package/dependencies/lmdb-data-v1/libraries/liblmdb/mtest.c +177 -0
  63. package/dependencies/lmdb-data-v1/libraries/liblmdb/mtest2.c +124 -0
  64. package/dependencies/lmdb-data-v1/libraries/liblmdb/mtest3.c +133 -0
  65. package/dependencies/lmdb-data-v1/libraries/liblmdb/mtest4.c +168 -0
  66. package/dependencies/lmdb-data-v1/libraries/liblmdb/mtest5.c +135 -0
  67. package/dependencies/lmdb-data-v1/libraries/liblmdb/mtest6.c +141 -0
  68. package/dependencies/lmdb-data-v1/libraries/liblmdb/sample-bdb.txt +73 -0
  69. package/dependencies/lmdb-data-v1/libraries/liblmdb/sample-mdb.txt +62 -0
  70. package/dependencies/lmdb-data-v1/libraries/liblmdb/tooltag +27 -0
  71. package/dependencies/lz4/LICENSE +11 -0
  72. package/dependencies/lz4/lib/LICENSE +24 -0
  73. package/dependencies/lz4/lib/README.md +137 -0
  74. package/dependencies/lz4/lib/dll/example/README.md +69 -0
  75. package/dependencies/lz4/lib/dll/example/fullbench-dll.sln +25 -0
  76. package/dependencies/lz4/lib/dll/example/fullbench-dll.vcxproj +182 -0
  77. package/dependencies/lz4/lib/dll/liblz4.def +62 -0
  78. package/dependencies/lz4/lib/liblz4-dll.rc.in +35 -0
  79. package/dependencies/lz4/lib/liblz4.pc.in +14 -0
  80. package/dependencies/lz4/lib/lz4.c +2495 -0
  81. package/dependencies/lz4/lib/lz4.h +774 -0
  82. package/dependencies/lz4/lib/lz4frame.c +1899 -0
  83. package/dependencies/lz4/lib/lz4frame.h +623 -0
  84. package/dependencies/lz4/lib/lz4frame_static.h +47 -0
  85. package/dependencies/lz4/lib/lz4hc.c +1615 -0
  86. package/dependencies/lz4/lib/lz4hc.h +413 -0
  87. package/dependencies/lz4/lib/xxhash.c +1030 -0
  88. package/dependencies/lz4/lib/xxhash.h +328 -0
  89. package/dependencies/v8/v8-fast-api-calls-v16.h +791 -0
  90. package/dependencies/v8/v8-fast-api-calls.h +888 -0
  91. package/dict/dict.txt +1 -0
  92. package/dict/dict2.txt +1 -0
  93. package/dist/index.cjs +2478 -0
  94. package/dist/index.cjs.map +1 -0
  95. package/index.d.ts +385 -0
  96. package/index.js +37 -0
  97. package/keys.js +102 -0
  98. package/level.js +27 -0
  99. package/native.js +82 -0
  100. package/node-index.js +28 -0
  101. package/open.js +414 -0
  102. package/package.json +70 -0
  103. package/read.js +590 -0
  104. package/rollup.config.js +12 -0
  105. package/src/compression.cpp +222 -0
  106. package/src/cursor.cpp +344 -0
  107. package/src/dbi.cpp +344 -0
  108. package/src/env.cpp +762 -0
  109. package/src/lmdb-js.cpp +64 -0
  110. package/src/lmdb-js.h +599 -0
  111. package/src/misc.cpp +324 -0
  112. package/src/ordered-binary.cpp +46 -0
  113. package/src/txn.cpp +257 -0
  114. package/src/v8-functions.cpp +166 -0
  115. package/src/windows.c +35 -0
  116. package/src/writer.cpp +452 -0
  117. package/util/RangeIterable.js +141 -0
  118. package/util/set-optional-deps.cjs +10 -0
  119. package/util/when.js +8 -0
  120. package/write.js +882 -0
package/src/misc.cpp ADDED
@@ -0,0 +1,324 @@
1
+ #include "lmdb-js.h"
2
+ #include <string.h>
3
+ #include <stdio.h>
4
+ #include <node_version.h>
5
+
6
+ using namespace Napi;
7
+
8
+ static thread_local char* globalUnsafePtr;
9
+ static thread_local size_t globalUnsafeSize;
10
+
11
+ void setupExportMisc(Napi::Env env, Object exports) {
12
+ Object versionObj = Object::New(env);
13
+
14
+ int major, minor, patch;
15
+ char *str = mdb_version(&major, &minor, &patch);
16
+ versionObj.Set("versionString", String::New(env, str));
17
+ versionObj.Set("major", Number::New(env, major));
18
+ versionObj.Set("minor", Number::New(env, minor));
19
+ versionObj.Set("patch", Number::New(env, patch));
20
+ #if ENABLE_V8_API
21
+ versionObj.Set("nodeCompiledVersion", Number::New(env, NODE_MAJOR_VERSION));
22
+ #endif
23
+
24
+ exports.Set("version", versionObj);
25
+ exports.Set("setGlobalBuffer", Function::New(env, setGlobalBuffer));
26
+ exports.Set("lmdbError", Function::New(env, lmdbError));
27
+ exports.Set("enableDirectV8", Function::New(env, enableDirectV8));
28
+ EXPORT_NAPI_FUNCTION("createBufferForAddress", createBufferForAddress);
29
+ EXPORT_NAPI_FUNCTION("getAddress", getViewAddress);
30
+ EXPORT_NAPI_FUNCTION("detachBuffer", detachBuffer);
31
+ }
32
+
33
+ void setFlagFromValue(int *flags, int flag, const char *name, bool defaultValue, Object options) {
34
+ Value opt = options.Get(name);
35
+ if (opt.IsBoolean() ? opt.As<Boolean>().Value() : defaultValue)
36
+ *flags |= flag;
37
+ }
38
+ /*
39
+ Value valToStringUnsafe(MDB_val &data) {
40
+ auto resource = new CustomExternalOneByteStringResource(&data);
41
+ auto str = Nan::New<v8::String>(resource);
42
+
43
+ return str.ToLocalChecked();
44
+ }*/
45
+
46
+ Value valToUtf8(Env env, MDB_val &data) {
47
+ return String::New(env, (const char*) data.mv_data, data.mv_size);
48
+ }
49
+
50
+ Value valToString(Env env, MDB_val &data) {
51
+ // UTF-16 buffer
52
+ const uint16_t *buffer = reinterpret_cast<const uint16_t*>(data.mv_data);
53
+ // Number of UTF-16 code points
54
+ size_t n = data.mv_size / sizeof(uint16_t);
55
+
56
+ // Check zero termination
57
+ if (n < 1 || buffer[n - 1] != 0) {
58
+ return throwError(env, "Invalid zero-terminated UTF-16 string");
59
+ }
60
+
61
+ size_t length = n - 1;
62
+ return String::New(env, (const char16_t*)data.mv_data, length);
63
+ }
64
+
65
+ bool valToBinaryFast(MDB_val &data, DbiWrap* dw) {
66
+ Compression* compression = dw->compression;
67
+ if (compression) {
68
+ if (data.mv_data == compression->decompressTarget) {
69
+ // already decompressed to the target, nothing more to do
70
+ } else {
71
+ if (data.mv_size > compression->decompressSize) {
72
+ return false;
73
+ }
74
+ // copy into the buffer target
75
+ memcpy(compression->decompressTarget, data.mv_data, data.mv_size);
76
+ }
77
+ } else {
78
+ if (data.mv_size > globalUnsafeSize) {
79
+ // TODO: Provide a direct reference if for really large blocks, but we do that we need to detach that in the next turn
80
+ /* if(data.mv_size > 64000) {
81
+ dw->SetUnsafeBuffer(data.mv_data, data.mv_size);
82
+ return Nan::New<Number>(data.mv_size);
83
+ }*/
84
+ return false;
85
+ }
86
+ memcpy(globalUnsafePtr, data.mv_data, data.mv_size);
87
+ }
88
+ return true;
89
+ }
90
+ Value valToBinaryUnsafe(MDB_val &data, DbiWrap* dw, Env env) {
91
+ valToBinaryFast(data, dw);
92
+ return Number::New(env, data.mv_size);
93
+ }
94
+
95
+
96
+ bool getVersionAndUncompress(MDB_val &data, DbiWrap* dw) {
97
+ //fprintf(stdout, "uncompressing %u\n", compressionThreshold);
98
+ unsigned char* charData = (unsigned char*) data.mv_data;
99
+ if (dw->hasVersions) {
100
+ memcpy((dw->ew->keyBuffer + 16), charData, 8);
101
+ // fprintf(stderr, "getVersion %u\n", lastVersion);
102
+ charData = charData + 8;
103
+ data.mv_data = charData;
104
+ data.mv_size -= 8;
105
+ }
106
+ if (data.mv_size == 0) {
107
+ return true;// successFunc(data);
108
+ }
109
+ unsigned char statusByte = dw->compression ? charData[0] : 0;
110
+ //fprintf(stdout, "uncompressing status %X\n", statusByte);
111
+ if (statusByte >= 250) {
112
+ bool isValid;
113
+ dw->compression->decompress(data, isValid, !dw->getFast);
114
+ if (!isValid)
115
+ return false;
116
+ //return Nan::Null();
117
+ }
118
+ return true;
119
+ }
120
+
121
+ Value lmdbError(const CallbackInfo& info) {
122
+ return throwLmdbError(info.Env(), info[0].As<Number>().Int32Value());
123
+ }
124
+
125
+ Value setGlobalBuffer(const CallbackInfo& info) {
126
+ napi_get_typedarray_info(info.Env(), info[0], nullptr, &globalUnsafeSize, (void**) &globalUnsafePtr, nullptr, nullptr);
127
+ return info.Env().Undefined();
128
+ }
129
+
130
+ /*Value getBufferForAddress) {
131
+ char* address = (char*) (size_t) Nan::To<v8::Number>(info[0]).ToLocalChecked()->Value();
132
+ std::unique_ptr<v8::BackingStore> backing = v8::ArrayBuffer::NewBackingStore(
133
+ address, 0x100000000, [](void*, size_t, void*){}, nullptr);
134
+ auto array_buffer = v8::ArrayBuffer::New(Isolate::GetCurrent(), std::move(backing));
135
+ info.GetReturnValue().Set(array_buffer);
136
+ }*/
137
+ NAPI_FUNCTION(createBufferForAddress) {
138
+ ARGS(2)
139
+ GET_INT64_ARG(0);
140
+ void* data = (void*) i64;
141
+ uint32_t length;
142
+ GET_UINT32_ARG(length, 1);
143
+ napi_create_external_buffer(env, length, data, nullptr, nullptr, &returnValue);
144
+ return returnValue;
145
+ }
146
+
147
+ NAPI_FUNCTION(getViewAddress) {
148
+ ARGS(1)
149
+ void* data;
150
+ napi_get_typedarray_info(env, args[0], nullptr, nullptr, &data, nullptr, nullptr);
151
+ napi_create_double(env, (double) (size_t) data, &returnValue);
152
+ return returnValue;
153
+ }
154
+
155
+ NAPI_FUNCTION(detachBuffer) {
156
+ ARGS(1)
157
+ #if (NAPI_VERSION > 6)
158
+ napi_detach_arraybuffer(env, args[0]);
159
+ #endif
160
+ RETURN_UNDEFINED;
161
+ }
162
+
163
+ Value lmdbNativeFunctions(const CallbackInfo& info) {
164
+ // no-op, just doing this to give a label to the native functions
165
+ return info.Env().Undefined();
166
+ }
167
+
168
+ Napi::Value throwLmdbError(Napi::Env env, int rc) {
169
+ if (rc < 0 && !(rc < -30700 && rc > -30800))
170
+ rc = -rc;
171
+ Error error = Error::New(env, mdb_strerror(rc));
172
+ error.Set("code", Number::New(env, rc));
173
+ error.ThrowAsJavaScriptException();
174
+ return env.Undefined();
175
+ }
176
+
177
+ Napi::Value throwError(Napi::Env env, const char* message) {
178
+ Error::New(env, message).ThrowAsJavaScriptException();
179
+ return env.Undefined();
180
+ }
181
+
182
+ int putWithVersion(MDB_txn * txn,
183
+ MDB_dbi dbi,
184
+ MDB_val * key,
185
+ MDB_val * data,
186
+ unsigned int flags, double version) {
187
+ // leave 8 header bytes available for version and copy in with reserved memory
188
+ char* sourceData = (char*) data->mv_data;
189
+ int size = data->mv_size;
190
+ data->mv_size = size + 8;
191
+ int rc = mdb_put(txn, dbi, key, data, flags | MDB_RESERVE);
192
+ if (rc == 0) {
193
+ // if put is successful, data->mv_data will point into the database where we copy the data to
194
+ memcpy((char*) data->mv_data + 8, sourceData, size);
195
+ *((double*) data->mv_data) = version;
196
+ }
197
+ data->mv_data = sourceData; // restore this so that if it points to data that needs to be freed, it points to the right place
198
+ return rc;
199
+ }
200
+
201
+
202
+ #ifdef _WIN32
203
+
204
+ int pthread_mutex_init(pthread_mutex_t *mutex, pthread_mutexattr_t *attr)
205
+ {
206
+ (void)attr;
207
+
208
+ if (mutex == NULL)
209
+ return 1;
210
+
211
+ InitializeCriticalSection(mutex);
212
+ return 0;
213
+ }
214
+
215
+ int pthread_mutex_destroy(pthread_mutex_t *mutex)
216
+ {
217
+ if (mutex == NULL)
218
+ return 1;
219
+ DeleteCriticalSection(mutex);
220
+ return 0;
221
+ }
222
+
223
+ int pthread_mutex_lock(pthread_mutex_t *mutex)
224
+ {
225
+ if (mutex == NULL)
226
+ return 1;
227
+ EnterCriticalSection(mutex);
228
+ return 0;
229
+ }
230
+
231
+ int pthread_mutex_unlock(pthread_mutex_t *mutex)
232
+ {
233
+ if (mutex == NULL)
234
+ return 1;
235
+ LeaveCriticalSection(mutex);
236
+ return 0;
237
+ }
238
+
239
+ int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *attr)
240
+ {
241
+ (void)attr;
242
+ if (cond == NULL)
243
+ return 1;
244
+ InitializeConditionVariable(cond);
245
+ return 0;
246
+ }
247
+
248
+ int pthread_cond_destroy(pthread_cond_t *cond)
249
+ {
250
+ /* Windows does not have a destroy for conditionals */
251
+ (void)cond;
252
+ return 0;
253
+ }
254
+
255
+ int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
256
+ {
257
+ if (cond == NULL || mutex == NULL)
258
+ return 1;
259
+ if (!SleepConditionVariableCS(cond, mutex, INFINITE))
260
+ return 1;
261
+ return 0;
262
+ }
263
+
264
+ int cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, uint64_t ms)
265
+ {
266
+ if (cond == NULL || mutex == NULL)
267
+ return 1;
268
+ if (!SleepConditionVariableCS(cond, mutex, ms))
269
+ return 1;
270
+ return 0;
271
+ }
272
+
273
+ int pthread_cond_signal(pthread_cond_t *cond)
274
+ {
275
+ if (cond == NULL)
276
+ return 1;
277
+ WakeConditionVariable(cond);
278
+ return 0;
279
+ }
280
+
281
+ int pthread_cond_broadcast(pthread_cond_t *cond)
282
+ {
283
+ if (cond == NULL)
284
+ return 1;
285
+ WakeAllConditionVariable(cond);
286
+ return 0;
287
+ }
288
+
289
+ #else
290
+ int cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, uint64_t cms)
291
+ {
292
+ struct timespec ts;
293
+ clock_gettime(CLOCK_REALTIME, &ts);
294
+ uint64_t ns = ts.tv_nsec + cms * 10000;
295
+ ts.tv_sec += ns / 1000000000;
296
+ ts.tv_nsec += ns % 1000000000;
297
+ return pthread_cond_timedwait(cond, mutex, &ts);
298
+ }
299
+
300
+ #endif
301
+
302
+ // This file contains code from the node-lmdb project
303
+ // Copyright (c) 2013-2017 Timur Kristóf
304
+ // Copyright (c) 2021 Kristopher Tate
305
+ // Licensed to you under the terms of the MIT license
306
+ //
307
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
308
+ // of this software and associated documentation files (the "Software"), to deal
309
+ // in the Software without restriction, including without limitation the rights
310
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
311
+ // copies of the Software, and to permit persons to whom the Software is
312
+ // furnished to do so, subject to the following conditions:
313
+
314
+ // The above copyright notice and this permission notice shall be included in
315
+ // all copies or substantial portions of the Software.
316
+
317
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
318
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
319
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
320
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
321
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
322
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
323
+ // THE SOFTWARE.
324
+
@@ -0,0 +1,46 @@
1
+ #include "lmdb-js.h"
2
+
3
+ #ifdef _WIN32
4
+ #define ntohl _byteswap_ulong
5
+ #define htonl _byteswap_ulong
6
+ #else
7
+ #include <arpa/inet.h>
8
+ #endif
9
+
10
+ // compare items by 32-bit comparison, a is user provided and assumed to be zero terminated/padded
11
+ // which allows us to do the full 32-bit comparisons safely
12
+ int compareFast(const MDB_val *a, const MDB_val *b) {
13
+ uint32_t* dataA = (uint32_t*) a->mv_data;
14
+ uint32_t* dataB = (uint32_t*) b->mv_data;
15
+ size_t remaining = b->mv_size;
16
+ uint32_t aVal, bVal;
17
+ while(remaining >= 4) {
18
+ aVal = ntohl(*dataA);
19
+ bVal = ntohl(*dataB);
20
+ if (aVal > bVal)
21
+ return 1;
22
+ if (aVal < bVal)
23
+ return -1;
24
+ /*diff = (int64_t) ntohl(*dataA) - (int64_t) ntohl(*dataB);
25
+ if (diff)
26
+ return diff;*/
27
+ dataA++;
28
+ dataB++;
29
+ remaining -= 4;
30
+ }
31
+ if (remaining) {
32
+ if (remaining == 1) {
33
+ aVal = *((uint8_t*) dataA);
34
+ bVal = *((uint8_t*) dataB);
35
+ } else {
36
+ aVal = ntohl(*dataA);
37
+ bVal = remaining == 2 ? (*((uint8_t*) dataB) << 24) + (*((uint8_t*) dataB + 1) << 16) :
38
+ ntohl(*dataB & 0x00ffffff);
39
+ }
40
+ if (aVal > bVal)
41
+ return 1;
42
+ if (aVal < bVal)
43
+ return -1;
44
+ }
45
+ return a->mv_size - b->mv_size;
46
+ }
package/src/txn.cpp ADDED
@@ -0,0 +1,257 @@
1
+ #include "lmdb-js.h"
2
+
3
+ using namespace Napi;
4
+
5
+ TxnTracked::TxnTracked(MDB_txn *txn, unsigned int flags) {
6
+ this->txn = txn;
7
+ this->flags = flags;
8
+ parent = nullptr;
9
+ }
10
+
11
+ TxnTracked::~TxnTracked() {
12
+ this->txn = nullptr;
13
+ }
14
+
15
+ TxnWrap::TxnWrap(const Napi::CallbackInfo& info) : ObjectWrap<TxnWrap>(info) {
16
+ EnvWrap *ew;
17
+ napi_unwrap(info.Env(), info[0], (void**)&ew);
18
+ int flags = 0;
19
+ TxnWrap *parentTw;
20
+ if (info[1].IsBoolean() && ew->writeWorker) { // this is from a transaction callback
21
+ txn = ew->writeWorker->AcquireTxn(&flags);
22
+ parentTw = nullptr;
23
+ } else {
24
+ if (info[1].IsObject()) {
25
+ Object options = info[1].As<Object>();
26
+
27
+ // Get flags from options
28
+
29
+ setFlagFromValue(&flags, MDB_RDONLY, "readOnly", false, options);
30
+ } else if (info[1].IsNumber()) {
31
+ flags = info[1].As<Number>();
32
+ }
33
+ MDB_txn *parentTxn;
34
+ if (info[2].IsObject()) {
35
+ napi_unwrap(info.Env(), info[2], (void**) &parentTw);
36
+ parentTxn = parentTw->txn;
37
+ } else {
38
+ parentTxn = nullptr;
39
+ parentTw = nullptr;
40
+ // Check existence of current write transaction
41
+ if (0 == (flags & MDB_RDONLY)) {
42
+ if (ew->currentWriteTxn != nullptr) {
43
+ throwError(info.Env(), "You have already opened a write transaction in the current process, can't open a second one.");
44
+ return;
45
+ }
46
+ //fprintf(stderr, "begin sync txn");
47
+ auto writeWorker = ew->writeWorker;
48
+ if (writeWorker) {
49
+ parentTxn = writeWorker->AcquireTxn(&flags); // see if we have a paused transaction
50
+ // else we create a child transaction from the current batch transaction. TODO: Except in WRITEMAP mode, where we need to indicate that the transaction should not be committed
51
+ }
52
+ }
53
+ }
54
+ //fprintf(stderr, "txn_begin from txn.cpp %u %p\n", flags, parentTxn);
55
+ int rc = mdb_txn_begin(ew->env, parentTxn, flags, &txn);
56
+ if (rc != 0) {
57
+ txn = nullptr;
58
+ if (rc == EINVAL) {
59
+ throwError(info.Env(), "Invalid parameter, which on MacOS is often due to more transactions than available robust locked semaphors (see docs for more info)");
60
+ return;
61
+ }
62
+ throwLmdbError(info.Env(), rc);
63
+ return;
64
+ }
65
+ }
66
+
67
+ // Set the current write transaction
68
+ if (0 == (flags & MDB_RDONLY)) {
69
+ ew->currentWriteTxn = this;
70
+ }
71
+ else {
72
+ ew->readTxns.push_back(this);
73
+ ew->currentReadTxn = txn;
74
+ }
75
+ this->parentTw = parentTw;
76
+ this->flags = flags;
77
+ this->ew = ew;
78
+ this->env = ew->env;
79
+ info.This().As<Object>().Set("address", Number::New(info.Env(), (size_t) this));
80
+ }
81
+
82
+ TxnWrap::~TxnWrap() {
83
+ // Close if not closed already
84
+ if (this->txn) {
85
+ mdb_txn_abort(txn);
86
+ this->removeFromEnvWrap();
87
+ }
88
+ }
89
+
90
+ void TxnWrap::removeFromEnvWrap() {
91
+ if (this->ew) {
92
+ if (this->ew->currentWriteTxn == this) {
93
+ this->ew->currentWriteTxn = this->parentTw;
94
+ }
95
+ else {
96
+ auto it = std::find(ew->readTxns.begin(), ew->readTxns.end(), this);
97
+ if (it != ew->readTxns.end()) {
98
+ ew->readTxns.erase(it);
99
+ }
100
+ }
101
+ this->ew = nullptr;
102
+ }
103
+ this->txn = nullptr;
104
+ }
105
+
106
+ int TxnWrap::begin(EnvWrap *ew, unsigned int flags) {
107
+ this->ew = ew;
108
+ this->flags = flags;
109
+ MDB_env *env = ew->env;
110
+ unsigned int envFlags;
111
+ mdb_env_get_flags(env, &envFlags);
112
+ if (flags & MDB_RDONLY) {
113
+ mdb_txn_begin(env, nullptr, flags & 0xf0000, &this->txn);
114
+ } else {
115
+ //fprintf(stderr, "begin sync txn %i\n", flags);
116
+
117
+ if (ew->writeTxn)
118
+ txn = ew->writeTxn->txn;
119
+ else if (ew->writeWorker) {
120
+ // try to acquire the txn from the current batch
121
+ txn = ew->writeWorker->AcquireTxn((int*) &flags);
122
+ //fprintf(stderr, "acquired %p %p %p\n", ew->writeWorker, txn, flags);
123
+ } else {
124
+ pthread_mutex_lock(ew->writingLock);
125
+ txn = nullptr;
126
+ }
127
+
128
+ if (txn) {
129
+ if (flags & TXN_ABORTABLE) {
130
+ if (envFlags & MDB_WRITEMAP)
131
+ flags &= ~TXN_ABORTABLE;
132
+ else {
133
+ // child txn
134
+ mdb_txn_begin(env, this->txn, flags & 0xf0000, &this->txn);
135
+ TxnTracked* childTxn = new TxnTracked(txn, flags);
136
+ childTxn->parent = ew->writeTxn;
137
+ ew->writeTxn = childTxn;
138
+ return 0;
139
+ }
140
+ }
141
+ } else {
142
+ mdb_txn_begin(env, nullptr, flags & 0xf0000, &this->txn);
143
+ flags |= TXN_ABORTABLE;
144
+ }
145
+ ew->writeTxn = new TxnTracked(txn, flags);
146
+ return 0;
147
+ }
148
+ // Set the current write transaction
149
+ if (0 == (flags & MDB_RDONLY)) {
150
+ ew->currentWriteTxn = this;
151
+ }
152
+ else {
153
+ ew->readTxns.push_back(this);
154
+ ew->currentReadTxn = txn;
155
+ ew->readTxnRenewed = true;
156
+ }
157
+ return 0;
158
+ }
159
+ Value TxnWrap::commit(const Napi::CallbackInfo& info) {
160
+ if (!this->txn) {
161
+ return throwError(info.Env(), "The transaction is already closed.");
162
+ }
163
+ int rc;
164
+ WriteWorker* writeWorker = this->ew->writeWorker;
165
+ if (writeWorker) {
166
+ // if (writeWorker->txn && env->writeMap)
167
+ // rc = 0
168
+ // else
169
+ rc = mdb_txn_commit(this->txn);
170
+
171
+ pthread_mutex_unlock(this->ew->writingLock);
172
+ }
173
+ else
174
+ rc = mdb_txn_commit(this->txn);
175
+ //fprintf(stdout, "commit done\n");
176
+ this->removeFromEnvWrap();
177
+
178
+ if (rc != 0) {
179
+ return throwLmdbError(info.Env(), rc);
180
+ }
181
+ return info.Env().Undefined();
182
+ }
183
+
184
+ Value TxnWrap::abort(const Napi::CallbackInfo& info) {
185
+ if (!this->txn) {
186
+ return throwError(info.Env(), "The transaction is already closed.");
187
+ }
188
+
189
+ mdb_txn_abort(this->txn);
190
+ this->removeFromEnvWrap();
191
+ return info.Env().Undefined();
192
+ }
193
+ NAPI_FUNCTION(resetTxn) {
194
+ ARGS(1)
195
+ GET_INT64_ARG(0);
196
+ TxnWrap* tw = (TxnWrap*) i64;
197
+ if (!tw->txn) {
198
+ THROW_ERROR("The transaction is already closed.");
199
+ }
200
+ tw->reset();
201
+ RETURN_UNDEFINED;
202
+ }
203
+ void resetTxnFFI(double twPointer) {
204
+ TxnWrap* tw = (TxnWrap*) (size_t) twPointer;
205
+ tw->reset();
206
+ }
207
+
208
+ void TxnWrap::reset() {
209
+ ew->readTxnRenewed = false;
210
+ mdb_txn_reset(txn);
211
+ }
212
+ Value TxnWrap::renew(const Napi::CallbackInfo& info) {
213
+ if (!this->txn) {
214
+ return throwError(info.Env(), "The transaction is already closed.");
215
+ }
216
+
217
+ int rc = mdb_txn_renew(this->txn);
218
+ if (rc != 0) {
219
+ return throwLmdbError(info.Env(), rc);
220
+ }
221
+ return info.Env().Undefined();
222
+ }
223
+ void TxnWrap::setupExports(Napi::Env env, Object exports) {
224
+ // TxnWrap: Prepare constructor template
225
+ Function TxnClass = DefineClass(env, "Txn", {
226
+ // TxnWrap: Add functions to the prototype
227
+ TxnWrap::InstanceMethod("commit", &TxnWrap::commit),
228
+ TxnWrap::InstanceMethod("abort", &TxnWrap::abort),
229
+ TxnWrap::InstanceMethod("renew", &TxnWrap::renew),
230
+ });
231
+ exports.Set("Txn", TxnClass);
232
+ EXPORT_NAPI_FUNCTION("resetTxn", resetTxn);
233
+ EXPORT_FUNCTION_ADDRESS("resetTxnPtr", resetTxnFFI);
234
+ //txnTpl->InstanceTemplate()->SetInternalFieldCount(1);
235
+ }
236
+ // This file contains code from the node-lmdb project
237
+ // Copyright (c) 2013-2017 Timur Kristóf
238
+ // Copyright (c) 2021 Kristopher Tate
239
+ // Licensed to you under the terms of the MIT license
240
+ //
241
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
242
+ // of this software and associated documentation files (the "Software"), to deal
243
+ // in the Software without restriction, including without limitation the rights
244
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
245
+ // copies of the Software, and to permit persons to whom the Software is
246
+ // furnished to do so, subject to the following conditions:
247
+
248
+ // The above copyright notice and this permission notice shall be included in
249
+ // all copies or substantial portions of the Software.
250
+
251
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
252
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
253
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
254
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
255
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
256
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
257
+ // THE SOFTWARE.