forked from Level/rocksdb
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.h
417 lines (340 loc) · 13 KB
/
util.h
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
#pragma once
#include <assert.h>
#include <napi-macros.h>
#include <node_api.h>
#include <rocksdb/db.h>
#include <rocksdb/slice.h>
#include <rocksdb/status.h>
#include <array>
#include <optional>
#include <string>
#define NAPI_STATUS_RETURN(call) \
{ \
auto _status = (call); \
if (_status != napi_ok) { \
return _status; \
} \
}
#define ROCKS_STATUS_THROWS_NAPI(call) \
{ \
auto _status = (call); \
if (!_status.ok()) { \
napi_throw(env, ToError(env, _status)); \
return NULL; \
} \
}
#define ROCKS_STATUS_RETURN_NAPI(call) \
{ \
auto _status = (call); \
if (!_status.ok()) { \
napi_throw(env, ToError(env, _status)); \
return napi_pending_exception; \
} \
}
#define ROCKS_STATUS_RETURN(call) \
{ \
auto _status = (call); \
if (!_status.ok()) { \
return _status; \
} \
}
template <typename T>
static void Finalize(napi_env env, void* data, void* hint) {
if (hint) {
delete reinterpret_cast<T*>(hint);
}
}
static void FinalizeFree(napi_env env, void* data, void* hint) {
if (hint) {
free(hint);
}
}
static napi_value CreateError(napi_env env, const std::optional<std::string_view>& code, const std::string_view& msg) {
napi_value codeValue = nullptr;
if (code) {
NAPI_STATUS_THROWS(napi_create_string_utf8(env, code->data(), code->size(), &codeValue));
}
napi_value msgValue;
NAPI_STATUS_THROWS(napi_create_string_utf8(env, msg.data(), msg.size(), &msgValue));
napi_value error;
NAPI_STATUS_THROWS(napi_create_error(env, codeValue, msgValue, &error));
return error;
}
static napi_value ToError(napi_env env, const rocksdb::Status& status) {
if (status.ok()) {
return 0;
}
const auto msg = status.ToString();
if (status.IsNotFound()) {
return CreateError(env, "LEVEL_NOT_FOUND", msg);
} else if (status.IsCorruption()) {
return CreateError(env, "LEVEL_CORRUPTION", msg);
} else if (status.IsTryAgain()) {
return CreateError(env, "LEVEL_TRYAGAIN", msg);
} else if (status.IsIOError()) {
if (msg.find("IO error: lock ") != std::string::npos) { // env_posix.cc
return CreateError(env, "LEVEL_LOCKED", msg);
} else if (msg.find("IO error: LockFile ") != std::string::npos) { // env_win.cc
return CreateError(env, "LEVEL_LOCKED", msg);
} else if (msg.find("IO error: While lock file") != std::string::npos) { // env_mac.cc
return CreateError(env, "LEVEL_LOCKED", msg);
} else {
return CreateError(env, "LEVEL_IO_ERROR", msg);
}
}
return CreateError(env, {}, msg);
}
static napi_status GetString(napi_env env, napi_value from, rocksdb::Slice& to) {
bool isBuffer;
NAPI_STATUS_RETURN(napi_is_buffer(env, from, &isBuffer));
if (isBuffer) {
char* buf = nullptr;
size_t length = 0;
NAPI_STATUS_RETURN(napi_get_buffer_info(env, from, reinterpret_cast<void**>(&buf), &length));
to = {buf, length};
return napi_ok;
}
napi_valuetype type;
NAPI_STATUS_RETURN(napi_typeof(env, from, &type));
if (type == napi_object) {
// Slice
napi_value value;
NAPI_STATUS_RETURN(napi_get_named_property(env, from, "buffer", &value));
char* buf = nullptr;
size_t length = 0;
NAPI_STATUS_RETURN(napi_get_buffer_info(env, value, reinterpret_cast<void**>(&buf), &length));
int pos = 0;
{
napi_value property;
NAPI_STATUS_RETURN(napi_get_named_property(env, from, "byteOffset", &property));
NAPI_STATUS_RETURN(napi_get_value_int32(env, property, &pos));
}
if (pos < 0 || pos > length) {
return napi_invalid_arg;
}
int len = length;
{
napi_value property;
NAPI_STATUS_RETURN(napi_get_named_property(env, from, "byteLength", &property));
NAPI_STATUS_RETURN(napi_get_value_int32(env, property, &pos));
}
if (len < 0 || len > length) {
return napi_invalid_arg;
}
if (pos + len > length) {
return napi_invalid_arg;
}
to = {buf + pos, static_cast<size_t>(len)};
return napi_ok;
}
return napi_invalid_arg;
}
static napi_status GetString(napi_env env, napi_value from, std::string& to) {
napi_valuetype type;
NAPI_STATUS_RETURN(napi_typeof(env, from, &type));
if (type == napi_string) {
size_t length = 0;
NAPI_STATUS_RETURN(napi_get_value_string_utf8(env, from, nullptr, 0, &length));
// TODO (perf): Avoid zero initialization...
to.resize(length, '\0');
NAPI_STATUS_RETURN(napi_get_value_string_utf8(env, from, &to[0], length + 1, &length));
to[length] = 0;
} else {
rocksdb::Slice slice;
NAPI_STATUS_RETURN(GetString(env, from, slice));
to = slice.ToString();
}
return napi_ok;
}
static napi_status GetString(napi_env env, napi_value from, rocksdb::PinnableSlice& to) {
napi_valuetype type;
NAPI_STATUS_RETURN(napi_typeof(env, from, &type));
if (type == napi_string) {
size_t length = 0;
NAPI_STATUS_RETURN(napi_get_value_string_utf8(env, from, nullptr, 0, &length));
to.GetSelf()->resize(length, '\0');
NAPI_STATUS_RETURN(napi_get_value_string_utf8(env, from, to.GetSelf()->data(), length + 1, &length));
to.PinSelf();
} else {
rocksdb::Slice slice;
NAPI_STATUS_RETURN(GetString(env, from, slice));
to.PinSelf(std::move(slice));
}
return napi_ok;
}
enum class Encoding { Invalid, Buffer, String };
static napi_status GetValue(napi_env env, napi_value value, bool& result) {
return napi_get_value_bool(env, value, &result);
}
static napi_status GetValue(napi_env env, napi_value value, uint32_t& result) {
return napi_get_value_uint32(env, value, &result);
}
static napi_status GetValue(napi_env env, napi_value value, int32_t& result) {
return napi_get_value_int32(env, value, &result);
}
static napi_status GetValue(napi_env env, napi_value value, int64_t& result) {
return napi_get_value_int64(env, value, &result);
}
static napi_status GetValue(napi_env env, napi_value value, uint64_t& result) {
int64_t result2;
NAPI_STATUS_RETURN(napi_get_value_int64(env, value, &result2));
result = static_cast<uint64_t>(result2);
return napi_ok;
}
static napi_status GetValue(napi_env env, napi_value value, std::string& result) {
return GetString(env, value, result);
}
static napi_status GetValue(napi_env env, napi_value value, rocksdb::PinnableSlice& result) {
return GetString(env, value, result);
}
static napi_status GetValue(napi_env env, napi_value value, rocksdb::Slice& result) {
return GetString(env, value, result);
}
static napi_status GetValue(napi_env env, napi_value value, rocksdb::ColumnFamilyHandle*& result) {
return napi_get_value_external(env, value, reinterpret_cast<void**>(&result));
}
static napi_status GetValue(napi_env env, napi_value value, Encoding& result) {
size_t size;
NAPI_STATUS_RETURN(napi_get_value_string_utf8(env, value, nullptr, 0, &size));
if (size == 6) {
result = Encoding::Buffer;
} else {
result = Encoding::String;
}
return napi_ok;
}
template <typename T>
static napi_status GetValue(napi_env env, napi_value value, std::optional<T>& result) {
result = T{};
return GetValue(env, value, *result);
}
template <typename T>
static napi_status GetProperty(napi_env env,
napi_value obj,
const std::string_view& key,
T& result,
bool required = false) {
napi_valuetype objType;
NAPI_STATUS_RETURN(napi_typeof(env, obj, &objType));
if (objType == napi_undefined || objType == napi_null) {
return required ? napi_invalid_arg : napi_ok;
}
if (objType != napi_object) {
return napi_invalid_arg;
}
bool has = false;
NAPI_STATUS_RETURN(napi_has_named_property(env, obj, key.data(), &has));
if (!has) {
return required ? napi_invalid_arg : napi_ok;
}
napi_value value;
NAPI_STATUS_RETURN(napi_get_named_property(env, obj, key.data(), &value));
napi_valuetype valueType;
NAPI_STATUS_RETURN(napi_typeof(env, value, &valueType));
if (valueType == napi_null || valueType == napi_undefined) {
return required ? napi_invalid_arg : napi_ok;
}
return GetValue(env, value, result);
}
template <typename T>
napi_status Convert(napi_env env, T&& s, Encoding encoding, napi_value& result) {
if (!s) {
return napi_get_null(env, &result);
} else if (encoding == Encoding::Buffer) {
return napi_create_buffer_copy(env, s->size(), s->data(), nullptr, &result);
} else if (encoding == Encoding::String) {
return napi_create_string_utf8(env, s->data(), s->size(), &result);
} else {
return napi_invalid_arg;
}
}
napi_status ConvertUnsafe(napi_env env, rocksdb::PinnableSlice&& s, Encoding encoding, napi_value& result) {
if (encoding == Encoding::Buffer) {
auto s2 = new rocksdb::PinnableSlice(std::move(s));
return napi_create_external_buffer(env, s2->size(), const_cast<char*>(s2->data()), Finalize<rocksdb::PinnableSlice>,
s2, &result);
} else if (encoding == Encoding::String) {
return napi_create_string_utf8(env, s.data(), s.size(), &result);
} else {
return napi_invalid_arg;
}
}
napi_status Convert(napi_env env, rocksdb::PinnableSlice&& s, Encoding encoding, napi_value& result) {
if (encoding == Encoding::Buffer) {
return napi_create_buffer_copy(env, s.size(), s.data(), nullptr, &result);
} else if (encoding == Encoding::String) {
return napi_create_string_utf8(env, s.data(), s.size(), &result);
} else {
return napi_invalid_arg;
}
}
template <typename State, typename T1, typename T2>
napi_status runAsync(State&& state,
const std::string& name,
napi_env env,
napi_value callback,
T1&& execute,
T2&& then) {
struct Worker final {
static void Execute(napi_env env, void* data) {
auto worker = reinterpret_cast<Worker*>(data);
worker->status = worker->execute(worker->state);
}
static void Complete(napi_env env, napi_status status, void* data) {
auto worker = std::unique_ptr<Worker>(reinterpret_cast<Worker*>(data));
napi_value callback;
NAPI_STATUS_THROWS_VOID(napi_get_reference_value(env, worker->callbackRef, &callback));
napi_value global;
NAPI_STATUS_THROWS_VOID(napi_get_global(env, &global));
if (worker->status.ok()) {
std::vector<napi_value> argv;
argv.resize(1);
NAPI_STATUS_THROWS_VOID(napi_get_null(env, &argv[0]));
const auto ret = worker->then(worker->state, env, argv);
if (ret == napi_ok) {
NAPI_STATUS_THROWS_VOID(napi_call_function(env, global, callback, argv.size(), argv.data(), nullptr));
} else {
const napi_extended_error_info* errInfo = nullptr;
NAPI_STATUS_THROWS_VOID(napi_get_last_error_info(env, &errInfo));
auto err = CreateError(env, std::nullopt,
!errInfo || !errInfo->error_message ? "empty error message" : errInfo->error_message);
NAPI_STATUS_THROWS_VOID(napi_call_function(env, global, callback, 1, &err, nullptr));
}
} else {
auto err = ToError(env, worker->status);
NAPI_STATUS_THROWS_VOID(napi_call_function(env, global, callback, 1, &err, nullptr));
}
}
~Worker() {
if (callbackRef) {
napi_delete_reference(env, callbackRef);
callbackRef = nullptr;
}
if (asyncWork) {
napi_delete_async_work(env, asyncWork);
asyncWork = nullptr;
}
}
napi_env env = nullptr;
typename std::decay<T1>::type execute;
typename std::decay<T2>::type then;
State state;
napi_ref callbackRef = nullptr;
napi_async_work asyncWork = nullptr;
rocksdb::Status status = rocksdb::Status::OK();
};
auto worker =
std::unique_ptr<Worker>(new Worker{env, std::forward<T1>(execute), std::forward<T2>(then), std::move(state)});
NAPI_STATUS_RETURN(napi_create_reference(env, callback, 1, &worker->callbackRef));
napi_value asyncResourceName;
NAPI_STATUS_RETURN(napi_create_string_utf8(env, name.data(), name.size(), &asyncResourceName));
NAPI_STATUS_RETURN(napi_create_async_work(env, callback, asyncResourceName, Worker::Execute, Worker::Complete,
worker.get(), &worker->asyncWork));
NAPI_STATUS_RETURN(napi_queue_async_work(env, worker->asyncWork));
worker.release();
return napi_ok;
}
template <typename State, typename T1, typename T2>
napi_status runAsync(const std::string& name, napi_env env, napi_value callback, T1&& execute, T2&& then) {
return runAsync<State>(State{}, name, env, callback, std::forward<T1>(execute), std::forward<T2>(then));
}