-
Notifications
You must be signed in to change notification settings - Fork 29
/
hash.cc
executable file
·175 lines (139 loc) · 5.58 KB
/
hash.cc
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
// MegaHash v1.0
// Copyright (c) 2019 Joseph Huckaby
// Based on DeepHash, (c) 2003 Joseph Huckaby
#include <stdio.h>
#include <stdint.h>
#include "hash.h"
Napi::FunctionReference MegaHash::constructor;
Napi::Object MegaHash::Init(Napi::Env env, Napi::Object exports) {
// initialize class
Napi::HandleScope scope(env);
Napi::Function func = DefineClass(env, "MegaHash", {
InstanceMethod("_set", &MegaHash::Set),
InstanceMethod("_get", &MegaHash::Get),
InstanceMethod("_has", &MegaHash::Has),
InstanceMethod("_remove", &MegaHash::Remove),
InstanceMethod("clear", &MegaHash::Clear),
InstanceMethod("stats", &MegaHash::Stats),
InstanceMethod("_firstKey", &MegaHash::FirstKey),
InstanceMethod("_nextKey", &MegaHash::NextKey)
});
constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
exports.Set("MegaHash", func);
return exports;
}
MegaHash::MegaHash(const Napi::CallbackInfo& info) : Napi::ObjectWrap<MegaHash>(info) {
// construct new hash table
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
// 8 buckets per list with 16 scatter is about the perfect balance of speed and memory
// FUTURE: Make this configurable from Node.js side?
this->hash = new Hash( 8, 16 );
}
MegaHash::~MegaHash() {
// cleanup and free memory
delete this->hash;
}
Napi::Value MegaHash::Set(const Napi::CallbackInfo& info) {
// store key/value pair, no return value
Napi::Env env = info.Env();
Napi::Buffer<unsigned char> keyBuf = info[0].As<Napi::Buffer<unsigned char>>();
unsigned char *key = keyBuf.Data();
MH_KLEN_T keyLength = (MH_KLEN_T)keyBuf.Length();
Napi::Buffer<unsigned char> valueBuf = info[1].As<Napi::Buffer<unsigned char>>();
unsigned char *value = valueBuf.Data();
MH_LEN_T valueLength = (MH_LEN_T)valueBuf.Length();
unsigned char flags = 0;
if (info.Length() > 2) {
flags = (unsigned char)info[2].As<Napi::Number>().Uint32Value();
}
Response resp = this->hash->store( key, keyLength, value, valueLength, flags );
return Napi::Number::New(env, (double)resp.result);
}
Napi::Value MegaHash::Get(const Napi::CallbackInfo& info) {
// fetch value given key
Napi::Env env = info.Env();
Napi::Buffer<unsigned char> keyBuf = info[0].As<Napi::Buffer<unsigned char>>();
unsigned char *key = keyBuf.Data();
MH_KLEN_T keyLength = (MH_KLEN_T)keyBuf.Length();
Response resp = this->hash->fetch( key, keyLength );
if (resp.result == MH_OK) {
Napi::Buffer<unsigned char> valueBuf = Napi::Buffer<unsigned char>::Copy( env, resp.content, resp.contentLength );
if (!valueBuf) return env.Undefined();
if (resp.flags) valueBuf.Set( "flags", (double)resp.flags );
return valueBuf;
}
else return env.Undefined();
}
Napi::Value MegaHash::Has(const Napi::CallbackInfo& info) {
// see if a key exists, return boolean true/value
Napi::Env env = info.Env();
Napi::Buffer<unsigned char> keyBuf = info[0].As<Napi::Buffer<unsigned char>>();
unsigned char *key = keyBuf.Data();
MH_KLEN_T keyLength = (MH_KLEN_T)keyBuf.Length();
Response resp = this->hash->fetch( key, keyLength );
return Napi::Boolean::New(env, (resp.result == MH_OK));
}
Napi::Value MegaHash::Remove(const Napi::CallbackInfo& info) {
// remove key/value pair, free up memory
Napi::Env env = info.Env();
Napi::Buffer<unsigned char> keyBuf = info[0].As<Napi::Buffer<unsigned char>>();
unsigned char *key = keyBuf.Data();
MH_KLEN_T keyLength = (MH_KLEN_T)keyBuf.Length();
Response resp = this->hash->remove( key, keyLength );
return Napi::Boolean::New(env, (resp.result == MH_OK));
}
Napi::Value MegaHash::Clear(const Napi::CallbackInfo& info) {
// delete some or all keys/values from hash, free all memory
unsigned char slice1 = 0;
unsigned char slice2 = 0;
if (info.Length() == 2) {
// clear thin slice
slice1 = (unsigned char)info[0].As<Napi::Number>().Uint32Value();
slice2 = (unsigned char)info[1].As<Napi::Number>().Uint32Value();
this->hash->clear( slice1, slice2 );
}
else if (info.Length() == 1) {
// clear thick slice
slice1 = (unsigned char)info[0].As<Napi::Number>().Uint32Value();
this->hash->clear( slice1 );
}
else {
// clear all
this->hash->clear();
}
return info.Env().Undefined();
}
Napi::Value MegaHash::Stats(const Napi::CallbackInfo& info) {
// return stats as node object
Napi::Env env = info.Env();
Napi::Object obj = Napi::Object::New(env);
obj.Set(Napi::String::New(env, "indexSize"), (double)this->hash->stats->indexSize);
obj.Set(Napi::String::New(env, "metaSize"), (double)this->hash->stats->metaSize);
obj.Set(Napi::String::New(env, "dataSize"), (double)this->hash->stats->dataSize);
obj.Set(Napi::String::New(env, "numKeys"), (double)this->hash->stats->numKeys);
obj.Set(Napi::String::New(env, "numIndexes"), (double)(this->hash->stats->indexSize / (int)sizeof(Index)));
return obj;
}
Napi::Value MegaHash::FirstKey(const Napi::CallbackInfo& info) {
// return first key in hash (in undefined order)
Napi::Env env = info.Env();
Response resp = this->hash->firstKey();
if (resp.result == MH_OK) {
return Napi::Buffer<unsigned char>::Copy( env, resp.content, resp.contentLength );
}
else return env.Undefined();
}
Napi::Value MegaHash::NextKey(const Napi::CallbackInfo& info) {
// return next key in hash given previous one (in undefined order)
Napi::Env env = info.Env();
Napi::Buffer<unsigned char> keyBuf = info[0].As<Napi::Buffer<unsigned char>>();
unsigned char *key = keyBuf.Data();
MH_KLEN_T keyLength = (MH_KLEN_T)keyBuf.Length();
Response resp = this->hash->nextKey( key, keyLength );
if (resp.result == MH_OK) {
return Napi::Buffer<unsigned char>::Copy( env, resp.content, resp.contentLength );
}
else return env.Undefined();
}