-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSnapshot.cc
76 lines (63 loc) · 2.04 KB
/
Snapshot.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
#include <node.h>
#include <nan.h>
#include "rocksdb/db.h"
#include "Snapshot.h"
#include "DBNode.h"
#include "Errors.h"
#include <iostream>
Nan::Persistent<v8::FunctionTemplate> snapshot_constructor;
Snapshot::Snapshot (rocksdb::DB* db) {
_snapshot = db->GetSnapshot();
}
Snapshot::~Snapshot () {
}
void Snapshot::Init() {
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(Snapshot::New);
tpl->SetClassName(Nan::New("Snapshot").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);
snapshot_constructor.Reset(tpl);
}
NAN_METHOD(Snapshot::New) {
// We expect GetSnapshot(<options>, <columnFamilyName>, dbNode), where both options and columnFamilyName are both optional
int rocksIndex = 0;
if (info.Length() == 1) {
rocksIndex = 0;
} else {
Nan::ThrowTypeError(ERR_WRONG_ARGS);
return;
}
DBNode* dbNode = Nan::ObjectWrap::Unwrap<DBNode>(info[rocksIndex].As<v8::Object>());
Snapshot* obj = new Snapshot(dbNode->db());
obj->Wrap(info.This());
info.GetReturnValue().Set(info.This());
}
void Snapshot::NewInstance(const Nan::FunctionCallbackInfo<v8::Value>& args) {
v8::Isolate* isolate = args.GetIsolate();
// Note: we pass an additional argument here which is the DBNode object
// that is creating the new Snapshot. Snapshots can't be created anywhere else.
const unsigned argc = args.Length() + 1;
v8::Local<v8::Value> *argv = new v8::Local<v8::Value>[argc];
for (unsigned int i=0; i<argc-1; i++) {
argv[i] = args[i];
}
argv[argc-1] = args.Holder();
v8::Local<v8::FunctionTemplate> cons = Nan::New<v8::FunctionTemplate>(snapshot_constructor);
v8::MaybeLocal<v8::Object> instance;
v8::Local<v8::Value> err;
bool hasException = false;
{
Nan::TryCatch tc;
instance = Nan::NewInstance(cons->GetFunction(), argc, argv);
if (tc.HasCaught()) {
err = tc.Exception();
hasException = true;
}
}
if (hasException) {
isolate->ThrowException(err);
} else {
args.GetReturnValue().Set(instance.ToLocalChecked());
}
delete [] argv;
argv = NULL;
}