k2hash 1.1.33 → 2.0.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.
- 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 +722 -560
- 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 -131
- package/src/binding.gyp +0 -146
package/src/k2h_keyqueue.h
CHANGED
|
@@ -27,47 +27,55 @@
|
|
|
27
27
|
//---------------------------------------------------------
|
|
28
28
|
// K2hKeyQueue Class
|
|
29
29
|
//---------------------------------------------------------
|
|
30
|
-
class K2hKeyQueue : public
|
|
30
|
+
class K2hKeyQueue : public Napi::ObjectWrap<K2hKeyQueue>
|
|
31
31
|
{
|
|
32
32
|
friend class K2hNode;
|
|
33
33
|
|
|
34
34
|
public:
|
|
35
|
-
static void Init(
|
|
36
|
-
static
|
|
37
|
-
static
|
|
35
|
+
static void Init(Napi::Env env, Napi::Object exports);
|
|
36
|
+
static Napi::Object NewInstance(Napi::Env env);
|
|
37
|
+
static Napi::Object NewInstance(Napi::Env env, const Napi::Value& arg);
|
|
38
38
|
|
|
39
|
-
|
|
40
|
-
|
|
39
|
+
static Napi::Object GetInstance(const Napi::CallbackInfo& info);
|
|
40
|
+
|
|
41
|
+
// Constructor / Destructor
|
|
42
|
+
explicit K2hKeyQueue(const Napi::CallbackInfo& info);
|
|
41
43
|
~K2hKeyQueue();
|
|
42
44
|
|
|
43
|
-
|
|
45
|
+
private:
|
|
46
|
+
Napi::Value New(const Napi::CallbackInfo& info);
|
|
47
|
+
|
|
48
|
+
Napi::Value On(const Napi::CallbackInfo& info);
|
|
49
|
+
Napi::Value OnPush(const Napi::CallbackInfo& info);
|
|
50
|
+
Napi::Value OnPop(const Napi::CallbackInfo& info);
|
|
51
|
+
Napi::Value OnCount(const Napi::CallbackInfo& info);
|
|
52
|
+
Napi::Value OnRead(const Napi::CallbackInfo& info);
|
|
53
|
+
Napi::Value OnRemove(const Napi::CallbackInfo& info);
|
|
44
54
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
55
|
+
Napi::Value Off(const Napi::CallbackInfo& info);
|
|
56
|
+
Napi::Value OffPush(const Napi::CallbackInfo& info);
|
|
57
|
+
Napi::Value OffPop(const Napi::CallbackInfo& info);
|
|
58
|
+
Napi::Value OffCount(const Napi::CallbackInfo& info);
|
|
59
|
+
Napi::Value OffRead(const Napi::CallbackInfo& info);
|
|
60
|
+
Napi::Value OffRemove(const Napi::CallbackInfo& info);
|
|
61
|
+
|
|
62
|
+
Napi::Value Initialize(const Napi::CallbackInfo& info);
|
|
63
|
+
Napi::Value IsEmpty(const Napi::CallbackInfo& info);
|
|
64
|
+
Napi::Value Count(const Napi::CallbackInfo& info);
|
|
65
|
+
Napi::Value Read(const Napi::CallbackInfo& info);
|
|
66
|
+
Napi::Value Push(const Napi::CallbackInfo& info);
|
|
67
|
+
Napi::Value Pop(const Napi::CallbackInfo& info);
|
|
68
|
+
Napi::Value Remove(const Napi::CallbackInfo& info);
|
|
69
|
+
Napi::Value Dump(const Napi::CallbackInfo& info);
|
|
70
|
+
|
|
71
|
+
public:
|
|
72
|
+
// constructor reference
|
|
73
|
+
static Napi::FunctionReference constructor;
|
|
57
74
|
|
|
58
|
-
|
|
59
|
-
static NAN_METHOD(IsEmpty);
|
|
60
|
-
static NAN_METHOD(Count);
|
|
61
|
-
static NAN_METHOD(Read);
|
|
62
|
-
static NAN_METHOD(Push);
|
|
63
|
-
static NAN_METHOD(Pop);
|
|
64
|
-
static NAN_METHOD(Remove);
|
|
65
|
-
static NAN_METHOD(Dump);
|
|
75
|
+
StackEmitCB _cbs;
|
|
66
76
|
|
|
67
77
|
private:
|
|
68
|
-
|
|
69
|
-
K2HKeyQueue _k2hkeyqueue;
|
|
70
|
-
StackEmitCB _cbs;
|
|
78
|
+
K2HKeyQueue _k2hkeyqueue;
|
|
71
79
|
};
|
|
72
80
|
|
|
73
81
|
#endif
|
package/src/k2h_keyqueue_async.h
CHANGED
|
@@ -34,74 +34,79 @@
|
|
|
34
34
|
// Callback function: function(string error)
|
|
35
35
|
//
|
|
36
36
|
//---------------------------------------------------------
|
|
37
|
-
class
|
|
37
|
+
class K2hkqPushAsyncWorker : public Napi::AsyncWorker
|
|
38
38
|
{
|
|
39
39
|
public:
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
40
|
+
K2hkqPushAsyncWorker(const Napi::Function& callback, K2HKeyQueue* pobj, const char* pkey, const char* pval, const char* ppass, const time_t* pexpire) :
|
|
41
|
+
Napi::AsyncWorker(callback), _callbackRef(Napi::Persistent(callback)), _pk2hkqobj(pobj),
|
|
42
|
+
_is_key_set(NULL != pkey), _strkey(pkey ? pkey : ""), _is_val_set(NULL != pval), _strval(pval ? pval : ""), _is_pass_set(NULL != ppass), _strpass(ppass ? ppass : ""), _expire(pexpire ? *pexpire : 0)
|
|
43
43
|
{
|
|
44
|
-
|
|
45
|
-
is_val_set = (NULL != pval);
|
|
46
|
-
is_pass_set = (NULL != ppass);
|
|
44
|
+
_callbackRef.Ref();
|
|
47
45
|
}
|
|
48
|
-
~K2hkqPushWorker() {}
|
|
49
46
|
|
|
50
|
-
|
|
47
|
+
~K2hkqPushAsyncWorker()
|
|
51
48
|
{
|
|
52
|
-
if(
|
|
53
|
-
|
|
49
|
+
if(_callbackRef){
|
|
50
|
+
_callbackRef.Unref();
|
|
51
|
+
_callbackRef.Reset();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Runs on worker thread
|
|
56
|
+
void Execute() override
|
|
57
|
+
{
|
|
58
|
+
if(!_pk2hkqobj){
|
|
59
|
+
SetError("No object is associated to async worker");
|
|
54
60
|
return;
|
|
55
61
|
}
|
|
56
|
-
if(!
|
|
57
|
-
|
|
62
|
+
if(!_is_key_set || !_is_val_set){
|
|
63
|
+
SetError("Specified key or value is empty(null)");
|
|
58
64
|
return;
|
|
59
65
|
}
|
|
60
66
|
|
|
61
67
|
// push
|
|
62
|
-
if(!
|
|
63
|
-
|
|
64
|
-
|
|
68
|
+
if(!_pk2hkqobj->Push(reinterpret_cast<const unsigned char*>(_strkey.c_str()), (_strkey.length() + 1), reinterpret_cast<const unsigned char*>(_strval.c_str()), (_strval.length() + 1), (_is_pass_set ? _strpass.c_str() : NULL), (_expire > 0 ? &_expire : NULL))){
|
|
69
|
+
SetError("Failed to push data for k2hkeyqueue.");
|
|
70
|
+
return;
|
|
65
71
|
}
|
|
66
72
|
}
|
|
67
73
|
|
|
68
|
-
|
|
74
|
+
// handler for success
|
|
75
|
+
void OnOK() override
|
|
69
76
|
{
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
v8::Local<v8::Value> argv[argc] = { Nan::Null() };
|
|
77
|
+
Napi::Env env = Env();
|
|
78
|
+
Napi::HandleScope scope(env);
|
|
73
79
|
|
|
74
|
-
if(
|
|
75
|
-
|
|
80
|
+
if(_callbackRef && _callbackRef.Value().IsFunction()){
|
|
81
|
+
_callbackRef.Call({ env.Null() });
|
|
76
82
|
}else{
|
|
77
|
-
|
|
78
|
-
return;
|
|
83
|
+
Napi::TypeError::New(env, "Internal error in async worker").ThrowAsJavaScriptException();
|
|
79
84
|
}
|
|
80
85
|
}
|
|
81
86
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
+
// handler for failure (by calling SetError)
|
|
88
|
+
void OnError(const Napi::Error& err) override
|
|
89
|
+
{
|
|
90
|
+
Napi::Env env = Env();
|
|
91
|
+
Napi::HandleScope scope(env);
|
|
87
92
|
|
|
88
|
-
if(
|
|
89
|
-
|
|
93
|
+
if(_callbackRef && _callbackRef.Value().IsFunction()){
|
|
94
|
+
_callbackRef.Call({ Napi::String::New(env, err.Message()) });
|
|
90
95
|
}else{
|
|
91
|
-
|
|
92
|
-
return;
|
|
96
|
+
Napi::TypeError::New(env, "Internal error in async worker").ThrowAsJavaScriptException();
|
|
93
97
|
}
|
|
94
|
-
|
|
98
|
+
}
|
|
95
99
|
|
|
96
100
|
private:
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
101
|
+
Napi::FunctionReference _callbackRef;
|
|
102
|
+
K2HKeyQueue* _pk2hkqobj;
|
|
103
|
+
bool _is_key_set;
|
|
104
|
+
std::string _strkey;
|
|
105
|
+
bool _is_val_set;
|
|
106
|
+
std::string _strval;
|
|
107
|
+
bool _is_pass_set;
|
|
108
|
+
std::string _strpass;
|
|
109
|
+
time_t _expire;
|
|
105
110
|
};
|
|
106
111
|
|
|
107
112
|
//---------------------------------------------------------
|
|
@@ -111,59 +116,65 @@ class K2hkqPushWorker : public Nan::AsyncWorker
|
|
|
111
116
|
// Callback function: function(string error[, int count])
|
|
112
117
|
//
|
|
113
118
|
//---------------------------------------------------------
|
|
114
|
-
class
|
|
119
|
+
class K2hkqCountAsyncWorker : public Napi::AsyncWorker
|
|
115
120
|
{
|
|
116
121
|
public:
|
|
117
|
-
|
|
118
|
-
|
|
122
|
+
K2hkqCountAsyncWorker(const Napi::Function& callback, K2HKeyQueue* pobj) :
|
|
123
|
+
Napi::AsyncWorker(callback), _callbackRef(Napi::Persistent(callback)), _pk2hkqobj(pobj), _rescount(0)
|
|
124
|
+
{
|
|
125
|
+
_callbackRef.Ref();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
~K2hkqCountAsyncWorker()
|
|
129
|
+
{
|
|
130
|
+
if(_callbackRef){
|
|
131
|
+
_callbackRef.Unref();
|
|
132
|
+
_callbackRef.Reset();
|
|
133
|
+
}
|
|
134
|
+
}
|
|
119
135
|
|
|
120
|
-
|
|
136
|
+
// Runs on worker thread
|
|
137
|
+
void Execute() override
|
|
121
138
|
{
|
|
122
|
-
if(!
|
|
123
|
-
|
|
139
|
+
if(!_pk2hkqobj){
|
|
140
|
+
SetError("No object is associated to async worker");
|
|
124
141
|
return;
|
|
125
142
|
}
|
|
126
143
|
|
|
127
144
|
// count
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
// [NOTE]
|
|
131
|
-
// This execution does not return error at any case.
|
|
145
|
+
_rescount = static_cast<int>(_pk2hkqobj->GetCount());
|
|
132
146
|
}
|
|
133
147
|
|
|
134
|
-
|
|
148
|
+
// handler for success
|
|
149
|
+
void OnOK() override
|
|
135
150
|
{
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
v8::Local<v8::Value> argv[argc] = { Nan::Null(), Nan::New(rescount) };
|
|
151
|
+
Napi::Env env = Env();
|
|
152
|
+
Napi::HandleScope scope(env);
|
|
139
153
|
|
|
140
|
-
if(
|
|
141
|
-
|
|
154
|
+
if(_callbackRef && _callbackRef.Value().IsFunction()){
|
|
155
|
+
_callbackRef.Call({ env.Null(), Napi::Number::New(env, _rescount) });
|
|
142
156
|
}else{
|
|
143
|
-
|
|
144
|
-
return;
|
|
157
|
+
Napi::TypeError::New(env, "Internal error in async worker").ThrowAsJavaScriptException();
|
|
145
158
|
}
|
|
146
159
|
}
|
|
147
160
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
const int argc = 1;
|
|
154
|
-
v8::Local<v8::Value> argv[argc] = { Nan::New<v8::String>(this->ErrorMessage()).ToLocalChecked() };
|
|
161
|
+
// handler for failure (by calling SetError)
|
|
162
|
+
void OnError(const Napi::Error& err) override
|
|
163
|
+
{
|
|
164
|
+
Napi::Env env = Env();
|
|
165
|
+
Napi::HandleScope scope(env);
|
|
155
166
|
|
|
156
|
-
if(
|
|
157
|
-
|
|
167
|
+
if(_callbackRef && _callbackRef.Value().IsFunction()){
|
|
168
|
+
_callbackRef.Call({ Napi::String::New(env, err.Message()) });
|
|
158
169
|
}else{
|
|
159
|
-
|
|
160
|
-
return;
|
|
170
|
+
Napi::TypeError::New(env, "Internal error in async worker").ThrowAsJavaScriptException();
|
|
161
171
|
}
|
|
162
|
-
|
|
172
|
+
}
|
|
163
173
|
|
|
164
174
|
private:
|
|
165
|
-
|
|
166
|
-
|
|
175
|
+
Napi::FunctionReference _callbackRef;
|
|
176
|
+
K2HKeyQueue* _pk2hkqobj;
|
|
177
|
+
int _rescount;
|
|
167
178
|
};
|
|
168
179
|
|
|
169
180
|
//---------------------------------------------------------
|
|
@@ -173,78 +184,86 @@ class K2hkqCountWorker : public Nan::AsyncWorker
|
|
|
173
184
|
// Callback function: function(string error[, string key, string value])
|
|
174
185
|
//
|
|
175
186
|
//---------------------------------------------------------
|
|
176
|
-
class
|
|
187
|
+
class K2hkqReadAsyncWorker : public Napi::AsyncWorker
|
|
177
188
|
{
|
|
178
189
|
public:
|
|
179
|
-
|
|
180
|
-
|
|
190
|
+
K2hkqReadAsyncWorker(const Napi::Function& callback, K2HKeyQueue* pobj, int pos, const char* ppass) :
|
|
191
|
+
Napi::AsyncWorker(callback), _callbackRef(Napi::Persistent(callback)), _pk2hkqobj(pobj), _getpos(pos), _is_pass_set(NULL != ppass), _strpass(ppass ? ppass : ""), _reskey(), _resval()
|
|
192
|
+
{
|
|
193
|
+
_callbackRef.Ref();
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
~K2hkqReadAsyncWorker()
|
|
197
|
+
{
|
|
198
|
+
if(_callbackRef){
|
|
199
|
+
_callbackRef.Unref();
|
|
200
|
+
_callbackRef.Reset();
|
|
201
|
+
}
|
|
202
|
+
}
|
|
181
203
|
|
|
182
|
-
|
|
204
|
+
// Runs on the worker thread
|
|
205
|
+
void Execute() override
|
|
183
206
|
{
|
|
184
|
-
if(!
|
|
185
|
-
|
|
207
|
+
if(!_pk2hkqobj){
|
|
208
|
+
SetError("No object is associated to async worker");
|
|
186
209
|
return;
|
|
187
210
|
}
|
|
188
|
-
if(0 >
|
|
189
|
-
|
|
211
|
+
if(0 > _getpos){
|
|
212
|
+
SetError("Specified position is under zero.");
|
|
190
213
|
return;
|
|
191
214
|
}
|
|
192
215
|
|
|
193
|
-
// read
|
|
194
216
|
unsigned char* pkey = NULL;
|
|
195
217
|
unsigned char* pval = NULL;
|
|
196
218
|
size_t keylen = 0;
|
|
197
219
|
size_t vallen = 0;
|
|
198
|
-
bool result =
|
|
220
|
+
bool result = _pk2hkqobj->Read(&pkey, keylen, &pval, vallen, _getpos, (_is_pass_set ? _strpass.c_str() : NULL));
|
|
199
221
|
if(!result || !pkey || 0 == keylen){
|
|
200
|
-
|
|
201
|
-
this->SetErrorMessage("Failed to read data for K2HKeyQueue.");
|
|
222
|
+
SetError("Failed to read data for K2HKeyQueue.");
|
|
202
223
|
}else{
|
|
203
|
-
|
|
204
|
-
reskey = std::string(reinterpret_cast<const char*>(pkey), keylen);
|
|
224
|
+
_reskey.assign(reinterpret_cast<const char*>(pkey), static_cast<size_t>(strlen(reinterpret_cast<const char*>(pkey))));
|
|
205
225
|
if(pval && 0 < vallen){
|
|
206
|
-
|
|
226
|
+
_resval.assign(reinterpret_cast<const char*>(pval), static_cast<size_t>(strlen(reinterpret_cast<const char*>(pval))));
|
|
207
227
|
}
|
|
208
228
|
}
|
|
209
229
|
K2H_Free(pkey);
|
|
210
230
|
K2H_Free(pval);
|
|
211
231
|
}
|
|
212
232
|
|
|
213
|
-
|
|
233
|
+
// handler for success
|
|
234
|
+
void OnOK() override
|
|
214
235
|
{
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
v8::Local<v8::Value> argv[argc] = { Nan::Null(), Nan::New<v8::String>(reskey.c_str()).ToLocalChecked(), Nan::New<v8::String>(resval.c_str()).ToLocalChecked() };
|
|
236
|
+
Napi::Env env = Env();
|
|
237
|
+
Napi::HandleScope scope(env);
|
|
218
238
|
|
|
219
|
-
if(
|
|
220
|
-
|
|
239
|
+
if(_callbackRef && _callbackRef.Value().IsFunction()){
|
|
240
|
+
_callbackRef.Call({ env.Null(), Napi::String::New(env, _reskey.c_str(), static_cast<size_t>(strlen(_reskey.c_str()))), Napi::String::New(env, _resval.c_str(), static_cast<size_t>(strlen(_resval.c_str()))) });
|
|
221
241
|
}else{
|
|
222
|
-
|
|
223
|
-
return;
|
|
242
|
+
Napi::TypeError::New(env, "Internal error in async worker").ThrowAsJavaScriptException();
|
|
224
243
|
}
|
|
225
244
|
}
|
|
226
245
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
246
|
+
// handler for failure (by calling SetError)
|
|
247
|
+
void OnError(const Napi::Error& err) override
|
|
248
|
+
{
|
|
249
|
+
Napi::Env env = Env();
|
|
250
|
+
Napi::HandleScope scope(env);
|
|
232
251
|
|
|
233
|
-
if(
|
|
234
|
-
|
|
252
|
+
if(_callbackRef && _callbackRef.Value().IsFunction()){
|
|
253
|
+
_callbackRef.Call({ Napi::String::New(env, err.Message()) });
|
|
235
254
|
}else{
|
|
236
|
-
|
|
237
|
-
return;
|
|
255
|
+
Napi::TypeError::New(env, "Internal error in async worker").ThrowAsJavaScriptException();
|
|
238
256
|
}
|
|
239
|
-
|
|
257
|
+
}
|
|
240
258
|
|
|
241
259
|
private:
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
std::string
|
|
247
|
-
std::string
|
|
260
|
+
Napi::FunctionReference _callbackRef;
|
|
261
|
+
K2HKeyQueue* _pk2hkqobj;
|
|
262
|
+
int _getpos;
|
|
263
|
+
bool _is_pass_set;
|
|
264
|
+
std::string _strpass;
|
|
265
|
+
std::string _reskey;
|
|
266
|
+
std::string _resval;
|
|
248
267
|
};
|
|
249
268
|
|
|
250
269
|
//---------------------------------------------------------
|
|
@@ -254,73 +273,81 @@ class K2hkqReadWorker : public Nan::AsyncWorker
|
|
|
254
273
|
// Callback function: function(string error[, string key, string value])
|
|
255
274
|
//
|
|
256
275
|
//---------------------------------------------------------
|
|
257
|
-
class
|
|
276
|
+
class K2hkqPopAsyncWorker : public Napi::AsyncWorker
|
|
258
277
|
{
|
|
259
278
|
public:
|
|
260
|
-
|
|
261
|
-
|
|
279
|
+
K2hkqPopAsyncWorker(const Napi::Function& callback, K2HKeyQueue* pobj, const char* ppass) :
|
|
280
|
+
Napi::AsyncWorker(callback), _callbackRef(Napi::Persistent(callback)), _pk2hkqobj(pobj), _is_pass_set(NULL != ppass), _strpass(ppass ? ppass : ""), _reskey(), _resval()
|
|
281
|
+
{
|
|
282
|
+
_callbackRef.Ref();
|
|
283
|
+
}
|
|
262
284
|
|
|
263
|
-
|
|
285
|
+
~K2hkqPopAsyncWorker()
|
|
264
286
|
{
|
|
265
|
-
if(
|
|
266
|
-
|
|
287
|
+
if(_callbackRef){
|
|
288
|
+
_callbackRef.Unref();
|
|
289
|
+
_callbackRef.Reset();
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// Runs on the worker thread
|
|
294
|
+
void Execute() override
|
|
295
|
+
{
|
|
296
|
+
if(!_pk2hkqobj){
|
|
297
|
+
SetError("No object is associated to async worker");
|
|
267
298
|
return;
|
|
268
299
|
}
|
|
269
300
|
|
|
270
|
-
// pop
|
|
271
301
|
unsigned char* pkey = NULL;
|
|
272
302
|
unsigned char* pval = NULL;
|
|
273
303
|
size_t keylen = 0;
|
|
274
304
|
size_t vallen = 0;
|
|
275
|
-
bool result =
|
|
305
|
+
bool result = _pk2hkqobj->Pop(&pkey, keylen, &pval, vallen, (_is_pass_set ? _strpass.c_str() : NULL));
|
|
276
306
|
if(!result || !pkey || 0 == keylen){
|
|
277
|
-
|
|
278
|
-
this->SetErrorMessage("Failed to pop data for K2HKeyQueue.");
|
|
307
|
+
SetError("Failed to pop data for K2HKeyQueue.");
|
|
279
308
|
}else{
|
|
280
|
-
|
|
281
|
-
reskey = std::string(reinterpret_cast<const char*>(pkey), keylen);
|
|
309
|
+
_reskey.assign(reinterpret_cast<const char*>(pkey), static_cast<size_t>(strlen(reinterpret_cast<const char*>(pkey))));
|
|
282
310
|
if(pval && 0 < vallen){
|
|
283
|
-
|
|
311
|
+
_resval.assign(reinterpret_cast<const char*>(pval), static_cast<size_t>(strlen(reinterpret_cast<const char*>(pval))));
|
|
284
312
|
}
|
|
285
313
|
}
|
|
286
314
|
K2H_Free(pkey);
|
|
287
315
|
K2H_Free(pval);
|
|
288
316
|
}
|
|
289
317
|
|
|
290
|
-
|
|
318
|
+
// handler for success
|
|
319
|
+
void OnOK() override
|
|
291
320
|
{
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
v8::Local<v8::Value> argv[argc] = { Nan::Null(), Nan::New<v8::String>(reskey.c_str()).ToLocalChecked(), Nan::New<v8::String>(resval.c_str()).ToLocalChecked() };
|
|
321
|
+
Napi::Env env = Env();
|
|
322
|
+
Napi::HandleScope scope(env);
|
|
295
323
|
|
|
296
|
-
if(
|
|
297
|
-
|
|
324
|
+
if(_callbackRef && _callbackRef.Value().IsFunction()){
|
|
325
|
+
_callbackRef.Call({ env.Null(), Napi::String::New(env, _reskey.c_str(), static_cast<size_t>(strlen(_reskey.c_str()))), Napi::String::New(env, _resval.c_str(), static_cast<size_t>(strlen(_resval.c_str()))) });
|
|
298
326
|
}else{
|
|
299
|
-
|
|
300
|
-
return;
|
|
327
|
+
Napi::TypeError::New(env, "Internal error in async worker").ThrowAsJavaScriptException();
|
|
301
328
|
}
|
|
302
329
|
}
|
|
303
330
|
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
331
|
+
// handler for failure (by calling SetError)
|
|
332
|
+
void OnError(const Napi::Error& err) override
|
|
333
|
+
{
|
|
334
|
+
Napi::Env env = Env();
|
|
335
|
+
Napi::HandleScope scope(env);
|
|
309
336
|
|
|
310
|
-
if(
|
|
311
|
-
|
|
337
|
+
if(_callbackRef && _callbackRef.Value().IsFunction()){
|
|
338
|
+
_callbackRef.Call({ Napi::String::New(env, err.Message()) });
|
|
312
339
|
}else{
|
|
313
|
-
|
|
314
|
-
return;
|
|
340
|
+
Napi::TypeError::New(env, "Internal error in async worker").ThrowAsJavaScriptException();
|
|
315
341
|
}
|
|
316
|
-
|
|
342
|
+
}
|
|
317
343
|
|
|
318
344
|
private:
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
std::string
|
|
323
|
-
std::string
|
|
345
|
+
Napi::FunctionReference _callbackRef;
|
|
346
|
+
K2HKeyQueue* _pk2hkqobj;
|
|
347
|
+
bool _is_pass_set;
|
|
348
|
+
std::string _strpass;
|
|
349
|
+
std::string _reskey;
|
|
350
|
+
std::string _resval;
|
|
324
351
|
};
|
|
325
352
|
|
|
326
353
|
//---------------------------------------------------------
|
|
@@ -330,65 +357,76 @@ class K2hkqPopWorker : public Nan::AsyncWorker
|
|
|
330
357
|
// Callback function: function(string error[, int removed_queue_count])
|
|
331
358
|
//
|
|
332
359
|
//---------------------------------------------------------
|
|
333
|
-
class
|
|
360
|
+
class K2hkqRemoveAsyncWorker : public Napi::AsyncWorker
|
|
334
361
|
{
|
|
335
362
|
public:
|
|
336
|
-
|
|
337
|
-
|
|
363
|
+
K2hkqRemoveAsyncWorker(const Napi::Function& callback, K2HKeyQueue* pobj, int count, const char* ppass) :
|
|
364
|
+
Napi::AsyncWorker(callback), _callbackRef(Napi::Persistent(callback)), _pk2hkqobj(pobj), _rmcount(count), _is_pass_set(NULL != ppass), _strpass(ppass ? ppass : ""), _removed_count(0)
|
|
365
|
+
{
|
|
366
|
+
_callbackRef.Ref();
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
~K2hkqRemoveAsyncWorker()
|
|
370
|
+
{
|
|
371
|
+
if(_callbackRef){
|
|
372
|
+
_callbackRef.Unref();
|
|
373
|
+
_callbackRef.Reset();
|
|
374
|
+
}
|
|
375
|
+
}
|
|
338
376
|
|
|
339
|
-
|
|
377
|
+
// Runs on worker thread
|
|
378
|
+
void Execute() override
|
|
340
379
|
{
|
|
341
|
-
if(!
|
|
342
|
-
|
|
380
|
+
if(!_pk2hkqobj){
|
|
381
|
+
SetError("No object is associated to async worker");
|
|
343
382
|
return;
|
|
344
383
|
}
|
|
345
|
-
if(
|
|
346
|
-
|
|
384
|
+
if(_rmcount <= 0){
|
|
385
|
+
SetError("Specified remove count is under 1.");
|
|
347
386
|
return;
|
|
348
387
|
}
|
|
349
388
|
|
|
350
389
|
// remove
|
|
351
|
-
|
|
352
|
-
if(-1 ==
|
|
353
|
-
|
|
354
|
-
|
|
390
|
+
_removed_count = _pk2hkqobj->Remove(_rmcount, NULL, NULL, (_is_pass_set ? _strpass.c_str() : NULL));
|
|
391
|
+
if(-1 == _removed_count){
|
|
392
|
+
SetError("Failed to remove queue for K2HKeyQueue.");
|
|
393
|
+
return;
|
|
355
394
|
}
|
|
356
395
|
}
|
|
357
396
|
|
|
358
|
-
|
|
397
|
+
// handler for success
|
|
398
|
+
void OnOK() override
|
|
359
399
|
{
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
v8::Local<v8::Value> argv[argc] = { Nan::Null(), Nan::New(removed_count) };
|
|
400
|
+
Napi::Env env = Env();
|
|
401
|
+
Napi::HandleScope scope(env);
|
|
363
402
|
|
|
364
|
-
if(
|
|
365
|
-
|
|
403
|
+
if(_callbackRef && _callbackRef.Value().IsFunction()){
|
|
404
|
+
_callbackRef.Call({ env.Null(), Napi::Number::New(env, static_cast<double>(_removed_count)) });
|
|
366
405
|
}else{
|
|
367
|
-
|
|
368
|
-
return;
|
|
406
|
+
Napi::TypeError::New(env, "Internal error in async worker").ThrowAsJavaScriptException();
|
|
369
407
|
}
|
|
370
408
|
}
|
|
371
409
|
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
410
|
+
// handler for failure (by calling SetError)
|
|
411
|
+
void OnError(const Napi::Error& err) override
|
|
412
|
+
{
|
|
413
|
+
Napi::Env env = Env();
|
|
414
|
+
Napi::HandleScope scope(env);
|
|
377
415
|
|
|
378
|
-
if(
|
|
379
|
-
|
|
416
|
+
if(_callbackRef && _callbackRef.Value().IsFunction()){
|
|
417
|
+
_callbackRef.Call({ Napi::String::New(env, err.Message()) });
|
|
380
418
|
}else{
|
|
381
|
-
|
|
382
|
-
return;
|
|
419
|
+
Napi::TypeError::New(env, "Internal error in async worker").ThrowAsJavaScriptException();
|
|
383
420
|
}
|
|
384
|
-
|
|
421
|
+
}
|
|
385
422
|
|
|
386
423
|
private:
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
424
|
+
Napi::FunctionReference _callbackRef;
|
|
425
|
+
K2HKeyQueue* _pk2hkqobj;
|
|
426
|
+
int _rmcount;
|
|
427
|
+
bool _is_pass_set;
|
|
428
|
+
std::string _strpass;
|
|
429
|
+
int _removed_count;
|
|
392
430
|
};
|
|
393
431
|
|
|
394
432
|
#endif
|