/
binding.cpp
548 lines (491 loc) · 18 KB
/
binding.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
//
// Author: Vlad Seryakov vseryakov@gmail.com
// April 2013
//
#include <node.h>
#include <node_object_wrap.h>
#include <node_buffer.h>
#include <node_version.h>
#include <v8.h>
#include <v8-profiler.h>
#include <uv.h>
#include <nan.h>
using namespace node;
using namespace v8;
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <list>
#include <map>
#include <queue>
#if defined __APPLE__ && __cplusplus < 201103L
#include <tr1/unordered_map>
using namespace std::tr1;
#else
#include <unordered_map>
#endif
using namespace std;
#define NAN_OPTIONAL_ARGUMENT_AS_BOOL(i, var, dflt) bool var = (info.Length() > (i) ? Nan::To<bool>(info[i]).FromJust() : dflt);
#define NAN_OPTIONAL_ARGUMENT_AS_INT(i, var, dflt) int var = (info.Length() > (i) ? Nan::To<int32_t>(info[i]).FromJust() : dflt);
#define NAN_OPTIONAL_ARGUMENT_AS_UINT(i, var, dflt) unsigned int var = (info.Length() > (i) ? Nan::To<int32_t>(info[i]).FromJust() : dflt);
#define NAN_OPTIONAL_ARGUMENT_AS_INT64(i, var, dflt) int64_t var = (info.Length() > (i) ? Nan::To<int64_t>(info[i]).FromJust() : dflt);
#define NAN_OPTIONAL_ARGUMENT_AS_UINT64(i, var, dflt) uint64_t var = (info.Length() > (i) ? Nan::To<int64_t>(info[i]).FromJust() : dflt);
#define NAN_OPTIONAL_ARGUMENT_AS_STRING(i, var) Nan::Utf8String var(info.Length() > (i) ? Nan::To<v8::String>(info[i]).ToLocalChecked() : Nan::EmptyString());
#define NAN_REQUIRE_ARGUMENT(i) if (info.Length() <= i || info[i]->IsUndefined()) {Nan::ThrowError("Argument " #i " is required");return;}
#define NAN_REQUIRE_ARGUMENT_STRING(i, var) if (info.Length() <= (i) || !info[i]->IsString()) {Nan::ThrowError("Argument " #i " must be a string"); return;} Nan::Utf8String var(Nan::To<v8::String>(info[i]).ToLocalChecked());
#define NAN_REQUIRE_ARGUMENT_AS_STRING(i, var) if (info.Length() <= (i)) {Nan::ThrowError("Argument " #i " must be a string"); return;} Nan::Utf8String var(Nan::To<v8::String>(info[i]).ToLocalChecked());
#define NAN_REQUIRE_ARGUMENT_OBJECT(i, var) if (info.Length() <= (i) || !info[i]->IsObject()) {Nan::ThrowError("Argument " #i " must be an object"); return;} Local<Object> var(Nan::To<v8::Object>(info[i]).ToLocalChecked());
#define NAN_REQUIRE_ARGUMENT_INT(i, var) if (info.Length() <= (i)) {Nan::ThrowError("Argument " #i " must be an integer"); return;} int var = Nan::To<int32_t>(info[i]).FromJust();
#define NAN_REQUIRE_ARGUMENT_INT64(i, var) if (info.Length() <= (i)) {Nan::ThrowError("Argument " #i " must be an integer"); return;} int64_t var = Nan::To<int64_t>(info[i]).FromJust();
#define NAN_REQUIRE_ARGUMENT_BOOL(i, var) if (info.Length() <= (i)) {Nan::ThrowError("Argument " #i " must be a boolean"); return;} int var = Nan::To<int32_t>(info[i]).FromJust();
#define NAN_REQUIRE_ARGUMENT_NUMBER(i, var) if (info.Length() <= (i)) {Nan::ThrowError("Argument " #i " must be a number"); return;} double var = Nan::To<double>(info[i]).FromJust();
#define NAN_REQUIRE_ARGUMENT_ARRAY(i, var) if (info.Length() <= (i) || !info[i]->IsArray()) {Nan::ThrowError("Argument " #i " must be an array"); return;} Local<Array> var = Local<Array>::Cast(info[i]);
#define NAN_REQUIRE_ARGUMENT_FUNCTION(i, var) if (info.Length() <= (i) || !info[i]->IsFunction()) {Nan::ThrowError("Argument " #i " must be a function"); return;} Local<Function> var = Local<Function>::Cast(info[i]);
#define NAN_TRY_CATCH_CALL(context, callback, argc, argv) { Nan::TryCatch try_catch; Nan::Call((callback), (context), (argc), (argv)); if (try_catch.HasCaught()) FatalException(try_catch); }
static const string Empty;
typedef map<string, string> bkStringMap;
struct LRUStringCache {
typedef unordered_map<string, pair<pair<string,uint64_t>,list<string>::iterator> > LRUStringItems;
size_t size;
size_t max;
list<string> lru;
LRUStringItems items;
// stats
size_t hits, misses, cleans, ins, dels;
LRUStringCache(int m = 100000): max(m) { clear(); }
~LRUStringCache() { clear(); }
const string& get(const string& k, uint64_t now) {
const LRUStringItems::iterator it = items.find(k);
if (it == items.end()) {
misses++;
return Empty;
}
hits++;
if (now > 0 && now > it->second.first.second) {
del(k);
return Empty;
}
lru.splice(lru.end(), lru, it->second.second);
return it->second.first.first;
}
const string& put(const string& k, const string& v, uint64_t expire) {
if (items.size() >= max) clean();
const LRUStringItems::iterator it = items.find(k);
if (it == items.end()) {
list<string>::iterator it = lru.insert(lru.end(), k);
pair<LRUStringItems::iterator,bool> p = items.insert(std::make_pair(k, std::make_pair(std::make_pair(v, expire), it)));
Nan::AdjustExternalMemory(k.size() + v.size());
size += k.size() + v.size();
ins++;
return p.first->second.first.first;
} else {
Nan::AdjustExternalMemory(-it->second.first.first.size());
it->second.first.first = v;
it->second.first.second = expire;
Nan::AdjustExternalMemory(v.size());
lru.splice(lru.end(), lru, it->second.second);
return it->second.first.first;
}
}
bool exists(const string &k) {
const LRUStringItems::iterator it = items.find(k);
return it != items.end();
}
const string &incr(const string& k, const string& v, int64_t expire) {
const string& o = get(k, 0);
char val[32];
sprintf(val, "%lld", atoll(o.c_str()) + atoll(v.c_str()));
return put(k, val, expire);
}
void del(const string &k) {
const LRUStringItems::iterator it = items.find(k);
if (it == items.end()) return;
size -= k.size() + it->second.first.first.size();
Nan::AdjustExternalMemory(-(k.size() + it->second.first.first.size()));
lru.erase(it->second.second);
items.erase(it);
dels++;
}
string front() {
return lru.size() ? lru.front() : Empty;
}
void clean() {
const LRUStringItems::iterator it = items.find(lru.front());
if (it == items.end()) return;
size -= it->first.size() + it->second.first.first.size();
items.erase(it);
lru.pop_front();
cleans++;
}
void clear() {
items.clear();
lru.clear();
Nan::AdjustExternalMemory(-size);
size = ins = dels = cleans = hits = misses = 0;
}
};
struct StringCache {
bkStringMap items;
bkStringMap::const_iterator nextIt;
uint64_t expire;
StringCache(): expire(0) {
nextIt = items.end();
}
~StringCache() { clear(0); }
const string &get(const string &key) {
bkStringMap::iterator it = items.find(key);
if (it != items.end()) return it->second;
return Empty;
}
string &put(const string &key, const string &val) {
bkStringMap::iterator it = items.find(key);
if (it != items.end()) {
Nan::AdjustExternalMemory(-it->second.size());
it->second = val;
Nan::AdjustExternalMemory(val.size());
return it->second;
} else {
it = items.insert(items.end(), std::pair<string,string>(key, val));
Nan::AdjustExternalMemory(key.size() + val.size());
return it->second;
}
}
bool exists(const string &k) {
bkStringMap::iterator it = items.find(k);
return it != items.end();
}
const string &incr(const string& k, const string& v) {
string o = get(k);
char val[32];
sprintf(val, "%lld", atoll(o.c_str()) + atoll(v.c_str()));
return put(k, val);
}
void del(const string &key) {
bkStringMap::iterator it = items.find(key);
if (it != items.end()) {
Nan::AdjustExternalMemory(-(it->first.size() + it->second.size()));
items.erase(it);
}
}
void clear(int ttl) {
bkStringMap::iterator it;
int n = 0;
for (it = items.begin(); it != items.end(); ++it) {
n += it->first.size() + it->second.size();
}
items.clear();
nextIt = items.end();
Nan::AdjustExternalMemory(-n);
expire = ttl > 0 ? time(0) + ttl : 0;
}
bool begin() {
nextIt = items.begin();
return true;
}
Local<Value> next() {
Nan::EscapableHandleScope scope;
if (nextIt == items.end()) return scope.Escape(Nan::Undefined());
Local<Array> obj = Nan::New<Array>();
Nan::Set(obj, Nan::New(0), Nan::New(nextIt->first).ToLocalChecked());
Nan::Set(obj, Nan::New(1), Nan::New(nextIt->second).ToLocalChecked());
nextIt++;
return scope.Escape(obj);
}
void each(Local<Function> cb) {
bkStringMap::const_iterator it = items.begin();
while (it != items.end()) {
Nan::HandleScope scope;
Local<Value> argv[2];
argv[0] = Nan::New(it->first).ToLocalChecked();
argv[1] = Nan::New(it->second).ToLocalChecked();
NAN_TRY_CATCH_CALL(Nan::GetCurrentContext()->Global(), cb, 2, argv);
it++;
}
}
};
typedef map<std::string, ::StringCache> Cache;
static Cache _cache;
static LRUStringCache _lru;
NAN_METHOD(clear)
{
NAN_REQUIRE_ARGUMENT_AS_STRING(0, name);
NAN_OPTIONAL_ARGUMENT_AS_INT(1, ttl, 0);
Cache::iterator itc = _cache.find(*name);
if (itc != _cache.end()) {
itc->second.clear(ttl);
_cache.erase(*name);
} else
if (ttl > 0) {
_cache[*name] = StringCache();
itc = _cache.find(*name);
itc->second.clear(ttl);
}
}
NAN_METHOD(put)
{
NAN_REQUIRE_ARGUMENT_AS_STRING(0, name);
NAN_REQUIRE_ARGUMENT_AS_STRING(1, key);
NAN_REQUIRE_ARGUMENT_AS_STRING(2, val);
Cache::iterator itc = _cache.find(*name);
if (itc == _cache.end()) {
_cache[*name] = StringCache();
itc = _cache.find(*name);
}
itc->second.put(*key, *val);
}
NAN_METHOD(incr)
{
NAN_REQUIRE_ARGUMENT_AS_STRING(0, name);
NAN_REQUIRE_ARGUMENT_AS_STRING(1, key);
NAN_REQUIRE_ARGUMENT_AS_STRING(2, val);
Cache::iterator itc = _cache.find(*name);
if (itc == _cache.end()) {
_cache[*name] = StringCache();
itc = _cache.find(*name);
}
string v = itc->second.incr(*key, *val);
info.GetReturnValue().Set(Nan::New(v).ToLocalChecked());
}
NAN_METHOD(del)
{
NAN_REQUIRE_ARGUMENT_AS_STRING(0, name);
NAN_REQUIRE_ARGUMENT_AS_STRING(1, key);
Cache::iterator itc = _cache.find(*name);
if (itc != _cache.end()) itc->second.del(*key);
}
NAN_METHOD(get)
{
NAN_REQUIRE_ARGUMENT_AS_STRING(0, name);
NAN_REQUIRE_ARGUMENT_AS_STRING(1, key);
Cache::iterator itc = _cache.find(*name);
if (itc != _cache.end()) {
info.GetReturnValue().Set(Nan::New(itc->second.get(*key)).ToLocalChecked());
return;
}
}
NAN_METHOD(exists)
{
NAN_REQUIRE_ARGUMENT_AS_STRING(0, name);
NAN_REQUIRE_ARGUMENT_AS_STRING(1, key);
Cache::iterator itc = _cache.find(*name);
if (itc != _cache.end()) {
info.GetReturnValue().Set(Nan::New(itc->second.exists(*key)));
} else {
info.GetReturnValue().Set(Nan::False());
}
}
NAN_METHOD(keys)
{
NAN_REQUIRE_ARGUMENT_AS_STRING(0, name);
Local<Array> keys = Nan::New<Array>();
Cache::iterator itc = _cache.find(*name);
if (itc != _cache.end()) {
bkStringMap::const_iterator it = itc->second.items.begin();
int i = 0;
while (it != itc->second.items.end()) {
Nan::Set(keys, Nan::New(i), Nan::New(it->first).ToLocalChecked());
it++;
i++;
}
}
info.GetReturnValue().Set(keys);
}
NAN_METHOD(names)
{
Local<Array> keys = Nan::New<Array>();
Cache::const_iterator it = _cache.begin();
int i = 0;
while (it != _cache.end()) {
Local<String> str = Nan::New(it->first).ToLocalChecked();
Nan::Set(keys, Nan::New(i), str);
it++;
i++;
}
info.GetReturnValue().Set(keys);
}
NAN_METHOD(size)
{
NAN_REQUIRE_ARGUMENT_AS_STRING(0, name);
int count = 0;
Cache::iterator itc = _cache.find(*name);
if (itc != _cache.end()) count = itc->second.items.size();
info.GetReturnValue().Set(Nan::New(count));
}
NAN_METHOD(each)
{
NAN_REQUIRE_ARGUMENT_AS_STRING(0, name);
NAN_REQUIRE_ARGUMENT_FUNCTION(1, cb);
Cache::iterator itc = _cache.find(*name);
if (itc != _cache.end()) itc->second.each(cb);
}
NAN_METHOD(begin)
{
NAN_REQUIRE_ARGUMENT_AS_STRING(0, name);
Cache::iterator itc = _cache.find(*name);
if (itc != _cache.end()) {
info.GetReturnValue().Set(Nan::New(itc->second.begin()));
} else {
info.GetReturnValue().Set(Nan::False());
}
}
NAN_METHOD(next)
{
NAN_REQUIRE_ARGUMENT_AS_STRING(0, name);
Cache::iterator itc = _cache.find(*name);
if (itc != _cache.end()) info.GetReturnValue().Set(itc->second.next());
}
NAN_METHOD(lruInit)
{
NAN_REQUIRE_ARGUMENT_INT(0, max);
if (max > 0) _lru.max = max;
}
NAN_METHOD(lruSize)
{
info.GetReturnValue().Set(Nan::New((double)_lru.size));
}
NAN_METHOD(lruCount)
{
info.GetReturnValue().Set(Nan::New((double)_lru.items.size()));
}
NAN_METHOD(lruClear)
{
_lru.clear();
}
NAN_METHOD(lruClean)
{
_lru.clean();
}
NAN_METHOD(lruPut)
{
NAN_REQUIRE_ARGUMENT_AS_STRING(0, key);
NAN_REQUIRE_ARGUMENT_AS_STRING(1, val);
NAN_OPTIONAL_ARGUMENT_AS_UINT64(2, expire, 0);
_lru.put(*key, *val, expire);
}
NAN_METHOD(lruIncr)
{
NAN_REQUIRE_ARGUMENT_AS_STRING(0, key);
NAN_REQUIRE_ARGUMENT_AS_STRING(1, val);
NAN_OPTIONAL_ARGUMENT_AS_UINT64(2, expire, 0);
const string& str = _lru.incr(*key, *val, expire);
info.GetReturnValue().Set(Nan::New(str).ToLocalChecked());
}
NAN_METHOD(lruGet)
{
NAN_REQUIRE_ARGUMENT_AS_STRING(0, key);
NAN_OPTIONAL_ARGUMENT_AS_UINT64(1, now, 0);
const string& str = _lru.get(*key, now);
info.GetReturnValue().Set(Nan::New(str).ToLocalChecked());
}
NAN_METHOD(lruFront)
{
const string& str = _lru.front();
info.GetReturnValue().Set(Nan::New(str).ToLocalChecked());
}
NAN_METHOD(lruDel)
{
NAN_REQUIRE_ARGUMENT_AS_STRING(0, key);
_lru.del(*key);
}
NAN_METHOD(lruExists)
{
NAN_REQUIRE_ARGUMENT_AS_STRING(0, key);
info.GetReturnValue().Set(Nan::New(_lru.exists(*key)));
}
NAN_METHOD(lruKeys)
{
NAN_OPTIONAL_ARGUMENT_AS_STRING(0, str);
NAN_OPTIONAL_ARGUMENT_AS_INT(1, details, 0);
Local<Array> keys = Nan::New<Array>();
char *key = *str;
int i = 0, n = strlen(key);
if (!details) {
list<string>::iterator it = _lru.lru.begin();
while (it != _lru.lru.end()) {
if (!*key || !strncmp(it->c_str(), key, n)) {
Nan::Set(keys, Nan::New(i), Nan::New(it->c_str()).ToLocalChecked());
i++;
}
it++;
}
} else {
LRUStringCache::LRUStringItems::iterator it = _lru.items.begin();
while (it != _lru.items.end()) {
if (!*key || !strncmp(it->first.c_str(), key, n)) {
switch (details) {
case 1: {
Local<Object> obj = Nan::New<Object>();
Nan::Set(obj, Nan::New("key").ToLocalChecked(), Nan::New(it->first).ToLocalChecked());
Nan::Set(obj, Nan::New("expire").ToLocalChecked(), Nan::New((double)it->second.first.second));
Nan::Set(keys, Nan::New(i), obj);
break;
}
default: {
Local<Object> obj = Nan::New<Object>();
Nan::Set(obj, Nan::New("key").ToLocalChecked(), Nan::New(it->first).ToLocalChecked());
Nan::Set(obj, Nan::New("expire").ToLocalChecked(), Nan::New((double)it->second.first.second));
Nan::Set(obj, Nan::New("value").ToLocalChecked(), Nan::New(it->second.first.first).ToLocalChecked());
Nan::Set(keys, Nan::New(i), obj);
}}
i++;
}
it++;
}
}
info.GetReturnValue().Set(keys);
}
static void ClearCacheTimer(uv_timer_t *req)
{
uint64_t now = time(0);
Cache::iterator itc = _cache.begin();
while (itc != _cache.end()) {
if (itc->second.expire > 0 && itc->second.expire < now) itc->second.clear(0);
itc++;
}
}
NAN_METHOD(lruStats)
{
Local<Object> obj = Nan::New<Object>();
Nan::Set(obj, Nan::New("inserted").ToLocalChecked(), Nan::New((double)_lru.ins));
Nan::Set(obj, Nan::New("deleted").ToLocalChecked(), Nan::New((double)_lru.dels));
Nan::Set(obj, Nan::New("cleanups").ToLocalChecked(), Nan::New((double)_lru.cleans));
Nan::Set(obj, Nan::New("hits").ToLocalChecked(), Nan::New((double)_lru.hits));
Nan::Set(obj, Nan::New("misses").ToLocalChecked(), Nan::New((double)_lru.misses));
Nan::Set(obj, Nan::New("max").ToLocalChecked(), Nan::New((double)_lru.max));
Nan::Set(obj, Nan::New("size").ToLocalChecked(), Nan::New((double)_lru.size));
Nan::Set(obj, Nan::New("count").ToLocalChecked(), Nan::New((double)_lru.items.size()));
info.GetReturnValue().Set(obj);
}
static NAN_MODULE_INIT(CacheInit) {
Nan::HandleScope scope;
NAN_EXPORT(target, put);
NAN_EXPORT(target, incr);
NAN_EXPORT(target, get);
NAN_EXPORT(target, exists);
NAN_EXPORT(target, del);
NAN_EXPORT(target, keys);
NAN_EXPORT(target, clear);
NAN_EXPORT(target, names);
NAN_EXPORT(target, size);
NAN_EXPORT(target, each);
NAN_EXPORT(target, begin);
NAN_EXPORT(target, next);
NAN_EXPORT(target, lruInit);
NAN_EXPORT(target, lruStats);
NAN_EXPORT(target, lruSize);
NAN_EXPORT(target, lruCount);
NAN_EXPORT(target, lruPut);
NAN_EXPORT(target, lruGet);
NAN_EXPORT(target, lruExists);
NAN_EXPORT(target, lruIncr);
NAN_EXPORT(target, lruDel);
NAN_EXPORT(target, lruKeys);
NAN_EXPORT(target, lruClear);
NAN_EXPORT(target, lruClean);
NAN_EXPORT(target, lruFront);
uv_timer_t *req = new uv_timer_t;
uv_timer_init(uv_default_loop(), req);
uv_timer_start(req, (uv_timer_cb)ClearCacheTimer, 0, 60000);
}
NODE_MODULE(binding, CacheInit);