-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathFileWriter.cc
165 lines (138 loc) · 4.99 KB
/
FileWriter.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
#include <node.h>
#include <nan.h>
#include "rocksdb/db.h"
#include "FileWriter.h"
#include "DBNode.h"
#include "Errors.h"
#include <iostream>
Nan::Persistent<v8::FunctionTemplate> filewriter_constructor;
FileWriter::FileWriter (rocksdb::ColumnFamilyHandle *handle, rocksdb::DB* db) {
rocksdb::EnvOptions env(db->GetDBOptions());
rocksdb::Options options = db->GetOptions();
const rocksdb::Comparator* comparator = handle->GetComparator();
_sstFileWriter = new rocksdb::SstFileWriter(env, options, comparator, handle, true);
}
FileWriter::~FileWriter () {
if (_sstFileWriter) {
delete _sstFileWriter;
_sstFileWriter = NULL;
}
}
void FileWriter::Init() {
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(FileWriter::New);
tpl->SetClassName(Nan::New("FileWriter").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);
Nan::SetPrototypeMethod(tpl, "open", FileWriter::Open);
Nan::SetPrototypeMethod(tpl, "add", FileWriter::Add);
Nan::SetPrototypeMethod(tpl, "finish", FileWriter::Finish);
Nan::SetPrototypeMethod(tpl, "fileSize", FileWriter::FileSize);
filewriter_constructor.Reset(tpl);
}
NAN_METHOD(FileWriter::New) {
// We expect new FileWriter(<columnFamilyName>, dbNode), where columnFamilyName is optional
int columnFamilyIndex = -1;
int rocksIndex = -1;
if (info.Length() == 1) {
rocksIndex = 0;
} else if (info.Length() == 2) {
// newFileWriter(columnFamilyIndex, dbNode)
columnFamilyIndex = 0;
rocksIndex = 1;
} else {
Nan::ThrowTypeError(ERR_WRONG_ARGS);
return;
}
DBNode* dbNode = Nan::ObjectWrap::Unwrap<DBNode>(info[rocksIndex].As<v8::Object>());
rocksdb::ColumnFamilyHandle *columnFamily = NULL;
if (columnFamilyIndex != -1) {
string family = string(*Nan::Utf8String(info[columnFamilyIndex]));
columnFamily = dbNode->GetColumnFamily(family);
} else {
columnFamily = dbNode->GetColumnFamily(rocksdb::kDefaultColumnFamilyName);
}
FileWriter* obj = new FileWriter(columnFamily, dbNode->db());
obj->Wrap(info.This());
info.GetReturnValue().Set(info.This());
}
void FileWriter::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 FileWriter. FileWriters 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>(filewriter_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(FileWriter::Open) {
int openIndex = -1;
if (info.Length() == 1) {
openIndex = 0;
} else {
Nan::ThrowTypeError(ERR_WRONG_ARGS);
return;
}
string path = string(*Nan::Utf8String(info[openIndex]));
FileWriter* fw = Nan::ObjectWrap::Unwrap<FileWriter>(info.Holder());
rocksdb::Status s = fw->_sstFileWriter->Open(path);
if (!s.ok()) {
Nan::ThrowError(s.getState());
return;
}
}
NAN_METHOD(FileWriter::Add) {
int keyIndex = -1;
int valueIndex = -1;
// 2 args, assume key value
if (info.Length() == 2) {
keyIndex = 0;
valueIndex = 1;
} 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>();
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])));
FileWriter* fw = Nan::ObjectWrap::Unwrap<FileWriter>(info.Holder());
rocksdb::Status s = fw->_sstFileWriter->Add(key, value);
if (!s.ok()) {
Nan::ThrowError(s.getState());
return;
}
}
NAN_METHOD(FileWriter::Finish) {
FileWriter* fw = Nan::ObjectWrap::Unwrap<FileWriter>(info.Holder());
rocksdb::Status s = fw->_sstFileWriter->Finish();
if (!s.ok()) {
Nan::ThrowError(s.getState());
return;
}
}
NAN_METHOD(FileWriter::FileSize) {
FileWriter* fw = Nan::ObjectWrap::Unwrap<FileWriter>(info.Holder());
info.GetReturnValue().Set(Nan::New<v8::Number>(fw->_sstFileWriter->FileSize()));
}