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_keyqueue.cc
CHANGED
|
@@ -22,7 +22,6 @@
|
|
|
22
22
|
#include "k2h_keyqueue.h"
|
|
23
23
|
#include "k2h_keyqueue_async.h"
|
|
24
24
|
|
|
25
|
-
using namespace v8;
|
|
26
25
|
using namespace std;
|
|
27
26
|
|
|
28
27
|
//---------------------------------------------------------
|
|
@@ -44,37 +43,60 @@ const char* stc_k2hkq_emitters[] = {
|
|
|
44
43
|
};
|
|
45
44
|
|
|
46
45
|
//---------------------------------------------------------
|
|
47
|
-
// Utility
|
|
46
|
+
// Utility (using StackEmitCB Class)
|
|
48
47
|
//---------------------------------------------------------
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
if(info.Length() <= pos){ \
|
|
53
|
-
Nan::ThrowSyntaxError("No callback is specified."); \
|
|
54
|
-
return; \
|
|
55
|
-
} \
|
|
56
|
-
Nan::Callback* cb = new Nan::Callback(); \
|
|
57
|
-
cb->SetFunction(info[pos].As<v8::Function>()); \
|
|
58
|
-
bool result = obj->_cbs.Set(pemitter, cb); \
|
|
59
|
-
info.GetReturnValue().Set(Nan::New(result)); \
|
|
60
|
-
}
|
|
48
|
+
static Napi::Value SetK2hkqEmitterCallback(const Napi::CallbackInfo& info, size_t pos, const char* pemitter)
|
|
49
|
+
{
|
|
50
|
+
Napi::Env env = info.Env();
|
|
61
51
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
52
|
+
// Unwrap
|
|
53
|
+
if(!info.This().IsObject() || !info.This().As<Napi::Object>().InstanceOf(K2hKeyQueue::constructor.Value())){
|
|
54
|
+
Napi::TypeError::New(env, "Invalid this object(K2hKeyQueue instance)").ThrowAsJavaScriptException();
|
|
55
|
+
return env.Undefined();
|
|
56
|
+
}
|
|
57
|
+
K2hKeyQueue* obj = Napi::ObjectWrap<K2hKeyQueue>::Unwrap(info.This().As<Napi::Object>());
|
|
58
|
+
|
|
59
|
+
// check parameter
|
|
60
|
+
if(info.Length() <= pos){
|
|
61
|
+
Napi::TypeError::New(env, "No callback is specified.").ThrowAsJavaScriptException();
|
|
62
|
+
return env.Undefined();
|
|
63
|
+
}
|
|
64
|
+
if(!info[pos].IsFunction()){
|
|
65
|
+
Napi::TypeError::New(env, "The parameter is not callback function.").ThrowAsJavaScriptException();
|
|
66
|
+
return env.Undefined();
|
|
67
|
+
}
|
|
68
|
+
Napi::Function cb = info[pos].As<Napi::Function>();
|
|
69
|
+
|
|
70
|
+
// set
|
|
71
|
+
bool result = obj->_cbs.Set(std::string(pemitter), cb);
|
|
72
|
+
return Napi::Boolean::New(env, result);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
static Napi::Value UnsetK2hkqEmitterCallback(const Napi::CallbackInfo& info, const char* pemitter)
|
|
76
|
+
{
|
|
77
|
+
Napi::Env env = info.Env();
|
|
78
|
+
|
|
79
|
+
// Unwrap
|
|
80
|
+
if(!info.This().IsObject() || !info.This().As<Napi::Object>().InstanceOf(K2hKeyQueue::constructor.Value())){
|
|
81
|
+
Napi::TypeError::New(env, "Invalid this object").ThrowAsJavaScriptException();
|
|
82
|
+
return env.Undefined();
|
|
83
|
+
}
|
|
84
|
+
K2hKeyQueue* obj = Napi::ObjectWrap<K2hKeyQueue>::Unwrap(info.This().As<Napi::Object>());
|
|
85
|
+
|
|
86
|
+
// unset
|
|
87
|
+
bool result = obj->_cbs.Unset(std::string(pemitter));
|
|
88
|
+
return Napi::Boolean::New(env, result);
|
|
89
|
+
}
|
|
68
90
|
|
|
69
91
|
//---------------------------------------------------------
|
|
70
92
|
// K2hKeyQueue Class
|
|
71
93
|
//---------------------------------------------------------
|
|
72
|
-
|
|
94
|
+
Napi::FunctionReference K2hKeyQueue::constructor;
|
|
73
95
|
|
|
74
96
|
//---------------------------------------------------------
|
|
75
97
|
// K2hKeyQueue Methods
|
|
76
98
|
//---------------------------------------------------------
|
|
77
|
-
K2hKeyQueue::K2hKeyQueue() :
|
|
99
|
+
K2hKeyQueue::K2hKeyQueue(const Napi::CallbackInfo& info) : Napi::ObjectWrap<K2hKeyQueue>(info), _cbs(), _k2hkeyqueue()
|
|
78
100
|
{
|
|
79
101
|
}
|
|
80
102
|
|
|
@@ -82,75 +104,89 @@ K2hKeyQueue::~K2hKeyQueue()
|
|
|
82
104
|
{
|
|
83
105
|
}
|
|
84
106
|
|
|
85
|
-
void K2hKeyQueue::Init(
|
|
107
|
+
void K2hKeyQueue::Init(Napi::Env env, Napi::Object exports)
|
|
86
108
|
{
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
109
|
+
Napi::Function funcs = DefineClass(env, "K2hKeyQueue", {
|
|
110
|
+
// DefineClass normally handles the constructor internally. Therefore, there is no need
|
|
111
|
+
// to include a static wrapper New() in the class prototype, which works the same way as
|
|
112
|
+
// when using NAN.
|
|
113
|
+
// For reference, the following example shows how to declare New as a static method.
|
|
114
|
+
// (Registration is not normally required.)
|
|
115
|
+
//
|
|
116
|
+
// K2hKeyQueue::InstanceMethod("new", &K2hKeyQueue::New),
|
|
117
|
+
|
|
118
|
+
// Prototype for event emitter
|
|
119
|
+
K2hKeyQueue::InstanceMethod("on", &K2hKeyQueue::On),
|
|
120
|
+
K2hKeyQueue::InstanceMethod("onPush", &K2hKeyQueue::OnPush),
|
|
121
|
+
K2hKeyQueue::InstanceMethod("onPop", &K2hKeyQueue::OnPop),
|
|
122
|
+
K2hKeyQueue::InstanceMethod("onCount", &K2hKeyQueue::OnCount),
|
|
123
|
+
K2hKeyQueue::InstanceMethod("onRead", &K2hKeyQueue::OnRead),
|
|
124
|
+
K2hKeyQueue::InstanceMethod("onRemove", &K2hKeyQueue::OnRemove),
|
|
125
|
+
K2hKeyQueue::InstanceMethod("off", &K2hKeyQueue::Off),
|
|
126
|
+
K2hKeyQueue::InstanceMethod("offPush", &K2hKeyQueue::OffPush),
|
|
127
|
+
K2hKeyQueue::InstanceMethod("offPop", &K2hKeyQueue::OffPop),
|
|
128
|
+
K2hKeyQueue::InstanceMethod("offCount", &K2hKeyQueue::OffCount),
|
|
129
|
+
K2hKeyQueue::InstanceMethod("offRead", &K2hKeyQueue::OffRead),
|
|
130
|
+
K2hKeyQueue::InstanceMethod("offRemove", &K2hKeyQueue::OffRemove),
|
|
131
|
+
|
|
132
|
+
// Prototype
|
|
133
|
+
K2hKeyQueue::InstanceMethod("init", &K2hKeyQueue::Initialize),
|
|
134
|
+
K2hKeyQueue::InstanceMethod("push", &K2hKeyQueue::Push),
|
|
135
|
+
K2hKeyQueue::InstanceMethod("count", &K2hKeyQueue::Count),
|
|
136
|
+
K2hKeyQueue::InstanceMethod("isEmpty", &K2hKeyQueue::IsEmpty),
|
|
137
|
+
K2hKeyQueue::InstanceMethod("read", &K2hKeyQueue::Read),
|
|
138
|
+
K2hKeyQueue::InstanceMethod("pop", &K2hKeyQueue::Pop),
|
|
139
|
+
K2hKeyQueue::InstanceMethod("remove", &K2hKeyQueue::Remove),
|
|
140
|
+
K2hKeyQueue::InstanceMethod("dump", &K2hKeyQueue::Dump)
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
constructor = Napi::Persistent(funcs);
|
|
144
|
+
constructor.SuppressDestruct();
|
|
145
|
+
|
|
146
|
+
// [NOTE]
|
|
147
|
+
// do NOT do exports.Set("K2hKeyQueue", func) here if InitAll will return createFn.
|
|
148
|
+
//
|
|
118
149
|
}
|
|
119
150
|
|
|
120
|
-
|
|
151
|
+
Napi::Value K2hKeyQueue::New(const Napi::CallbackInfo& info)
|
|
121
152
|
{
|
|
122
|
-
if(info.IsConstructCall()){
|
|
153
|
+
if(info.IsConstructCall()){
|
|
123
154
|
// Invoked as constructor: new K2hKeyQueue()
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
}
|
|
155
|
+
// DefineClass 経由のコンストラクタが呼ばれているはずなので、ここでは this を返すだけで良い
|
|
156
|
+
return info.This();
|
|
157
|
+
}else{
|
|
158
|
+
// Invoked as plain function K2hKeyQueue(), turn into construct call.
|
|
159
|
+
if(0 < info.Length()){
|
|
160
|
+
return constructor.New({info[0]});
|
|
161
|
+
}else{
|
|
162
|
+
return constructor.New({});
|
|
163
|
+
}
|
|
164
|
+
}
|
|
134
165
|
}
|
|
135
166
|
|
|
136
|
-
|
|
167
|
+
// NewInstance( no parameter )
|
|
168
|
+
Napi::Object K2hKeyQueue::NewInstance(Napi::Env env)
|
|
137
169
|
{
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
info.GetReturnValue().Set(Nan::NewInstance(cons, argc, argv).ToLocalChecked());
|
|
170
|
+
Napi::EscapableHandleScope scope(env);
|
|
171
|
+
Napi::Object obj = constructor.New({}).As<Napi::Object>();
|
|
172
|
+
return scope.Escape(napi_value(obj)).ToObject();
|
|
142
173
|
}
|
|
143
174
|
|
|
144
|
-
|
|
175
|
+
// NewInstance( 1 parameter )
|
|
176
|
+
Napi::Object K2hKeyQueue::NewInstance(Napi::Env env, const Napi::Value& arg)
|
|
145
177
|
{
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
Local<Function> cons = Nan::New<Function>(constructor);
|
|
151
|
-
Local<Object> instance = Nan::NewInstance(cons, argc, argv).ToLocalChecked();
|
|
178
|
+
Napi::EscapableHandleScope scope(env);
|
|
179
|
+
Napi::Object obj = constructor.New({ arg }).As<Napi::Object>();
|
|
180
|
+
return scope.Escape(napi_value(obj)).ToObject();
|
|
181
|
+
}
|
|
152
182
|
|
|
153
|
-
|
|
183
|
+
Napi::Object K2hKeyQueue::GetInstance(const Napi::CallbackInfo& info)
|
|
184
|
+
{
|
|
185
|
+
if(0 < info.Length()){
|
|
186
|
+
return K2hKeyQueue::constructor.New({info[0]});
|
|
187
|
+
}else{
|
|
188
|
+
return K2hKeyQueue::constructor.New({});
|
|
189
|
+
}
|
|
154
190
|
}
|
|
155
191
|
|
|
156
192
|
/// \defgroup nodejs_methods
|
|
@@ -171,28 +207,36 @@ Local<Object> K2hKeyQueue::GetInstance(Nan::NAN_METHOD_ARGS_TYPE info)
|
|
|
171
207
|
* @return return true for success, false for failure
|
|
172
208
|
*/
|
|
173
209
|
|
|
174
|
-
|
|
210
|
+
Napi::Value K2hKeyQueue::On(const Napi::CallbackInfo& info)
|
|
175
211
|
{
|
|
212
|
+
Napi::Env env = info.Env();
|
|
213
|
+
|
|
214
|
+
// check
|
|
215
|
+
if(!info.This().IsObject()||!info.This().As<Napi::Object>().InstanceOf(K2hKeyQueue::constructor.Value())){
|
|
216
|
+
Napi::TypeError::New(env, "Invalid this object(K2hKeyQueue instance)").ThrowAsJavaScriptException();
|
|
217
|
+
return env.Undefined();
|
|
218
|
+
}
|
|
176
219
|
if(info.Length() < 1){
|
|
177
|
-
|
|
178
|
-
return;
|
|
220
|
+
Napi::TypeError::New(env, "No handle emitter name is specified.").ThrowAsJavaScriptException();
|
|
221
|
+
return env.Undefined();
|
|
179
222
|
}else if(info.Length() < 2){
|
|
180
|
-
|
|
181
|
-
return;
|
|
223
|
+
Napi::TypeError::New(env, "No callback is specified.").ThrowAsJavaScriptException();
|
|
224
|
+
return env.Undefined();
|
|
182
225
|
}
|
|
183
226
|
|
|
184
227
|
// check emitter name
|
|
185
|
-
|
|
186
|
-
const char*
|
|
187
|
-
if(
|
|
188
|
-
string msg
|
|
189
|
-
msg
|
|
190
|
-
msg
|
|
191
|
-
|
|
192
|
-
return;
|
|
228
|
+
std::string emitter = info[0].ToString().Utf8Value();
|
|
229
|
+
const char* pemitter = GetNormalizationEmitter(emitter.c_str(), stc_k2hkq_emitters);
|
|
230
|
+
if(!pemitter){
|
|
231
|
+
std::string msg = "Unknown ";
|
|
232
|
+
msg += emitter;
|
|
233
|
+
msg += " emitter";
|
|
234
|
+
Napi::TypeError::New(env, msg).ThrowAsJavaScriptException();
|
|
235
|
+
return env.Undefined();
|
|
193
236
|
}
|
|
237
|
+
|
|
194
238
|
// add callback
|
|
195
|
-
SetK2hkqEmitterCallback(info, 1, pemitter);
|
|
239
|
+
return SetK2hkqEmitterCallback(info, 1, pemitter);
|
|
196
240
|
}
|
|
197
241
|
|
|
198
242
|
/**
|
|
@@ -208,9 +252,9 @@ NAN_METHOD(K2hKeyQueue::On)
|
|
|
208
252
|
* @return return true for success, false for failure
|
|
209
253
|
*/
|
|
210
254
|
|
|
211
|
-
|
|
255
|
+
Napi::Value K2hKeyQueue::OnPush(const Napi::CallbackInfo& info)
|
|
212
256
|
{
|
|
213
|
-
SetK2hkqEmitterCallback(info, 0, stc_k2hkq_emitters[K2HKQ_EMITTER_POS_PUSH]);
|
|
257
|
+
return SetK2hkqEmitterCallback(info, 0, stc_k2hkq_emitters[K2HKQ_EMITTER_POS_PUSH]);
|
|
214
258
|
}
|
|
215
259
|
|
|
216
260
|
/**
|
|
@@ -226,9 +270,9 @@ NAN_METHOD(K2hKeyQueue::OnPush)
|
|
|
226
270
|
* @return return true for success, false for failure
|
|
227
271
|
*/
|
|
228
272
|
|
|
229
|
-
|
|
273
|
+
Napi::Value K2hKeyQueue::OnPop(const Napi::CallbackInfo& info)
|
|
230
274
|
{
|
|
231
|
-
SetK2hkqEmitterCallback(info, 0, stc_k2hkq_emitters[K2HKQ_EMITTER_POS_POP]);
|
|
275
|
+
return SetK2hkqEmitterCallback(info, 0, stc_k2hkq_emitters[K2HKQ_EMITTER_POS_POP]);
|
|
232
276
|
}
|
|
233
277
|
|
|
234
278
|
/**
|
|
@@ -244,9 +288,9 @@ NAN_METHOD(K2hKeyQueue::OnPop)
|
|
|
244
288
|
* @return return true for success, false for failure
|
|
245
289
|
*/
|
|
246
290
|
|
|
247
|
-
|
|
291
|
+
Napi::Value K2hKeyQueue::OnCount(const Napi::CallbackInfo& info)
|
|
248
292
|
{
|
|
249
|
-
SetK2hkqEmitterCallback(info, 0, stc_k2hkq_emitters[K2HKQ_EMITTER_POS_COUNT]);
|
|
293
|
+
return SetK2hkqEmitterCallback(info, 0, stc_k2hkq_emitters[K2HKQ_EMITTER_POS_COUNT]);
|
|
250
294
|
}
|
|
251
295
|
|
|
252
296
|
/**
|
|
@@ -262,9 +306,9 @@ NAN_METHOD(K2hKeyQueue::OnCount)
|
|
|
262
306
|
* @return return true for success, false for failure
|
|
263
307
|
*/
|
|
264
308
|
|
|
265
|
-
|
|
309
|
+
Napi::Value K2hKeyQueue::OnRead(const Napi::CallbackInfo& info)
|
|
266
310
|
{
|
|
267
|
-
SetK2hkqEmitterCallback(info, 0, stc_k2hkq_emitters[K2HKQ_EMITTER_POS_READ]);
|
|
311
|
+
return SetK2hkqEmitterCallback(info, 0, stc_k2hkq_emitters[K2HKQ_EMITTER_POS_READ]);
|
|
268
312
|
}
|
|
269
313
|
|
|
270
314
|
/**
|
|
@@ -280,9 +324,9 @@ NAN_METHOD(K2hKeyQueue::OnRead)
|
|
|
280
324
|
* @return return true for success, false for failure
|
|
281
325
|
*/
|
|
282
326
|
|
|
283
|
-
|
|
327
|
+
Napi::Value K2hKeyQueue::OnRemove(const Napi::CallbackInfo& info)
|
|
284
328
|
{
|
|
285
|
-
SetK2hkqEmitterCallback(info, 0, stc_k2hkq_emitters[K2HKQ_EMITTER_POS_REMOVE]);
|
|
329
|
+
return SetK2hkqEmitterCallback(info, 0, stc_k2hkq_emitters[K2HKQ_EMITTER_POS_REMOVE]);
|
|
286
330
|
}
|
|
287
331
|
|
|
288
332
|
/**
|
|
@@ -298,25 +342,27 @@ NAN_METHOD(K2hKeyQueue::OnRemove)
|
|
|
298
342
|
* @return return true for success, false for failure
|
|
299
343
|
*/
|
|
300
344
|
|
|
301
|
-
|
|
345
|
+
Napi::Value K2hKeyQueue::Off(const Napi::CallbackInfo& info)
|
|
302
346
|
{
|
|
347
|
+
Napi::Env env = info.Env();
|
|
348
|
+
|
|
303
349
|
if(info.Length() < 1){
|
|
304
|
-
|
|
305
|
-
return;
|
|
350
|
+
Napi::TypeError::New(env, "No handle emitter name is specified.").ThrowAsJavaScriptException();
|
|
351
|
+
return env.Undefined();
|
|
306
352
|
}
|
|
307
353
|
|
|
308
354
|
// check emitter name
|
|
309
|
-
|
|
310
|
-
const char*
|
|
311
|
-
if(
|
|
312
|
-
string
|
|
313
|
-
msg
|
|
314
|
-
msg
|
|
315
|
-
|
|
316
|
-
return;
|
|
355
|
+
std::string emitter = info[0].ToString().Utf8Value();
|
|
356
|
+
const char* pemitter = GetNormalizationEmitter(emitter.c_str(), stc_k2hkq_emitters);
|
|
357
|
+
if (nullptr == pemitter) {
|
|
358
|
+
std::string msg = "Unknown ";
|
|
359
|
+
msg += emitter;
|
|
360
|
+
msg += " emitter";
|
|
361
|
+
Napi::TypeError::New(env, msg).ThrowAsJavaScriptException();
|
|
362
|
+
return env.Undefined();
|
|
317
363
|
}
|
|
318
364
|
// unset callback
|
|
319
|
-
UnsetK2hkqEmitterCallback(info, pemitter);
|
|
365
|
+
return UnsetK2hkqEmitterCallback(info, pemitter);
|
|
320
366
|
}
|
|
321
367
|
|
|
322
368
|
/**
|
|
@@ -329,9 +375,9 @@ NAN_METHOD(K2hKeyQueue::Off)
|
|
|
329
375
|
* @return return true for success, false for failure
|
|
330
376
|
*/
|
|
331
377
|
|
|
332
|
-
|
|
378
|
+
Napi::Value K2hKeyQueue::OffPush(const Napi::CallbackInfo& info)
|
|
333
379
|
{
|
|
334
|
-
UnsetK2hkqEmitterCallback(info, stc_k2hkq_emitters[K2HKQ_EMITTER_POS_PUSH]);
|
|
380
|
+
return UnsetK2hkqEmitterCallback(info, stc_k2hkq_emitters[K2HKQ_EMITTER_POS_PUSH]);
|
|
335
381
|
}
|
|
336
382
|
|
|
337
383
|
/**
|
|
@@ -344,9 +390,9 @@ NAN_METHOD(K2hKeyQueue::OffPush)
|
|
|
344
390
|
* @return return true for success, false for failure
|
|
345
391
|
*/
|
|
346
392
|
|
|
347
|
-
|
|
393
|
+
Napi::Value K2hKeyQueue::OffPop(const Napi::CallbackInfo& info)
|
|
348
394
|
{
|
|
349
|
-
UnsetK2hkqEmitterCallback(info, stc_k2hkq_emitters[K2HKQ_EMITTER_POS_POP]);
|
|
395
|
+
return UnsetK2hkqEmitterCallback(info, stc_k2hkq_emitters[K2HKQ_EMITTER_POS_POP]);
|
|
350
396
|
}
|
|
351
397
|
|
|
352
398
|
/**
|
|
@@ -359,9 +405,9 @@ NAN_METHOD(K2hKeyQueue::OffPop)
|
|
|
359
405
|
* @return return true for success, false for failure
|
|
360
406
|
*/
|
|
361
407
|
|
|
362
|
-
|
|
408
|
+
Napi::Value K2hKeyQueue::OffCount(const Napi::CallbackInfo& info)
|
|
363
409
|
{
|
|
364
|
-
UnsetK2hkqEmitterCallback(info, stc_k2hkq_emitters[K2HKQ_EMITTER_POS_COUNT]);
|
|
410
|
+
return UnsetK2hkqEmitterCallback(info, stc_k2hkq_emitters[K2HKQ_EMITTER_POS_COUNT]);
|
|
365
411
|
}
|
|
366
412
|
|
|
367
413
|
/**
|
|
@@ -374,9 +420,9 @@ NAN_METHOD(K2hKeyQueue::OffCount)
|
|
|
374
420
|
* @return return true for success, false for failure
|
|
375
421
|
*/
|
|
376
422
|
|
|
377
|
-
|
|
423
|
+
Napi::Value K2hKeyQueue::OffRead(const Napi::CallbackInfo& info)
|
|
378
424
|
{
|
|
379
|
-
UnsetK2hkqEmitterCallback(info, stc_k2hkq_emitters[K2HKQ_EMITTER_POS_READ]);
|
|
425
|
+
return UnsetK2hkqEmitterCallback(info, stc_k2hkq_emitters[K2HKQ_EMITTER_POS_READ]);
|
|
380
426
|
}
|
|
381
427
|
|
|
382
428
|
/**
|
|
@@ -389,9 +435,9 @@ NAN_METHOD(K2hKeyQueue::OffRead)
|
|
|
389
435
|
* @return return true for success, false for failure
|
|
390
436
|
*/
|
|
391
437
|
|
|
392
|
-
|
|
438
|
+
Napi::Value K2hKeyQueue::OffRemove(const Napi::CallbackInfo& info)
|
|
393
439
|
{
|
|
394
|
-
UnsetK2hkqEmitterCallback(info, stc_k2hkq_emitters[K2HKQ_EMITTER_POS_REMOVE]);
|
|
440
|
+
return UnsetK2hkqEmitterCallback(info, stc_k2hkq_emitters[K2HKQ_EMITTER_POS_REMOVE]);
|
|
395
441
|
}
|
|
396
442
|
|
|
397
443
|
/**
|
|
@@ -411,25 +457,40 @@ NAN_METHOD(K2hKeyQueue::OffRemove)
|
|
|
411
457
|
*
|
|
412
458
|
* @return return true for success, false for failure
|
|
413
459
|
*/
|
|
414
|
-
|
|
460
|
+
|
|
461
|
+
Napi::Value K2hKeyQueue::Initialize(const Napi::CallbackInfo& info)
|
|
415
462
|
{
|
|
416
|
-
|
|
463
|
+
Napi::Env env = info.Env();
|
|
417
464
|
|
|
465
|
+
// check
|
|
418
466
|
if(info.Length() < 1){
|
|
419
|
-
|
|
420
|
-
return;
|
|
467
|
+
Napi::TypeError::New(env, "No k2hash object is specified.").ThrowAsJavaScriptException();
|
|
468
|
+
return env.Undefined();
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
// Unwrap
|
|
472
|
+
if(!info.This().IsObject()||!info.This().As<Napi::Object>().InstanceOf(K2hKeyQueue::constructor.Value())){
|
|
473
|
+
Napi::TypeError::New(env, "Invalid this object(K2hKeyQueue instance)").ThrowAsJavaScriptException();
|
|
474
|
+
return env.Undefined();
|
|
475
|
+
}
|
|
476
|
+
K2hKeyQueue* obj = Napi::ObjectWrap<K2hKeyQueue>::Unwrap(info.This().As<Napi::Object>());
|
|
477
|
+
|
|
478
|
+
// parse positional optional args
|
|
479
|
+
if(!info[0].IsObject()){
|
|
480
|
+
Napi::TypeError::New(env, "First parameter must be a k2hnode object").ThrowAsJavaScriptException();
|
|
481
|
+
return env.Undefined();
|
|
421
482
|
}
|
|
422
|
-
K2hNode*
|
|
423
|
-
|
|
483
|
+
K2hNode* objNode = Napi::ObjectWrap<K2hNode>::Unwrap(info[0].As<Napi::Object>());
|
|
484
|
+
|
|
485
|
+
bool is_fifo = (info.Length() < 2 ? true : info[1].ToBoolean().Value());
|
|
424
486
|
std::string prefix;
|
|
425
|
-
if(
|
|
426
|
-
|
|
427
|
-
prefix = std::string(*buf);
|
|
487
|
+
if(2 < info.Length()){
|
|
488
|
+
prefix = info[2].ToString().Utf8Value();
|
|
428
489
|
}
|
|
429
490
|
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
)
|
|
491
|
+
// Execute
|
|
492
|
+
bool res = obj->_k2hkeyqueue.Init(&objNode->_k2hshm, is_fifo, (prefix.empty() ? NULL : reinterpret_cast<const unsigned char*>(prefix.c_str())), (prefix.empty() ? 0UL : (prefix.length() + 1)));
|
|
493
|
+
return Napi::Boolean::New(env, res);
|
|
433
494
|
}
|
|
434
495
|
|
|
435
496
|
/**
|
|
@@ -456,94 +517,110 @@ NAN_METHOD(K2hKeyQueue::Init)
|
|
|
456
517
|
*
|
|
457
518
|
*/
|
|
458
519
|
|
|
459
|
-
|
|
520
|
+
Napi::Value K2hKeyQueue::Push(const Napi::CallbackInfo& info)
|
|
460
521
|
{
|
|
522
|
+
Napi::Env env = info.Env();
|
|
523
|
+
|
|
461
524
|
if(info.Length() < 2){
|
|
462
|
-
|
|
463
|
-
return;
|
|
464
|
-
}
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
525
|
+
Napi::TypeError::New(env, "No key name or no value are specified.").ThrowAsJavaScriptException();
|
|
526
|
+
return env.Undefined();
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
// Unwrap
|
|
530
|
+
if(!info.This().IsObject()||!info.This().As<Napi::Object>().InstanceOf(K2hKeyQueue::constructor.Value())){
|
|
531
|
+
Napi::TypeError::New(env, "Invalid this object(K2hKeyQueue instance)").ThrowAsJavaScriptException();
|
|
532
|
+
return env.Undefined();
|
|
533
|
+
}
|
|
534
|
+
K2hKeyQueue* obj = Napi::ObjectWrap<K2hKeyQueue>::Unwrap(info.This().As<Napi::Object>());
|
|
535
|
+
|
|
536
|
+
std::string strkey;
|
|
537
|
+
std::string strval;
|
|
538
|
+
bool is_pass_set = false;
|
|
539
|
+
std::string strpass;
|
|
540
|
+
bool is_exp_set = false;
|
|
541
|
+
time_t expire = 0;
|
|
542
|
+
|
|
543
|
+
// initial callback comes from emitter map if set
|
|
544
|
+
Napi::Function maybeCallback;
|
|
545
|
+
bool hasCallback = false;
|
|
546
|
+
Napi::FunctionReference* emitterCbRef = obj->_cbs.Find(stc_k2hkq_emitters[K2HKQ_EMITTER_POS_PUSH]);
|
|
547
|
+
if(emitterCbRef){
|
|
548
|
+
maybeCallback = emitterCbRef->Value();
|
|
549
|
+
hasCallback = true;
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
// parse positional optional args
|
|
553
|
+
if(info[0].IsNull()){
|
|
554
|
+
Napi::TypeError::New(env, "key is empty.").ThrowAsJavaScriptException();
|
|
555
|
+
return env.Undefined();
|
|
478
556
|
}else{
|
|
479
|
-
|
|
480
|
-
strkey = std::string(*buf);
|
|
557
|
+
strkey = info[0].ToString().Utf8Value();
|
|
481
558
|
}
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
559
|
+
|
|
560
|
+
if(info[1].IsNull()){
|
|
561
|
+
Napi::TypeError::New(env, "value is empty.").ThrowAsJavaScriptException();
|
|
562
|
+
return env.Undefined();
|
|
485
563
|
}else{
|
|
486
|
-
|
|
487
|
-
strval = std::string(*buf);
|
|
564
|
+
strval = info[1].ToString().Utf8Value();
|
|
488
565
|
}
|
|
566
|
+
|
|
489
567
|
if(2 < info.Length()){
|
|
490
|
-
if(info[2]
|
|
568
|
+
if(info[2].IsFunction()){
|
|
491
569
|
if(3 < info.Length()){
|
|
492
|
-
|
|
493
|
-
return;
|
|
570
|
+
Napi::TypeError::New(env, "Last parameter is not callback function.").ThrowAsJavaScriptException();
|
|
571
|
+
return env.Undefined();
|
|
494
572
|
}
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
strpass
|
|
503
|
-
is_pass_set
|
|
573
|
+
maybeCallback = info[2].As<Napi::Function>();
|
|
574
|
+
hasCallback = true;
|
|
575
|
+
}else if(info[2].IsNumber()){
|
|
576
|
+
int nexire = info[2].ToNumber().Int32Value();
|
|
577
|
+
expire = static_cast<time_t>(nexire);
|
|
578
|
+
is_exp_set = true;
|
|
579
|
+
}else if(info[2].IsString()){
|
|
580
|
+
strpass = info[2].ToString().Utf8Value();
|
|
581
|
+
is_pass_set = true;
|
|
504
582
|
}else{
|
|
505
|
-
|
|
506
|
-
return;
|
|
583
|
+
Napi::TypeError::New(env, "Unknown third parameter.").ThrowAsJavaScriptException();
|
|
584
|
+
return env.Undefined();
|
|
507
585
|
}
|
|
508
586
|
}
|
|
587
|
+
|
|
509
588
|
if(3 < info.Length()){
|
|
510
|
-
if(info[3]
|
|
589
|
+
if(info[3].IsFunction()){
|
|
511
590
|
if(4 < info.Length()){
|
|
512
|
-
|
|
513
|
-
return;
|
|
591
|
+
Napi::TypeError::New(env, "Last parameter is not callback function.").ThrowAsJavaScriptException();
|
|
592
|
+
return env.Undefined();
|
|
514
593
|
}
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
594
|
+
maybeCallback = info[3].As<Napi::Function>();
|
|
595
|
+
hasCallback = true;
|
|
596
|
+
}else if(info[3].IsNumber() && !is_exp_set){
|
|
597
|
+
int nexire = info[3].ToNumber().Int32Value();
|
|
598
|
+
expire = static_cast<time_t>(nexire);
|
|
599
|
+
is_exp_set = true;
|
|
519
600
|
}else{
|
|
520
|
-
|
|
521
|
-
return;
|
|
601
|
+
Napi::TypeError::New(env, "Unknown forth parameter.").ThrowAsJavaScriptException();
|
|
602
|
+
return env.Undefined();
|
|
522
603
|
}
|
|
523
604
|
}
|
|
605
|
+
|
|
524
606
|
if(4 < info.Length()){
|
|
525
|
-
if(5 < info.Length() || !info[4]
|
|
526
|
-
|
|
527
|
-
return;
|
|
607
|
+
if(5 < info.Length() || !info[4].IsFunction()){
|
|
608
|
+
Napi::TypeError::New(env, "Last parameter is not callback function.").ThrowAsJavaScriptException();
|
|
609
|
+
return env.Undefined();
|
|
528
610
|
}
|
|
529
|
-
|
|
611
|
+
maybeCallback = info[4].As<Napi::Function>();
|
|
612
|
+
hasCallback = true;
|
|
530
613
|
}
|
|
531
614
|
|
|
532
|
-
//
|
|
533
|
-
if(
|
|
534
|
-
|
|
535
|
-
|
|
615
|
+
// Execute
|
|
616
|
+
if(hasCallback){
|
|
617
|
+
// Queue async worker
|
|
618
|
+
K2hkqPushAsyncWorker* worker = new K2hkqPushAsyncWorker(maybeCallback, &(obj->_k2hkeyqueue), strkey.c_str(), strval.c_str(), (is_pass_set ? strpass.c_str() : NULL), (is_exp_set && expire > 0 ? &expire : NULL));
|
|
619
|
+
worker->Queue();
|
|
620
|
+
return Napi::Boolean::New(env, true);
|
|
536
621
|
}else{
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
reinterpret_cast<const unsigned char*>(strkey.c_str()),
|
|
540
|
-
(strkey.length() + 1),
|
|
541
|
-
reinterpret_cast<const unsigned char*>(strval.c_str()),
|
|
542
|
-
(strval.length() + 1),
|
|
543
|
-
(is_pass_set ? strpass.c_str() : NULL),
|
|
544
|
-
(expire <= 0 ? NULL : &expire)
|
|
545
|
-
)
|
|
546
|
-
));
|
|
622
|
+
bool res = obj->_k2hkeyqueue.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), (is_exp_set && expire > 0 ? &expire : NULL));
|
|
623
|
+
return Napi::Boolean::New(env, res);
|
|
547
624
|
}
|
|
548
625
|
}
|
|
549
626
|
|
|
@@ -567,26 +644,44 @@ NAN_METHOD(K2hKeyQueue::Push)
|
|
|
567
644
|
*
|
|
568
645
|
*/
|
|
569
646
|
|
|
570
|
-
|
|
647
|
+
Napi::Value K2hKeyQueue::Count(const Napi::CallbackInfo& info)
|
|
571
648
|
{
|
|
572
|
-
|
|
573
|
-
|
|
649
|
+
Napi::Env env = info.Env();
|
|
650
|
+
|
|
651
|
+
// Unwrap
|
|
652
|
+
if(!info.This().IsObject()||!info.This().As<Napi::Object>().InstanceOf(K2hKeyQueue::constructor.Value())){
|
|
653
|
+
Napi::TypeError::New(env, "Invalid this object(K2hKeyQueue instance)").ThrowAsJavaScriptException();
|
|
654
|
+
return env.Undefined();
|
|
655
|
+
}
|
|
656
|
+
K2hKeyQueue* obj = Napi::ObjectWrap<K2hKeyQueue>::Unwrap(info.This().As<Napi::Object>());
|
|
657
|
+
|
|
658
|
+
// initial callback comes from emitter map if set
|
|
659
|
+
Napi::Function maybeCallback;
|
|
660
|
+
bool hasCallback = false;
|
|
661
|
+
Napi::FunctionReference* emitterCbRef = obj->_cbs.Find(stc_k2hkq_emitters[K2HKQ_EMITTER_POS_COUNT]);
|
|
662
|
+
if(emitterCbRef){
|
|
663
|
+
maybeCallback = emitterCbRef->Value();
|
|
664
|
+
hasCallback = true;
|
|
665
|
+
}
|
|
574
666
|
|
|
667
|
+
// parse positional optional args
|
|
575
668
|
if(0 < info.Length()){
|
|
576
|
-
if(1 < info.Length() || !info[0]
|
|
577
|
-
|
|
578
|
-
return;
|
|
669
|
+
if(1 < info.Length() || !info[0].IsFunction()){
|
|
670
|
+
Napi::TypeError::New(env, "Last parameter is not callback function.").ThrowAsJavaScriptException();
|
|
671
|
+
return env.Undefined();
|
|
579
672
|
}
|
|
580
|
-
|
|
673
|
+
maybeCallback = info[0].As<Napi::Function>();
|
|
674
|
+
hasCallback = true;
|
|
581
675
|
}
|
|
582
676
|
|
|
583
|
-
//
|
|
584
|
-
if(
|
|
585
|
-
|
|
586
|
-
|
|
677
|
+
// Execute
|
|
678
|
+
if(hasCallback){
|
|
679
|
+
K2hkqCountAsyncWorker* worker = new K2hkqCountAsyncWorker(maybeCallback, &(obj->_k2hkeyqueue));
|
|
680
|
+
worker->Queue();
|
|
681
|
+
return Napi::Boolean::New(env, true);
|
|
587
682
|
}else{
|
|
588
|
-
|
|
589
|
-
|
|
683
|
+
uint64_t cnt = obj->_k2hkeyqueue.GetCount();
|
|
684
|
+
return Napi::Number::New(env, static_cast<double>(cnt));
|
|
590
685
|
}
|
|
591
686
|
}
|
|
592
687
|
|
|
@@ -598,15 +693,20 @@ NAN_METHOD(K2hKeyQueue::Count)
|
|
|
598
693
|
* @return returns true if the queue is empty. false for not empty.
|
|
599
694
|
*/
|
|
600
695
|
|
|
601
|
-
|
|
696
|
+
Napi::Value K2hKeyQueue::IsEmpty(const Napi::CallbackInfo& info)
|
|
602
697
|
{
|
|
603
|
-
|
|
698
|
+
Napi::Env env = info.Env();
|
|
604
699
|
|
|
605
|
-
//
|
|
700
|
+
// Unwrap
|
|
701
|
+
if(!info.This().IsObject() || !info.This().As<Napi::Object>().InstanceOf(K2hKeyQueue::constructor.Value())){
|
|
702
|
+
Napi::TypeError::New(env, "Invalid this object(K2hKeyQueue instance)").ThrowAsJavaScriptException();
|
|
703
|
+
return env.Undefined();
|
|
704
|
+
}
|
|
705
|
+
K2hKeyQueue* obj = Napi::ObjectWrap<K2hKeyQueue>::Unwrap(info.This().As<Napi::Object>());
|
|
606
706
|
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
)
|
|
707
|
+
// Execute
|
|
708
|
+
bool res = obj->_k2hkeyqueue.IsEmpty();
|
|
709
|
+
return Napi::Boolean::New(env, res);
|
|
610
710
|
}
|
|
611
711
|
|
|
612
712
|
/**
|
|
@@ -637,60 +737,81 @@ NAN_METHOD(K2hKeyQueue::IsEmpty)
|
|
|
637
737
|
*
|
|
638
738
|
*/
|
|
639
739
|
|
|
640
|
-
|
|
740
|
+
Napi::Value K2hKeyQueue::Read(const Napi::CallbackInfo& info)
|
|
641
741
|
{
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
742
|
+
Napi::Env env = info.Env();
|
|
743
|
+
|
|
744
|
+
// Unwrap
|
|
745
|
+
if(!info.This().IsObject() || !info.This().As<Napi::Object>().InstanceOf(K2hKeyQueue::constructor.Value())){
|
|
746
|
+
Napi::TypeError::New(env, "Invalid this object(K2hKeyQueue instance)").ThrowAsJavaScriptException();
|
|
747
|
+
return env.Undefined();
|
|
748
|
+
}
|
|
749
|
+
K2hKeyQueue* obj = Napi::ObjectWrap<K2hKeyQueue>::Unwrap(info.This().As<Napi::Object>());
|
|
750
|
+
|
|
751
|
+
int pos = 0;
|
|
752
|
+
bool is_pass_set = false;
|
|
753
|
+
std::string strpass;
|
|
754
|
+
|
|
755
|
+
// initial callback comes from emitter map if set
|
|
756
|
+
Napi::Function maybeCallback;
|
|
757
|
+
bool hasCallback = false;
|
|
758
|
+
Napi::FunctionReference* emitterCbRef = obj->_cbs.Find(stc_k2hkq_emitters[K2HKQ_EMITTER_POS_READ]);
|
|
759
|
+
if(emitterCbRef){
|
|
760
|
+
maybeCallback = emitterCbRef->Value();
|
|
761
|
+
hasCallback = true;
|
|
762
|
+
}
|
|
647
763
|
|
|
764
|
+
// parse positional optional args
|
|
648
765
|
if(0 < info.Length()){
|
|
649
|
-
if(info[0]
|
|
650
|
-
pos
|
|
651
|
-
}else if(info[0]
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
}else if(info[0]->IsFunction()){
|
|
766
|
+
if(info[0].IsNumber()){
|
|
767
|
+
pos = info[0].ToNumber().Int32Value();
|
|
768
|
+
}else if(info[0].IsString()){
|
|
769
|
+
strpass = info[0].ToString().Utf8Value();
|
|
770
|
+
is_pass_set = true;
|
|
771
|
+
}else if(info[0].IsFunction()){
|
|
656
772
|
if(1 < info.Length()){
|
|
657
|
-
|
|
658
|
-
return;
|
|
773
|
+
Napi::TypeError::New(env, "Last parameter is not callback function.").ThrowAsJavaScriptException();
|
|
774
|
+
return env.Undefined();
|
|
659
775
|
}
|
|
660
|
-
|
|
776
|
+
maybeCallback = info[0].As<Napi::Function>();
|
|
777
|
+
hasCallback = true;
|
|
661
778
|
}else{
|
|
662
|
-
|
|
663
|
-
return;
|
|
779
|
+
Napi::TypeError::New(env, "Unknown first parameter.").ThrowAsJavaScriptException();
|
|
780
|
+
return env.Undefined();
|
|
664
781
|
}
|
|
665
782
|
}
|
|
783
|
+
|
|
666
784
|
if(1 < info.Length()){
|
|
667
|
-
if(info[1]
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
}else if(info[1]->IsFunction()){
|
|
785
|
+
if(info[1].IsString() && !is_pass_set){
|
|
786
|
+
strpass = info[1].ToString().Utf8Value();
|
|
787
|
+
is_pass_set = true;
|
|
788
|
+
}else if(info[1].IsFunction()){
|
|
672
789
|
if(2 < info.Length()){
|
|
673
|
-
|
|
674
|
-
return;
|
|
790
|
+
Napi::TypeError::New(env, "Last parameter is not callback function.").ThrowAsJavaScriptException();
|
|
791
|
+
return env.Undefined();
|
|
675
792
|
}
|
|
676
|
-
|
|
793
|
+
maybeCallback = info[1].As<Napi::Function>();
|
|
794
|
+
hasCallback = true;
|
|
677
795
|
}else{
|
|
678
|
-
|
|
679
|
-
return;
|
|
796
|
+
Napi::TypeError::New(env, "Unknown second parameter.").ThrowAsJavaScriptException();
|
|
797
|
+
return env.Undefined();
|
|
680
798
|
}
|
|
681
799
|
}
|
|
800
|
+
|
|
682
801
|
if(2 < info.Length()){
|
|
683
|
-
if(3 < info.Length() || !info[2]
|
|
684
|
-
|
|
685
|
-
return;
|
|
802
|
+
if(3 < info.Length() || !info[2].IsFunction()){
|
|
803
|
+
Napi::TypeError::New(env, "Last parameter is not callback function.").ThrowAsJavaScriptException();
|
|
804
|
+
return env.Undefined();
|
|
686
805
|
}
|
|
687
|
-
|
|
806
|
+
maybeCallback = info[2].As<Napi::Function>();
|
|
807
|
+
hasCallback = true;
|
|
688
808
|
}
|
|
689
809
|
|
|
690
|
-
//
|
|
691
|
-
if(
|
|
692
|
-
|
|
693
|
-
|
|
810
|
+
// Execute
|
|
811
|
+
if(hasCallback){
|
|
812
|
+
K2hkqReadAsyncWorker* worker = new K2hkqReadAsyncWorker(maybeCallback, &(obj->_k2hkeyqueue), pos, (is_pass_set ? strpass.c_str() : NULL));
|
|
813
|
+
worker->Queue();
|
|
814
|
+
return Napi::Boolean::New(env, true);
|
|
694
815
|
}else{
|
|
695
816
|
unsigned char* pkey = NULL;
|
|
696
817
|
unsigned char* pval = NULL;
|
|
@@ -698,19 +819,22 @@ NAN_METHOD(K2hKeyQueue::Read)
|
|
|
698
819
|
size_t vallen = 0;
|
|
699
820
|
bool result = obj->_k2hkeyqueue.Read(&pkey, keylen, &pval, vallen, pos, (is_pass_set ? strpass.c_str() : NULL));
|
|
700
821
|
if(!result || !pkey || 0 == keylen){
|
|
701
|
-
|
|
822
|
+
K2H_Free(pkey);
|
|
823
|
+
K2H_Free(pval);
|
|
824
|
+
return env.Null();
|
|
702
825
|
}else{
|
|
703
|
-
|
|
704
|
-
|
|
826
|
+
Napi::Array kvarr = Napi::Array::New(env, 2);
|
|
827
|
+
|
|
828
|
+
kvarr.Set((uint32_t)0, Napi::String::New(env, std::string(reinterpret_cast<const char*>(pkey), static_cast<size_t>(strlen(reinterpret_cast<const char*>(pkey))))));
|
|
705
829
|
if(pval && 0 < vallen){
|
|
706
|
-
|
|
830
|
+
kvarr.Set((uint32_t)1, Napi::String::New(env, std::string(reinterpret_cast<const char*>(pval), static_cast<size_t>(strlen(reinterpret_cast<const char*>(pval))))));
|
|
707
831
|
}else{
|
|
708
|
-
|
|
832
|
+
kvarr.Set((uint32_t)1, Napi::String::New(env, ""));
|
|
709
833
|
}
|
|
710
|
-
|
|
834
|
+
K2H_Free(pkey);
|
|
835
|
+
K2H_Free(pval);
|
|
836
|
+
return kvarr;
|
|
711
837
|
}
|
|
712
|
-
K2H_Free(pkey);
|
|
713
|
-
K2H_Free(pval);
|
|
714
838
|
}
|
|
715
839
|
}
|
|
716
840
|
|
|
@@ -735,41 +859,61 @@ NAN_METHOD(K2hKeyQueue::Read)
|
|
|
735
859
|
*
|
|
736
860
|
*/
|
|
737
861
|
|
|
738
|
-
|
|
862
|
+
Napi::Value K2hKeyQueue::Pop(const Napi::CallbackInfo& info)
|
|
739
863
|
{
|
|
740
|
-
|
|
741
|
-
bool is_pass_set = false;
|
|
742
|
-
string strpass;
|
|
743
|
-
Nan::Callback* callback = obj->_cbs.Find(stc_k2hkq_emitters[K2HKQ_EMITTER_POS_POP]);
|
|
864
|
+
Napi::Env env = info.Env();
|
|
744
865
|
|
|
866
|
+
// Unwrap
|
|
867
|
+
if(!info.This().IsObject() || !info.This().As<Napi::Object>().InstanceOf(K2hKeyQueue::constructor.Value())){
|
|
868
|
+
Napi::TypeError::New(env, "Invalid this object(K2hKeyQueue instance)").ThrowAsJavaScriptException();
|
|
869
|
+
return env.Undefined();
|
|
870
|
+
}
|
|
871
|
+
K2hKeyQueue* obj = Napi::ObjectWrap<K2hKeyQueue>::Unwrap(info.This().As<Napi::Object>());
|
|
872
|
+
|
|
873
|
+
bool is_pass_set = false;
|
|
874
|
+
std::string strpass;
|
|
875
|
+
|
|
876
|
+
// initial callback comes from emitter map if set
|
|
877
|
+
Napi::Function maybeCallback;
|
|
878
|
+
bool hasCallback = false;
|
|
879
|
+
Napi::FunctionReference* emitterCbRef = obj->_cbs.Find(stc_k2hkq_emitters[K2HKQ_EMITTER_POS_POP]);
|
|
880
|
+
if(emitterCbRef){
|
|
881
|
+
maybeCallback = emitterCbRef->Value();
|
|
882
|
+
hasCallback = true;
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
// parse positional optional args
|
|
745
886
|
if(0 < info.Length()){
|
|
746
|
-
if(info[0]
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
}else if(info[0]->IsFunction()){
|
|
887
|
+
if(info[0].IsString()){
|
|
888
|
+
strpass = info[0].ToString().Utf8Value();
|
|
889
|
+
is_pass_set = true;
|
|
890
|
+
}else if(info[0].IsFunction()){
|
|
751
891
|
if(1 < info.Length()){
|
|
752
|
-
|
|
753
|
-
return;
|
|
892
|
+
Napi::TypeError::New(env, "Last parameter is not callback function.").ThrowAsJavaScriptException();
|
|
893
|
+
return env.Undefined();
|
|
754
894
|
}
|
|
755
|
-
|
|
895
|
+
maybeCallback = info[0].As<Napi::Function>();
|
|
896
|
+
hasCallback = true;
|
|
756
897
|
}else{
|
|
757
|
-
|
|
758
|
-
return;
|
|
898
|
+
Napi::TypeError::New(env, "Unknown first parameter.").ThrowAsJavaScriptException();
|
|
899
|
+
return env.Undefined();
|
|
759
900
|
}
|
|
760
901
|
}
|
|
902
|
+
|
|
761
903
|
if(1 < info.Length()){
|
|
762
|
-
if(2 < info.Length() || !info[1]
|
|
763
|
-
|
|
764
|
-
return;
|
|
904
|
+
if(2 < info.Length() || !info[1].IsFunction()){
|
|
905
|
+
Napi::TypeError::New(env, "Last parameter is not callback function.").ThrowAsJavaScriptException();
|
|
906
|
+
return env.Undefined();
|
|
765
907
|
}
|
|
766
|
-
|
|
908
|
+
maybeCallback = info[1].As<Napi::Function>();
|
|
909
|
+
hasCallback = true;
|
|
767
910
|
}
|
|
768
911
|
|
|
769
|
-
//
|
|
770
|
-
if(
|
|
771
|
-
|
|
772
|
-
|
|
912
|
+
// Execute
|
|
913
|
+
if(hasCallback){
|
|
914
|
+
K2hkqPopAsyncWorker* worker = new K2hkqPopAsyncWorker(maybeCallback, &(obj->_k2hkeyqueue), (is_pass_set ? strpass.c_str() : NULL));
|
|
915
|
+
worker->Queue();
|
|
916
|
+
return Napi::Boolean::New(env, true);
|
|
773
917
|
}else{
|
|
774
918
|
unsigned char* pkey = NULL;
|
|
775
919
|
unsigned char* pval = NULL;
|
|
@@ -777,19 +921,22 @@ NAN_METHOD(K2hKeyQueue::Pop)
|
|
|
777
921
|
size_t vallen = 0;
|
|
778
922
|
bool result = obj->_k2hkeyqueue.Pop(&pkey, keylen, &pval, vallen, (is_pass_set ? strpass.c_str() : NULL));
|
|
779
923
|
if(!result || !pkey || 0 == keylen){
|
|
780
|
-
|
|
924
|
+
K2H_Free(pkey);
|
|
925
|
+
K2H_Free(pval);
|
|
926
|
+
return env.Null();
|
|
781
927
|
}else{
|
|
782
|
-
|
|
783
|
-
|
|
928
|
+
Napi::Array kvarr = Napi::Array::New(env, 2);
|
|
929
|
+
|
|
930
|
+
kvarr.Set((uint32_t)0, Napi::String::New(env, std::string(reinterpret_cast<const char*>(pkey), static_cast<size_t>(strlen(reinterpret_cast<const char*>(pkey))))));
|
|
784
931
|
if(pval && 0 < vallen){
|
|
785
|
-
|
|
932
|
+
kvarr.Set((uint32_t)1, Napi::String::New(env, std::string(reinterpret_cast<const char*>(pval), static_cast<size_t>(strlen(reinterpret_cast<const char*>(pval))))));
|
|
786
933
|
}else{
|
|
787
|
-
|
|
934
|
+
kvarr.Set((uint32_t)1, Napi::String::New(env, ""));
|
|
788
935
|
}
|
|
789
|
-
|
|
936
|
+
K2H_Free(pkey);
|
|
937
|
+
K2H_Free(pval);
|
|
938
|
+
return kvarr;
|
|
790
939
|
}
|
|
791
|
-
K2H_Free(pkey);
|
|
792
|
-
K2H_Free(pval);
|
|
793
940
|
}
|
|
794
941
|
}
|
|
795
942
|
|
|
@@ -816,59 +963,81 @@ NAN_METHOD(K2hKeyQueue::Pop)
|
|
|
816
963
|
*
|
|
817
964
|
*/
|
|
818
965
|
|
|
819
|
-
|
|
966
|
+
Napi::Value K2hKeyQueue::Remove(const Napi::CallbackInfo& info)
|
|
820
967
|
{
|
|
968
|
+
Napi::Env env = info.Env();
|
|
969
|
+
|
|
821
970
|
if(info.Length() < 1){
|
|
822
|
-
|
|
823
|
-
return;
|
|
971
|
+
Napi::TypeError::New(env, "Need to specify first parameter for queue count.").ThrowAsJavaScriptException();
|
|
972
|
+
return env.Undefined();
|
|
824
973
|
}
|
|
825
974
|
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
975
|
+
// Unwrap
|
|
976
|
+
if(!info.This().IsObject() || !info.This().As<Napi::Object>().InstanceOf(K2hKeyQueue::constructor.Value())){
|
|
977
|
+
Napi::TypeError::New(env, "Invalid this object(K2hKeyQueue instance)").ThrowAsJavaScriptException();
|
|
978
|
+
return env.Undefined();
|
|
979
|
+
}
|
|
980
|
+
K2hKeyQueue* obj = Napi::ObjectWrap<K2hKeyQueue>::Unwrap(info.This().As<Napi::Object>());
|
|
981
|
+
|
|
982
|
+
int count = 0;
|
|
983
|
+
bool is_pass_set = false;
|
|
984
|
+
std::string strpass;
|
|
985
|
+
|
|
986
|
+
// initial callback comes from emitter map if set
|
|
987
|
+
Napi::Function maybeCallback;
|
|
988
|
+
bool hasCallback = false;
|
|
989
|
+
Napi::FunctionReference* emitterCbRef = obj->_cbs.Find(stc_k2hkq_emitters[K2HKQ_EMITTER_POS_REMOVE]);
|
|
990
|
+
if(emitterCbRef){
|
|
991
|
+
maybeCallback = emitterCbRef->Value();
|
|
992
|
+
hasCallback = true;
|
|
993
|
+
}
|
|
831
994
|
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
995
|
+
// parse positional optional args
|
|
996
|
+
if(!info[0].IsNumber()){
|
|
997
|
+
Napi::TypeError::New(env, "First parameter must be number.").ThrowAsJavaScriptException();
|
|
998
|
+
return env.Undefined();
|
|
835
999
|
}else{
|
|
836
|
-
count
|
|
1000
|
+
count = info[0].ToNumber().Int32Value();
|
|
837
1001
|
if(count <= 0){
|
|
838
|
-
|
|
839
|
-
return;
|
|
1002
|
+
Napi::TypeError::New(env, "Remove queue count must be over 0.").ThrowAsJavaScriptException();
|
|
1003
|
+
return env.Undefined();
|
|
840
1004
|
}
|
|
841
1005
|
}
|
|
1006
|
+
|
|
842
1007
|
if(1 < info.Length()){
|
|
843
|
-
if(info[1]
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
}else if(info[1]->IsFunction()){
|
|
1008
|
+
if(info[1].IsString()){
|
|
1009
|
+
strpass = info[1].ToString().Utf8Value();
|
|
1010
|
+
is_pass_set = true;
|
|
1011
|
+
}else if(info[1].IsFunction()){
|
|
848
1012
|
if(2 < info.Length()){
|
|
849
|
-
|
|
850
|
-
return;
|
|
1013
|
+
Napi::TypeError::New(env, "Last parameter is not callback function.").ThrowAsJavaScriptException();
|
|
1014
|
+
return env.Undefined();
|
|
851
1015
|
}
|
|
852
|
-
|
|
1016
|
+
maybeCallback = info[1].As<Napi::Function>();
|
|
1017
|
+
hasCallback = true;
|
|
853
1018
|
}else{
|
|
854
|
-
|
|
855
|
-
return;
|
|
1019
|
+
Napi::TypeError::New(env, "Unknown second parameter.").ThrowAsJavaScriptException();
|
|
1020
|
+
return env.Undefined();
|
|
856
1021
|
}
|
|
857
1022
|
}
|
|
1023
|
+
|
|
858
1024
|
if(2 < info.Length()){
|
|
859
|
-
if(3 < info.Length() || !info[2]
|
|
860
|
-
|
|
861
|
-
return;
|
|
1025
|
+
if(3 < info.Length() || !info[2].IsFunction()){
|
|
1026
|
+
Napi::TypeError::New(env, "Last parameter is not callback function.").ThrowAsJavaScriptException();
|
|
1027
|
+
return env.Undefined();
|
|
862
1028
|
}
|
|
863
|
-
|
|
1029
|
+
maybeCallback = info[2].As<Napi::Function>();
|
|
1030
|
+
hasCallback = true;
|
|
864
1031
|
}
|
|
865
1032
|
|
|
866
|
-
//
|
|
867
|
-
if(
|
|
868
|
-
|
|
869
|
-
|
|
1033
|
+
// Execute
|
|
1034
|
+
if(hasCallback){
|
|
1035
|
+
K2hkqRemoveAsyncWorker* worker = new K2hkqRemoveAsyncWorker(maybeCallback, &(obj->_k2hkeyqueue), count, (is_pass_set ? strpass.c_str() : NULL));
|
|
1036
|
+
worker->Queue();
|
|
1037
|
+
return Napi::Boolean::New(env, true);
|
|
870
1038
|
}else{
|
|
871
|
-
|
|
1039
|
+
int result = obj->_k2hkeyqueue.Remove(count, NULL, NULL, (is_pass_set ? strpass.c_str() : NULL));
|
|
1040
|
+
return Napi::Number::New(env, static_cast<double>(result));
|
|
872
1041
|
}
|
|
873
1042
|
}
|
|
874
1043
|
|
|
@@ -885,25 +1054,30 @@ NAN_METHOD(K2hKeyQueue::Remove)
|
|
|
885
1054
|
* @return return true for success, false for failure
|
|
886
1055
|
*/
|
|
887
1056
|
|
|
888
|
-
|
|
1057
|
+
Napi::Value K2hKeyQueue::Dump(const Napi::CallbackInfo& info)
|
|
889
1058
|
{
|
|
890
|
-
|
|
1059
|
+
Napi::Env env = info.Env();
|
|
1060
|
+
|
|
1061
|
+
// Unwrap
|
|
1062
|
+
if(!info.This().IsObject() || !info.This().As<Napi::Object>().InstanceOf(K2hKeyQueue::constructor.Value())){
|
|
1063
|
+
Napi::TypeError::New(env, "Invalid this object(K2hKeyQueue instance)").ThrowAsJavaScriptException();
|
|
1064
|
+
return env.Undefined();
|
|
1065
|
+
}
|
|
1066
|
+
K2hKeyQueue* obj = Napi::ObjectWrap<K2hKeyQueue>::Unwrap(info.This().As<Napi::Object>());
|
|
891
1067
|
|
|
892
|
-
int
|
|
893
|
-
FILE*
|
|
1068
|
+
int fd = (0 < info.Length() && info[0].IsNumber()) ? info[0].ToNumber().Int32Value() : -1;
|
|
1069
|
+
FILE* fp = (-1 == fd ? stdout : fdopen(fd, "a"));
|
|
894
1070
|
if(!fp){
|
|
895
|
-
|
|
896
|
-
return;
|
|
1071
|
+
Napi::Error::New(env, "could not open output stream.").ThrowAsJavaScriptException();
|
|
1072
|
+
return env.Undefined();
|
|
897
1073
|
}
|
|
898
1074
|
|
|
899
|
-
|
|
900
|
-
obj->_k2hkeyqueue.Dump(fp)
|
|
901
|
-
));
|
|
1075
|
+
obj->_k2hkeyqueue.Dump(fp);
|
|
902
1076
|
|
|
903
1077
|
// Need to flush stream here!
|
|
904
1078
|
fflush(fp);
|
|
905
1079
|
|
|
906
|
-
|
|
1080
|
+
return Napi::Boolean::New(env, true);
|
|
907
1081
|
}
|
|
908
1082
|
|
|
909
1083
|
//@}
|