This repository has been archived by the owner on Dec 1, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
batch.cc
142 lines (107 loc) · 3.88 KB
/
batch.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
#include <node.h>
#include <node_buffer.h>
#include <nan.h>
#include "database.h"
#include "batch_async.h"
#include "batch.h"
#include "common.h"
namespace leveldown {
static Nan::Persistent<v8::FunctionTemplate> batch_constructor;
Batch::Batch (leveldown::Database* database, bool sync) : database(database) {
options = new rocksdb::WriteOptions();
options->sync = sync;
batch = new rocksdb::WriteBatch();
hasData = false;
}
Batch::~Batch () {
delete options;
delete batch;
}
rocksdb::Status Batch::Write () {
return database->WriteBatchToDatabase(options, batch);
}
void Batch::Init () {
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(Batch::New);
batch_constructor.Reset(tpl);
tpl->SetClassName(Nan::New("Batch").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);
Nan::SetPrototypeMethod(tpl, "put", Batch::Put);
Nan::SetPrototypeMethod(tpl, "del", Batch::Del);
Nan::SetPrototypeMethod(tpl, "clear", Batch::Clear);
Nan::SetPrototypeMethod(tpl, "write", Batch::Write);
}
NAN_METHOD(Batch::New) {
Database* database = Nan::ObjectWrap::Unwrap<Database>(info[0]->ToObject());
v8::Local<v8::Object> optionsObj;
if (info.Length() > 1 && info[1]->IsObject()) {
optionsObj = v8::Local<v8::Object>::Cast(info[1]);
}
bool sync = BooleanOptionValue(optionsObj, "sync");
Batch* batch = new Batch(database, sync);
batch->Wrap(info.This());
info.GetReturnValue().Set(info.This());
}
v8::Local<v8::Value> Batch::NewInstance (
v8::Local<v8::Object> database
, v8::Local<v8::Object> optionsObj
) {
Nan::EscapableHandleScope scope;
Nan::MaybeLocal<v8::Object> maybeInstance;
v8::Local<v8::Object> instance;
v8::Local<v8::FunctionTemplate> constructorHandle =
Nan::New<v8::FunctionTemplate>(batch_constructor);
if (optionsObj.IsEmpty()) {
v8::Local<v8::Value> argv[1] = { database };
maybeInstance = Nan::NewInstance(constructorHandle->GetFunction(), 1, argv);
} else {
v8::Local<v8::Value> argv[2] = { database, optionsObj };
maybeInstance = Nan::NewInstance(constructorHandle->GetFunction(), 2, argv);
}
if (maybeInstance.IsEmpty())
Nan::ThrowError("Could not create new Batch instance");
else
instance = maybeInstance.ToLocalChecked();
return scope.Escape(instance);
}
NAN_METHOD(Batch::Put) {
Batch* batch = ObjectWrap::Unwrap<Batch>(info.Holder());
v8::Local<v8::Function> callback; // purely for the error macros
v8::Local<v8::Value> keyBuffer = info[0];
v8::Local<v8::Value> valueBuffer = info[1];
LD_STRING_OR_BUFFER_TO_SLICE(key, keyBuffer, key)
LD_STRING_OR_BUFFER_TO_SLICE(value, valueBuffer, value)
batch->batch->Put(key, value);
if (!batch->hasData)
batch->hasData = true;
DisposeStringOrBufferFromSlice(keyBuffer, key);
DisposeStringOrBufferFromSlice(valueBuffer, value);
info.GetReturnValue().Set(info.Holder());
}
NAN_METHOD(Batch::Del) {
Batch* batch = ObjectWrap::Unwrap<Batch>(info.Holder());
v8::Local<v8::Function> callback; // purely for the error macros
v8::Local<v8::Value> keyBuffer = info[0];
LD_STRING_OR_BUFFER_TO_SLICE(key, keyBuffer, key)
batch->batch->Delete(key);
if (!batch->hasData)
batch->hasData = true;
DisposeStringOrBufferFromSlice(keyBuffer, key);
info.GetReturnValue().Set(info.Holder());
}
NAN_METHOD(Batch::Clear) {
Batch* batch = ObjectWrap::Unwrap<Batch>(info.Holder());
batch->batch->Clear();
batch->hasData = false;
info.GetReturnValue().Set(info.Holder());
}
NAN_METHOD(Batch::Write) {
Batch* batch = ObjectWrap::Unwrap<Batch>(info.Holder());
Nan::Callback *callback =
new Nan::Callback(v8::Local<v8::Function>::Cast(info[0]));
BatchWriteWorker* worker = new BatchWriteWorker(batch, callback);
// persist to prevent accidental GC
v8::Local<v8::Object> _this = info.This();
worker->SaveToPersistent("batch", _this);
Nan::AsyncQueueWorker(worker);
}
} // namespace leveldown