Skip to content

Commit bb6c52a

Browse files
committed
removed unused files and added IDFromTimestamp function
Signed-off-by: Utkarsh Srivastava <srivastavautkarsh8097@gmail.com>
1 parent 61040b3 commit bb6c52a

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

binding.gyp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
"cflags_cc!": [ "-fno-exceptions" ],
66
"sources": [
77
"cppsrc/main.cpp",
8-
"cppsrc/generate_hash.cpp"
98
],
109
"cflags_cc": [
1110
"-std=c++17"

cppsrc/main.cpp

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,21 @@ class Snowflake : public Napi::ObjectWrap<Snowflake>
6161
Napi::Value getUniqueIDBigInt(const Napi::CallbackInfo &info);
6262
Napi::Value getTimestampFromID(const Napi::CallbackInfo &info);
6363
Napi::Value getNodeIDFomID(const Napi::CallbackInfo &info);
64+
Napi::Value getIDFromTimestamp(const Napi::CallbackInfo &info);
6465
};
6566

6667
Napi::Object Snowflake::Init(Napi::Env env, Napi::Object exports)
6768
{
6869
// This method is used to hook the accessor and method callbacks
69-
auto func = DefineClass(env, "Snowflake", {InstanceMethod("getUniqueID", &Snowflake::getUniqueIDBigInt), InstanceMethod("getTimestampFromID", &Snowflake::getTimestampFromID), InstanceMethod("getNodeIDFromID", &Snowflake::getNodeIDFomID)});
70+
auto func = DefineClass(
71+
env,
72+
"Snowflake",
73+
{
74+
InstanceMethod("getUniqueID", &Snowflake::getUniqueIDBigInt),
75+
InstanceMethod("getTimestampFromID", &Snowflake::getTimestampFromID),
76+
InstanceMethod("getNodeIDFromID", &Snowflake::getNodeIDFomID),
77+
InstanceMethod("getIDFromTimestamp", &Snowflake::getIDFromTimestamp),
78+
});
7079

7180
// Create a peristent reference to the class constructor. This will allow
7281
// a function called on a class prototype and a function
@@ -152,13 +161,14 @@ Napi::Value Snowflake::getTimestampFromID(const Napi::CallbackInfo &info)
152161

153162
uniqueID = std::stoull(first.Utf8Value());
154163
}
155-
else if (info[0].IsNumber())
164+
else if (info[0].IsBigInt())
156165
{
157-
uniqueID = info[0].As<Napi::Number>().Int64Value();
166+
bool lossless = true;
167+
uniqueID = info[0].As<Napi::BigInt>().Int64Value(&lossless);
158168
}
159169
else
160170
{
161-
Napi::TypeError::New(env, "Number or string expected").ThrowAsJavaScriptException();
171+
Napi::TypeError::New(env, "BigInt or string expected").ThrowAsJavaScriptException();
162172
}
163173

164174
uint64_t timestamp = uniqueID >> (TOTAL_BITS - EPOCH_BITS);
@@ -167,9 +177,7 @@ Napi::Value Snowflake::getTimestampFromID(const Napi::CallbackInfo &info)
167177
}
168178

169179
/**
170-
* Returns timestamp at which the id was generated by retreiving
171-
* the first 42 bits of the id, which are filled with current timestamp
172-
* bits
180+
* Returns NodeID/Machine ID at which the id was generated
173181
*/
174182
Napi::Value Snowflake::getNodeIDFomID(const Napi::CallbackInfo &info)
175183
{
@@ -182,13 +190,14 @@ Napi::Value Snowflake::getNodeIDFomID(const Napi::CallbackInfo &info)
182190

183191
uniqueID = std::stoull(first.Utf8Value());
184192
}
185-
else if (info[0].IsNumber())
193+
else if (info[0].IsBigInt())
186194
{
187-
uniqueID = info[0].As<Napi::Number>().Int64Value();
195+
bool lossless = true;
196+
uniqueID = info[0].As<Napi::BigInt>().Int64Value(&lossless);
188197
}
189198
else
190199
{
191-
Napi::TypeError::New(env, "Number or string expected").ThrowAsJavaScriptException();
200+
Napi::TypeError::New(env, "BigInt or string expected").ThrowAsJavaScriptException();
192201
}
193202

194203
int BITS = TOTAL_BITS - NODE_ID_BITS - SEQUENCE_BITS;
@@ -197,6 +206,39 @@ Napi::Value Snowflake::getNodeIDFomID(const Napi::CallbackInfo &info)
197206
return Napi::Number::New(env, machineID);
198207
}
199208

209+
/**
210+
* getIDFromTimestamp takes in a timestamp and will produce
211+
* id corresponding to that timestamp. It will generate ID with
212+
* sequence number 0.
213+
*
214+
* This method can be useful in writing SQL queries
215+
* where you want results which were created after
216+
* a certain timestamp
217+
*/
218+
Napi::Value Snowflake::getIDFromTimestamp(const Napi::CallbackInfo &info)
219+
{
220+
auto env = info.Env();
221+
uint64_t timestamp = 0;
222+
223+
if (info[0].IsString())
224+
{
225+
auto first = info[0].As<Napi::String>();
226+
timestamp = std::stoull(first.Utf8Value());
227+
}
228+
else if (info[0].IsNumber())
229+
{
230+
timestamp = info[0].As<Napi::Number>().Int64Value();
231+
}
232+
233+
uint64_t currentTimestamp = timestamp - this->_CUSTOM_EPOCH;
234+
235+
uint64_t id{};
236+
id = currentTimestamp << (TOTAL_BITS - EPOCH_BITS);
237+
id |= (this->_nodeID << (TOTAL_BITS - EPOCH_BITS - NODE_ID_BITS));
238+
239+
return Napi::BigInt::New(env, id);
240+
}
241+
200242
// ////////////////////////////////////////////////////////////////////////////////////////
201243

202244
Napi::Object InitAll(Napi::Env env, Napi::Object exports)

0 commit comments

Comments
 (0)