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
/
database.cc
526 lines (435 loc) · 15.5 KB
/
database.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
#include <node.h>
#include <node_buffer.h>
#include <rocksdb/db.h>
#include <rocksdb/write_batch.h>
#include "leveldown.h"
#include "database.h"
#include "async.h"
#include "database_async.h"
#include "batch.h"
#include "iterator.h"
#include "common.h"
namespace leveldown {
static Nan::Persistent<v8::FunctionTemplate> database_constructor;
Database::Database (const v8::Local<v8::Value>& from)
: location(new Nan::Utf8String(from))
, db(NULL)
, currentIteratorId(0)
, pendingCloseWorker(NULL)
, blockCache(NULL)
, filterPolicy(NULL) {};
Database::~Database () {
if (db != NULL)
delete db;
delete location;
};
/* Calls from worker threads, NO V8 HERE *****************************/
rocksdb::Status Database::OpenDatabase (
rocksdb::Options* options,
bool readOnly
) {
if (readOnly) {
return rocksdb::DB::OpenForReadOnly(*options, **location, &db);
} else {
return rocksdb::DB::Open(*options, **location, &db);
}
}
rocksdb::Status Database::PutToDatabase (
rocksdb::WriteOptions* options
, rocksdb::Slice key
, rocksdb::Slice value
) {
return db->Put(*options, key, value);
}
rocksdb::Status Database::GetFromDatabase (
rocksdb::ReadOptions* options
, rocksdb::Slice key
, std::string& value
) {
return db->Get(*options, key, &value);
}
rocksdb::Status Database::DeleteFromDatabase (
rocksdb::WriteOptions* options
, rocksdb::Slice key
) {
return db->Delete(*options, key);
}
rocksdb::Status Database::WriteBatchToDatabase (
rocksdb::WriteOptions* options
, rocksdb::WriteBatch* batch
) {
return db->Write(*options, batch);
}
uint64_t Database::ApproximateSizeFromDatabase (const rocksdb::Range* range) {
uint64_t size;
db->GetApproximateSizes(range, 1, &size);
return size;
}
void Database::CompactRangeFromDatabase (const rocksdb::Slice* start,
const rocksdb::Slice* end) {
rocksdb::CompactRangeOptions options;
db->CompactRange(options, start, end);
}
void Database::GetPropertyFromDatabase (
const rocksdb::Slice& property
, std::string* value) {
db->GetProperty(property, value);
}
rocksdb::Iterator* Database::NewIterator (rocksdb::ReadOptions* options) {
return db->NewIterator(*options);
}
const rocksdb::Snapshot* Database::NewSnapshot () {
return db->GetSnapshot();
}
void Database::ReleaseSnapshot (const rocksdb::Snapshot* snapshot) {
return db->ReleaseSnapshot(snapshot);
}
void Database::ReleaseIterator (uint32_t id) {
// called each time an Iterator is End()ed, in the main thread
// we have to remove our reference to it and if it's the last iterator
// we have to invoke a pending CloseWorker if there is one
// if there is a pending CloseWorker it means that we're waiting for
// iterators to end before we can close them
iterators.erase(id);
if (iterators.empty() && pendingCloseWorker != NULL) {
Nan::AsyncQueueWorker((AsyncWorker*)pendingCloseWorker);
pendingCloseWorker = NULL;
}
}
void Database::CloseDatabase () {
delete db;
db = NULL;
if (blockCache) {
// According to
// https://github.com/facebook/rocksdb/wiki/basic-operations#cache
// it doesn't look like this needs to be deleted by hand anymore.
// delete blockCache;
blockCache = NULL;
}
if (filterPolicy) {
delete filterPolicy;
filterPolicy = NULL;
}
}
/* V8 exposed functions *****************************/
NAN_METHOD(LevelDOWN) {
v8::Local<v8::String> location = info[0].As<v8::String>();
info.GetReturnValue().Set(Database::NewInstance(location));
}
void Database::Init () {
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(Database::New);
database_constructor.Reset(tpl);
tpl->SetClassName(Nan::New("Database").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);
Nan::SetPrototypeMethod(tpl, "open", Database::Open);
Nan::SetPrototypeMethod(tpl, "close", Database::Close);
Nan::SetPrototypeMethod(tpl, "put", Database::Put);
Nan::SetPrototypeMethod(tpl, "get", Database::Get);
Nan::SetPrototypeMethod(tpl, "del", Database::Delete);
Nan::SetPrototypeMethod(tpl, "batch", Database::Batch);
Nan::SetPrototypeMethod(tpl, "approximateSize", Database::ApproximateSize);
Nan::SetPrototypeMethod(tpl, "compactRange", Database::CompactRange);
Nan::SetPrototypeMethod(tpl, "getProperty", Database::GetProperty);
Nan::SetPrototypeMethod(tpl, "iterator", Database::Iterator);
}
NAN_METHOD(Database::New) {
Database* obj = new Database(info[0]);
obj->Wrap(info.This());
info.GetReturnValue().Set(info.This());
}
v8::Local<v8::Value> Database::NewInstance (v8::Local<v8::String> &location) {
Nan::EscapableHandleScope scope;
Nan::MaybeLocal<v8::Object> maybeInstance;
v8::Local<v8::Object> instance;
v8::Local<v8::FunctionTemplate> constructorHandle =
Nan::New<v8::FunctionTemplate>(database_constructor);
v8::Local<v8::Value> argv[] = { location };
maybeInstance = Nan::NewInstance(constructorHandle->GetFunction(), 1, argv);
if (maybeInstance.IsEmpty())
Nan::ThrowError("Could not create new Database instance");
else
instance = maybeInstance.ToLocalChecked();
return scope.Escape(instance);
}
NAN_METHOD(Database::Open) {
LD_METHOD_SETUP_COMMON(open, 0, 1)
bool readOnly = BooleanOptionValue(optionsObj, "readOnly", false);
bool createIfMissing = BooleanOptionValue(optionsObj, "createIfMissing", true);
bool errorIfExists = BooleanOptionValue(optionsObj, "errorIfExists");
bool compression = BooleanOptionValue(optionsObj, "compression", true);
uint32_t cacheSize = UInt32OptionValue(optionsObj, "cacheSize", 8 << 20);
uint32_t writeBufferSize = UInt32OptionValue(
optionsObj
, "writeBufferSize"
, 4 << 20
);
uint32_t blockSize = UInt32OptionValue(optionsObj, "blockSize", 4096);
uint32_t maxOpenFiles = UInt32OptionValue(optionsObj, "maxOpenFiles", 1000);
uint32_t blockRestartInterval = UInt32OptionValue(
optionsObj
, "blockRestartInterval"
, 16
);
uint32_t maxFileSize = UInt32OptionValue(optionsObj, "maxFileSize", 2 << 20);
database->blockCache = cacheSize ? rocksdb::NewLRUCache(cacheSize) :
NULL;
database->filterPolicy = rocksdb::NewBloomFilterPolicy(10);
OpenWorker* worker = new OpenWorker(
database
, new Nan::Callback(callback)
, database->blockCache
, database->filterPolicy
, createIfMissing
, errorIfExists
, compression
, writeBufferSize
, blockSize
, maxOpenFiles
, blockRestartInterval
, maxFileSize
, readOnly
);
// persist to prevent accidental GC
v8::Local<v8::Object> _this = info.This();
worker->SaveToPersistent("database", _this);
Nan::AsyncQueueWorker(worker);
}
// for an empty callback to iterator.end()
NAN_METHOD(EmptyMethod) {
}
NAN_METHOD(Database::Close) {
LD_METHOD_SETUP_COMMON_ONEARG(close)
CloseWorker* worker = new CloseWorker(
database
, new Nan::Callback(callback)
);
// persist to prevent accidental GC
v8::Local<v8::Object> _this = info.This();
worker->SaveToPersistent("database", _this);
if (!database->iterators.empty()) {
// yikes, we still have iterators open! naughty naughty.
// we have to queue up a CloseWorker and manually close each of them.
// the CloseWorker will be invoked once they are all cleaned up
database->pendingCloseWorker = worker;
for (
std::map< uint32_t, leveldown::Iterator * >::iterator it
= database->iterators.begin()
; it != database->iterators.end()
; ++it) {
// for each iterator still open, first check if it's already in
// the process of ending (ended==true means an async End() is
// in progress), if not, then we call End() with an empty callback
// function and wait for it to hit ReleaseIterator() where our
// CloseWorker will be invoked
leveldown::Iterator *iterator = it->second;
if (!iterator->ended) {
v8::Local<v8::Function> end =
v8::Local<v8::Function>::Cast(iterator->handle()->Get(
Nan::New<v8::String>("end").ToLocalChecked()));
v8::Local<v8::Value> argv[] = {
Nan::New<v8::FunctionTemplate>(EmptyMethod)->GetFunction() // empty callback
};
Nan::AsyncResource ar("rocksdb:iterator.end");
ar.runInAsyncScope(iterator->handle(), end, 1, argv);
}
}
} else {
Nan::AsyncQueueWorker(worker);
}
}
NAN_METHOD(Database::Put) {
LD_METHOD_SETUP_COMMON(put, 2, 3)
v8::Local<v8::Object> keyHandle = info[0].As<v8::Object>();
v8::Local<v8::Object> valueHandle = info[1].As<v8::Object>();
LD_STRING_OR_BUFFER_TO_SLICE(key, keyHandle, key);
LD_STRING_OR_BUFFER_TO_SLICE(value, valueHandle, value);
bool sync = BooleanOptionValue(optionsObj, "sync");
WriteWorker* worker = new WriteWorker(
database
, new Nan::Callback(callback)
, key
, value
, sync
, keyHandle
, valueHandle
);
// persist to prevent accidental GC
v8::Local<v8::Object> _this = info.This();
worker->SaveToPersistent("database", _this);
Nan::AsyncQueueWorker(worker);
}
NAN_METHOD(Database::Get) {
LD_METHOD_SETUP_COMMON(get, 1, 2)
v8::Local<v8::Object> keyHandle = info[0].As<v8::Object>();
LD_STRING_OR_BUFFER_TO_SLICE(key, keyHandle, key);
bool asBuffer = BooleanOptionValue(optionsObj, "asBuffer", true);
bool fillCache = BooleanOptionValue(optionsObj, "fillCache", true);
ReadWorker* worker = new ReadWorker(
database
, new Nan::Callback(callback)
, key
, asBuffer
, fillCache
, keyHandle
);
// persist to prevent accidental GC
v8::Local<v8::Object> _this = info.This();
worker->SaveToPersistent("database", _this);
Nan::AsyncQueueWorker(worker);
}
NAN_METHOD(Database::Delete) {
LD_METHOD_SETUP_COMMON(del, 1, 2)
v8::Local<v8::Object> keyHandle = info[0].As<v8::Object>();
LD_STRING_OR_BUFFER_TO_SLICE(key, keyHandle, key);
bool sync = BooleanOptionValue(optionsObj, "sync");
DeleteWorker* worker = new DeleteWorker(
database
, new Nan::Callback(callback)
, key
, sync
, keyHandle
);
// persist to prevent accidental GC
v8::Local<v8::Object> _this = info.This();
worker->SaveToPersistent("database", _this);
Nan::AsyncQueueWorker(worker);
}
NAN_METHOD(Database::Batch) {
if ((info.Length() == 0 || info.Length() == 1) && !info[0]->IsArray()) {
v8::Local<v8::Object> optionsObj;
if (info.Length() > 0 && info[0]->IsObject()) {
optionsObj = info[0].As<v8::Object>();
}
info.GetReturnValue().Set(Batch::NewInstance(info.This(), optionsObj));
return;
}
LD_METHOD_SETUP_COMMON(batch, 1, 2);
bool sync = BooleanOptionValue(optionsObj, "sync");
v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(info[0]);
rocksdb::WriteBatch* batch = new rocksdb::WriteBatch();
bool hasData = false;
for (unsigned int i = 0; i < array->Length(); i++) {
if (!array->Get(i)->IsObject())
continue;
v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(array->Get(i));
v8::Local<v8::Value> keyBuffer = obj->Get(Nan::New("key").ToLocalChecked());
v8::Local<v8::Value> type = obj->Get(Nan::New("type").ToLocalChecked());
if (type->StrictEquals(Nan::New("del").ToLocalChecked())) {
LD_STRING_OR_BUFFER_TO_SLICE(key, keyBuffer, key)
batch->Delete(key);
if (!hasData)
hasData = true;
DisposeStringOrBufferFromSlice(keyBuffer, key);
} else if (type->StrictEquals(Nan::New("put").ToLocalChecked())) {
v8::Local<v8::Value> valueBuffer = obj->Get(Nan::New("value").ToLocalChecked());
LD_STRING_OR_BUFFER_TO_SLICE(key, keyBuffer, key)
LD_STRING_OR_BUFFER_TO_SLICE(value, valueBuffer, value)
batch->Put(key, value);
if (!hasData)
hasData = true;
DisposeStringOrBufferFromSlice(keyBuffer, key);
DisposeStringOrBufferFromSlice(valueBuffer, value);
}
}
// don't allow an empty batch through
if (hasData) {
BatchWorker* worker = new BatchWorker(
database
, new Nan::Callback(callback)
, batch
, sync
);
// persist to prevent accidental GC
v8::Local<v8::Object> _this = info.This();
worker->SaveToPersistent("database", _this);
Nan::AsyncQueueWorker(worker);
} else {
LD_RUN_CALLBACK("rocksdb:db.batch", callback, 0, NULL);
}
}
NAN_METHOD(Database::ApproximateSize) {
v8::Local<v8::Object> startHandle = info[0].As<v8::Object>();
v8::Local<v8::Object> endHandle = info[1].As<v8::Object>();
LD_METHOD_SETUP_COMMON(approximateSize, -1, 2)
LD_STRING_OR_BUFFER_TO_SLICE(start, startHandle, start)
LD_STRING_OR_BUFFER_TO_SLICE(end, endHandle, end)
ApproximateSizeWorker* worker = new ApproximateSizeWorker(
database
, new Nan::Callback(callback)
, start
, end
, startHandle
, endHandle
);
// persist to prevent accidental GC
v8::Local<v8::Object> _this = info.This();
worker->SaveToPersistent("database", _this);
Nan::AsyncQueueWorker(worker);
}
NAN_METHOD(Database::CompactRange) {
v8::Local<v8::Object> startHandle = info[0].As<v8::Object>();
v8::Local<v8::Object> endHandle = info[1].As<v8::Object>();
LD_METHOD_SETUP_COMMON(compactRange, -1, 2)
LD_STRING_OR_BUFFER_TO_SLICE(start, startHandle, start)
LD_STRING_OR_BUFFER_TO_SLICE(end, endHandle, end)
CompactRangeWorker* worker = new CompactRangeWorker(
database
, new Nan::Callback(callback)
, start
, end
, startHandle
, endHandle
);
// persist to prevent accidental GC
v8::Local<v8::Object> _this = info.This();
worker->SaveToPersistent("database", _this);
Nan::AsyncQueueWorker(worker);
}
NAN_METHOD(Database::GetProperty) {
v8::Local<v8::Value> propertyHandle = info[0].As<v8::Object>();
v8::Local<v8::Function> callback; // for LD_STRING_OR_BUFFER_TO_SLICE
LD_STRING_OR_BUFFER_TO_SLICE(property, propertyHandle, property)
leveldown::Database* database =
Nan::ObjectWrap::Unwrap<leveldown::Database>(info.This());
std::string* value = new std::string();
database->GetPropertyFromDatabase(property, value);
v8::Local<v8::String> returnValue
= Nan::New<v8::String>(value->c_str(), value->length()).ToLocalChecked();
delete value;
delete[] property.data();
info.GetReturnValue().Set(returnValue);
}
NAN_METHOD(Database::Iterator) {
Database* database = Nan::ObjectWrap::Unwrap<Database>(info.This());
v8::Local<v8::Object> optionsObj;
if (info.Length() > 0 && info[0]->IsObject()) {
optionsObj = v8::Local<v8::Object>::Cast(info[0]);
}
// each iterator gets a unique id for this Database, so we can
// easily store & lookup on our `iterators` map
uint32_t id = database->currentIteratorId++;
Nan::TryCatch try_catch;
v8::Local<v8::Object> iteratorHandle = Iterator::NewInstance(
info.This()
, Nan::New<v8::Number>(id)
, optionsObj
);
if (try_catch.HasCaught()) {
// NB: node::FatalException can segfault here if there is no room on stack.
return Nan::ThrowError("Fatal Error in Database::Iterator!");
}
leveldown::Iterator *iterator =
Nan::ObjectWrap::Unwrap<leveldown::Iterator>(iteratorHandle);
database->iterators[id] = iterator;
// register our iterator
/*
v8::Local<v8::Object> obj = Nan::New<v8::Object>();
obj->Set(Nan::New("iterator"), iteratorHandle);
Nan::Persistent<v8::Object> persistent;
persistent.Reset(nan_isolate, obj);
database->iterators.insert(std::pair< uint32_t, Nan::Persistent<v8::Object> & >
(id, persistent));
*/
info.GetReturnValue().Set(iteratorHandle);
}
} // namespace leveldown