-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathcursor.cpp
More file actions
387 lines (320 loc) · 15.3 KB
/
cursor.cpp
File metadata and controls
387 lines (320 loc) · 15.3 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
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
// This file is part of node-lmdb, the Node.js binding for lmdb
// Copyright (c) 2013-2017 Timur Kristóf
// 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.
#include "node-lmdb.h"
#include <string.h>
using namespace v8;
using namespace node;
CursorWrap::CursorWrap(MDB_cursor *cursor) {
this->cursor = cursor;
this->keyType = NodeLmdbKeyType::StringKey;
this->freeKey = nullptr;
}
CursorWrap::~CursorWrap() {
if (this->cursor) {
this->dw->Unref();
this->tw->Unref();
mdb_cursor_close(this->cursor);
}
if (this->freeKey) {
this->freeKey(this->key);
}
}
NAN_METHOD(CursorWrap::ctor) {
Nan::HandleScope scope;
if (info.Length() < 2) {
return Nan::ThrowError("Wrong number of arguments");
}
// Extra pessimism...
Nan::MaybeLocal<v8::Object> arg0 = Nan::To<v8::Object>(info[0]);
Nan::MaybeLocal<v8::Object> arg1 = Nan::To<v8::Object>(info[1]);
if (arg0.IsEmpty() || arg1.IsEmpty()) {
return Nan::ThrowError("Invalid arguments to the Cursor constructor. First must be a Txn, second must be a Dbi.");
}
// Unwrap Txn and Dbi
TxnWrap *tw = Nan::ObjectWrap::Unwrap<TxnWrap>(arg0.ToLocalChecked());
DbiWrap *dw = Nan::ObjectWrap::Unwrap<DbiWrap>(arg1.ToLocalChecked());
// Get key type
auto keyType = keyTypeFromOptions(info[2], dw->keyType);
if (dw->keyType == NodeLmdbKeyType::Uint32Key && keyType != NodeLmdbKeyType::Uint32Key) {
return Nan::ThrowError("You specified keyIsUint32 on the Dbi, so you can't use other key types with it.");
}
// Open the cursor
MDB_cursor *cursor;
int rc = mdb_cursor_open(tw->txn, dw->dbi, &cursor);
if (rc != 0) {
return throwLmdbError(rc);
}
// Create wrapper
CursorWrap* cw = new CursorWrap(cursor);
cw->dw = dw;
cw->dw->Ref();
cw->tw = tw;
cw->tw->Ref();
cw->keyType = keyType;
cw->Wrap(info.This());
return info.GetReturnValue().Set(info.This());
}
NAN_METHOD(CursorWrap::close) {
Nan::HandleScope scope;
CursorWrap *cw = Nan::ObjectWrap::Unwrap<CursorWrap>(info.This());
mdb_cursor_close(cw->cursor);
cw->dw->Unref();
cw->tw->Unref();
cw->cursor = nullptr;
}
NAN_METHOD(CursorWrap::del) {
Nan::HandleScope scope;
if (info.Length() != 0 && info.Length() != 1) {
return Nan::ThrowError("cursor.del: Incorrect number of arguments provided, arguments: options (optional).");
}
int flags = 0;
if (info.Length() == 1) {
if (!info[0]->IsObject()) {
return Nan::ThrowError("cursor.del: Invalid options argument. It should be an object.");
}
auto options = Nan::To<v8::Object>(info[0]).ToLocalChecked();
setFlagFromValue(&flags, MDB_NODUPDATA, "noDupData", false, options);
}
CursorWrap *cw = Nan::ObjectWrap::Unwrap<CursorWrap>(info.This());
int rc = mdb_cursor_del(cw->cursor, flags);
if (rc != 0) {
return throwLmdbError(rc);
}
}
Nan::NAN_METHOD_RETURN_TYPE CursorWrap::getCommon(
Nan::NAN_METHOD_ARGS_TYPE info,
MDB_cursor_op op,
argtokey_callback_t (*setKey)(CursorWrap* cw, Nan::NAN_METHOD_ARGS_TYPE info, MDB_val&, bool&),
void (*setData)(CursorWrap* cw, Nan::NAN_METHOD_ARGS_TYPE info, MDB_val&),
void (*freeData)(CursorWrap* cw, Nan::NAN_METHOD_ARGS_TYPE info, MDB_val&),
Local<Value> (*convertFunc)(MDB_val &data)
) {
Nan::HandleScope scope;
int al = info.Length();
CursorWrap *cw = Nan::ObjectWrap::Unwrap<CursorWrap>(info.This());
// When a new key is manually set
if (setKey) {
// Free old key if necessary
if (cw->freeKey) {
cw->freeKey(cw->key);
cw->freeKey = nullptr;
}
// Set new key and assign the deleter function
bool keyIsValid;
cw->freeKey = setKey(cw, info, cw->key, keyIsValid);
if (!keyIsValid) {
// setKey already threw an error, no need to throw here
return;
}
}
// When data is manually set
if (setData) {
setData(cw, info, cw->data);
}
// Temporary thing, so that we can free up the data if we want to
MDB_val tempdata;
tempdata.mv_size = cw->data.mv_size;
tempdata.mv_data = cw->data.mv_data;
// Temporary bookkeeping for the current key
MDB_val tempKey;
tempKey.mv_size = cw->key.mv_size;
tempKey.mv_data = cw->key.mv_data;
// Call LMDB
int rc = mdb_cursor_get(cw->cursor, &(cw->key), &(cw->data), op);
// Check if key points inside LMDB
if (tempKey.mv_data != cw->key.mv_data) {
// cw->key points inside the database now,
// so we should free the old key now.
if (cw->freeKey) {
cw->freeKey(tempKey);
cw->freeKey = nullptr;
}
}
if (rc == MDB_NOTFOUND) {
return info.GetReturnValue().Set(Nan::Null());
}
else if (rc != 0) {
return throwLmdbError(rc);
}
Local<Value> keyHandle = Nan::Undefined();
if (cw->key.mv_size) {
keyHandle = keyToHandle(cw->key, cw->keyType);
}
Local<Value> dataHandle = Nan::Undefined();
if (convertFunc) {
dataHandle = convertFunc(cw->data);
if (al > 0) {
const auto &callbackFunc = info[al - 1];
if (callbackFunc->IsFunction()) {
// In this case, we expect the key/data pair to be correctly filled
constexpr const unsigned argc = 2;
Local<Value> argv[argc] = { keyHandle, dataHandle };
Nan::Call(Nan::Callback(Local<Function>::Cast(callbackFunc)), argc, argv);
}
}
}
if (freeData) {
freeData(cw, info, tempdata);
}
if (convertFunc) {
return info.GetReturnValue().Set(dataHandle);
}
else if (cw->key.mv_size) {
return info.GetReturnValue().Set(keyHandle);
}
return info.GetReturnValue().Set(Nan::True());
}
Nan::NAN_METHOD_RETURN_TYPE CursorWrap::getCommon(Nan::NAN_METHOD_ARGS_TYPE info, MDB_cursor_op op) {
return getCommon(info, op, nullptr, nullptr, nullptr, nullptr);
}
NAN_METHOD(CursorWrap::getCurrentString) {
return getCommon(info, MDB_GET_CURRENT, nullptr, nullptr, nullptr, valToString);
}
NAN_METHOD(CursorWrap::getCurrentStringUnsafe) {
return getCommon(info, MDB_GET_CURRENT, nullptr, nullptr, nullptr, valToStringUnsafe);
}
NAN_METHOD(CursorWrap::getCurrentBinary) {
return getCommon(info, MDB_GET_CURRENT, nullptr, nullptr, nullptr, valToBinary);
}
NAN_METHOD(CursorWrap::getCurrentBinaryUnsafe) {
return getCommon(info, MDB_GET_CURRENT, nullptr, nullptr, nullptr, valToBinaryUnsafe);
}
NAN_METHOD(CursorWrap::getCurrentNumber) {
return getCommon(info, MDB_GET_CURRENT, nullptr, nullptr, nullptr, valToNumber);
}
NAN_METHOD(CursorWrap::getCurrentBoolean) {
return getCommon(info, MDB_GET_CURRENT, nullptr, nullptr, nullptr, valToBoolean);
}
#define MAKE_GET_FUNC(name, op) NAN_METHOD(CursorWrap::name) { return getCommon(info, op); }
MAKE_GET_FUNC(goToFirst, MDB_FIRST);
MAKE_GET_FUNC(goToLast, MDB_LAST);
MAKE_GET_FUNC(goToNext, MDB_NEXT);
MAKE_GET_FUNC(goToPrev, MDB_PREV);
MAKE_GET_FUNC(goToFirstDup, MDB_FIRST_DUP);
MAKE_GET_FUNC(goToLastDup, MDB_LAST_DUP);
MAKE_GET_FUNC(goToNextDup, MDB_NEXT_DUP);
MAKE_GET_FUNC(goToPrevDup, MDB_PREV_DUP);
static void fillDataFromArg1(CursorWrap* cw, Nan::NAN_METHOD_ARGS_TYPE info, MDB_val &data) {
if (info[1]->IsString()) {
CustomExternalStringResource::writeTo(Local<String>::Cast(info[1]), &data);
}
else if (node::Buffer::HasInstance(info[1])) {
data.mv_size = node::Buffer::Length(info[1]);
data.mv_data = node::Buffer::Data(info[1]);
}
else if (info[1]->IsNumber()) {
data.mv_size = sizeof(double);
data.mv_data = new double;
auto local = Nan::To<v8::Number>(info[1]).ToLocalChecked();
*((double*)data.mv_data) = local->Value();
}
else if (info[1]->IsBoolean()) {
data.mv_size = sizeof(double);
data.mv_data = new bool;
auto local = Nan::To<v8::Boolean>(info[1]).ToLocalChecked();
*((bool*)data.mv_data) = local->Value();
}
else {
Nan::ThrowError("Invalid data type.");
}
}
static void freeDataFromArg1(CursorWrap* cw, Nan::NAN_METHOD_ARGS_TYPE info, MDB_val &data) {
if (info[1]->IsString()) {
delete[] (uint16_t*)data.mv_data;
}
else if (node::Buffer::HasInstance(info[1])) {
// I think the data is owned by the node::Buffer so we don't need to free it - need to clarify
}
else if (info[1]->IsNumber()) {
delete (double*)data.mv_data;
}
else if (info[1]->IsBoolean()) {
delete (bool*)data.mv_data;
}
else {
Nan::ThrowError("Invalid data type.");
}
}
template<size_t keyIndex, size_t optionsIndex>
inline argtokey_callback_t cursorArgToKey(CursorWrap* cw, Nan::NAN_METHOD_ARGS_TYPE info, MDB_val &key, bool &keyIsValid) {
auto keyType = inferAndValidateKeyType(info[keyIndex], info[optionsIndex], cw->keyType, keyIsValid);
if (keyIsValid) {
return argToKey(info[keyIndex], key, keyType, keyIsValid);
}
return nullptr;
}
NAN_METHOD(CursorWrap::goToKey) {
if (info.Length() != 1 && info.Length() != 2) {
return Nan::ThrowError("You called cursor.goToKey with an incorrect number of arguments. Arguments are: key (mandatory), options (optional).");
}
return getCommon(info, MDB_SET_KEY, cursorArgToKey<0, 1>, nullptr, nullptr, nullptr);
}
NAN_METHOD(CursorWrap::goToRange) {
if (info.Length() != 1 && info.Length() != 2) {
return Nan::ThrowError("You called cursor.goToRange with an incorrect number of arguments. Arguments are: key (mandatory), options (optional).");
}
return getCommon(info, MDB_SET_RANGE, cursorArgToKey<0, 1>, nullptr, nullptr, nullptr);
}
NAN_METHOD(CursorWrap::goToDup) {
if (info.Length() != 2 && info.Length() != 3) {
return Nan::ThrowError("You called cursor.goToDup with an incorrect number of arguments. Arguments are: key (mandatory), data (mandatory), options (optional).");
}
return getCommon(info, MDB_GET_BOTH, cursorArgToKey<0, 2>, fillDataFromArg1, freeDataFromArg1, nullptr);
}
NAN_METHOD(CursorWrap::goToDupRange) {
if (info.Length() != 2 && info.Length() != 3) {
return Nan::ThrowError("You called cursor.goToDupRange with an incorrect number of arguments. Arguments are: key (mandatory), data (mandatory), options (optional).");
}
return getCommon(info, MDB_GET_BOTH_RANGE, cursorArgToKey<0, 2>, fillDataFromArg1, freeDataFromArg1, nullptr);
}
void CursorWrap::setupExports(Local<Object> exports) {
// CursorWrap: Prepare constructor template
Local<FunctionTemplate> cursorTpl = Nan::New<FunctionTemplate>(CursorWrap::ctor);
cursorTpl->SetClassName(Nan::New<String>("Cursor").ToLocalChecked());
cursorTpl->InstanceTemplate()->SetInternalFieldCount(1);
// CursorWrap: Add functions to the prototype
cursorTpl->PrototypeTemplate()->Set(Nan::New<String>("close").ToLocalChecked(), Nan::New<FunctionTemplate>(CursorWrap::close));
cursorTpl->PrototypeTemplate()->Set(Nan::New<String>("getCurrentString").ToLocalChecked(), Nan::New<FunctionTemplate>(CursorWrap::getCurrentString));
cursorTpl->PrototypeTemplate()->Set(Nan::New<String>("getCurrentStringUnsafe").ToLocalChecked(), Nan::New<FunctionTemplate>(CursorWrap::getCurrentStringUnsafe));
cursorTpl->PrototypeTemplate()->Set(Nan::New<String>("getCurrentBinary").ToLocalChecked(), Nan::New<FunctionTemplate>(CursorWrap::getCurrentBinary));
cursorTpl->PrototypeTemplate()->Set(Nan::New<String>("getCurrentBinaryUnsafe").ToLocalChecked(), Nan::New<FunctionTemplate>(CursorWrap::getCurrentBinaryUnsafe));
cursorTpl->PrototypeTemplate()->Set(Nan::New<String>("getCurrentNumber").ToLocalChecked(), Nan::New<FunctionTemplate>(CursorWrap::getCurrentNumber));
cursorTpl->PrototypeTemplate()->Set(Nan::New<String>("getCurrentBoolean").ToLocalChecked(), Nan::New<FunctionTemplate>(CursorWrap::getCurrentBoolean));
cursorTpl->PrototypeTemplate()->Set(Nan::New<String>("goToFirst").ToLocalChecked(), Nan::New<FunctionTemplate>(CursorWrap::goToFirst));
cursorTpl->PrototypeTemplate()->Set(Nan::New<String>("goToLast").ToLocalChecked(), Nan::New<FunctionTemplate>(CursorWrap::goToLast));
cursorTpl->PrototypeTemplate()->Set(Nan::New<String>("goToNext").ToLocalChecked(), Nan::New<FunctionTemplate>(CursorWrap::goToNext));
cursorTpl->PrototypeTemplate()->Set(Nan::New<String>("goToPrev").ToLocalChecked(), Nan::New<FunctionTemplate>(CursorWrap::goToPrev));
cursorTpl->PrototypeTemplate()->Set(Nan::New<String>("goToKey").ToLocalChecked(), Nan::New<FunctionTemplate>(CursorWrap::goToKey));
cursorTpl->PrototypeTemplate()->Set(Nan::New<String>("goToRange").ToLocalChecked(), Nan::New<FunctionTemplate>(CursorWrap::goToRange));
cursorTpl->PrototypeTemplate()->Set(Nan::New<String>("goToFirstDup").ToLocalChecked(), Nan::New<FunctionTemplate>(CursorWrap::goToFirstDup));
cursorTpl->PrototypeTemplate()->Set(Nan::New<String>("goToLastDup").ToLocalChecked(), Nan::New<FunctionTemplate>(CursorWrap::goToLastDup));
cursorTpl->PrototypeTemplate()->Set(Nan::New<String>("goToNextDup").ToLocalChecked(), Nan::New<FunctionTemplate>(CursorWrap::goToNextDup));
cursorTpl->PrototypeTemplate()->Set(Nan::New<String>("goToPrevDup").ToLocalChecked(), Nan::New<FunctionTemplate>(CursorWrap::goToPrevDup));
cursorTpl->PrototypeTemplate()->Set(Nan::New<String>("goToDup").ToLocalChecked(), Nan::New<FunctionTemplate>(CursorWrap::goToDup));
cursorTpl->PrototypeTemplate()->Set(Nan::New<String>("goToDupRange").ToLocalChecked(), Nan::New<FunctionTemplate>(CursorWrap::goToDupRange));
cursorTpl->PrototypeTemplate()->Set(Nan::New<String>("del").ToLocalChecked(), Nan::New<FunctionTemplate>(CursorWrap::del));
// Set exports
Maybe<bool> exportResult = exports->Set(Nan::GetCurrentContext(), Nan::New<String>("Cursor").ToLocalChecked(), cursorTpl->GetFunction(Nan::GetCurrentContext()).ToLocalChecked());
// Assert that the export succeeded.
// Note the comment here about the return value of V8Object::Set:
// > Set only return[s] Just(true) or Empty(), so if it should never fail, use result.Check().
// https://github.com/nodejs/node/blob/cafd44dc7eff20cfa6c36b289e24efd793d4422a/deps/v8/include/v8-object.h#L236-L244
exportResult.Check();
}