-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnode_database.cpp
More file actions
92 lines (80 loc) · 3.17 KB
/
node_database.cpp
File metadata and controls
92 lines (80 loc) · 3.17 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
#include "include/node_database.h"
Napi::Object NodeDatabase::Init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
Napi::Function t = DefineClass(env, "NodeDatabase",
{
InstanceMethod("initAsync", &NodeDatabase::InitAsync),
InstanceMethod("initSync", &NodeDatabase::InitSync),
InstanceMethod("close", &NodeDatabase::Close),
StaticMethod("getVersion", &NodeDatabase::GetVersion),
StaticMethod("getStorageVersion", &NodeDatabase::GetStorageVersion),
});
exports.Set("NodeDatabase", t);
return exports;
}
NodeDatabase::NodeDatabase(const Napi::CallbackInfo& info) : Napi::ObjectWrap<NodeDatabase>(info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
databasePath = info[0].ToString();
bufferPoolSize = info[1].As<Napi::Number>().Int64Value();
enableCompression = info[2].As<Napi::Boolean>().Value();
readOnly = info[3].As<Napi::Boolean>().Value();
maxDBSize = info[4].As<Napi::Number>().Int64Value();
autoCheckpoint = info[5].As<Napi::Boolean>().Value();
checkpointThreshold = info[6].As<Napi::Number>().Int64Value();
throwOnWalReplayFailure = info[7].As<Napi::Boolean>().Value();
enableChecksums = info[8].As<Napi::Boolean>().Value();
}
Napi::Value NodeDatabase::InitSync(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
try {
InitCppDatabase();
} catch (const std::exception& exc) {
Napi::Error::New(env, exc.what()).ThrowAsJavaScriptException();
}
return env.Undefined();
}
Napi::Value NodeDatabase::InitAsync(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
auto callback = info[0].As<Napi::Function>();
auto* asyncWorker = new DatabaseInitAsyncWorker(callback, this);
asyncWorker->Queue();
return info.Env().Undefined();
}
void NodeDatabase::InitCppDatabase() {
auto systemConfig = SystemConfig();
if (bufferPoolSize > 0) {
systemConfig.bufferPoolSize = bufferPoolSize;
}
if (!enableCompression) {
systemConfig.enableCompression = enableCompression;
}
systemConfig.readOnly = readOnly;
if (maxDBSize > 0) {
systemConfig.maxDBSize = maxDBSize;
}
systemConfig.autoCheckpoint = autoCheckpoint;
systemConfig.throwOnWalReplayFailure = throwOnWalReplayFailure;
systemConfig.enableChecksums = enableChecksums;
if (checkpointThreshold >= 0) {
systemConfig.checkpointThreshold = checkpointThreshold;
}
this->database = std::make_shared<Database>(databasePath, systemConfig);
}
void NodeDatabase::Close(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
database.reset();
}
Napi::Value NodeDatabase::GetVersion(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
return Napi::String::New(env, Version::getVersion());
}
Napi::Value NodeDatabase::GetStorageVersion(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
return Napi::Number::New(env, Version::getStorageVersion());
}