k2hash 1.1.34 → 2.0.1
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/binding.gyp +106 -0
- package/build/cjs/index.js +296 -0
- package/build/esm/index.js +293 -0
- package/buildutils/make_node_prebuild_variables.sh +359 -0
- package/buildutils/node_prebuild.sh +371 -0
- package/buildutils/node_prebuild_install.sh +307 -0
- package/index.js +4 -1
- package/index.mjs +154 -0
- package/package.json +76 -20
- package/src/index.ts +378 -0
- package/src/k2h_cbs.cc +27 -41
- package/src/k2h_cbs.h +17 -13
- package/src/k2h_common.h +1 -1
- package/src/k2h_keyqueue.cc +513 -339
- package/src/k2h_keyqueue.h +38 -30
- package/src/k2h_keyqueue_async.h +222 -184
- package/src/k2h_queue.cc +483 -304
- package/src/k2h_queue.h +38 -30
- package/src/k2h_queue_async.h +218 -173
- package/src/k2h_shm.cc +1963 -1221
- package/src/k2h_shm.h +109 -104
- package/src/k2h_shm_async.h +721 -564
- package/src/k2hash.cc +22 -8
- package/src/k2hkeyqueue.cc +21 -8
- package/src/k2hqueue.cc +21 -8
- package/types/index.d.ts +570 -0
- package/ChangeLog +0 -137
- package/src/binding.gyp +0 -146
package/src/k2h_shm_async.h
CHANGED
|
@@ -35,63 +35,75 @@
|
|
|
35
35
|
// Callback function: function(string error)
|
|
36
36
|
//
|
|
37
37
|
//---------------------------------------------------------
|
|
38
|
-
class
|
|
38
|
+
class CreateAsyncWorker : public Napi::AsyncWorker
|
|
39
39
|
{
|
|
40
40
|
public:
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
{
|
|
45
|
-
|
|
41
|
+
CreateAsyncWorker(const Napi::Function& callback, K2HShm* k2hshm, const std::string& filename, bool isfullmapping, int mask_bitcnt, int cmask_bitcnt, int max_element_cnt, size_t pagesize) :
|
|
42
|
+
Napi::AsyncWorker(callback), _callbackRef(Napi::Persistent(callback)), _k2hshm(k2hshm),
|
|
43
|
+
_filename(filename), _isfullmapping(isfullmapping), _mask_bitcnt(mask_bitcnt), _cmask_bitcnt(cmask_bitcnt), _max_element_cnt(max_element_cnt), _pagesize(pagesize), _result(false)
|
|
44
|
+
{
|
|
45
|
+
_callbackRef.Ref();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
~CreateAsyncWorker() override
|
|
49
|
+
{
|
|
50
|
+
if(_callbackRef){
|
|
51
|
+
_callbackRef.Unref();
|
|
52
|
+
_callbackRef.Reset();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
46
55
|
|
|
47
|
-
|
|
56
|
+
// Run on worker thread
|
|
57
|
+
void Execute() override
|
|
48
58
|
{
|
|
49
|
-
if(!
|
|
50
|
-
|
|
59
|
+
if(!_k2hshm){
|
|
60
|
+
SetError("No object is associated to async worker");
|
|
51
61
|
return;
|
|
52
62
|
}
|
|
53
|
-
if(!
|
|
54
|
-
//
|
|
55
|
-
|
|
63
|
+
if(!_k2hshm->Create(_filename.c_str(), _isfullmapping, _mask_bitcnt, _cmask_bitcnt, _max_element_cnt, _pagesize)){
|
|
64
|
+
SetError(std::string("Failed to create K2HASH file: ") + _filename); // call SetError method in Napi::AsyncWorker
|
|
65
|
+
return;
|
|
56
66
|
}
|
|
67
|
+
_result = true;
|
|
57
68
|
}
|
|
58
69
|
|
|
59
|
-
|
|
70
|
+
// handler for success
|
|
71
|
+
void OnOK() override
|
|
60
72
|
{
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
v8::Local<v8::Value> argv[argc] = { Nan::Null() };
|
|
73
|
+
Napi::Env env = Env();
|
|
74
|
+
Napi::HandleScope scope(env);
|
|
64
75
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
Nan::ThrowSyntaxError("Internal error in async worker");
|
|
69
|
-
return;
|
|
76
|
+
// The first argument is null and the second argument is the result.
|
|
77
|
+
if(!_callbackRef.IsEmpty()){
|
|
78
|
+
_callbackRef.Value().Call({ env.Null(), Napi::Boolean::New(env, _result) });
|
|
70
79
|
}
|
|
71
80
|
}
|
|
72
81
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
82
|
+
// handler for failure (by calling SetError)
|
|
83
|
+
void OnError(const Napi::Error& err) override
|
|
84
|
+
{
|
|
85
|
+
Napi::Env env = Env();
|
|
86
|
+
Napi::HandleScope scope(env);
|
|
78
87
|
|
|
79
|
-
|
|
80
|
-
|
|
88
|
+
// The first argument is the error message.
|
|
89
|
+
if(!_callbackRef.IsEmpty()){
|
|
90
|
+
_callbackRef.Value().Call({ err.Value() });
|
|
81
91
|
}else{
|
|
82
|
-
|
|
83
|
-
|
|
92
|
+
// Throw error
|
|
93
|
+
err.ThrowAsJavaScriptException();
|
|
84
94
|
}
|
|
85
|
-
|
|
95
|
+
}
|
|
86
96
|
|
|
87
97
|
private:
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
int
|
|
93
|
-
int
|
|
94
|
-
|
|
98
|
+
Napi::FunctionReference _callbackRef;
|
|
99
|
+
K2HShm* _k2hshm;
|
|
100
|
+
std::string _filename;
|
|
101
|
+
bool _isfullmapping;
|
|
102
|
+
int _mask_bitcnt;
|
|
103
|
+
int _cmask_bitcnt;
|
|
104
|
+
int _max_element_cnt;
|
|
105
|
+
size_t _pagesize;
|
|
106
|
+
bool _result;
|
|
95
107
|
};
|
|
96
108
|
|
|
97
109
|
//---------------------------------------------------------
|
|
@@ -101,66 +113,78 @@ class CreateWorker : public Nan::AsyncWorker
|
|
|
101
113
|
// Callback function: function(string error)
|
|
102
114
|
//
|
|
103
115
|
//---------------------------------------------------------
|
|
104
|
-
class
|
|
116
|
+
class OpenAsyncWorker : public Napi::AsyncWorker
|
|
105
117
|
{
|
|
106
118
|
public:
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
{
|
|
111
|
-
|
|
119
|
+
OpenAsyncWorker(const Napi::Function& callback, K2HShm* k2hshm, const std::string& filename, bool isReadOnly, bool isCreate, bool isTempFile, bool isfullmapping, int mask_bitcnt, int cmask_bitcnt, int max_element_cnt, size_t pagesize) :
|
|
120
|
+
Napi::AsyncWorker(callback), _callbackRef(Napi::Persistent(callback)), _k2hshm(k2hshm),
|
|
121
|
+
_filename(filename), _isReadOnly(isReadOnly), _isCreate(isCreate), _isTempFile(isTempFile), _isfullmapping(isfullmapping), _mask_bitcnt(mask_bitcnt), _cmask_bitcnt(cmask_bitcnt), _max_element_cnt(max_element_cnt), _pagesize(pagesize), _result(false)
|
|
122
|
+
{
|
|
123
|
+
_callbackRef.Ref();
|
|
124
|
+
}
|
|
112
125
|
|
|
113
|
-
|
|
126
|
+
~OpenAsyncWorker() override
|
|
114
127
|
{
|
|
115
|
-
if(
|
|
116
|
-
|
|
128
|
+
if(_callbackRef){
|
|
129
|
+
_callbackRef.Unref();
|
|
130
|
+
_callbackRef.Reset();
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Run on worker thread
|
|
135
|
+
void Execute() override
|
|
136
|
+
{
|
|
137
|
+
if(!_k2hshm){
|
|
138
|
+
SetError("No object is associated to async worker");
|
|
117
139
|
return;
|
|
118
140
|
}
|
|
119
|
-
if(!
|
|
120
|
-
//
|
|
121
|
-
|
|
141
|
+
if(!_k2hshm->Attach(_filename.c_str(), _isReadOnly, _isCreate, _isTempFile, _isfullmapping, _mask_bitcnt, _cmask_bitcnt, _max_element_cnt, _pagesize)){
|
|
142
|
+
SetError(std::string("Failed to attach(open) k2hash object.")); // call SetError method in Napi::AsyncWorker
|
|
143
|
+
return;
|
|
122
144
|
}
|
|
145
|
+
_result = true;
|
|
123
146
|
}
|
|
124
147
|
|
|
125
|
-
|
|
148
|
+
// handler for success
|
|
149
|
+
void OnOK() override
|
|
126
150
|
{
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
v8::Local<v8::Value> argv[argc] = { Nan::Null() };
|
|
151
|
+
Napi::Env env = Env();
|
|
152
|
+
Napi::HandleScope scope(env);
|
|
130
153
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
Nan::ThrowSyntaxError("Internal error in async worker");
|
|
135
|
-
return;
|
|
154
|
+
// The first argument is null and the second argument is the result.
|
|
155
|
+
if(!_callbackRef.IsEmpty()){
|
|
156
|
+
_callbackRef.Value().Call({ env.Null(), Napi::Boolean::New(env, _result) });
|
|
136
157
|
}
|
|
137
158
|
}
|
|
138
159
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
160
|
+
// handler for failure (by calling SetError)
|
|
161
|
+
void OnError(const Napi::Error& err) override
|
|
162
|
+
{
|
|
163
|
+
Napi::Env env = Env();
|
|
164
|
+
Napi::HandleScope scope(env);
|
|
144
165
|
|
|
145
|
-
|
|
146
|
-
|
|
166
|
+
// The first argument is the error message.
|
|
167
|
+
if(!_callbackRef.IsEmpty()){
|
|
168
|
+
_callbackRef.Value().Call({ err.Value() });
|
|
147
169
|
}else{
|
|
148
|
-
|
|
149
|
-
|
|
170
|
+
// Throw error
|
|
171
|
+
err.ThrowAsJavaScriptException();
|
|
150
172
|
}
|
|
151
|
-
|
|
173
|
+
}
|
|
152
174
|
|
|
153
175
|
private:
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
bool
|
|
158
|
-
bool
|
|
159
|
-
bool
|
|
160
|
-
|
|
161
|
-
int
|
|
162
|
-
int
|
|
163
|
-
|
|
176
|
+
Napi::FunctionReference _callbackRef;
|
|
177
|
+
K2HShm* _k2hshm;
|
|
178
|
+
std::string _filename;
|
|
179
|
+
bool _isReadOnly;
|
|
180
|
+
bool _isCreate;
|
|
181
|
+
bool _isTempFile;
|
|
182
|
+
bool _isfullmapping;
|
|
183
|
+
int _mask_bitcnt;
|
|
184
|
+
int _cmask_bitcnt;
|
|
185
|
+
int _max_element_cnt;
|
|
186
|
+
size_t _pagesize;
|
|
187
|
+
bool _result;
|
|
164
188
|
};
|
|
165
189
|
|
|
166
190
|
//---------------------------------------------------------
|
|
@@ -170,54 +194,65 @@ class OpenWorker : public Nan::AsyncWorker
|
|
|
170
194
|
// Callback function: function(string error)
|
|
171
195
|
//
|
|
172
196
|
//---------------------------------------------------------
|
|
173
|
-
class
|
|
197
|
+
class CloseAsyncWorker : public Napi::AsyncWorker
|
|
174
198
|
{
|
|
175
199
|
public:
|
|
176
|
-
|
|
177
|
-
|
|
200
|
+
CloseAsyncWorker(const Napi::Function& callback, K2HShm* k2hshm) : Napi::AsyncWorker(callback), _callbackRef(Napi::Persistent(callback)), _k2hshm(k2hshm)
|
|
201
|
+
{
|
|
202
|
+
_callbackRef.Ref();
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
~CloseAsyncWorker() override
|
|
206
|
+
{
|
|
207
|
+
if(_callbackRef){
|
|
208
|
+
_callbackRef.Unref();
|
|
209
|
+
_callbackRef.Reset();
|
|
210
|
+
}
|
|
211
|
+
}
|
|
178
212
|
|
|
179
|
-
|
|
213
|
+
// Run on worker thread
|
|
214
|
+
void Execute() override
|
|
180
215
|
{
|
|
181
|
-
if(!
|
|
182
|
-
|
|
216
|
+
if(!_k2hshm){
|
|
217
|
+
SetError("No object is associated to async worker");
|
|
183
218
|
return;
|
|
184
219
|
}
|
|
185
|
-
if(!
|
|
186
|
-
//
|
|
187
|
-
|
|
220
|
+
if(!_k2hshm->Detach()){
|
|
221
|
+
SetError(std::string("Failed to close k2hash object.")); // call SetError method in Napi::AsyncWorker
|
|
222
|
+
return;
|
|
188
223
|
}
|
|
189
224
|
}
|
|
190
225
|
|
|
191
|
-
|
|
226
|
+
// handler for success
|
|
227
|
+
void OnOK() override
|
|
192
228
|
{
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
v8::Local<v8::Value> argv[argc] = { Nan::Null() };
|
|
229
|
+
Napi::Env env = Env();
|
|
230
|
+
Napi::HandleScope scope(env);
|
|
196
231
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
Nan::ThrowSyntaxError("Internal error in async worker");
|
|
201
|
-
return;
|
|
232
|
+
// The first argument is null and the second argument is the result.
|
|
233
|
+
if(!_callbackRef.IsEmpty()){
|
|
234
|
+
_callbackRef.Value().Call({ env.Null(), Napi::Boolean::New(env, true) });
|
|
202
235
|
}
|
|
203
236
|
}
|
|
204
237
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
238
|
+
// handler for failure (by calling SetError)
|
|
239
|
+
void OnError(const Napi::Error& err) override
|
|
240
|
+
{
|
|
241
|
+
Napi::Env env = Env();
|
|
242
|
+
Napi::HandleScope scope(env);
|
|
210
243
|
|
|
211
|
-
|
|
212
|
-
|
|
244
|
+
// The first argument is the error message.
|
|
245
|
+
if(!_callbackRef.IsEmpty()){
|
|
246
|
+
_callbackRef.Value().Call({ err.Value() });
|
|
213
247
|
}else{
|
|
214
|
-
|
|
215
|
-
|
|
248
|
+
// Throw error
|
|
249
|
+
err.ThrowAsJavaScriptException();
|
|
216
250
|
}
|
|
217
|
-
|
|
251
|
+
}
|
|
218
252
|
|
|
219
253
|
private:
|
|
220
|
-
|
|
254
|
+
Napi::FunctionReference _callbackRef;
|
|
255
|
+
K2HShm* _k2hshm;
|
|
221
256
|
};
|
|
222
257
|
|
|
223
258
|
//---------------------------------------------------------
|
|
@@ -227,104 +262,117 @@ class CloseWorker : public Nan::AsyncWorker
|
|
|
227
262
|
// Callback function: function(string error[, string value])
|
|
228
263
|
//
|
|
229
264
|
//---------------------------------------------------------
|
|
230
|
-
class
|
|
265
|
+
class GetValueAsyncWorker : public Napi::AsyncWorker
|
|
231
266
|
{
|
|
232
267
|
public:
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
268
|
+
GetValueAsyncWorker(const Napi::Function& callback, K2HShm* k2hshm, const char* pkey, const char* psubkey, bool attrchk, const char* ppass) :
|
|
269
|
+
Napi::AsyncWorker(callback), _callbackRef(Napi::Persistent(callback)), _k2hshm(k2hshm),
|
|
270
|
+
_is_key_set(pkey != nullptr), _strkey(pkey ? pkey : ""), _is_skey_set(psubkey != nullptr), _strsubkey(psubkey ? psubkey : ""), _is_attr_check(attrchk), _is_pass_set(ppass != nullptr), _strpass(ppass ? ppass : ""), _presult(nullptr)
|
|
236
271
|
{
|
|
237
|
-
|
|
238
|
-
is_skey_set = (NULL != psubkey);
|
|
239
|
-
is_pass_set = (NULL != ppass);
|
|
272
|
+
_callbackRef.Ref();
|
|
240
273
|
}
|
|
241
|
-
|
|
274
|
+
|
|
275
|
+
~GetValueAsyncWorker() override
|
|
242
276
|
{
|
|
243
|
-
|
|
277
|
+
if(_callbackRef){
|
|
278
|
+
_callbackRef.Unref();
|
|
279
|
+
_callbackRef.Reset();
|
|
280
|
+
}
|
|
281
|
+
K2H_Free(_presult);
|
|
244
282
|
}
|
|
245
283
|
|
|
246
|
-
|
|
284
|
+
// Run on worker thread
|
|
285
|
+
void Execute() override
|
|
247
286
|
{
|
|
248
|
-
if(!
|
|
249
|
-
|
|
287
|
+
if(!_k2hshm){
|
|
288
|
+
SetError("No object is associated to async worker");
|
|
250
289
|
return;
|
|
251
290
|
}
|
|
252
|
-
if(!
|
|
253
|
-
|
|
291
|
+
if(!_is_key_set){
|
|
292
|
+
SetError("Specified key is empty(null)");
|
|
254
293
|
return;
|
|
255
294
|
}
|
|
256
295
|
|
|
257
296
|
// check subkey if specified
|
|
258
|
-
if(
|
|
297
|
+
if(_is_skey_set){
|
|
259
298
|
// subkey is specified, thus need to check the key has it.
|
|
260
299
|
bool found = false;
|
|
261
|
-
K2HSubKeys* sk =
|
|
300
|
+
K2HSubKeys* sk = _k2hshm->GetSubKeys(_strkey.c_str());
|
|
262
301
|
if(sk){
|
|
263
302
|
strarr_t strarr;
|
|
264
303
|
sk->StringArray(strarr);
|
|
265
|
-
|
|
266
|
-
if(0 == strcmp(iter->c_str(), strsubkey.c_str())){
|
|
267
|
-
found = true;
|
|
268
|
-
break;
|
|
269
|
-
}
|
|
270
|
-
}
|
|
304
|
+
found = std::any_of(strarr.begin(), strarr.end(), [&](const auto &_str){ return _str == _strsubkey; });
|
|
271
305
|
delete sk;
|
|
272
306
|
}
|
|
273
307
|
if(!found){
|
|
274
|
-
|
|
275
|
-
this->SetErrorMessage("There is no specified subkey in key subkey list.");
|
|
308
|
+
SetError("There is no specified subkey in key subkey list.");
|
|
276
309
|
return;
|
|
277
310
|
}
|
|
278
|
-
|
|
311
|
+
_strkey = _strsubkey;
|
|
279
312
|
}
|
|
280
313
|
|
|
281
314
|
// get value
|
|
282
|
-
|
|
283
|
-
if(!
|
|
284
|
-
|
|
285
|
-
this->SetErrorMessage("Failed to get value from key/subkey or the value is empty(null).");
|
|
315
|
+
_presult = _k2hshm->Get(_strkey.c_str(), _is_attr_check, (_is_pass_set ? _strpass.c_str() : NULL));
|
|
316
|
+
if(!_presult){
|
|
317
|
+
SetError("Failed to get value from key/subkey or the value is empty(null).");
|
|
286
318
|
return;
|
|
287
319
|
}
|
|
288
320
|
}
|
|
289
321
|
|
|
290
|
-
|
|
322
|
+
// handler for success
|
|
323
|
+
void OnOK() override
|
|
291
324
|
{
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
v8::Local<v8::Value> argv[argc] = { Nan::Null(), Nan::New<v8::String>(presult).ToLocalChecked() };
|
|
325
|
+
Napi::Env env = Env();
|
|
326
|
+
Napi::HandleScope scope(env);
|
|
295
327
|
|
|
296
|
-
|
|
297
|
-
|
|
328
|
+
// The first argument is null and the second argument is the result.
|
|
329
|
+
if(!_callbackRef.IsEmpty()){
|
|
330
|
+
Napi::String jsValue = Napi::String::New(env, (_presult ? _presult : ""), (_presult ? static_cast<size_t>(strlen(_presult)) : 0));
|
|
331
|
+
_callbackRef.Value().Call({ env.Null(), jsValue });
|
|
298
332
|
}else{
|
|
299
|
-
|
|
300
|
-
return;
|
|
333
|
+
Napi::TypeError::New(env, "Internal error in async worker").ThrowAsJavaScriptException();
|
|
301
334
|
}
|
|
302
335
|
}
|
|
303
336
|
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
337
|
+
// handler for failure (by calling SetError)
|
|
338
|
+
void OnError(const Napi::Error& err) override
|
|
339
|
+
{
|
|
340
|
+
Napi::Env env = Env();
|
|
341
|
+
Napi::HandleScope scope(env);
|
|
342
|
+
|
|
343
|
+
std::string msg;
|
|
344
|
+
if(err.Value().IsObject()){
|
|
345
|
+
Napi::Object obj = err.Value().As<Napi::Object>();
|
|
346
|
+
Napi::Value msgval = obj.Get("message");
|
|
347
|
+
if(msgval.IsString()){
|
|
348
|
+
msg = msgval.As<Napi::String>().Utf8Value();
|
|
349
|
+
}else{
|
|
350
|
+
Napi::String _str = obj.ToString();
|
|
351
|
+
msg = _str.Utf8Value();
|
|
352
|
+
}
|
|
353
|
+
}else{
|
|
354
|
+
msg = "Unknown error";
|
|
355
|
+
}
|
|
309
356
|
|
|
310
|
-
|
|
311
|
-
|
|
357
|
+
// The first argument is the error message.
|
|
358
|
+
if(!_callbackRef.IsEmpty()){
|
|
359
|
+
_callbackRef.Value().Call({ Napi::String::New(env, msg), env.Null() });
|
|
312
360
|
}else{
|
|
313
|
-
|
|
314
|
-
return;
|
|
361
|
+
err.ThrowAsJavaScriptException();
|
|
315
362
|
}
|
|
316
|
-
|
|
363
|
+
}
|
|
317
364
|
|
|
318
365
|
private:
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
bool
|
|
326
|
-
|
|
327
|
-
|
|
366
|
+
Napi::FunctionReference _callbackRef;
|
|
367
|
+
K2HShm* _k2hshm;
|
|
368
|
+
bool _is_key_set;
|
|
369
|
+
std::string _strkey;
|
|
370
|
+
bool _is_skey_set;
|
|
371
|
+
std::string _strsubkey;
|
|
372
|
+
bool _is_attr_check;
|
|
373
|
+
bool _is_pass_set;
|
|
374
|
+
std::string _strpass;
|
|
375
|
+
char* _presult;
|
|
328
376
|
};
|
|
329
377
|
|
|
330
378
|
//---------------------------------------------------------
|
|
@@ -334,82 +382,101 @@ class GetValueWorker : public Nan::AsyncWorker
|
|
|
334
382
|
// Callback function: function(string error, array subkeys)
|
|
335
383
|
//
|
|
336
384
|
//---------------------------------------------------------
|
|
337
|
-
class
|
|
385
|
+
class GetSubkeysAsyncWorker : public Napi::AsyncWorker
|
|
338
386
|
{
|
|
339
387
|
public:
|
|
340
|
-
|
|
388
|
+
GetSubkeysAsyncWorker(const Napi::Function& callback, K2HShm* pobj, const char* pkey) :
|
|
389
|
+
Napi::AsyncWorker(callback), _callbackRef(Napi::Persistent(callback)), _k2hshm(pobj), _is_key_set(pkey != nullptr), _strkey(pkey ? pkey : ""), _subkey_array()
|
|
341
390
|
{
|
|
342
|
-
|
|
391
|
+
_callbackRef.Ref();
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
~GetSubkeysAsyncWorker() override
|
|
395
|
+
{
|
|
396
|
+
if(_callbackRef){
|
|
397
|
+
_callbackRef.Unref();
|
|
398
|
+
_callbackRef.Reset();
|
|
399
|
+
}
|
|
343
400
|
}
|
|
344
|
-
~GetSubkeysWorker() {}
|
|
345
401
|
|
|
346
|
-
|
|
402
|
+
// Run on worker thread
|
|
403
|
+
void Execute() override
|
|
347
404
|
{
|
|
348
|
-
if(!
|
|
349
|
-
|
|
405
|
+
if(!_k2hshm){
|
|
406
|
+
SetError("No object is associated to async worker");
|
|
350
407
|
return;
|
|
351
408
|
}
|
|
352
|
-
if(!
|
|
353
|
-
|
|
409
|
+
if(!_is_key_set){
|
|
410
|
+
SetError("Specified key is empty(null)");
|
|
354
411
|
return;
|
|
355
412
|
}
|
|
356
413
|
|
|
357
|
-
// get
|
|
358
|
-
K2HSubKeys* sk =
|
|
414
|
+
// get subkeys
|
|
415
|
+
K2HSubKeys* sk = _k2hshm->GetSubKeys(_strkey.c_str());
|
|
359
416
|
if(sk){
|
|
360
|
-
|
|
361
|
-
sk->StringArray(
|
|
362
|
-
delete
|
|
363
|
-
if(0 ==
|
|
364
|
-
|
|
365
|
-
this->SetErrorMessage("Failed to get subkey because the key does not have any subkey.");
|
|
417
|
+
_subkey_array.clear();
|
|
418
|
+
sk->StringArray(_subkey_array);
|
|
419
|
+
delete sk;
|
|
420
|
+
if(0 == _subkey_array.size()){
|
|
421
|
+
SetError("Failed to get subkey because the key does not have any subkey.");
|
|
366
422
|
return;
|
|
367
423
|
}
|
|
368
424
|
}else{
|
|
369
|
-
|
|
370
|
-
this->SetErrorMessage("Failed to get subkey from key or key does not have any subkey.");
|
|
425
|
+
SetError("Failed to get subkey from key or key does not have any subkey.");
|
|
371
426
|
return;
|
|
372
427
|
}
|
|
373
428
|
}
|
|
374
429
|
|
|
375
|
-
|
|
430
|
+
// handler for success
|
|
431
|
+
void OnOK() override
|
|
376
432
|
{
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
callback->Call(argc, argv);
|
|
433
|
+
Napi::Env env = Env();
|
|
434
|
+
Napi::HandleScope scope(env);
|
|
435
|
+
|
|
436
|
+
if(!_callbackRef.IsEmpty()){
|
|
437
|
+
Napi::Array retarr = Napi::Array::New(env, _subkey_array.size());
|
|
438
|
+
uint32_t idx = 0;
|
|
439
|
+
for(const auto &_str : _subkey_array){
|
|
440
|
+
retarr.Set(idx++, Napi::String::New(env, _str.c_str(), static_cast<size_t>(strlen(_str.c_str()))));
|
|
441
|
+
}
|
|
442
|
+
_callbackRef.Value().Call({ env.Null(), retarr });
|
|
388
443
|
}else{
|
|
389
|
-
|
|
390
|
-
return;
|
|
444
|
+
Napi::TypeError::New(env, "Internal error in async worker").ThrowAsJavaScriptException();
|
|
391
445
|
}
|
|
392
446
|
}
|
|
393
447
|
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
448
|
+
// handler for failure (by calling SetError)
|
|
449
|
+
void OnError(const Napi::Error& err) override
|
|
450
|
+
{
|
|
451
|
+
Napi::Env env = Env();
|
|
452
|
+
Napi::HandleScope scope(env);
|
|
453
|
+
|
|
454
|
+
std::string msg;
|
|
455
|
+
if(err.Value().IsObject()){
|
|
456
|
+
Napi::Object obj = err.Value().As<Napi::Object>();
|
|
457
|
+
Napi::Value mv = obj.Get("message");
|
|
458
|
+
if(mv.IsString()){
|
|
459
|
+
msg = mv.As<Napi::String>().Utf8Value();
|
|
460
|
+
}else{
|
|
461
|
+
msg = err.Message();
|
|
462
|
+
}
|
|
463
|
+
}else{
|
|
464
|
+
msg = err.Message();
|
|
465
|
+
}
|
|
399
466
|
|
|
400
|
-
if(
|
|
401
|
-
|
|
467
|
+
if(!_callbackRef.IsEmpty()){
|
|
468
|
+
_callbackRef.Value().Call({ Napi::String::New(env, msg), env.Null() });
|
|
402
469
|
}else{
|
|
403
|
-
|
|
404
|
-
return;
|
|
470
|
+
err.ThrowAsJavaScriptException();
|
|
405
471
|
}
|
|
406
|
-
|
|
472
|
+
}
|
|
407
473
|
|
|
408
474
|
private:
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
475
|
+
Napi::FunctionReference _callbackRef;
|
|
476
|
+
K2HShm* _k2hshm;
|
|
477
|
+
bool _is_key_set;
|
|
478
|
+
std::string _strkey;
|
|
479
|
+
strarr_t _subkey_array;
|
|
413
480
|
};
|
|
414
481
|
|
|
415
482
|
//---------------------------------------------------------
|
|
@@ -419,82 +486,101 @@ class GetSubkeysWorker : public Nan::AsyncWorker
|
|
|
419
486
|
// Callback function: function(string error, array attrs)
|
|
420
487
|
//
|
|
421
488
|
//---------------------------------------------------------
|
|
422
|
-
class
|
|
489
|
+
class GetAttrsAsyncWorker : public Napi::AsyncWorker
|
|
423
490
|
{
|
|
424
491
|
public:
|
|
425
|
-
|
|
492
|
+
GetAttrsAsyncWorker(const Napi::Function& callback, K2HShm* pobj, const char* pkey) :
|
|
493
|
+
Napi::AsyncWorker(callback), _callbackRef(Napi::Persistent(callback)), _k2hshm(pobj), _is_key_set(pkey != nullptr), _strkey(pkey ? pkey : ""), _attrs_array()
|
|
494
|
+
{
|
|
495
|
+
_callbackRef.Ref();
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
~GetAttrsAsyncWorker() override
|
|
426
499
|
{
|
|
427
|
-
|
|
500
|
+
if(_callbackRef){
|
|
501
|
+
_callbackRef.Unref();
|
|
502
|
+
_callbackRef.Reset();
|
|
503
|
+
}
|
|
428
504
|
}
|
|
429
|
-
~GetAttrsWorker() {}
|
|
430
505
|
|
|
431
|
-
|
|
506
|
+
// Run on worker thread
|
|
507
|
+
void Execute() override
|
|
432
508
|
{
|
|
433
|
-
if(!
|
|
434
|
-
|
|
509
|
+
if(!_k2hshm){
|
|
510
|
+
SetError("No object is associated to async worker");
|
|
435
511
|
return;
|
|
436
512
|
}
|
|
437
|
-
if(!
|
|
438
|
-
|
|
513
|
+
if(!_is_key_set){
|
|
514
|
+
SetError("Specified key is empty(null)");
|
|
439
515
|
return;
|
|
440
516
|
}
|
|
441
517
|
|
|
442
518
|
// get attributes
|
|
443
|
-
K2HAttrs* attrs =
|
|
519
|
+
K2HAttrs* attrs = _k2hshm->GetAttrs(_strkey.c_str());
|
|
444
520
|
if(attrs){
|
|
445
|
-
|
|
446
|
-
attrs->KeyStringArray(
|
|
521
|
+
_attrs_array.clear();
|
|
522
|
+
attrs->KeyStringArray(_attrs_array);
|
|
447
523
|
delete attrs;
|
|
448
|
-
if(0 ==
|
|
449
|
-
|
|
450
|
-
this->SetErrorMessage("Failed to get attributes because the key does not have any attribute.");
|
|
524
|
+
if(0 == _attrs_array.size()){
|
|
525
|
+
SetError("Failed to get attributes because the key does not have any attribute.");
|
|
451
526
|
return;
|
|
452
527
|
}
|
|
453
528
|
}else{
|
|
454
|
-
|
|
455
|
-
this->SetErrorMessage("Failed to get attributes from key or key does not have any attribute.");
|
|
529
|
+
SetError("Failed to get attributes from key or key does not have any attribute.");
|
|
456
530
|
return;
|
|
457
531
|
}
|
|
458
532
|
}
|
|
459
533
|
|
|
460
|
-
|
|
534
|
+
// handler for success
|
|
535
|
+
void OnOK() override
|
|
461
536
|
{
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
callback->Call(argc, argv);
|
|
537
|
+
Napi::Env env = Env();
|
|
538
|
+
Napi::HandleScope scope(env);
|
|
539
|
+
|
|
540
|
+
if(!_callbackRef.IsEmpty()){
|
|
541
|
+
Napi::Array retarr = Napi::Array::New(env, _attrs_array.size());
|
|
542
|
+
uint32_t idx = 0;
|
|
543
|
+
for(const auto &_str: _attrs_array){
|
|
544
|
+
retarr.Set(idx++, Napi::String::New(env, _str.c_str(), static_cast<size_t>(strlen(_str.c_str()))));
|
|
545
|
+
}
|
|
546
|
+
_callbackRef.Value().Call({ env.Null(), retarr });
|
|
473
547
|
}else{
|
|
474
|
-
|
|
475
|
-
return;
|
|
548
|
+
Napi::TypeError::New(env, "Internal error in async worker").ThrowAsJavaScriptException();
|
|
476
549
|
}
|
|
477
550
|
}
|
|
478
551
|
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
552
|
+
// handler for failure (by calling SetError)
|
|
553
|
+
void OnError(const Napi::Error& err) override
|
|
554
|
+
{
|
|
555
|
+
Napi::Env env = Env();
|
|
556
|
+
Napi::HandleScope scope(env);
|
|
557
|
+
|
|
558
|
+
std::string msg;
|
|
559
|
+
if(err.Value().IsObject()){
|
|
560
|
+
Napi::Object obj = err.Value().As<Napi::Object>();
|
|
561
|
+
Napi::Value mv = obj.Get("message");
|
|
562
|
+
if(mv.IsString()){
|
|
563
|
+
msg = mv.As<Napi::String>().Utf8Value();
|
|
564
|
+
}else{
|
|
565
|
+
msg = err.Message();
|
|
566
|
+
}
|
|
567
|
+
}else{
|
|
568
|
+
msg = err.Message();
|
|
569
|
+
}
|
|
484
570
|
|
|
485
|
-
if(
|
|
486
|
-
|
|
571
|
+
if(!_callbackRef.IsEmpty()){
|
|
572
|
+
_callbackRef.Value().Call({ Napi::String::New(env, msg), env.Null() });
|
|
487
573
|
}else{
|
|
488
|
-
|
|
489
|
-
return;
|
|
574
|
+
err.ThrowAsJavaScriptException();
|
|
490
575
|
}
|
|
491
|
-
|
|
576
|
+
}
|
|
492
577
|
|
|
493
578
|
private:
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
579
|
+
Napi::FunctionReference _callbackRef;
|
|
580
|
+
K2HShm* _k2hshm;
|
|
581
|
+
bool _is_key_set;
|
|
582
|
+
std::string _strkey;
|
|
583
|
+
strarr_t _attrs_array;
|
|
498
584
|
};
|
|
499
585
|
|
|
500
586
|
//---------------------------------------------------------
|
|
@@ -504,100 +590,120 @@ class GetAttrsWorker : public Nan::AsyncWorker
|
|
|
504
590
|
// Callback function: function(string error[, string value])
|
|
505
591
|
//
|
|
506
592
|
//---------------------------------------------------------
|
|
507
|
-
class
|
|
593
|
+
class GetAttrValueAsyncWorker : public Napi::AsyncWorker
|
|
508
594
|
{
|
|
509
595
|
public:
|
|
510
|
-
|
|
596
|
+
GetAttrValueAsyncWorker(const Napi::Function& callback, K2HShm* pobj, const char* pkey, const char* pattr) :
|
|
597
|
+
Napi::AsyncWorker(callback), _callbackRef(Napi::Persistent(callback)), _k2hshm(pobj), _is_key_set(pkey != nullptr), _strkey(pkey ? pkey : ""), _is_attr_set(pattr != nullptr), _strattr(pattr ? pattr : ""), _attrval()
|
|
598
|
+
{
|
|
599
|
+
_callbackRef.Ref();
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
~GetAttrValueAsyncWorker() override
|
|
511
603
|
{
|
|
512
|
-
|
|
513
|
-
|
|
604
|
+
if(_callbackRef){
|
|
605
|
+
_callbackRef.Unref();
|
|
606
|
+
_callbackRef.Reset();
|
|
607
|
+
}
|
|
514
608
|
}
|
|
515
|
-
~GetAttrValueWorker() {}
|
|
516
609
|
|
|
517
|
-
|
|
610
|
+
// Run on worker thread
|
|
611
|
+
void Execute() override
|
|
518
612
|
{
|
|
519
|
-
if(!
|
|
520
|
-
|
|
613
|
+
if(!_k2hshm){
|
|
614
|
+
SetError("No object is associated to async worker");
|
|
521
615
|
return;
|
|
522
616
|
}
|
|
523
|
-
if(!
|
|
524
|
-
|
|
617
|
+
if(!_is_key_set){
|
|
618
|
+
SetError("Specified key is empty(null)");
|
|
525
619
|
return;
|
|
526
620
|
}
|
|
527
|
-
if(!
|
|
528
|
-
|
|
621
|
+
if(!_is_attr_set){
|
|
622
|
+
SetError("Specified attribute key is empty(null)");
|
|
529
623
|
return;
|
|
530
624
|
}
|
|
531
625
|
|
|
532
626
|
// get attributes
|
|
533
|
-
K2HAttrs* attrs =
|
|
627
|
+
K2HAttrs* attrs = _k2hshm->GetAttrs(_strkey.c_str());
|
|
534
628
|
if(attrs){
|
|
535
629
|
bool is_found = false;
|
|
536
630
|
for(K2HAttrs::iterator iter = attrs->begin(); iter != attrs->end(); ++iter){
|
|
537
631
|
if(0UL == iter->keylength || !iter->pkey){
|
|
538
632
|
continue;
|
|
539
633
|
}
|
|
540
|
-
if(iter->keylength != static_cast<size_t>(
|
|
634
|
+
if(iter->keylength != static_cast<size_t>(_strattr.length() + 1)){
|
|
541
635
|
continue;
|
|
542
636
|
}
|
|
543
|
-
if(0 == memcmp(iter->pkey,
|
|
637
|
+
if(0 == memcmp(iter->pkey, _strattr.c_str(), iter->keylength)){
|
|
544
638
|
// found
|
|
545
639
|
if(0 < iter->vallength && iter->pval){
|
|
546
|
-
|
|
640
|
+
_attrval.assign(reinterpret_cast<const char*>(iter->pval), iter->vallength);
|
|
547
641
|
}else{
|
|
548
|
-
|
|
642
|
+
SetError("Failed to get attribute value because the attr does not have any value.");
|
|
549
643
|
}
|
|
550
|
-
is_found
|
|
644
|
+
is_found = true;
|
|
551
645
|
break;
|
|
552
646
|
}
|
|
553
647
|
}
|
|
554
648
|
delete attrs;
|
|
555
649
|
if(!is_found){
|
|
556
|
-
|
|
557
|
-
this->SetErrorMessage("Failed to get attribute value because the attr does not have any value.");
|
|
650
|
+
SetError("Failed to get attribute value because the attr does not have any value.");
|
|
558
651
|
}
|
|
559
652
|
}else{
|
|
560
|
-
|
|
561
|
-
this->SetErrorMessage("Failed to get attribute value from key or key does not have attribute.");
|
|
653
|
+
SetError("Failed to get attribute value from key or key does not have attribute.");
|
|
562
654
|
return;
|
|
563
655
|
}
|
|
564
656
|
}
|
|
565
657
|
|
|
566
|
-
|
|
658
|
+
// handler for success
|
|
659
|
+
void OnOK() override
|
|
567
660
|
{
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
v8::Local<v8::Value> argv[argc] = { Nan::Null(), Nan::New<v8::String>(attrval.c_str()).ToLocalChecked() };
|
|
661
|
+
Napi::Env env = Env();
|
|
662
|
+
Napi::HandleScope scope(env);
|
|
571
663
|
|
|
572
|
-
if(
|
|
573
|
-
|
|
664
|
+
if(!_callbackRef.IsEmpty()){
|
|
665
|
+
Napi::String jsVal = Napi::String::New(env, _attrval.c_str(), static_cast<size_t>(strlen(_attrval.c_str())));
|
|
666
|
+
_callbackRef.Value().Call({ env.Null(), jsVal });
|
|
574
667
|
}else{
|
|
575
|
-
|
|
576
|
-
return;
|
|
668
|
+
Napi::TypeError::New(env, "Internal error in async worker").ThrowAsJavaScriptException();
|
|
577
669
|
}
|
|
578
670
|
}
|
|
579
671
|
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
672
|
+
// handler for failure (by calling SetError)
|
|
673
|
+
void OnError(const Napi::Error& err) override
|
|
674
|
+
{
|
|
675
|
+
Napi::Env env = Env();
|
|
676
|
+
Napi::HandleScope scope(env);
|
|
677
|
+
|
|
678
|
+
// Extract error message
|
|
679
|
+
std::string msg;
|
|
680
|
+
if(err.Value().IsObject()){
|
|
681
|
+
Napi::Object obj = err.Value().As<Napi::Object>();
|
|
682
|
+
Napi::Value mv = obj.Get("message");
|
|
683
|
+
if(mv.IsString()){
|
|
684
|
+
msg = mv.As<Napi::String>().Utf8Value();
|
|
685
|
+
}else{
|
|
686
|
+
msg = err.Message();
|
|
687
|
+
}
|
|
688
|
+
}else{
|
|
689
|
+
msg = err.Message();
|
|
690
|
+
}
|
|
585
691
|
|
|
586
|
-
if(
|
|
587
|
-
|
|
692
|
+
if(!_callbackRef.IsEmpty()){
|
|
693
|
+
_callbackRef.Value().Call({ Napi::String::New(env, msg) });
|
|
588
694
|
}else{
|
|
589
|
-
|
|
590
|
-
return;
|
|
695
|
+
err.ThrowAsJavaScriptException();
|
|
591
696
|
}
|
|
592
|
-
|
|
697
|
+
}
|
|
593
698
|
|
|
594
699
|
private:
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
std::string
|
|
700
|
+
Napi::FunctionReference _callbackRef;
|
|
701
|
+
K2HShm* _k2hshm;
|
|
702
|
+
bool _is_key_set;
|
|
703
|
+
std::string _strkey;
|
|
704
|
+
bool _is_attr_set;
|
|
705
|
+
std::string _strattr;
|
|
706
|
+
std::string _attrval;
|
|
601
707
|
};
|
|
602
708
|
|
|
603
709
|
//---------------------------------------------------------
|
|
@@ -607,84 +713,89 @@ class GetAttrValueWorker : public Nan::AsyncWorker
|
|
|
607
713
|
// Callback function: function(string error)
|
|
608
714
|
//
|
|
609
715
|
//---------------------------------------------------------
|
|
610
|
-
class
|
|
716
|
+
class SetValueAsyncWorker : public Napi::AsyncWorker
|
|
611
717
|
{
|
|
612
718
|
public:
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
719
|
+
SetValueAsyncWorker(const Napi::Function& callback, K2HShm* pobj, const char* pkey, const char* psubkey, const char* pval, const char* ppass, const time_t* p_expire) :
|
|
720
|
+
Napi::AsyncWorker(callback), _callbackRef(Napi::Persistent(callback)), _k2hshm(pobj),
|
|
721
|
+
_is_key_set(pkey != nullptr), _strkey(pkey ? pkey : ""), _is_skey_set(psubkey != nullptr), _strsubkey(psubkey ? psubkey : ""), _is_val_set(pval != nullptr), _strval(pval ? pval : ""), _is_pass_set(ppass != nullptr), _strpass(ppass ? ppass : ""), _expire(p_expire ? *p_expire : 0)
|
|
616
722
|
{
|
|
617
|
-
|
|
618
|
-
is_skey_set = (NULL != psubkey);
|
|
619
|
-
is_val_set = (NULL != pval);
|
|
620
|
-
is_pass_set = (NULL != ppass);
|
|
723
|
+
_callbackRef.Ref();
|
|
621
724
|
}
|
|
622
|
-
~SetValueWorker() {}
|
|
623
725
|
|
|
624
|
-
|
|
726
|
+
~SetValueAsyncWorker() override
|
|
625
727
|
{
|
|
626
|
-
if(
|
|
627
|
-
|
|
728
|
+
if(_callbackRef){
|
|
729
|
+
_callbackRef.Unref();
|
|
730
|
+
_callbackRef.Reset();
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
// Run on worker thread
|
|
735
|
+
void Execute() override
|
|
736
|
+
{
|
|
737
|
+
if(!_k2hshm){
|
|
738
|
+
SetError("No object is associated to async worker");
|
|
628
739
|
return;
|
|
629
740
|
}
|
|
630
|
-
if(!
|
|
631
|
-
|
|
741
|
+
if(!_is_key_set){
|
|
742
|
+
SetError("Specified key is empty(null)");
|
|
632
743
|
return;
|
|
633
744
|
}
|
|
634
745
|
|
|
635
|
-
bool result;
|
|
636
|
-
if(
|
|
746
|
+
bool result = false;
|
|
747
|
+
if(_is_skey_set){
|
|
637
748
|
// subkey is specified
|
|
638
|
-
result =
|
|
749
|
+
result = _k2hshm->AddSubkey(_strkey.c_str(), _strsubkey.c_str(), (_is_val_set ? _strval.c_str() : NULL), (_is_pass_set ? _strpass.c_str() : NULL), (_expire > 0 ? &_expire : NULL));
|
|
639
750
|
}else{
|
|
640
751
|
// subkey is not specified
|
|
641
|
-
result =
|
|
752
|
+
result = _k2hshm->Set(_strkey.c_str(), (_is_val_set ? _strval.c_str() : NULL), (_is_pass_set ? _strpass.c_str() : NULL), (_expire > 0 ? &_expire : NULL));
|
|
642
753
|
}
|
|
643
754
|
if(!result){
|
|
644
|
-
|
|
645
|
-
|
|
755
|
+
SetError("Failed to set key/subkey and value.");
|
|
756
|
+
return;
|
|
646
757
|
}
|
|
647
758
|
}
|
|
648
759
|
|
|
649
|
-
|
|
760
|
+
// handler for success
|
|
761
|
+
void OnOK() override
|
|
650
762
|
{
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
v8::Local<v8::Value> argv[argc] = { Nan::Null() };
|
|
763
|
+
Napi::Env env = Env();
|
|
764
|
+
Napi::HandleScope scope(env);
|
|
654
765
|
|
|
655
|
-
if(
|
|
656
|
-
|
|
766
|
+
if(!_callbackRef.IsEmpty()){
|
|
767
|
+
_callbackRef.Value().Call({ env.Null() });
|
|
657
768
|
}else{
|
|
658
|
-
|
|
659
|
-
return;
|
|
769
|
+
Napi::TypeError::New(env, "Internal error in async worker").ThrowAsJavaScriptException();
|
|
660
770
|
}
|
|
661
771
|
}
|
|
662
772
|
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
773
|
+
// handler for failure (by calling SetError)
|
|
774
|
+
void OnError(const Napi::Error& err) override
|
|
775
|
+
{
|
|
776
|
+
Napi::Env env = Env();
|
|
777
|
+
Napi::HandleScope scope(env);
|
|
668
778
|
|
|
669
|
-
|
|
670
|
-
|
|
779
|
+
std::string msg = err.Message();
|
|
780
|
+
if(!_callbackRef.IsEmpty()){
|
|
781
|
+
_callbackRef.Value().Call({ Napi::String::New(env, msg) });
|
|
671
782
|
}else{
|
|
672
|
-
|
|
673
|
-
return;
|
|
783
|
+
err.ThrowAsJavaScriptException();
|
|
674
784
|
}
|
|
675
|
-
|
|
785
|
+
}
|
|
676
786
|
|
|
677
787
|
private:
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
788
|
+
Napi::FunctionReference _callbackRef;
|
|
789
|
+
K2HShm* _k2hshm;
|
|
790
|
+
bool _is_key_set;
|
|
791
|
+
std::string _strkey;
|
|
792
|
+
bool _is_skey_set;
|
|
793
|
+
std::string _strsubkey;
|
|
794
|
+
bool _is_val_set;
|
|
795
|
+
std::string _strval;
|
|
796
|
+
bool _is_pass_set;
|
|
797
|
+
std::string _strpass;
|
|
798
|
+
time_t _expire;
|
|
688
799
|
};
|
|
689
800
|
|
|
690
801
|
//---------------------------------------------------------
|
|
@@ -694,77 +805,82 @@ class SetValueWorker : public Nan::AsyncWorker
|
|
|
694
805
|
// Callback function: function(string error)
|
|
695
806
|
//
|
|
696
807
|
//---------------------------------------------------------
|
|
697
|
-
class
|
|
808
|
+
class AddSubkeyAsyncWorker : public Napi::AsyncWorker
|
|
698
809
|
{
|
|
699
810
|
public:
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
811
|
+
AddSubkeyAsyncWorker(const Napi::Function& callback, K2HShm* pobj, const char* pkey, const char* psubkey, const char* pval) :
|
|
812
|
+
Napi::AsyncWorker(callback), _callbackRef(Napi::Persistent(callback)), _k2hshm(pobj), _is_key_set(pkey != nullptr), _strkey(pkey ? pkey : ""), _is_skey_set(psubkey != nullptr), _strsubkey(psubkey ? psubkey : ""), _is_val_set(pval != nullptr), _strval(pval ? pval : "")
|
|
813
|
+
{
|
|
814
|
+
_callbackRef.Ref();
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
~AddSubkeyAsyncWorker() override
|
|
703
818
|
{
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
819
|
+
if(_callbackRef){
|
|
820
|
+
_callbackRef.Unref();
|
|
821
|
+
_callbackRef.Reset();
|
|
822
|
+
}
|
|
707
823
|
}
|
|
708
|
-
~AddSubkeyWorker() {}
|
|
709
824
|
|
|
710
|
-
|
|
825
|
+
// Run on worker thread
|
|
826
|
+
void Execute() override
|
|
711
827
|
{
|
|
712
|
-
if(!
|
|
713
|
-
|
|
828
|
+
if(!_k2hshm){
|
|
829
|
+
SetError("No object is associated to async worker");
|
|
714
830
|
return;
|
|
715
831
|
}
|
|
716
|
-
if(!
|
|
717
|
-
|
|
832
|
+
if(!_is_key_set){
|
|
833
|
+
SetError("Specified key is empty(null)");
|
|
718
834
|
return;
|
|
719
835
|
}
|
|
720
|
-
if(!
|
|
721
|
-
|
|
836
|
+
if(!_is_skey_set){
|
|
837
|
+
SetError("Specified subkey is empty(null)");
|
|
722
838
|
return;
|
|
723
839
|
}
|
|
724
840
|
|
|
725
841
|
// add subkey
|
|
726
|
-
if(!
|
|
727
|
-
|
|
728
|
-
|
|
842
|
+
if(!_k2hshm->AddSubkey(_strkey.c_str(), _strsubkey.c_str(), (_is_val_set ? _strval.c_str() : NULL))){
|
|
843
|
+
SetError("Failed to set subkey and value to key.");
|
|
844
|
+
return;
|
|
729
845
|
}
|
|
730
846
|
}
|
|
731
847
|
|
|
732
|
-
|
|
848
|
+
// handler for success
|
|
849
|
+
void OnOK() override
|
|
733
850
|
{
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
v8::Local<v8::Value> argv[argc] = { Nan::Null() };
|
|
851
|
+
Napi::Env env = Env();
|
|
852
|
+
Napi::HandleScope scope(env);
|
|
737
853
|
|
|
738
|
-
if(
|
|
739
|
-
|
|
854
|
+
if(!_callbackRef.IsEmpty()){
|
|
855
|
+
_callbackRef.Value().Call({ env.Null() });
|
|
740
856
|
}else{
|
|
741
|
-
|
|
742
|
-
return;
|
|
857
|
+
Napi::TypeError::New(env, "Internal error in async worker").ThrowAsJavaScriptException();
|
|
743
858
|
}
|
|
744
859
|
}
|
|
745
860
|
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
861
|
+
// handler for failure (by calling SetError)
|
|
862
|
+
void OnError(const Napi::Error& err) override
|
|
863
|
+
{
|
|
864
|
+
Napi::Env env = Env();
|
|
865
|
+
Napi::HandleScope scope(env);
|
|
751
866
|
|
|
752
|
-
|
|
753
|
-
|
|
867
|
+
std::string msg = err.Message();
|
|
868
|
+
if(!_callbackRef.IsEmpty()){
|
|
869
|
+
_callbackRef.Value().Call({ Napi::String::New(env, msg) });
|
|
754
870
|
}else{
|
|
755
|
-
|
|
756
|
-
return;
|
|
871
|
+
err.ThrowAsJavaScriptException();
|
|
757
872
|
}
|
|
758
|
-
|
|
873
|
+
}
|
|
759
874
|
|
|
760
875
|
private:
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
876
|
+
Napi::FunctionReference _callbackRef;
|
|
877
|
+
K2HShm* _k2hshm;
|
|
878
|
+
bool _is_key_set;
|
|
879
|
+
std::string _strkey;
|
|
880
|
+
bool _is_skey_set;
|
|
881
|
+
std::string _strsubkey;
|
|
882
|
+
bool _is_val_set;
|
|
883
|
+
std::string _strval;
|
|
768
884
|
};
|
|
769
885
|
|
|
770
886
|
//---------------------------------------------------------
|
|
@@ -774,84 +890,91 @@ class AddSubkeyWorker : public Nan::AsyncWorker
|
|
|
774
890
|
// Callback function: function(string error)
|
|
775
891
|
//
|
|
776
892
|
//---------------------------------------------------------
|
|
777
|
-
class
|
|
893
|
+
class AddSubkeysAsyncWorker : public Napi::AsyncWorker
|
|
778
894
|
{
|
|
779
895
|
public:
|
|
780
|
-
|
|
896
|
+
AddSubkeysAsyncWorker(const Napi::Function& callback, K2HShm* pobj, const char* pkey, const unsigned char* psubkeys, size_t skey_length) :
|
|
897
|
+
Napi::AsyncWorker(callback), _callbackRef(Napi::Persistent(callback)), _k2hshm(pobj), _is_key_set(pkey != nullptr), _strkey(pkey ? pkey : ""), _bySubkeys(nullptr), _skeylen(0), _alloc_error(false)
|
|
781
898
|
{
|
|
782
899
|
if(psubkeys && 0UL < skey_length){
|
|
783
|
-
if(
|
|
784
|
-
memcpy(
|
|
785
|
-
|
|
900
|
+
if(nullptr != (_bySubkeys = reinterpret_cast<unsigned char*>(malloc(skey_length)))){
|
|
901
|
+
memcpy(_bySubkeys, psubkeys, skey_length);
|
|
902
|
+
_skeylen = skey_length;
|
|
786
903
|
}else{
|
|
787
904
|
// could not allocate memory.
|
|
788
|
-
|
|
905
|
+
_alloc_error = true;
|
|
789
906
|
}
|
|
790
907
|
}
|
|
791
|
-
|
|
908
|
+
_callbackRef.Ref();
|
|
792
909
|
}
|
|
793
|
-
|
|
910
|
+
|
|
911
|
+
~AddSubkeysAsyncWorker() override
|
|
794
912
|
{
|
|
795
|
-
|
|
913
|
+
if(_callbackRef){
|
|
914
|
+
_callbackRef.Unref();
|
|
915
|
+
_callbackRef.Reset();
|
|
916
|
+
}
|
|
917
|
+
K2H_Free(_bySubkeys);
|
|
796
918
|
}
|
|
797
919
|
|
|
798
|
-
|
|
920
|
+
// Run on worker thread
|
|
921
|
+
void Execute() override
|
|
799
922
|
{
|
|
800
|
-
if(!
|
|
801
|
-
|
|
923
|
+
if(!_k2hshm){
|
|
924
|
+
SetError("No object is associated to async worker");
|
|
802
925
|
return;
|
|
803
926
|
}
|
|
804
|
-
if(!
|
|
805
|
-
|
|
927
|
+
if(!_is_key_set){
|
|
928
|
+
SetError("Specified key is empty(null)");
|
|
806
929
|
return;
|
|
807
930
|
}
|
|
808
|
-
if(
|
|
809
|
-
|
|
931
|
+
if(_alloc_error){
|
|
932
|
+
SetError("Could not allocate memory");
|
|
810
933
|
return;
|
|
811
934
|
}
|
|
812
935
|
|
|
813
936
|
// add subkeys
|
|
814
|
-
if(!
|
|
815
|
-
|
|
816
|
-
|
|
937
|
+
if(!_k2hshm->ReplaceSubkeys(reinterpret_cast<const unsigned char*>(_strkey.c_str()), _strkey.length() + 1, _bySubkeys, _skeylen)){
|
|
938
|
+
SetError("Failed to replace subkeys to key.");
|
|
939
|
+
return;
|
|
817
940
|
}
|
|
818
941
|
}
|
|
819
942
|
|
|
820
|
-
|
|
943
|
+
// handler for success
|
|
944
|
+
void OnOK() override
|
|
821
945
|
{
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
v8::Local<v8::Value> argv[argc] = { Nan::Null() };
|
|
946
|
+
Napi::Env env = Env();
|
|
947
|
+
Napi::HandleScope scope(env);
|
|
825
948
|
|
|
826
|
-
if(
|
|
827
|
-
|
|
949
|
+
if(!_callbackRef.IsEmpty()){
|
|
950
|
+
_callbackRef.Value().Call({ env.Null() });
|
|
828
951
|
}else{
|
|
829
|
-
|
|
830
|
-
return;
|
|
952
|
+
Napi::TypeError::New(env, "Internal error in async worker").ThrowAsJavaScriptException();
|
|
831
953
|
}
|
|
832
954
|
}
|
|
833
955
|
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
956
|
+
// handler for failure (by calling SetError)
|
|
957
|
+
void OnError(const Napi::Error& err) override
|
|
958
|
+
{
|
|
959
|
+
Napi::Env env = Env();
|
|
960
|
+
Napi::HandleScope scope(env);
|
|
839
961
|
|
|
840
|
-
|
|
841
|
-
|
|
962
|
+
std::string msg = err.Message();
|
|
963
|
+
if(!_callbackRef.IsEmpty()){
|
|
964
|
+
_callbackRef.Value().Call({ Napi::String::New(env, msg) });
|
|
842
965
|
}else{
|
|
843
|
-
|
|
844
|
-
return;
|
|
966
|
+
err.ThrowAsJavaScriptException();
|
|
845
967
|
}
|
|
846
|
-
|
|
968
|
+
}
|
|
847
969
|
|
|
848
970
|
private:
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
971
|
+
Napi::FunctionReference _callbackRef;
|
|
972
|
+
K2HShm* _k2hshm;
|
|
973
|
+
bool _is_key_set;
|
|
974
|
+
std::string _strkey;
|
|
975
|
+
unsigned char* _bySubkeys;
|
|
976
|
+
size_t _skeylen;
|
|
977
|
+
bool _alloc_error;
|
|
855
978
|
};
|
|
856
979
|
|
|
857
980
|
//---------------------------------------------------------
|
|
@@ -861,77 +984,95 @@ class AddSubkeysWorker : public Nan::AsyncWorker
|
|
|
861
984
|
// Callback function: function(string error)
|
|
862
985
|
//
|
|
863
986
|
//---------------------------------------------------------
|
|
864
|
-
class
|
|
987
|
+
class AddAttrAsyncWorker : public Napi::AsyncWorker
|
|
865
988
|
{
|
|
866
989
|
public:
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
990
|
+
AddAttrAsyncWorker(const Napi::Function& callback, K2HShm* pobj, const char* pkey, const char* pattrkey, const char* pattrval) :
|
|
991
|
+
Napi::AsyncWorker(callback), _callbackRef(Napi::Persistent(callback)), _k2hshm(pobj), _is_key_set(pkey != nullptr), _strkey(pkey ? pkey : ""), _is_attr_set(pattrkey != nullptr), _strattr(pattrkey ? pattrkey : ""), _is_val_set(pattrval != nullptr), _strval(pattrval ? pattrval : "")
|
|
992
|
+
{
|
|
993
|
+
_callbackRef.Ref();
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
~AddAttrAsyncWorker() override
|
|
870
997
|
{
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
998
|
+
if(_callbackRef){
|
|
999
|
+
_callbackRef.Unref();
|
|
1000
|
+
_callbackRef.Reset();
|
|
1001
|
+
}
|
|
874
1002
|
}
|
|
875
|
-
~AddAttrWorker() {}
|
|
876
1003
|
|
|
877
|
-
|
|
1004
|
+
// Run on worker thread
|
|
1005
|
+
void Execute() override
|
|
878
1006
|
{
|
|
879
|
-
if(!
|
|
880
|
-
|
|
1007
|
+
if(!_k2hshm){
|
|
1008
|
+
SetError("No object is associated to async worker");
|
|
881
1009
|
return;
|
|
882
1010
|
}
|
|
883
|
-
if(!
|
|
884
|
-
|
|
1011
|
+
if(!_is_key_set){
|
|
1012
|
+
SetError("Specified key is empty(null)");
|
|
885
1013
|
return;
|
|
886
1014
|
}
|
|
887
|
-
if(!
|
|
888
|
-
|
|
1015
|
+
if(!_is_attr_set){
|
|
1016
|
+
SetError("Specified attribute name is empty(null)");
|
|
889
1017
|
return;
|
|
890
1018
|
}
|
|
891
1019
|
|
|
892
1020
|
// add attribute
|
|
893
|
-
if(!
|
|
894
|
-
|
|
895
|
-
|
|
1021
|
+
if(!_k2hshm->AddAttr(_strkey.c_str(), _strattr.c_str(), (_is_val_set ? _strval.c_str() : NULL))){
|
|
1022
|
+
SetError("Failed to set attribute and value to key.");
|
|
1023
|
+
return;
|
|
896
1024
|
}
|
|
897
1025
|
}
|
|
898
1026
|
|
|
899
|
-
|
|
1027
|
+
// handler for success
|
|
1028
|
+
void OnOK() override
|
|
900
1029
|
{
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
v8::Local<v8::Value> argv[argc] = { Nan::Null() };
|
|
1030
|
+
Napi::Env env = Env();
|
|
1031
|
+
Napi::HandleScope scope(env);
|
|
904
1032
|
|
|
905
|
-
if(
|
|
906
|
-
|
|
1033
|
+
if(!_callbackRef.IsEmpty()){
|
|
1034
|
+
_callbackRef.Value().Call({ env.Null() });
|
|
907
1035
|
}else{
|
|
908
|
-
|
|
909
|
-
return;
|
|
1036
|
+
Napi::TypeError::New(env, "Internal error in async worker").ThrowAsJavaScriptException();
|
|
910
1037
|
}
|
|
911
1038
|
}
|
|
912
1039
|
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
1040
|
+
// handler for failure (by calling SetError)
|
|
1041
|
+
void OnError(const Napi::Error& err) override
|
|
1042
|
+
{
|
|
1043
|
+
Napi::Env env = Env();
|
|
1044
|
+
Napi::HandleScope scope(env);
|
|
1045
|
+
|
|
1046
|
+
// Extract message
|
|
1047
|
+
std::string msg;
|
|
1048
|
+
if(err.Value().IsObject()){
|
|
1049
|
+
Napi::Object obj = err.Value().As<Napi::Object>();
|
|
1050
|
+
Napi::Value mv = obj.Get("message");
|
|
1051
|
+
if(mv.IsString()){
|
|
1052
|
+
msg = mv.As<Napi::String>().Utf8Value();
|
|
1053
|
+
}else{
|
|
1054
|
+
msg = err.Message();
|
|
1055
|
+
}
|
|
1056
|
+
}else{
|
|
1057
|
+
msg = err.Message();
|
|
1058
|
+
}
|
|
918
1059
|
|
|
919
|
-
if(
|
|
920
|
-
|
|
1060
|
+
if(!_callbackRef.IsEmpty()){
|
|
1061
|
+
_callbackRef.Value().Call({ Napi::String::New(env, msg) });
|
|
921
1062
|
}else{
|
|
922
|
-
|
|
923
|
-
return;
|
|
1063
|
+
err.ThrowAsJavaScriptException();
|
|
924
1064
|
}
|
|
925
|
-
|
|
1065
|
+
}
|
|
926
1066
|
|
|
927
1067
|
private:
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
1068
|
+
Napi::FunctionReference _callbackRef;
|
|
1069
|
+
K2HShm* _k2hshm;
|
|
1070
|
+
bool _is_key_set;
|
|
1071
|
+
std::string _strkey;
|
|
1072
|
+
bool _is_attr_set;
|
|
1073
|
+
std::string _strattr;
|
|
1074
|
+
bool _is_val_set;
|
|
1075
|
+
std::string _strval;
|
|
935
1076
|
};
|
|
936
1077
|
|
|
937
1078
|
//---------------------------------------------------------
|
|
@@ -941,81 +1082,88 @@ class AddAttrWorker : public Nan::AsyncWorker
|
|
|
941
1082
|
// Callback function: function(string error)
|
|
942
1083
|
//
|
|
943
1084
|
//---------------------------------------------------------
|
|
944
|
-
class
|
|
1085
|
+
class RemoveAsyncWorker : public Napi::AsyncWorker
|
|
945
1086
|
{
|
|
946
1087
|
public:
|
|
947
|
-
|
|
1088
|
+
RemoveAsyncWorker(const Napi::Function& callback, K2HShm* pobj, const char* pkey, const char* psubkey, bool is_all) :
|
|
1089
|
+
Napi::AsyncWorker(callback), _callbackRef(Napi::Persistent(callback)), _k2hshm(pobj), _is_key_set(pkey != nullptr), _strkey(pkey ? pkey : ""), _is_skey_set(psubkey != nullptr), _strsubkey(psubkey ? psubkey : ""), _is_remove_all(is_all)
|
|
948
1090
|
{
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
}else{
|
|
953
|
-
is_skey_set = (NULL != psubkey);
|
|
1091
|
+
if(_is_remove_all){
|
|
1092
|
+
_strsubkey.clear();
|
|
1093
|
+
_is_skey_set = false;
|
|
954
1094
|
}
|
|
1095
|
+
_callbackRef.Ref();
|
|
955
1096
|
}
|
|
956
|
-
~RemoveWorker() {}
|
|
957
1097
|
|
|
958
|
-
|
|
1098
|
+
~RemoveAsyncWorker() override
|
|
959
1099
|
{
|
|
960
|
-
if(
|
|
961
|
-
|
|
1100
|
+
if(_callbackRef){
|
|
1101
|
+
_callbackRef.Unref();
|
|
1102
|
+
_callbackRef.Reset();
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
|
|
1106
|
+
// Run on worker thread
|
|
1107
|
+
void Execute() override
|
|
1108
|
+
{
|
|
1109
|
+
if(!_k2hshm){
|
|
1110
|
+
SetError("No object is associated to async worker");
|
|
962
1111
|
return;
|
|
963
1112
|
}
|
|
964
|
-
if(!
|
|
965
|
-
|
|
1113
|
+
if(!_is_key_set){
|
|
1114
|
+
SetError("Specified key is empty(null)");
|
|
966
1115
|
return;
|
|
967
1116
|
}
|
|
968
1117
|
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
result = pk2hobj->Remove(strkey.c_str(), strsubkey.c_str());
|
|
1118
|
+
bool result = false;
|
|
1119
|
+
if(_is_remove_all){
|
|
1120
|
+
result = _k2hshm->Remove(_strkey.c_str(), true);
|
|
1121
|
+
}else if(_is_skey_set){
|
|
1122
|
+
result = _k2hshm->Remove(_strkey.c_str(), _strsubkey.c_str());
|
|
975
1123
|
}else{
|
|
976
|
-
result =
|
|
1124
|
+
result = _k2hshm->Remove(_strkey.c_str(), false);
|
|
977
1125
|
}
|
|
978
1126
|
if(!result){
|
|
979
|
-
|
|
980
|
-
|
|
1127
|
+
SetError("Failed to remove key (and subkey).");
|
|
1128
|
+
return;
|
|
981
1129
|
}
|
|
982
1130
|
}
|
|
983
1131
|
|
|
984
|
-
|
|
1132
|
+
// handler for success
|
|
1133
|
+
void OnOK() override
|
|
985
1134
|
{
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
v8::Local<v8::Value> argv[argc] = { Nan::Null() };
|
|
1135
|
+
Napi::Env env = Env();
|
|
1136
|
+
Napi::HandleScope scope(env);
|
|
989
1137
|
|
|
990
|
-
if(
|
|
991
|
-
|
|
1138
|
+
if(!_callbackRef.IsEmpty()){
|
|
1139
|
+
_callbackRef.Value().Call({ env.Null() });
|
|
992
1140
|
}else{
|
|
993
|
-
|
|
994
|
-
return;
|
|
1141
|
+
Napi::TypeError::New(env, "Internal error in async worker").ThrowAsJavaScriptException();
|
|
995
1142
|
}
|
|
996
1143
|
}
|
|
997
1144
|
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1145
|
+
// handler for failure (by calling SetError)
|
|
1146
|
+
void OnError(const Napi::Error& err) override
|
|
1147
|
+
{
|
|
1148
|
+
Napi::Env env = Env();
|
|
1149
|
+
Napi::HandleScope scope(env);
|
|
1003
1150
|
|
|
1004
|
-
|
|
1005
|
-
|
|
1151
|
+
std::string msg = err.Message();
|
|
1152
|
+
if(!_callbackRef.IsEmpty()){
|
|
1153
|
+
_callbackRef.Value().Call({ Napi::String::New(env, msg) });
|
|
1006
1154
|
}else{
|
|
1007
|
-
|
|
1008
|
-
return;
|
|
1155
|
+
err.ThrowAsJavaScriptException();
|
|
1009
1156
|
}
|
|
1010
|
-
|
|
1157
|
+
}
|
|
1011
1158
|
|
|
1012
1159
|
private:
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1160
|
+
Napi::FunctionReference _callbackRef;
|
|
1161
|
+
K2HShm* _k2hshm;
|
|
1162
|
+
bool _is_key_set;
|
|
1163
|
+
std::string _strkey;
|
|
1164
|
+
bool _is_skey_set;
|
|
1165
|
+
std::string _strsubkey;
|
|
1166
|
+
bool _is_remove_all;
|
|
1019
1167
|
};
|
|
1020
1168
|
|
|
1021
1169
|
//---------------------------------------------------------
|
|
@@ -1025,75 +1173,84 @@ class RemoveWorker : public Nan::AsyncWorker
|
|
|
1025
1173
|
// Callback function: function(string error)
|
|
1026
1174
|
//
|
|
1027
1175
|
//---------------------------------------------------------
|
|
1028
|
-
class
|
|
1176
|
+
class ArchiveAsyncWorker : public Napi::AsyncWorker
|
|
1029
1177
|
{
|
|
1030
1178
|
public:
|
|
1031
|
-
|
|
1179
|
+
ArchiveAsyncWorker(const Napi::Function& callback, K2HShm* pobj, const char* file, bool is_error_skip, bool is_load) :
|
|
1180
|
+
Napi::AsyncWorker(callback), _callbackRef(Napi::Persistent(callback)), _k2hshm(pobj), _is_file_set(file != nullptr), _strfile(file ? file : ""), _errskip(is_error_skip), _is_load_type(is_load)
|
|
1181
|
+
{
|
|
1182
|
+
_callbackRef.Ref();
|
|
1183
|
+
}
|
|
1184
|
+
|
|
1185
|
+
~ArchiveAsyncWorker() override
|
|
1032
1186
|
{
|
|
1033
|
-
|
|
1187
|
+
if(_callbackRef){
|
|
1188
|
+
_callbackRef.Unref();
|
|
1189
|
+
_callbackRef.Reset();
|
|
1190
|
+
}
|
|
1034
1191
|
}
|
|
1035
|
-
~ArchiveWorker() {}
|
|
1036
1192
|
|
|
1037
|
-
|
|
1193
|
+
// Run on worker thread
|
|
1194
|
+
void Execute() override
|
|
1038
1195
|
{
|
|
1039
|
-
if(!
|
|
1040
|
-
|
|
1196
|
+
if(!_k2hshm){
|
|
1197
|
+
SetError("No object is associated to async worker");
|
|
1041
1198
|
return;
|
|
1042
1199
|
}
|
|
1043
|
-
if(!
|
|
1044
|
-
|
|
1200
|
+
if(!_is_file_set){
|
|
1201
|
+
SetError("Specified file name is empty(null)");
|
|
1045
1202
|
return;
|
|
1046
1203
|
}
|
|
1047
1204
|
|
|
1048
|
-
if(
|
|
1205
|
+
if(_is_load_type){
|
|
1049
1206
|
// load archive
|
|
1050
|
-
if(!k2h_load_archive(reinterpret_cast<k2h_h>(
|
|
1051
|
-
|
|
1052
|
-
|
|
1207
|
+
if(!k2h_load_archive(reinterpret_cast<k2h_h>(_k2hshm), _strfile.c_str(), _errskip)){
|
|
1208
|
+
SetError("Failed to load archive file.");
|
|
1209
|
+
return;
|
|
1053
1210
|
}
|
|
1054
1211
|
}else{
|
|
1055
1212
|
// put archive
|
|
1056
|
-
if(!k2h_put_archive(reinterpret_cast<k2h_h>(
|
|
1057
|
-
|
|
1058
|
-
|
|
1213
|
+
if(!k2h_put_archive(reinterpret_cast<k2h_h>(_k2hshm), _strfile.c_str(), _errskip)){
|
|
1214
|
+
SetError("Failed to put archive file.");
|
|
1215
|
+
return;
|
|
1059
1216
|
}
|
|
1060
1217
|
}
|
|
1061
1218
|
}
|
|
1062
1219
|
|
|
1063
|
-
|
|
1220
|
+
// handler for success
|
|
1221
|
+
void OnOK() override
|
|
1064
1222
|
{
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
v8::Local<v8::Value> argv[argc] = { Nan::Null() };
|
|
1223
|
+
Napi::Env env = Env();
|
|
1224
|
+
Napi::HandleScope scope(env);
|
|
1068
1225
|
|
|
1069
|
-
if(
|
|
1070
|
-
|
|
1226
|
+
if(!_callbackRef.IsEmpty()){
|
|
1227
|
+
_callbackRef.Value().Call({ env.Null() });
|
|
1071
1228
|
}else{
|
|
1072
|
-
|
|
1073
|
-
return;
|
|
1229
|
+
Napi::TypeError::New(env, "Internal error in async worker").ThrowAsJavaScriptException();
|
|
1074
1230
|
}
|
|
1075
1231
|
}
|
|
1076
1232
|
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1233
|
+
// handler for failure (by calling SetError)
|
|
1234
|
+
void OnError(const Napi::Error& err) override
|
|
1235
|
+
{
|
|
1236
|
+
Napi::Env env = Env();
|
|
1237
|
+
Napi::HandleScope scope(env);
|
|
1082
1238
|
|
|
1083
|
-
|
|
1084
|
-
|
|
1239
|
+
std::string msg = err.Message();
|
|
1240
|
+
if(!_callbackRef.IsEmpty()){
|
|
1241
|
+
_callbackRef.Value().Call({ Napi::String::New(env, msg) });
|
|
1085
1242
|
}else{
|
|
1086
|
-
|
|
1087
|
-
return;
|
|
1243
|
+
err.ThrowAsJavaScriptException();
|
|
1088
1244
|
}
|
|
1089
|
-
|
|
1245
|
+
}
|
|
1090
1246
|
|
|
1091
1247
|
private:
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
bool
|
|
1248
|
+
Napi::FunctionReference _callbackRef;
|
|
1249
|
+
K2HShm* _k2hshm;
|
|
1250
|
+
bool _is_file_set;
|
|
1251
|
+
std::string _strfile;
|
|
1252
|
+
bool _errskip;
|
|
1253
|
+
bool _is_load_type;
|
|
1097
1254
|
};
|
|
1098
1255
|
|
|
1099
1256
|
#endif
|