-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRowKeyPredicate.cpp
More file actions
382 lines (366 loc) · 12.3 KB
/
RowKeyPredicate.cpp
File metadata and controls
382 lines (366 loc) · 12.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
/*
Copyright (c) 2020 TOSHIBA Digital Solutions Corporation.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <string>
#include "RowKeyPredicate.h"
namespace griddb {
#if NAPI_VERSION <= 5
Napi::FunctionReference RowKeyPredicate::constructor;
#endif
Napi::Object RowKeyPredicate::init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
Napi::Function func = DefineClass(env, "RowKeyPredicate", {
InstanceMethod("setRange",
&RowKeyPredicate::setRange),
InstanceMethod("getRange",
&RowKeyPredicate::getRange),
InstanceMethod("setDistinctKeys",
&RowKeyPredicate::setDistinctKeys),
InstanceMethod("getDistinctKeys",
&RowKeyPredicate::getDistinctKeys),
InstanceAccessor("keyType",
&RowKeyPredicate::getKeyType, nullptr)
});
#if NAPI_VERSION > 5
Napi::FunctionReference* constructor = new Napi::FunctionReference();
*constructor = Napi::Persistent(func);
Util::setInstanceData(env, "RowKeyPredicate", constructor);
#else
constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
#endif
exports.Set("RowKeyPredicate", func);
return exports;
}
RowKeyPredicate::RowKeyPredicate(const Napi::CallbackInfo &info) :
Napi::ObjectWrap<RowKeyPredicate>(info) {
Napi::Env env = info.Env();
if (info.Length() != 2 || !info[0].IsExternal() || !info[1].IsExternal()) {
// Throw error
THROW_EXCEPTION_WITH_STR(env, "Wrong arguments", NULL)
return;
}
predicate = info[0].As<Napi::External<GSRowKeyPredicate>>().Data();
type = (GSType) *(info[1].As<Napi::External<int>>().Data());
}
RowKeyPredicate::~RowKeyPredicate() {
if (predicate != NULL) {
gsCloseRowKeyPredicate(&predicate);
predicate = NULL;
}
}
Napi::Value RowKeyPredicate::getKeyType(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
return Napi::Number::New(env, type);
}
GSRowKeyPredicate* RowKeyPredicate::getPredicate() {
return predicate;
}
void RowKeyPredicate::setRange(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
if (info.Length() != 2) {
// Throw error
THROW_EXCEPTION_WITH_STR(env, "Wrong arguments\n", NULL)
return;
}
GSResult ret;
switch (type) {
case GS_TYPE_INTEGER: {
if (!info[0].IsNumber() || !info[1].IsNumber()) {
THROW_EXCEPTION_WITH_STR(env, "Wrong arguments", NULL)
return;
}
int startKey = info[0].ToNumber();
int finishKey = info[1].ToNumber();
ret = gsSetPredicateStartKeyByInteger(predicate,
(const int32_t *) &startKey);
if (!GS_SUCCEEDED(ret)) {
THROW_EXCEPTION_WITH_CODE(env, ret, predicate)
return;
}
ret = gsSetPredicateFinishKeyByInteger(predicate,
(const int32_t *) &finishKey);
if (!GS_SUCCEEDED(ret)) {
THROW_EXCEPTION_WITH_CODE(env, ret, predicate)
return;
}
break;
}
case GS_TYPE_STRING:
if (!info[0].IsString() || !info[1].IsString()) {
THROW_EXCEPTION_WITH_STR(env, "Wrong arguments", NULL)
return;
}
ret = gsSetPredicateStartKeyByString(predicate,
(const char *)info[0].ToString().Utf8Value().c_str());
if (!GS_SUCCEEDED(ret)) {
THROW_EXCEPTION_WITH_CODE(env, ret, predicate)
return;
}
ret = gsSetPredicateFinishKeyByString(predicate,
(const char *)info[1].ToString().Utf8Value().c_str());
if (!GS_SUCCEEDED(ret)) {
THROW_EXCEPTION_WITH_CODE(env, ret, predicate)
return;
}
break;
case GS_TYPE_LONG: {
if (!info[0].IsNumber() || !info[1].IsNumber()) {
THROW_EXCEPTION_WITH_STR(env, "Wrong arguments", NULL)
return;
}
int64_t startKey = info[0].ToNumber();
int64_t finishKey = info[1].ToNumber();
ret = gsSetPredicateStartKeyByLong(predicate,
(const int64_t *) &startKey);
if (!GS_SUCCEEDED(ret)) {
THROW_EXCEPTION_WITH_CODE(env, ret, predicate)
return;
}
ret = gsSetPredicateFinishKeyByLong(predicate,
(const int64_t *) &finishKey);
if (!GS_SUCCEEDED(ret)) {
THROW_EXCEPTION_WITH_CODE(env, ret, predicate)
return;
}
break;
}
case GS_TYPE_TIMESTAMP: {
Napi::Value startKey = info[0].As<Napi::Value>();
Napi::Value finishKey = info[1].As<Napi::Value>();
GSTimestamp tmpTimestampStart;
GSTimestamp tmpTimestampFinish;
try {
tmpTimestampStart = Util::toGsTimestamp(env, &startKey);
tmpTimestampFinish = Util::toGsTimestamp(env, &finishKey);
} catch (const Napi::Error &e) {
e.ThrowAsJavaScriptException();
return;
}
ret = gsSetPredicateStartKeyByTimestamp(predicate,
&tmpTimestampStart);
if (!GS_SUCCEEDED(ret)) {
THROW_EXCEPTION_WITH_CODE(env, ret, predicate)
return;
}
ret = gsSetPredicateFinishKeyByTimestamp(predicate,
&tmpTimestampFinish);
if (!GS_SUCCEEDED(ret)) {
THROW_EXCEPTION_WITH_CODE(env, ret, &predicate)
return;
}
break;
}
default:
THROW_EXCEPTION_WITH_STR(env, "Not support type\n", NULL)
return;
break;
}
}
// Get range
Napi::Value RowKeyPredicate::getRange(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
if (info.Length() != 0) {
// Throw error
THROW_EXCEPTION_WITH_STR(env, "Wrong arguments", NULL)
return env.Null();
}
GSValue *startKey = NULL;
GSValue *endKey = NULL;
GSResult ret;
int length = 2;
Napi::Array array = Napi::Array::New(env, length);
ret = gsGetPredicateStartKeyGeneral(predicate,
(const GSValue **) &startKey);
if (!GS_SUCCEEDED(ret)) {
THROW_EXCEPTION_WITH_CODE(env, ret, predicate)
return env.Null();
}
if (startKey != NULL) {
switch (type) {
case GS_TYPE_STRING: {
array.Set<const GSChar *>("0", startKey->asString);
break;
}
case GS_TYPE_INTEGER: {
array.Set<int32_t>("0", startKey->asInteger);
break;
}
case GS_TYPE_LONG: {
array.Set<int64_t>("0", startKey->asLong);
break;
}
case GS_TYPE_TIMESTAMP: {
array["0"] = Util::fromTimestamp(env,
static_cast<GSTimestamp *>(&startKey->asTimestamp));
break;
}
default:
THROW_EXCEPTION_WITH_STR(env, "Not support type\n", NULL)
return env.Null();
break;
}
}
ret = gsGetPredicateFinishKeyGeneral(predicate, (const GSValue **)&endKey);
if (!GS_SUCCEEDED(ret)) {
THROW_EXCEPTION_WITH_CODE(env, ret, predicate)
return env.Null();
}
if (endKey != NULL) {
switch (type) {
case GS_TYPE_STRING: {
array.Set<const GSChar *>("1", endKey->asString);
break;
}
case GS_TYPE_INTEGER: {
array.Set<int32_t>("1", endKey->asInteger);
break;
}
case GS_TYPE_LONG: {
array.Set<int64_t>("1", endKey->asLong);
break;
}
case GS_TYPE_TIMESTAMP: {
array["1"] = Util::fromTimestamp(env,
static_cast<GSTimestamp *>(&endKey->asTimestamp));
break;
}
default:
THROW_EXCEPTION_WITH_STR(env, "Not support type\n", NULL)
return env.Null();
break;
}
}
return array;
}
void RowKeyPredicate::setDistinctKeys(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
if (info.Length() != 1) {
// Throw error
THROW_EXCEPTION_WITH_STR(env, "Wrong arguments\n", NULL)
return;
}
Napi::Array arrayForSet = info[0].As<Napi::Array>();
size_t keyCount = arrayForSet.Length();
GSResult ret;
for (size_t i = 0; i < keyCount; i++) {
switch (type) {
case GS_TYPE_INTEGER: {
Napi::Value value = arrayForSet[i];
if (!value.IsNumber()) {
THROW_EXCEPTION_WITH_STR(env, "Wrong arguments", NULL)
return;
}
int32_t key = value.ToNumber().Int32Value();
ret = gsAddPredicateKeyByInteger(predicate, key);
if (!GS_SUCCEEDED(ret)) {
THROW_EXCEPTION_WITH_CODE(env, ret, predicate)
return;
}
break;
}
case GS_TYPE_STRING: {
Napi::Value value = arrayForSet[i];
if (!value.IsString()) {
THROW_EXCEPTION_WITH_STR(env, "Wrong arguments", NULL)
return;
}
ret = gsAddPredicateKeyByString(predicate,
value.ToString().Utf8Value().c_str());
if (!GS_SUCCEEDED(ret)) {
THROW_EXCEPTION_WITH_CODE(env, ret, predicate)
return;
}
break;
}
case GS_TYPE_LONG: {
Napi::Value value = arrayForSet[i];
if (!value.IsNumber()) {
THROW_EXCEPTION_WITH_STR(env, "Wrong arguments", NULL)
return;
}
int64_t key = value.ToNumber().Int64Value();
ret = gsAddPredicateKeyByLong(predicate, key);
if (!GS_SUCCEEDED(ret)) {
THROW_EXCEPTION_WITH_CODE(env, ret, predicate)
return;
}
break;
}
case GS_TYPE_TIMESTAMP: {
Napi::Value value = arrayForSet[i];
GSTimestamp key;
try {
key = Util::toGsTimestamp(env, &value);
} catch (const Napi::Error &e) {
e.ThrowAsJavaScriptException();
break;
}
ret = gsAddPredicateKeyByTimestamp(predicate, key);
if (!GS_SUCCEEDED(ret)) {
THROW_EXCEPTION_WITH_CODE(env, ret, predicate)
return;
}
break;
}
default:
THROW_EXCEPTION_WITH_STR(env, "Not support type\n", NULL)
return;
break;
}
}
}
Napi::Value RowKeyPredicate::getDistinctKeys(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
if (info.Length() != 0) {
// Throw error
THROW_EXCEPTION_WITH_STR(env, "Wrong arguments", NULL)
return env.Null();
}
size_t size;
GSValue *keyList;
GSResult ret;
ret = gsGetPredicateDistinctKeysGeneral(predicate,
(const GSValue **)&keyList, &size);
if (!GS_SUCCEEDED(ret)) {
THROW_EXCEPTION_WITH_CODE(env, ret, predicate)
return env.Null();
}
Napi::Array array = Napi::Array::New(env, size);
for (int i = 0; i < static_cast<int>(size); i++) {
switch (type) {
case GS_TYPE_STRING: {
array.Set<const GSChar *>(i, keyList[i].asString);
break;
}
case GS_TYPE_INTEGER: {
array.Set<int32_t>(i, keyList[i].asInteger);
break;
}
case GS_TYPE_LONG: {
array.Set<int64_t>(i, keyList[i].asLong);
break;
}
case GS_TYPE_TIMESTAMP: {
array[i] = Util::fromTimestamp(env,
static_cast<GSTimestamp *>(&keyList[i].asTimestamp));
break;
}
default:
THROW_EXCEPTION_WITH_STR(env, "Not support type\n", NULL)
return env.Null();
break;
}
}
return array;
}
} // namespace griddb