mdbxmou 0.2.5 → 0.2.6
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.
- package/README.md +8 -2
- package/package.json +1 -1
- package/src/dbimou.hpp +3 -0
- package/src/txnmou.cpp +5 -4
- package/src/txnmou.hpp +6 -10
- package/src/typemou.hpp +85 -9
- package/src/valuemou.hpp +3 -2
package/README.md
CHANGED
|
@@ -132,8 +132,11 @@ const result = await env.query([
|
|
|
132
132
|
|
|
133
133
|
#### Methods
|
|
134
134
|
|
|
135
|
-
**createMap(db_name | keyMode, [keyMode | valueMode], [valueMode]) → DBI**
|
|
135
|
+
**createMap([db_name | keyMode], [keyMode | valueMode], [valueMode]) → DBI**
|
|
136
136
|
```javascript
|
|
137
|
+
// No arguments - default DB with string keys
|
|
138
|
+
const dbi = txn.createMap();
|
|
139
|
+
|
|
137
140
|
// One argument - keyMode only
|
|
138
141
|
const dbi = txn.createMap(MDBX_Param.keyMode.ordinal);
|
|
139
142
|
|
|
@@ -152,8 +155,11 @@ const namedDbi = txn.createMap("my-table", MDBX_Param.keyMode.ordinal, MDBX_Para
|
|
|
152
155
|
|
|
153
156
|
> **Note**: Use `createMap` in write transactions - it will create the database if it doesn't exist, or open it if it does. This is safer for new environments.
|
|
154
157
|
|
|
155
|
-
**openMap(db_name | keyMode, [keyMode]) → DBI**
|
|
158
|
+
**openMap([db_name | keyMode], [keyMode]) → DBI**
|
|
156
159
|
```javascript
|
|
160
|
+
// No arguments - default DB with string keys
|
|
161
|
+
const dbi = txn.openMap();
|
|
162
|
+
|
|
157
163
|
// One argument - keyMode only
|
|
158
164
|
// Number keyMode - keys returned as numbers
|
|
159
165
|
const dbi = txn.openMap(MDBX_Param.keyMode.ordinal);
|
package/package.json
CHANGED
package/src/dbimou.hpp
CHANGED
|
@@ -33,6 +33,9 @@ public:
|
|
|
33
33
|
, dbi{}
|
|
34
34
|
{ }
|
|
35
35
|
|
|
36
|
+
//~dbimou() { fprintf(stderr, "dbimou::~dbimou %p key_cap=%zu val_cap=%zu\n",this, key_buf_.capacity(), val_buf_.capacity()); }
|
|
37
|
+
//void Finalize(Napi::Env env) { fprintf(stderr, "~dbimou %p key_cap=%zu val_cap=%zu\n",this, key_buf_.capacity(), val_buf_.capacity()); }
|
|
38
|
+
|
|
36
39
|
Napi::Value get_id(const Napi::CallbackInfo& info) {
|
|
37
40
|
return Napi::BigInt::New(info.Env(), static_cast<int64_t>(id_));
|
|
38
41
|
}
|
package/src/txnmou.cpp
CHANGED
|
@@ -97,7 +97,7 @@ Napi::Value txnmou::get_dbi(const Napi::CallbackInfo& info, db_mode db_mode)
|
|
|
97
97
|
if (arg0.IsString()) {
|
|
98
98
|
db_name = arg0.As<Napi::String>().Utf8Value();
|
|
99
99
|
key_mode = parse_key_mode(env, arg1, key_flag);
|
|
100
|
-
} else if (arg0.IsNumber()) {
|
|
100
|
+
} else if (arg0.IsNumber() || arg0.IsBigInt()) {
|
|
101
101
|
key_mode = parse_key_mode(env, arg0, key_flag);
|
|
102
102
|
value_mode = value_mode::parse(arg1);
|
|
103
103
|
} else {
|
|
@@ -108,12 +108,13 @@ Napi::Value txnmou::get_dbi(const Napi::CallbackInfo& info, db_mode db_mode)
|
|
|
108
108
|
auto arg0 = info[0];
|
|
109
109
|
if (arg0.IsString()) {
|
|
110
110
|
db_name = arg0.As<Napi::String>().Utf8Value();
|
|
111
|
-
} else {
|
|
111
|
+
} else if (arg0.IsNumber() || arg0.IsBigInt()) {
|
|
112
112
|
key_mode = parse_key_mode(env, arg0, key_flag);
|
|
113
|
+
} else {
|
|
114
|
+
throw Napi::Error::New(env, "Invalid argument type: expected string (db_name) or number (key_mode)");
|
|
113
115
|
}
|
|
114
|
-
} else {
|
|
115
|
-
throw Napi::Error::New(env, "Invalid number of arguments for get_dbi");
|
|
116
116
|
}
|
|
117
|
+
// arg_count == 0: используем значения по умолчанию (строковый ключ, default db)
|
|
117
118
|
|
|
118
119
|
check();
|
|
119
120
|
|
package/src/txnmou.hpp
CHANGED
|
@@ -10,16 +10,10 @@ class txnmou final
|
|
|
10
10
|
: public Napi::ObjectWrap<txnmou>
|
|
11
11
|
{
|
|
12
12
|
private:
|
|
13
|
-
envmou* env_{nullptr};
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
void operator()(MDBX_txn* txn) const noexcept {
|
|
18
|
-
mdbx_txn_abort(txn);
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
std::unique_ptr<MDBX_txn, free_txn> txn_{};
|
|
13
|
+
envmou* env_{nullptr};
|
|
14
|
+
|
|
15
|
+
std::unique_ptr<MDBX_txn,
|
|
16
|
+
txnmou_managed::free_txn> txn_{};
|
|
23
17
|
txn_mode mode_{};
|
|
24
18
|
|
|
25
19
|
void check() const {
|
|
@@ -46,6 +40,8 @@ public:
|
|
|
46
40
|
}
|
|
47
41
|
}
|
|
48
42
|
|
|
43
|
+
//void Finalize(Napi::Env env) { fprintf(stderr, "txnmou::Finalize %p\n", this); }
|
|
44
|
+
|
|
49
45
|
static void init(const char *class_name, Napi::Env env);
|
|
50
46
|
|
|
51
47
|
Napi::Value commit(const Napi::CallbackInfo&);
|
package/src/typemou.hpp
CHANGED
|
@@ -10,21 +10,97 @@ namespace mdbxmou {
|
|
|
10
10
|
using buffer_type = std::vector<char>;
|
|
11
11
|
|
|
12
12
|
struct txnmou_managed final
|
|
13
|
-
: mdbx::
|
|
13
|
+
: mdbx::txn
|
|
14
14
|
{
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
// свободу txn
|
|
16
|
+
struct free_txn
|
|
17
|
+
{
|
|
18
|
+
void operator()(MDBX_txn *txn) const noexcept {
|
|
19
|
+
mdbx_txn_abort(txn);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
std::unique_ptr<MDBX_txn, free_txn> guard_;
|
|
24
|
+
|
|
25
|
+
txnmou_managed(MDBX_txn* txn) noexcept
|
|
26
|
+
: mdbx::txn(txn)
|
|
27
|
+
, guard_(txn)
|
|
28
|
+
{ }
|
|
29
|
+
|
|
30
|
+
// Move конструктор
|
|
31
|
+
txnmou_managed(txnmou_managed&& other) noexcept
|
|
32
|
+
: mdbx::txn(other.txn::handle_)
|
|
33
|
+
, guard_(std::move(other.guard_))
|
|
34
|
+
{
|
|
35
|
+
other.txn::handle_ = nullptr;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Запрет копирования
|
|
39
|
+
txnmou_managed(const txnmou_managed&) = delete;
|
|
40
|
+
txnmou_managed& operator=(const txnmou_managed&) = delete;
|
|
41
|
+
|
|
42
|
+
~txnmou_managed() noexcept {
|
|
43
|
+
// guard_ автоматически откатит транзакцию
|
|
44
|
+
txn::handle_ = nullptr;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Commit транзакции
|
|
48
|
+
void commit() {
|
|
49
|
+
if (guard_) {
|
|
50
|
+
auto rc = ::mdbx_txn_commit(guard_.release());
|
|
51
|
+
txn::handle_ = nullptr;
|
|
52
|
+
if (rc != MDBX_SUCCESS) {
|
|
53
|
+
throw std::runtime_error(::mdbx_strerror(rc));
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Abort транзакции
|
|
59
|
+
void abort() {
|
|
60
|
+
if (guard_) {
|
|
61
|
+
auto rc = ::mdbx_txn_abort(guard_.release());
|
|
62
|
+
txn::handle_ = nullptr;
|
|
63
|
+
if (rc != MDBX_SUCCESS) {
|
|
64
|
+
throw std::runtime_error(::mdbx_strerror(rc));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
18
67
|
}
|
|
19
68
|
};
|
|
20
69
|
|
|
21
70
|
struct cursormou_managed final
|
|
22
|
-
: mdbx::
|
|
71
|
+
: mdbx::cursor
|
|
23
72
|
{
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
73
|
+
// свободу курсору
|
|
74
|
+
struct free_cursor
|
|
75
|
+
{
|
|
76
|
+
void operator()(MDBX_cursor* c) const noexcept {
|
|
77
|
+
if (c) ::mdbx_cursor_close(c);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
std::unique_ptr<MDBX_cursor, free_cursor> guard_;
|
|
82
|
+
|
|
83
|
+
explicit cursormou_managed(MDBX_cursor* cursor) noexcept
|
|
84
|
+
: mdbx::cursor(cursor)
|
|
85
|
+
, guard_(cursor)
|
|
86
|
+
{ }
|
|
87
|
+
|
|
88
|
+
// Move конструктор
|
|
89
|
+
cursormou_managed(cursormou_managed&& other) noexcept
|
|
90
|
+
: mdbx::cursor(other.cursor::handle_)
|
|
91
|
+
, guard_(std::move(other.guard_))
|
|
92
|
+
{
|
|
93
|
+
other.cursor::handle_ = nullptr;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Запрет копирования
|
|
97
|
+
cursormou_managed(const cursormou_managed&) = delete;
|
|
98
|
+
cursormou_managed& operator=(const cursormou_managed&) = delete;
|
|
99
|
+
|
|
100
|
+
~cursormou_managed() noexcept {
|
|
101
|
+
// guard_ автоматически закроет курсор
|
|
102
|
+
cursor::handle_ = nullptr;
|
|
103
|
+
}
|
|
28
104
|
};
|
|
29
105
|
|
|
30
106
|
struct env_flag {
|
package/src/valuemou.hpp
CHANGED
|
@@ -38,9 +38,8 @@ public:
|
|
|
38
38
|
if (status != napi_ok) {
|
|
39
39
|
throw Napi::Error::New(env, "napi_get_value_string_utf8 length");
|
|
40
40
|
}
|
|
41
|
-
|
|
41
|
+
|
|
42
42
|
mem.reserve(length + 1);
|
|
43
|
-
mem.resize(length);
|
|
44
43
|
|
|
45
44
|
status = napi_get_value_string_utf8(
|
|
46
45
|
env, arg0, mem.data(), mem.capacity(), nullptr);
|
|
@@ -48,6 +47,8 @@ public:
|
|
|
48
47
|
throw Napi::Error::New(env, "napi_get_value_string_utf8 copyout");
|
|
49
48
|
}
|
|
50
49
|
|
|
50
|
+
mem.resize(length);
|
|
51
|
+
|
|
51
52
|
assign(mem.data(), mem.size());
|
|
52
53
|
}
|
|
53
54
|
|