Repository navigation
Expand file tree
/
Copy pathnode_database.cpp
More file actions
147 lines (131 loc) · 4.7 KB
/
Copy pathnode_database.cpp
File metadata and controls
147 lines (131 loc) · 4.7 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
#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;
}
namespace {
// Null/undefined means "use the engine default"; the setting is left unset.
bool getOptionalInt64(const Napi::CallbackInfo& info, size_t index, int64_t& out) {
if (index >= info.Length() || info[index].IsNull() || info[index].IsUndefined()) {
return false;
}
out = info[index].As<Napi::Number>().Int64Value();
return true;
}
bool getOptionalBool(const Napi::CallbackInfo& info, size_t index, bool& out) {
if (index >= info.Length() || info[index].IsNull() || info[index].IsUndefined()) {
return false;
}
out = info[index].As<Napi::Boolean>().Value();
return true;
}
} // namespace
NodeDatabase::NodeDatabase(const Napi::CallbackInfo& info) : Napi::ObjectWrap<NodeDatabase>(info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
databasePath = info[0].ToString();
int64_t intValue = 0;
bool boolValue = false;
if (getOptionalInt64(info, 1, intValue)) {
bufferPoolSize = intValue;
}
if (getOptionalBool(info, 2, boolValue)) {
enableCompression = boolValue;
}
if (getOptionalBool(info, 3, boolValue)) {
readOnly = boolValue;
}
if (getOptionalInt64(info, 4, intValue)) {
maxDBSize = intValue;
}
if (getOptionalBool(info, 5, boolValue)) {
autoCheckpoint = boolValue;
}
if (getOptionalInt64(info, 6, intValue)) {
checkpointThreshold = intValue;
}
if (getOptionalBool(info, 7, boolValue)) {
throwOnWalReplayFailure = boolValue;
}
if (getOptionalBool(info, 8, boolValue)) {
enableChecksums = boolValue;
}
if (getOptionalBool(info, 9, boolValue)) {
enableDefaultHashIndex = boolValue;
}
}
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() {
// Start from the engine defaults and only apply explicitly set settings,
// so engine-side default changes propagate automatically.
auto systemConfig = SystemConfig();
if (bufferPoolSize && *bufferPoolSize > 0) {
systemConfig.bufferPoolSize = static_cast<uint64_t>(*bufferPoolSize);
}
if (enableCompression) {
systemConfig.enableCompression = *enableCompression;
}
if (readOnly) {
systemConfig.readOnly = *readOnly;
}
if (maxDBSize && *maxDBSize > 0) {
systemConfig.maxDBSize = static_cast<uint64_t>(*maxDBSize);
}
if (autoCheckpoint) {
systemConfig.autoCheckpoint = *autoCheckpoint;
}
if (throwOnWalReplayFailure) {
systemConfig.throwOnWalReplayFailure = *throwOnWalReplayFailure;
}
if (enableChecksums) {
systemConfig.enableChecksums = *enableChecksums;
}
if (enableDefaultHashIndex) {
systemConfig.enableDefaultHashIndex = *enableDefaultHashIndex;
}
if (checkpointThreshold && *checkpointThreshold >= 0) {
systemConfig.checkpointThreshold = static_cast<uint64_t>(*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());
}