-
Notifications
You must be signed in to change notification settings - Fork 39
/
misc.cpp
323 lines (280 loc) · 9.22 KB
/
misc.cpp
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
#include "lmdb-js.h"
#include <string.h>
#include <stdio.h>
#include <node_version.h>
using namespace Napi;
static thread_local char* globalUnsafePtr;
static thread_local size_t globalUnsafeSize;
void setupExportMisc(Napi::Env env, Object exports) {
Object versionObj = Object::New(env);
int major, minor, patch;
char *str = mdb_version(&major, &minor, &patch);
versionObj.Set("versionString", String::New(env, str));
versionObj.Set("major", Number::New(env, major));
versionObj.Set("minor", Number::New(env, minor));
versionObj.Set("patch", Number::New(env, patch));
#if ENABLE_V8_API
versionObj.Set("nodeCompiledVersion", Number::New(env, NODE_MAJOR_VERSION));
#endif
exports.Set("version", versionObj);
exports.Set("setGlobalBuffer", Function::New(env, setGlobalBuffer));
exports.Set("lmdbError", Function::New(env, lmdbError));
exports.Set("enableDirectV8", Function::New(env, enableDirectV8));
EXPORT_NAPI_FUNCTION("createBufferForAddress", createBufferForAddress);
EXPORT_NAPI_FUNCTION("getAddress", getViewAddress);
EXPORT_NAPI_FUNCTION("detachBuffer", detachBuffer);
}
void setFlagFromValue(int *flags, int flag, const char *name, bool defaultValue, Object options) {
Value opt = options.Get(name);
if (opt.IsBoolean() ? opt.As<Boolean>().Value() : defaultValue)
*flags |= flag;
}
/*
Value valToStringUnsafe(MDB_val &data) {
auto resource = new CustomExternalOneByteStringResource(&data);
auto str = Nan::New<v8::String>(resource);
return str.ToLocalChecked();
}*/
Value valToUtf8(Env env, MDB_val &data) {
return String::New(env, (const char*) data.mv_data, data.mv_size);
}
Value valToString(Env env, MDB_val &data) {
// UTF-16 buffer
const uint16_t *buffer = reinterpret_cast<const uint16_t*>(data.mv_data);
// Number of UTF-16 code points
size_t n = data.mv_size / sizeof(uint16_t);
// Check zero termination
if (n < 1 || buffer[n - 1] != 0) {
return throwError(env, "Invalid zero-terminated UTF-16 string");
}
size_t length = n - 1;
return String::New(env, (const char16_t*)data.mv_data, length);
}
bool valToBinaryFast(MDB_val &data, DbiWrap* dw) {
Compression* compression = dw->compression;
if (compression) {
if (data.mv_data == compression->decompressTarget) {
// already decompressed to the target, nothing more to do
} else {
if (data.mv_size > compression->decompressSize) {
return false;
}
// copy into the buffer target
memcpy(compression->decompressTarget, data.mv_data, data.mv_size);
}
} else {
if (data.mv_size > globalUnsafeSize) {
// TODO: Provide a direct reference if for really large blocks, but we do that we need to detach that in the next turn
/* if(data.mv_size > 64000) {
dw->SetUnsafeBuffer(data.mv_data, data.mv_size);
return Nan::New<Number>(data.mv_size);
}*/
return false;
}
memcpy(globalUnsafePtr, data.mv_data, data.mv_size);
}
return true;
}
Value valToBinaryUnsafe(MDB_val &data, DbiWrap* dw, Env env) {
valToBinaryFast(data, dw);
return Number::New(env, data.mv_size);
}
bool getVersionAndUncompress(MDB_val &data, DbiWrap* dw) {
//fprintf(stdout, "uncompressing %u\n", compressionThreshold);
unsigned char* charData = (unsigned char*) data.mv_data;
if (dw->hasVersions) {
*((double*) (dw->ew->keyBuffer + 16)) = *((double*) charData);
// fprintf(stderr, "getVersion %u\n", lastVersion);
charData = charData + 8;
data.mv_data = charData;
data.mv_size -= 8;
}
if (data.mv_size == 0) {
return true;// successFunc(data);
}
unsigned char statusByte = dw->compression ? charData[0] : 0;
//fprintf(stdout, "uncompressing status %X\n", statusByte);
if (statusByte >= 250) {
bool isValid;
dw->compression->decompress(data, isValid, !dw->getFast);
if (!isValid)
return false;
//return Nan::Null();
}
return true;
}
Value lmdbError(const CallbackInfo& info) {
return throwLmdbError(info.Env(), info[0].As<Number>().Int32Value());
}
Value setGlobalBuffer(const CallbackInfo& info) {
napi_get_typedarray_info(info.Env(), info[0], nullptr, &globalUnsafeSize, (void**) &globalUnsafePtr, nullptr, nullptr);
return info.Env().Undefined();
}
/*Value getBufferForAddress) {
char* address = (char*) (size_t) Nan::To<v8::Number>(info[0]).ToLocalChecked()->Value();
std::unique_ptr<v8::BackingStore> backing = v8::ArrayBuffer::NewBackingStore(
address, 0x100000000, [](void*, size_t, void*){}, nullptr);
auto array_buffer = v8::ArrayBuffer::New(Isolate::GetCurrent(), std::move(backing));
info.GetReturnValue().Set(array_buffer);
}*/
NAPI_FUNCTION(createBufferForAddress) {
ARGS(2)
void* data;
GET_INT64_ARG(data, 0);
size_t length;
GET_INT64_ARG(length, 1);
napi_create_external_buffer(env, length, data, nullptr, nullptr, &returnValue);
return returnValue;
}
NAPI_FUNCTION(getViewAddress) {
ARGS(1)
void* data;
napi_get_typedarray_info(env, args[0], nullptr, nullptr, &data, nullptr, nullptr);
RETURN_INT64((int64_t) data);
}
NAPI_FUNCTION(detachBuffer) {
ARGS(1)
#if (NAPI_VERSION > 6)
napi_detach_arraybuffer(env, args[0]);
#endif
RETURN_UNDEFINED;
}
Value lmdbNativeFunctions(const CallbackInfo& info) {
// no-op, just doing this to give a label to the native functions
return info.Env().Undefined();
}
Napi::Value throwLmdbError(Napi::Env env, int rc) {
if (rc < 0 && !(rc < -30700 && rc > -30800))
rc = -rc;
Error error = Error::New(env, mdb_strerror(rc));
error.Set("code", Number::New(env, rc));
error.ThrowAsJavaScriptException();
return env.Undefined();
}
Napi::Value throwError(Napi::Env env, const char* message) {
Error::New(env, message).ThrowAsJavaScriptException();
return env.Undefined();
}
int putWithVersion(MDB_txn * txn,
MDB_dbi dbi,
MDB_val * key,
MDB_val * data,
unsigned int flags, double version) {
// leave 8 header bytes available for version and copy in with reserved memory
char* sourceData = (char*) data->mv_data;
int size = data->mv_size;
data->mv_size = size + 8;
int rc = mdb_put(txn, dbi, key, data, flags | MDB_RESERVE);
if (rc == 0) {
// if put is successful, data->mv_data will point into the database where we copy the data to
memcpy((char*) data->mv_data + 8, sourceData, size);
*((double*) data->mv_data) = version;
}
data->mv_data = sourceData; // restore this so that if it points to data that needs to be freed, it points to the right place
return rc;
}
#ifdef _WIN32
int pthread_mutex_init(pthread_mutex_t *mutex, pthread_mutexattr_t *attr)
{
(void)attr;
if (mutex == NULL)
return 1;
InitializeCriticalSection(mutex);
return 0;
}
int pthread_mutex_destroy(pthread_mutex_t *mutex)
{
if (mutex == NULL)
return 1;
DeleteCriticalSection(mutex);
return 0;
}
int pthread_mutex_lock(pthread_mutex_t *mutex)
{
if (mutex == NULL)
return 1;
EnterCriticalSection(mutex);
return 0;
}
int pthread_mutex_unlock(pthread_mutex_t *mutex)
{
if (mutex == NULL)
return 1;
LeaveCriticalSection(mutex);
return 0;
}
int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *attr)
{
(void)attr;
if (cond == NULL)
return 1;
InitializeConditionVariable(cond);
return 0;
}
int pthread_cond_destroy(pthread_cond_t *cond)
{
/* Windows does not have a destroy for conditionals */
(void)cond;
return 0;
}
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
{
if (cond == NULL || mutex == NULL)
return 1;
if (!SleepConditionVariableCS(cond, mutex, INFINITE))
return 1;
return 0;
}
int cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, uint64_t ms)
{
if (cond == NULL || mutex == NULL)
return 1;
if (!SleepConditionVariableCS(cond, mutex, ms))
return 1;
return 0;
}
int pthread_cond_signal(pthread_cond_t *cond)
{
if (cond == NULL)
return 1;
WakeConditionVariable(cond);
return 0;
}
int pthread_cond_broadcast(pthread_cond_t *cond)
{
if (cond == NULL)
return 1;
WakeAllConditionVariable(cond);
return 0;
}
#else
int cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, uint64_t cms)
{
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
uint64_t ns = ts.tv_nsec + cms * 10000;
ts.tv_sec += ns / 1000000000;
ts.tv_nsec += ns % 1000000000;
return pthread_cond_timedwait(cond, mutex, &ts);
}
#endif
// This file contains code from the node-lmdb project
// Copyright (c) 2013-2017 Timur Kristóf
// Copyright (c) 2021 Kristopher Tate
// Licensed to you under the terms of the MIT license
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.