mdbxmou 0.2.2 → 0.2.5

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.
@@ -0,0 +1,450 @@
1
+ #include "envmou.hpp"
2
+ #include "txnmou.hpp"
3
+ #include "async/envmou_copy_to.hpp"
4
+ #include "async/envmou_query.hpp"
5
+ #include "async/envmou_open.hpp"
6
+ #include "async/envmou_keys.hpp"
7
+ #include "async/envmou_close.hpp"
8
+
9
+ #ifdef _WIN32
10
+ #include <windows.h>
11
+ #else
12
+ #include <pthread.h>
13
+ #endif
14
+
15
+ namespace mdbxmou {
16
+
17
+ Napi::FunctionReference envmou::ctor{};
18
+
19
+ void envmou::init(const char *class_name, Napi::Env env, Napi::Object exports)
20
+ {
21
+ auto func = DefineClass(env, class_name, {
22
+ InstanceMethod("open", &envmou::open),
23
+ InstanceMethod("openSync", &envmou::open_sync),
24
+ InstanceMethod("close", &envmou::close),
25
+ InstanceMethod("closeSync", &envmou::close_sync),
26
+ InstanceMethod("copyTo", &envmou::copy_to),
27
+ InstanceMethod("copyToSync", &envmou::copy_to_sync),
28
+ InstanceMethod("version", &envmou::get_version),
29
+ InstanceMethod("startRead", &envmou::start_read),
30
+ InstanceMethod("startWrite", &envmou::start_write),
31
+ InstanceMethod("query", &envmou::query),
32
+ InstanceMethod("keys", &envmou::keys)
33
+ });
34
+ ctor = Napi::Persistent(func);
35
+ ctor.SuppressDestruct();
36
+ exports.Set(class_name, func);
37
+ }
38
+
39
+ mdbx::env::geometry envmou::parse_geometry(const Napi::Value& arg0)
40
+ {
41
+ mdbx::env::geometry geom{};
42
+
43
+ auto obj = arg0.As<Napi::Object>();
44
+
45
+ if (obj.Has("fixedSize")) {
46
+ auto value = obj.Get("fixedSize").As<Napi::Number>();
47
+ auto fixed_size = static_cast<intptr_t>(value.Int64Value());
48
+ return geom.make_fixed(fixed_size);
49
+ } else if (obj.Has("dynamicSize")) {
50
+ auto arr = obj.Get("dynamicSize").As<Napi::Array>();
51
+ if (arr.Length() != 2) {
52
+ throw Napi::TypeError::New(obj.Env(), "dynamicSize must be an array of two numbers");
53
+ }
54
+ auto v1 = arr.Get(0u).As<Napi::Number>();
55
+ auto v2 = arr.Get(1u).As<Napi::Number>();
56
+ auto size_lower = static_cast<intptr_t>(v1.Int64Value());
57
+ auto size_upper = static_cast<intptr_t>(v2.Int64Value());
58
+ return geom.make_dynamic(size_lower, size_upper);
59
+ } else {
60
+ if (obj.Has("sizeNow")) {
61
+ auto value = obj.Get("sizeNow").As<Napi::Number>();
62
+ geom.size_now = static_cast<intptr_t>(value.Int64Value());
63
+ }
64
+ if (obj.Has("sizeUpper")) {
65
+ auto value = obj.Get("sizeUpper").As<Napi::Number>();
66
+ geom.size_upper = static_cast<intptr_t>(value.Int64Value());
67
+ }
68
+ if (obj.Has("growthStep")) {
69
+ auto value = obj.Get("growthStep").As<Napi::Number>();
70
+ geom.growth_step = static_cast<intptr_t>(value.Int64Value());
71
+ }
72
+ if (obj.Has("shrinkThreshold")) {
73
+ auto value = obj.Get("shrinkThreshold").As<Napi::Number>();
74
+ geom.shrink_threshold = static_cast<intptr_t>(value.Int64Value());
75
+ }
76
+ if (obj.Has("pageSize")) {
77
+ auto value = obj.Get("pageSize").As<Napi::Number>();
78
+ geom.pagesize = static_cast<intptr_t>(value.Int64Value());
79
+ }
80
+ }
81
+ return geom;
82
+ }
83
+
84
+ env_arg0 envmou::parse(const Napi::Value& arg0)
85
+ {
86
+ env_arg0 rc;
87
+
88
+ auto obj = arg0.As<Napi::Object>();
89
+
90
+ rc.path = obj.Get("path").As<Napi::String>().Utf8Value();
91
+ if (obj.Has("maxDbi")) {
92
+ auto value = obj.Get("maxDbi").As<Napi::Number>();
93
+ rc.max_dbi = static_cast<MDBX_dbi>(value.Uint32Value());
94
+ }
95
+
96
+ if (obj.Has("geometry")) {
97
+ rc.geom = parse_geometry(obj.Get("geometry"));
98
+ }
99
+
100
+ if (obj.Has("flags")) {
101
+ rc.flag = env_flag::parse(obj.Get("flags"));
102
+ }
103
+
104
+ if (obj.Has("mode")) {
105
+ auto value = obj.Get("mode").As<Napi::Number>();
106
+ rc.file_mode = static_cast<mdbx_mode_t>(value.Int32Value());
107
+ }
108
+
109
+ if (obj.Has("keyFlag")) {
110
+ rc.key_flag = base_flag::parse_key(obj.Get("keyFlag"));
111
+ }
112
+
113
+ if (obj.Has("valueFlag")) {
114
+ rc.value_flag = base_flag::parse_value(obj.Get("valueFlag"));
115
+ }
116
+
117
+ return rc;
118
+ }
119
+
120
+ Napi::Value envmou::open(const Napi::CallbackInfo& info)
121
+ {
122
+ auto env = info.Env();
123
+ auto arg0 = parse(info[0].As<Napi::Object>());
124
+
125
+ try {
126
+ // асинхронный вызов разлочится внутри worker'a
127
+ if (!try_lock()) {
128
+ throw std::runtime_error("in progress");
129
+ }
130
+
131
+ if (is_open()) {
132
+ throw std::runtime_error("already opened");
133
+ }
134
+
135
+ auto* worker = new async_open(env, *this, arg0);
136
+ Napi::Promise promise = worker->GetPromise();
137
+ worker->Queue();
138
+ return promise;
139
+ } catch (const std::exception& e) {
140
+ unlock();
141
+ throw Napi::Error::New(env, e.what());
142
+ } catch (...) {
143
+ unlock();
144
+ throw;
145
+ }
146
+
147
+ return env.Undefined();
148
+ }
149
+
150
+ Napi::Value envmou::open_sync(const Napi::CallbackInfo& info)
151
+ {
152
+ auto env = info.Env();
153
+ auto arg0 = parse(info[0].As<Napi::Object>());
154
+
155
+ try {
156
+ lock_guard l(*this);
157
+ if (is_open()) {
158
+ throw std::runtime_error("already opened");
159
+ }
160
+ attach(create_and_open(arg0), arg0);
161
+ } catch (const std::exception& e) {
162
+ throw Napi::Error::New(env, e.what());
163
+ }
164
+
165
+ return env.Undefined();
166
+ }
167
+
168
+ MDBX_env* envmou::create_and_open(const env_arg0& arg0)
169
+ {
170
+ MDBX_env *env;
171
+ auto rc = mdbx_env_create(&env);
172
+ if (rc != MDBX_SUCCESS) {
173
+ throw std::runtime_error(mdbx_strerror(rc));
174
+ }
175
+
176
+ rc = mdbx_env_set_maxdbs(env, arg0.max_dbi);
177
+ if (rc != MDBX_SUCCESS) {
178
+ mdbx_env_close(env);
179
+ throw std::runtime_error(mdbx_strerror(rc));
180
+ }
181
+
182
+ rc = mdbx_env_set_maxreaders(env, arg0.max_readers);
183
+ if (rc != MDBX_SUCCESS) {
184
+ mdbx_env_close(env);
185
+ throw std::runtime_error(mdbx_strerror(rc));
186
+ }
187
+
188
+ auto& geom = arg0.geom;
189
+ rc = mdbx_env_set_geometry(env, geom.size_lower, geom.size_now,
190
+ geom.size_upper, geom.growth_step, geom.shrink_threshold, geom.pagesize);
191
+ if (rc != MDBX_SUCCESS) {
192
+ mdbx_env_close(env);
193
+ throw std::runtime_error(mdbx_strerror(rc));
194
+ }
195
+
196
+ #ifdef _WIN32
197
+ auto id = static_cast<std::uint32_t>(GetCurrentThreadId());
198
+ #else
199
+ auto id = static_cast<std::uint32_t>(pthread_self());
200
+ #endif
201
+ // выдадим параметры mode, flag и id потока в котором открывается env
202
+ rc = mdbx_env_open(env, arg0.path.c_str(), arg0.flag, arg0.file_mode);
203
+ if (rc != MDBX_SUCCESS) {
204
+ mdbx_env_close(env);
205
+ throw std::runtime_error(mdbx_strerror(rc));
206
+ }
207
+
208
+ return env;
209
+ }
210
+
211
+ void envmou::attach(MDBX_env* env, const env_arg0& arg0)
212
+ {
213
+ arg0_ = arg0;
214
+
215
+ auto rc = mdbx_env_set_userctx(env, &arg0_);
216
+ if (rc != MDBX_SUCCESS) {
217
+ mdbx_env_close(env);
218
+ throw std::runtime_error(mdbx_strerror(rc));
219
+ }
220
+
221
+ env_.reset(env);
222
+ }
223
+
224
+ Napi::Value envmou::close(const Napi::CallbackInfo& info)
225
+ {
226
+ auto env = info.Env();
227
+ try {
228
+ // асинхронный вызов разлочится внутри worker'a
229
+ if (!try_lock()) {
230
+ throw std::runtime_error("in progress");
231
+ }
232
+
233
+ if (!is_open()) {
234
+ return env.Undefined();
235
+ }
236
+
237
+ if (trx_count_.load() > 0) {
238
+ throw std::runtime_error("active transactions");
239
+ }
240
+
241
+ auto* worker = new async_close(env, *this);
242
+ Napi::Promise promise = worker->GetPromise();
243
+ worker->Queue();
244
+ return promise;
245
+ } catch (const std::exception& e) {
246
+ unlock();
247
+ throw Napi::Error::New(env, e.what());
248
+ } catch (...) {
249
+ unlock();
250
+ throw;
251
+ }
252
+
253
+ return env.Undefined();
254
+ }
255
+
256
+ Napi::Value envmou::close_sync(const Napi::CallbackInfo& info)
257
+ {
258
+ auto env = info.Env();
259
+ try {
260
+ lock_guard l(*this);
261
+
262
+ do_close();
263
+
264
+ } catch (const std::exception& e) {
265
+ throw Napi::Error::New(env, e.what());
266
+ }
267
+ return env.Undefined();
268
+ }
269
+
270
+ Napi::Value envmou::copy_to_sync(const Napi::CallbackInfo& info)
271
+ {
272
+ auto env = info.Env();
273
+
274
+ if (info.Length() < 1 || !info[0].IsString()) {
275
+ throw Napi::TypeError::New(env, "expected a string argument for the destination path");
276
+ }
277
+
278
+ MDBX_copy_flags_t flags{MDBX_CP_COMPACT};
279
+ if ((info.Length() > 1) && info[1].IsNumber()) {
280
+ flags = static_cast<MDBX_copy_flags_t>(info[1].As<Napi::Number>().Uint32Value());
281
+ }
282
+
283
+ try {
284
+ auto dest_path = info[0].As<Napi::String>().Utf8Value();
285
+
286
+ lock_guard l(*this);
287
+
288
+ check();
289
+
290
+ auto rc = mdbx_env_copy(*this, dest_path.c_str(), flags);
291
+ if (rc != MDBX_SUCCESS) {
292
+ throw Napi::Error::New(env, mdbx_strerror(rc));
293
+ }
294
+
295
+ } catch (const std::exception& e) {
296
+ throw Napi::Error::New(env, e.what());
297
+ }
298
+
299
+ return env.Undefined();
300
+ }
301
+
302
+ Napi::Value envmou::copy_to(const Napi::CallbackInfo& info)
303
+ {
304
+ Napi::Env env = info.Env();
305
+
306
+ if (info.Length() < 1 || !info[0].IsString()) {
307
+ throw Napi::TypeError::New(env, "copyTo(path: string[, flags?: number]) -> Promise<void>");
308
+ }
309
+
310
+ MDBX_copy_flags_t flags{MDBX_CP_COMPACT};
311
+ if (info.Length() > 1 && info[1].IsNumber()) {
312
+ flags = static_cast<MDBX_copy_flags_t>(info[1].As<Napi::Number>().Uint32Value());
313
+ }
314
+
315
+ try {
316
+ auto dest = info[0].As<Napi::String>().Utf8Value();
317
+
318
+ if (!try_lock()) {
319
+ throw std::runtime_error("in progress");
320
+ }
321
+
322
+ check();
323
+
324
+ auto* worker = new mdbxmou::async_copy(env, *this, std::move(dest), flags);
325
+ Napi::Promise promise = worker->GetPromise();
326
+ worker->Queue();
327
+ return promise;
328
+ } catch (const std::exception& e) {
329
+ throw Napi::Error::New(env, e.what());
330
+ }
331
+
332
+ return env.Undefined();
333
+ }
334
+
335
+ Napi::Value envmou::get_version(const Napi::CallbackInfo& info)
336
+ {
337
+ std::string version = "mdbx v" + std::to_string(MDBX_VERSION_MAJOR);
338
+ version += "." + std::to_string(MDBX_VERSION_MINOR);
339
+ return Napi::Value::From(info.Env(), version);
340
+ }
341
+
342
+ Napi::Value envmou::start_transaction(const Napi::CallbackInfo& info, txn_mode mode)
343
+ {
344
+ auto env = info.Env();
345
+
346
+ try {
347
+ lock_guard l(*this);
348
+
349
+ check();
350
+
351
+ MDBX_txn* txn;
352
+ auto rc = mdbx_txn_begin(*this, nullptr, mode, &txn);
353
+ if (rc != MDBX_SUCCESS) {
354
+ throw Napi::Error::New(env, std::string("Env: ") + mdbx_strerror(rc));
355
+ }
356
+
357
+ // Создаем новый объект txnmou
358
+ auto txn_obj = txnmou::ctor.New({});
359
+ auto txn_wrapper = txnmou::Unwrap(txn_obj);
360
+ txn_wrapper->attach(*this, txn, mode, nullptr);
361
+
362
+ return txn_obj;
363
+ } catch (const std::exception& e) {
364
+ throw Napi::Error::New(env, e.what());
365
+ }
366
+ }
367
+
368
+ Napi::Value envmou::query(const Napi::CallbackInfo& info)
369
+ {
370
+ Napi::Env env = info.Env();
371
+
372
+ txn_mode mode{};
373
+
374
+ if (info.Length() < 1) {
375
+ throw Napi::TypeError::New(env, "expected array of requests");
376
+ }
377
+
378
+ if (info.Length() > 1 || info[1].IsNumber()) {
379
+ mode = txn_mode::parse(info[1].As<Napi::Number>());
380
+ }
381
+
382
+ try
383
+ {
384
+ lock_guard lock(*this);
385
+
386
+ check();
387
+
388
+ auto conf = get_env_userctx(*this);
389
+
390
+ auto arg0 = info[0];
391
+ query_request query = parse_query(mode, arg0);
392
+ auto* worker = new async_query(env, *this, mode,
393
+ std::move(query), arg0.IsObject());
394
+ auto promise = worker->GetPromise();
395
+ worker->Queue();
396
+
397
+ // Увеличиваем счетчик транзакций после успешного создания
398
+ ++(*this);
399
+
400
+ return promise;
401
+ } catch (const std::exception& e) {
402
+ throw Napi::Error::New(env, e.what());
403
+ } catch (...) {
404
+ throw Napi::Error::New(env, "envmou::query");
405
+ }
406
+ return env.Undefined();
407
+ }
408
+
409
+ Napi::Value envmou::keys(const Napi::CallbackInfo& info)
410
+ {
411
+ Napi::Env env = info.Env();
412
+
413
+ txn_mode mode{};
414
+
415
+ if (info.Length() < 1) {
416
+ throw Napi::TypeError::New(env,
417
+ "expected array of requests: [{ db: String, db_mode: Number, key_mode: Number, key_flag: Number, value_mode: Number, value_flag: Number }, ...]");
418
+ }
419
+
420
+ if (info.Length() > 1 || info[1].IsNumber()) {
421
+ mode = txn_mode::parse(info[1].As<Napi::Number>());
422
+ }
423
+
424
+ try
425
+ {
426
+ lock_guard lock(*this);
427
+
428
+ check();
429
+
430
+ auto arg0 = info[0];
431
+ keys_request query = parse_keys(arg0);
432
+
433
+ auto* worker = new async_keys(env, *this, mode,
434
+ std::move(query), arg0.IsObject());
435
+ auto promise = worker->GetPromise();
436
+ worker->Queue();
437
+
438
+ // Увеличиваем счетчик транзакций после успешного создания
439
+ ++(*this);
440
+
441
+ return promise;
442
+ } catch (const std::exception& e) {
443
+ throw Napi::Error::New(env, e.what());
444
+ } catch (...) {
445
+ throw Napi::Error::New(env, "envmou::keys");
446
+ }
447
+ return env.Undefined();
448
+ }
449
+
450
+ } // namespace mdbxmou
@@ -0,0 +1,116 @@
1
+ #pragma once
2
+
3
+ #include "txnmou.hpp"
4
+ #include <memory>
5
+ #include <atomic>
6
+ #include <mutex>
7
+
8
+ namespace mdbxmou {
9
+
10
+ // Forward declaration
11
+ class txnmou;
12
+
13
+ class envmou final
14
+ : public Napi::ObjectWrap<envmou>
15
+ {
16
+ static Napi::FunctionReference ctor;
17
+ static mdbx::env::geometry parse_geometry(const Napi::Value& obj);
18
+ static env_arg0 parse(const Napi::Value& obj);
19
+
20
+ struct free_env {
21
+ void operator()(MDBX_env* env) const {
22
+ auto rc = mdbx_env_close(env);
23
+ if (rc != MDBX_SUCCESS) {
24
+ fprintf(stderr, "mdbx_env_close %s\n", mdbx_strerror(rc));
25
+ }
26
+ }
27
+ };
28
+
29
+ std::unique_ptr<MDBX_env, free_env> env_{};
30
+ std::atomic<std::size_t> trx_count_{};
31
+ env_arg0 arg0_{};
32
+ std::mutex lock_{};
33
+
34
+ bool is_open() const {
35
+ return env_ != nullptr;
36
+ }
37
+
38
+ // Inline метод для проверки что база данных открыта
39
+ void check() const {
40
+ if (!is_open()) {
41
+ throw std::runtime_error("closed");
42
+ }
43
+ }
44
+
45
+ // Общий метод для создания транзакций
46
+ Napi::Value start_transaction(const Napi::CallbackInfo& info, txn_mode mode);
47
+
48
+ public:
49
+ envmou(const Napi::CallbackInfo& i)
50
+ : ObjectWrap{i}
51
+ { }
52
+
53
+ static MDBX_env* create_and_open(const env_arg0& arg0);
54
+ void attach(MDBX_env* env, const env_arg0& arg0);
55
+
56
+ operator MDBX_env*() const noexcept {
57
+ return env_.get();
58
+ }
59
+
60
+ static void init(const char *class_name, Napi::Env env, Napi::Object exports);
61
+
62
+ envmou& operator++() noexcept {
63
+ ++trx_count_;
64
+ return *this;
65
+ }
66
+
67
+ envmou& operator--() noexcept {
68
+ --trx_count_;
69
+ return *this;
70
+ }
71
+
72
+ Napi::Value open(const Napi::CallbackInfo&);
73
+ Napi::Value open_sync(const Napi::CallbackInfo&);
74
+ Napi::Value close(const Napi::CallbackInfo&);
75
+ Napi::Value close_sync(const Napi::CallbackInfo&);
76
+ Napi::Value get_version(const Napi::CallbackInfo&);
77
+ Napi::Value copy_to_sync(const Napi::CallbackInfo&);
78
+ Napi::Value copy_to(const Napi::CallbackInfo&);
79
+ // метод для групповых вставок или чтения
80
+ // внутри транзакция, получение db и чтение/запись
81
+ Napi::Value query(const Napi::CallbackInfo&);
82
+ Napi::Value keys(const Napi::CallbackInfo&);
83
+
84
+ // Методы для создания транзакций
85
+ Napi::Value start_read(const Napi::CallbackInfo& info) {
86
+ return start_transaction(info, {txn_mode::ro});
87
+ }
88
+ Napi::Value start_write(const Napi::CallbackInfo& info) {
89
+ return start_transaction(info, {});
90
+ }
91
+
92
+ using lock_guard = std::lock_guard<envmou>;
93
+ // для защиты асинхронных операций
94
+ void lock() {
95
+ auto rc = lock_.try_lock();
96
+ if (!rc) {
97
+ throw std::runtime_error("operation in progress");
98
+ }
99
+ }
100
+ void unlock() {
101
+ lock_.unlock();
102
+ }
103
+ bool try_lock() {
104
+ bool locked = lock_.try_lock();
105
+ return locked;
106
+ }
107
+
108
+ void do_close() {
109
+ if (trx_count_.load() > 0) {
110
+ throw std::runtime_error("transaction in progress");
111
+ }
112
+ env_.reset();
113
+ }
114
+ };
115
+
116
+ } // namespace mdbxmou
package/src/envmou.cpp CHANGED
@@ -357,7 +357,7 @@ Napi::Value envmou::start_transaction(const Napi::CallbackInfo& info, txn_mode m
357
357
  // Создаем новый объект txnmou
358
358
  auto txn_obj = txnmou::ctor.New({});
359
359
  auto txn_wrapper = txnmou::Unwrap(txn_obj);
360
- txn_wrapper->attach(*this, txn, mode, nullptr);
360
+ txn_wrapper->attach(*this, txn, mode);
361
361
 
362
362
  return txn_obj;
363
363
  } catch (const std::exception& e) {