-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathBatch.cc
More file actions
158 lines (133 loc) · 4.71 KB
/
Batch.cc
File metadata and controls
158 lines (133 loc) · 4.71 KB
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
#include <node.h>
#include <nan.h>
#include "rocksdb/db.h"
#include "Batch.h"
#include "DBNode.h"
#include <iostream>
#include "Errors.h"
//using namespace std;
Nan::Persistent<v8::FunctionTemplate> batch_constructor;
Batch::Batch(DBNode *dbNode) {
_dbNode = dbNode;
}
Batch::~Batch() {
}
void Batch::Init() {
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(Batch::New);
tpl->SetClassName(Nan::New("Batch").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);
Nan::SetPrototypeMethod(tpl, "put", Batch::Put);
Nan::SetPrototypeMethod(tpl, "del", Batch::Delete);
batch_constructor.Reset(tpl);
}
NAN_METHOD(Batch::New) {
// We expect Batch(dbNode)
int rocksIndex = -1;
if (info.Length() == 1) {
rocksIndex = 0;
} else {
Nan::ThrowTypeError(ERR_WRONG_ARGS);
return;
}
DBNode* rocks = Nan::ObjectWrap::Unwrap<DBNode>(info[rocksIndex].As<v8::Object>());
Batch* obj = new Batch(rocks);
obj->Wrap(info.This());
info.GetReturnValue().Set(info.This());
}
void Batch::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 Batch. Batches 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>(batch_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;
}
NAN_METHOD(Batch::Put) {
int familyIndex = -1;
int keyIndex = -1;
int valueIndex = -1;
// 2 args, assume key value
if (info.Length() == 2) {
keyIndex = 0;
valueIndex = 1;
} else if (info.Length() == 3) {
// 3 args is (family, key, value)
familyIndex = 0;
keyIndex = 1;
valueIndex = 2;
} else {
Nan::ThrowTypeError(ERR_WRONG_ARGS);
return;
}
v8::Local<v8::Object> keyObj = info[keyIndex].As<v8::Object>();
v8::Local<v8::Object> valueObj = info[valueIndex].As<v8::Object>();
Batch* rocksDBBatch = ObjectWrap::Unwrap<Batch>(info.Holder());
rocksdb::ColumnFamilyHandle *columnFamily = NULL;
if (familyIndex != -1) {
string family = string(*Nan::Utf8String(info[familyIndex]));
columnFamily = rocksDBBatch->_dbNode->GetColumnFamily(family);
} else {
columnFamily = rocksDBBatch->_dbNode->GetColumnFamily(rocksdb::kDefaultColumnFamilyName);
}
if (columnFamily == NULL) {
Nan::ThrowError(ERR_CF_DOES_NOT_EXIST);
return;
}
rocksdb::Slice key = node::Buffer::HasInstance(info[keyIndex]) ? rocksdb::Slice(node::Buffer::Data(keyObj), node::Buffer::Length(keyObj))
: rocksdb::Slice(string(*Nan::Utf8String(info[keyIndex])));
rocksdb::Slice value = node::Buffer::HasInstance(info[valueIndex]) ? rocksdb::Slice(node::Buffer::Data(valueObj), node::Buffer::Length(valueObj))
: rocksdb::Slice(string(*Nan::Utf8String(info[valueIndex])));
rocksDBBatch->_batch.Put(columnFamily, key, value);
}
NAN_METHOD(Batch::Delete) {
int familyIndex = -1;
int keyIndex = -1;
// if only one arg, assume it's the key
if (info.Length() == 1) {
keyIndex = 0;
} else if (info.Length() == 2) {
// the two args are (family, key)
familyIndex = 0;
keyIndex = 1;
} else {
Nan::ThrowTypeError(ERR_WRONG_ARGS);
return;
}
rocksdb::Slice key = node::Buffer::HasInstance(info[keyIndex]) ? rocksdb::Slice(node::Buffer::Data(info[keyIndex]->ToObject()), node::Buffer::Length(info[keyIndex]->ToObject()))
: rocksdb::Slice(string(*Nan::Utf8String(info[keyIndex])));
Batch* rocksDBBatch = ObjectWrap::Unwrap<Batch>(info.Holder());
rocksdb::ColumnFamilyHandle *columnFamily = NULL;
if (familyIndex != -1) {
string family = string(*Nan::Utf8String(info[familyIndex]));
columnFamily = rocksDBBatch->_dbNode->GetColumnFamily(family);
} else {
columnFamily = rocksDBBatch->_dbNode->GetColumnFamily(rocksdb::kDefaultColumnFamilyName);
}
if (columnFamily == NULL) {
Nan::ThrowError(ERR_CF_DOES_NOT_EXIST);
return;
}
rocksDBBatch->_batch.Delete(columnFamily, key);
}