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/src/k2h_cbs.cc CHANGED
@@ -22,7 +22,6 @@
22
22
  #include <fullock/flckbaselist.tcc>
23
23
  #include "k2h_cbs.h"
24
24
 
25
- using namespace v8;
26
25
  using namespace std;
27
26
  using namespace fullock;
28
27
 
@@ -40,68 +39,55 @@ StackEmitCB::~StackEmitCB()
40
39
  flck_unlock_noshared_mutex(&lockval); // UNLOCK
41
40
  }
42
41
 
43
- // [NOTE]
44
- // This method does not lock, thus must lock before calling this.
45
- //
46
- Nan::Callback* StackEmitCB::RawFind(const char* pemitname)
42
+ bool StackEmitCB::Set(const std::string& emitter, const Napi::Function& cb)
47
43
  {
48
- string stremit = pemitname ? pemitname : "";
49
- Nan::Callback* cbfunc = NULL;
50
- if(stremit.empty()){
51
- return cbfunc;
52
- }
53
- if(EmitCbsMap.end() != EmitCbsMap.find(stremit)){
54
- cbfunc = EmitCbsMap[stremit];
44
+ while(!flck_trylock_noshared_mutex(&lockval)); // LOCK
45
+
46
+ // clear if existed
47
+ auto it = EmitCbsMap.find(emitter);
48
+ if(EmitCbsMap.end() != it){
49
+ it->second.Reset();
50
+ EmitCbsMap.erase(it);
55
51
  }
56
- return cbfunc;
57
- }
58
52
 
59
- Nan::Callback* StackEmitCB::Find(const char* pemitname)
60
- {
61
- while(!flck_trylock_noshared_mutex(&lockval)); // LOCK
62
- Nan::Callback* cbfunc = RawFind(pemitname);
53
+ // Insert new persistent reference
54
+ Napi::FunctionReference ref = Napi::Persistent(cb);
55
+ EmitCbsMap.emplace(emitter, std::move(ref));
56
+
63
57
  flck_unlock_noshared_mutex(&lockval); // UNLOCK
64
58
 
65
- return cbfunc;
59
+ return true;
66
60
  }
67
61
 
68
- bool StackEmitCB::Set(const char* pemitname, Nan::Callback* cbfunc)
62
+ bool StackEmitCB::Unset(const std::string& emitter)
69
63
  {
70
- string stremit = pemitname ? pemitname : "";
71
- if(stremit.empty()){
72
- return false;
73
- }
74
-
75
64
  while(!flck_trylock_noshared_mutex(&lockval)); // LOCK
76
65
 
77
- const Nan::Callback* oldcbfunc = RawFind(pemitname);
78
- if(oldcbfunc){
79
- EmitCbsMap.erase(stremit);
80
- }
81
- if(cbfunc){
82
- EmitCbsMap[stremit] = cbfunc;
66
+ auto it = EmitCbsMap.find(emitter);
67
+ if(EmitCbsMap.end() == it){
68
+ flck_unlock_noshared_mutex(&lockval); // UNLOCK
69
+ return false;
83
70
  }
71
+ it->second.Reset();
72
+ EmitCbsMap.erase(it);
73
+
84
74
  flck_unlock_noshared_mutex(&lockval); // UNLOCK
85
75
 
86
76
  return true;
87
77
  }
88
78
 
89
- bool StackEmitCB::Unset(const char* pemitname)
79
+ Napi::FunctionReference* StackEmitCB::Find(const std::string& emitter)
90
80
  {
91
- string stremit = pemitname ? pemitname : "";
92
- if(stremit.empty()){
93
- return false;
94
- }
95
-
96
81
  while(!flck_trylock_noshared_mutex(&lockval)); // LOCK
97
82
 
98
- const Nan::Callback* oldcbfunc = RawFind(pemitname);
99
- if(oldcbfunc){
100
- EmitCbsMap.erase(stremit);
83
+ auto it = EmitCbsMap.find(emitter);
84
+ if(EmitCbsMap.end() == it){
85
+ flck_unlock_noshared_mutex(&lockval); // UNLOCK
86
+ return nullptr;
101
87
  }
102
88
  flck_unlock_noshared_mutex(&lockval); // UNLOCK
103
89
 
104
- return true;
90
+ return &it->second;
105
91
  }
106
92
 
107
93
  /*
package/src/k2h_cbs.h CHANGED
@@ -21,32 +21,36 @@
21
21
  #ifndef K2H_CBS_H
22
22
  #define K2H_CBS_H
23
23
 
24
+ #include <string>
25
+ #include <unordered_map>
24
26
  #include "k2h_common.h"
25
27
 
26
28
  //---------------------------------------------------------
27
29
  // Typedefs
28
30
  //---------------------------------------------------------
29
- typedef std::map<std::string, Nan::Callback*> cbsmap;
31
+ typedef std::unordered_map<std::string, Napi::FunctionReference> cbsmap;
30
32
 
31
33
  //---------------------------------------------------------
32
34
  // StackEmitCB Class
33
35
  //---------------------------------------------------------
34
36
  class StackEmitCB
35
37
  {
36
- protected:
37
- cbsmap EmitCbsMap;
38
- volatile int lockval; // lock variable for mapping
39
-
40
- protected:
41
- Nan::Callback* RawFind(const char* pemitname);
42
-
43
38
  public:
44
39
  StackEmitCB();
45
40
  virtual ~StackEmitCB();
46
41
 
47
- Nan::Callback* Find(const char* pemitname);
48
- bool Set(const char* pemitname, Nan::Callback* cbfunc);
49
- bool Unset(const char* pemitname);
42
+ // Set returns true if set succeeded
43
+ bool Set(const std::string& emitter, const Napi::Function& cb);
44
+
45
+ // Unset returns true if removed
46
+ bool Unset(const std::string& emitter);
47
+
48
+ // Find returns pointer to FunctionReference if set, otherwise nullptr
49
+ Napi::FunctionReference* Find(const std::string& emitter);
50
+
51
+ private:
52
+ cbsmap EmitCbsMap;
53
+ volatile int lockval; // lock variable for mapping
50
54
  };
51
55
 
52
56
  //---------------------------------------------------------
@@ -55,14 +59,14 @@ class StackEmitCB
55
59
  inline const char* GetNormalizationEmitter(const char* emitter, const char* emitters[])
56
60
  {
57
61
  if(!emitter){
58
- return NULL;
62
+ return nullptr;
59
63
  }
60
64
  for(const char** ptmp = &emitters[0]; ptmp && *ptmp; ++ptmp){
61
65
  if(0 == strcasecmp(*ptmp, emitter)){
62
66
  return *ptmp;
63
67
  }
64
68
  }
65
- return NULL;
69
+ return nullptr;
66
70
  }
67
71
 
68
72
  #endif
package/src/k2h_common.h CHANGED
@@ -31,7 +31,7 @@
31
31
  #include <k2hash/k2hqueue.h>
32
32
  #include <k2hash/k2hutil.h>
33
33
 
34
- #include <nan.h>
34
+ #include <napi.h>
35
35
  #include <string>
36
36
  #include <iostream>
37
37
  #include <map>