-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathContainerInfo.cpp
652 lines (574 loc) · 22.1 KB
/
ContainerInfo.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
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/*
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 "ContainerInfo.h"
namespace griddb {
Napi::FunctionReference ContainerInfo::constructor;
Napi::Object ContainerInfo::init(const Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
Napi::Function func = DefineClass(env, "ContainerInfo", {
InstanceAccessor("name",
&ContainerInfo::getName,
&ContainerInfo::setName),
InstanceAccessor("type",
&ContainerInfo::getType,
&ContainerInfo::setType),
InstanceAccessor("rowKey",
&ContainerInfo::getRowKeyAssigned,
&ContainerInfo::setRowKeyAssigned),
InstanceAccessor("columnInfoList",
&ContainerInfo::getColumnInfoList,
&ContainerInfo::setColumnInfoList),
InstanceAccessor("expiration",
&ContainerInfo::getExpiration,
&ContainerInfo::setExpiration),
});
constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
exports.Set("ContainerInfo", func);
return exports;
}
ContainerInfo::ContainerInfo(const Napi::CallbackInfo &info) :
Napi::ObjectWrap<ContainerInfo>(info),
mContainerInfo(GS_CONTAINER_INFO_INITIALIZER), mExpInfo(NULL) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
int length = info.Length();
if (length == 1 && info[0].IsExternal()) {
// Use only for method : Store.getContainerInfo()
GSContainerInfo *containerInfo = (info[0].As<
Napi::External<GSContainerInfo>>().Data());
init(env, containerInfo->name, containerInfo->type,
containerInfo->columnInfoList, containerInfo->columnCount,
containerInfo->rowKeyAssigned, NULL);
// Assign values from argument to mContainer
GSTimeSeriesProperties *gsProps = NULL;
GSTriggerInfo *triggerInfoList = NULL;
try {
if (containerInfo->timeSeriesProperties) {
gsProps = new GSTimeSeriesProperties();
}
if (containerInfo->triggerInfoList) {
triggerInfoList = new GSTriggerInfo();
}
if (containerInfo->dataAffinity) {
mContainerInfo.dataAffinity = Util::strdup(
containerInfo->dataAffinity);
} else {
mContainerInfo.dataAffinity = NULL;
}
} catch (std::bad_alloc&) {
// Case allocation memory error
if (gsProps) {
delete gsProps;
}
if (triggerInfoList) {
delete triggerInfoList;
}
if (mContainerInfo.dataAffinity) {
delete[] mContainerInfo.dataAffinity;
}
THROW_EXCEPTION_WITH_STR(env, "Memory allocation error", NULL)
return;
}
if (containerInfo->timeSeriesProperties) {
memcpy(gsProps, containerInfo->timeSeriesProperties,
sizeof(GSTimeSeriesProperties));
}
mContainerInfo.timeSeriesProperties = gsProps;
if (containerInfo->triggerInfoList) {
memcpy(triggerInfoList, containerInfo->triggerInfoList,
sizeof(GSTriggerInfo));
}
mContainerInfo.triggerInfoList = triggerInfoList;
mContainerInfo.columnOrderIgnorable =
containerInfo->columnOrderIgnorable;
mContainerInfo.triggerInfoCount = containerInfo->triggerInfoCount;
mColumnInfoList.columnInfo = NULL;
mColumnInfoList.size = 0;
return;
} else if (length == 1 && info[0].IsObject()) {
// Case create ContainerInfo : new griddb.ContainerInfo({..});
Napi::Object obj = info[0].As<Napi::Object>();
// Name attribute
if (!obj.Has("name")) {
THROW_EXCEPTION_WITH_STR(env, "Missing name attribute", NULL)
return;
}
if (!obj.Get("name").IsString()) {
THROW_EXCEPTION_WITH_STR(
env, "Name attribute should be string", NULL)
return;
}
std::string name = obj.Get("name").ToString().Utf8Value();
// Type attribute
GSContainerType type = GS_CONTAINER_COLLECTION;
if (obj.Has("type") && !obj.Get("type").IsNumber()) {
THROW_EXCEPTION_WITH_STR(
env, "Attribute type should be number", NULL)
return;
}
type = obj.Get("type").ToNumber().Int32Value();
// RowKey attribute
bool rowKey = true;
if (obj.Has("rowKey") && !obj.Get("rowKey").IsBoolean()) {
THROW_EXCEPTION_WITH_STR(
env, "Attribute rowKey should be bool", NULL)
return;
}
if (obj.Has("rowKey")) {
rowKey = obj.Get("rowKey").ToBoolean();
}
// ExpirationInfo
ExpirationInfo *expiration = NULL;
if (obj.Has("expiration")) {
expiration = Napi::ObjectWrap<ExpirationInfo>::Unwrap(
obj.Get("expiration").As<Napi::Object>());
}
// columnInfoList attribute
if (!obj.Has("columnInfoList")) {
THROW_EXCEPTION_WITH_STR(env,
"Missing columnInfoList attribute", NULL)
return;
}
if (!obj.Get("columnInfoList").IsArray()) {
THROW_EXCEPTION_WITH_STR(env,
"Attribute columnInfoList should be array", NULL)
return;
}
Napi::Array columnInfoListArray =
obj.Get("columnInfoList").As<Napi::Array>();
int propsCount = columnInfoListArray.Length();
GSColumnInfo *props = new GSColumnInfo[propsCount]();
for (int i = 0; i < propsCount; i++) {
if (!((Napi::Value) columnInfoListArray[i]).IsArray()) {
// Free memory of props
this->freeMemoryPropsList(props, propsCount);
THROW_EXCEPTION_WITH_STR(env,
"Element of columnInfoList should be array", NULL)
return;
}
Napi::Array fieldInfo = ((Napi::Value) columnInfoListArray[i]).As<
Napi::Array>();
if (fieldInfo.Length() < 2 || fieldInfo.Length() > 3) {
// Free memory of props
this->freeMemoryPropsList(props, propsCount);
THROW_EXCEPTION_WITH_STR(env,
"Expected 2 or 3 elements for ColumnInfo property",
NULL)
return;
}
bool firstFieldElementIsString =
((Napi::Value) (fieldInfo["0"])).IsString();
if (!firstFieldElementIsString) {
this->freeMemoryPropsList(props, propsCount);
THROW_EXCEPTION_WITH_STR(env,
"Expected 1st element of ColumnInfo property is string",
NULL)
return;
}
props[i].name = Util::strdup(((Napi::Value) (fieldInfo["0"])).
ToString().Utf8Value().c_str());
bool secondFieldElementIsNumber =
((Napi::Value) (fieldInfo["1"])).IsNumber();
if (!secondFieldElementIsNumber) {
// Free memory of props
this->freeMemoryPropsList(props, propsCount);
THROW_EXCEPTION_WITH_STR(env,
"Expected 1st element of ColumnInfo property is string",
NULL)
return;
}
props[i].type = ((Napi::Value) (fieldInfo[1])).
ToNumber().Int32Value();
if (fieldInfo.Length() == 3) {
bool thirdFieldElementIsNumber =
((Napi::Value) (fieldInfo["2"])).IsNumber();
if (!thirdFieldElementIsNumber) {
this->freeMemoryPropsList(props, propsCount);
THROW_EXCEPTION_WITH_STR(env,
"Expected Integer as type of Column options", NULL)
return;
}
props[i].options = ((Napi::Value) (fieldInfo["2"])).
ToNumber().Int32Value();
}
}
try {
this->init(env, name.c_str(), type, props, propsCount,
rowKey, expiration);
} catch (const Napi::Error &e) {
this->freeMemoryPropsList(props, propsCount);
NAPI_THROW(e);
}
this->freeMemoryPropsList(props, propsCount);
} else {
THROW_EXCEPTION_WITH_STR(env, "Invalid input", NULL)
}
}
/**
* Initialize values of Container Info object
*/
void ContainerInfo::init(const Napi::Env &env, const GSChar *name,
GSContainerType type, const GSColumnInfo *props, int propsCount,
bool rowKeyAssigned, ExpirationInfo *expiration) {
GSColumnInfo *columnInfoList = NULL;
const GSChar *containerName = NULL;
GSTimeSeriesProperties *timeProps = NULL;
try {
if (propsCount > 0 && props != NULL) {
columnInfoList = new GSColumnInfo[propsCount]();
// Copy memory of GSColumnInfo list
memcpy(columnInfoList, props, propsCount * sizeof(GSColumnInfo));
// Copy memory of columns name
for (int i = 0; i < propsCount; i++) {
if (props[i].name != NULL) {
columnInfoList[i].name = Util::strdup(props[i].name);
} else {
columnInfoList[i].name = NULL;
}
}
}
if (expiration != NULL) {
timeProps = new GSTimeSeriesProperties();
}
// Container name memory is copied via strdup function
if (name != NULL) {
containerName = Util::strdup(name);
}
} catch (std::bad_alloc&) {
if (columnInfoList) {
for (int i = 0; i < propsCount; i++) {
if (columnInfoList[i].name) {
delete[] columnInfoList[i].name;
}
}
delete[] columnInfoList;
}
if (containerName) {
delete[] containerName;
}
if (timeProps) {
delete timeProps;
}
THROW_EXCEPTION_WITH_STR(env, "Memory allocation error", NULL)
return;
}
if (expiration != NULL) {
memcpy(timeProps, expiration->gs_ts(), sizeof(GSTimeSeriesProperties));
}
mContainerInfo.name = containerName;
mContainerInfo.type = type;
mContainerInfo.columnCount = (size_t) propsCount;
mContainerInfo.columnInfoList = columnInfoList;
mContainerInfo.rowKeyAssigned = rowKeyAssigned;
if (timeProps != NULL) {
mContainerInfo.timeSeriesProperties = timeProps;
}
mExpInfo = NULL;
mColumnInfoList.columnInfo = NULL;
mColumnInfoList.size = 0;
}
void ContainerInfo::freeMemoryPropsList(GSColumnInfo *props,
int propsCount) {
if (props == NULL) {
return;
}
for (int i = 0; i < propsCount; i++) {
if (props[i].name) {
delete[] props[i].name;
}
}
delete[] props;
}
ContainerInfo::~ContainerInfo() {
// Free memory for the copy of container name
if (mContainerInfo.name) {
delete[] mContainerInfo.name;
}
// Free memory for the copy of ColumnInfo list
if (mContainerInfo.columnInfoList) {
// Free memory of columns name
for (int i = 0; i < static_cast<int>(mContainerInfo.columnCount); i++) {
if (mContainerInfo.columnInfoList[i].name) {
delete[] mContainerInfo.columnInfoList[i].name;
}
}
delete[] mContainerInfo.columnInfoList;
}
// Free memory of TimeSeriesProperties if existed
if (mContainerInfo.timeSeriesProperties) {
delete mContainerInfo.timeSeriesProperties;
}
// Free memory of dataAffinity if existed
if (mContainerInfo.dataAffinity) {
delete[] mContainerInfo.dataAffinity;
}
// Free memory of triggerInfoList if existed
if (mContainerInfo.triggerInfoList) {
delete mContainerInfo.triggerInfoList;
}
if (mExpInfo != NULL) {
delete mExpInfo;
}
}
Napi::Value ContainerInfo::getName(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
return Napi::String::New(env, mContainerInfo.name);
}
void ContainerInfo::setName(const Napi::CallbackInfo &info,
const Napi::Value &value) {
Napi::Env env = info.Env();
if (!value.IsString()) {
THROW_EXCEPTION_WITH_STR(env, "String expected", NULL)
return;
}
std::string containerName = value.ToString().Utf8Value();
if (mContainerInfo.name) {
delete[] mContainerInfo.name;
}
mContainerInfo.name = Util::strdup(containerName.c_str());
}
Napi::Value ContainerInfo::getType(const Napi::CallbackInfo &info) {
return Napi::Number::New(info.Env(), mContainerInfo.type);
}
void ContainerInfo::setType(const Napi::CallbackInfo &info,
const Napi::Value &value) {
Napi::Env env = info.Env();
int length = info.Length();
if (length != 1 || !info[0].IsNumber()) {
THROW_EXCEPTION_WITH_STR(env, "Number expected", NULL)
return;
}
GSContainerType containerType = info[0].As<Napi::Number>().Int32Value();
mContainerInfo.type = containerType;
}
Napi::Value ContainerInfo::getRowKeyAssigned(
const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
return Napi::Boolean::New(env, mContainerInfo.rowKeyAssigned);
}
void ContainerInfo::setRowKeyAssigned(const Napi::CallbackInfo &info,
const Napi::Value &value) {
Napi::Env env = info.Env();
int length = info.Length();
if (length <= 0 || !info[0].IsBoolean()) {
THROW_EXCEPTION_WITH_STR(env, "Boolean expected", NULL)
return;
}
bool rowkeyAssigned = info[0].As<Napi::Boolean>();
mContainerInfo.rowKeyAssigned = (rowkeyAssigned == true)? GS_TRUE: GS_FALSE;
}
void ContainerInfo::setColumnInfoList(const Napi::CallbackInfo &info,
const Napi::Value &value) {
ColumnInfoList columnInfoList;
Napi::Env env = info.Env();
if (!value.IsArray()) {
THROW_EXCEPTION_WITH_STR(env, "Expected array as input", NULL)
return;
}
Napi::Array arr = value.As<Napi::Array>();
size_t len = (size_t) arr.Length();
if (len == 0) {
THROW_EXCEPTION_WITH_STR(env, "Expected not empty array as input", NULL)
return;
}
GSColumnInfo *containerInfo;
try {
containerInfo = new GSColumnInfo[len]();
} catch (std::bad_alloc&) {
THROW_EXCEPTION_WITH_STR(env, "Memory allocation error", NULL)
return;
}
columnInfoList.columnInfo = containerInfo;
columnInfoList.size = len;
Napi::Array colInfo;
Napi::Value key;
Napi::Value value1;
Napi::Value options;
for (int i = 0; i < static_cast<int>(len); i++) {
if (!(arr.Get(i)).IsArray()) {
this->freeColumnInfoList(&columnInfoList);
THROW_EXCEPTION_WITH_STR(env,
"Expected array property as ColumnInfo element", NULL)
return;
}
colInfo = arr.Get(i).As<Napi::Array>();
if (colInfo.Length() < 2) {
this->freeColumnInfoList(&columnInfoList);
THROW_EXCEPTION_WITH_STR(env,
"Expected at least two elements for ColumnInfo property",
NULL)
return;
}
key = colInfo.Get("0");
value1 = colInfo.Get("1");
if (!key.IsString()) {
this->freeColumnInfoList(&columnInfoList);
THROW_EXCEPTION_WITH_STR(
env, "Expected string for column name", NULL)
return;
}
if (!value1.IsNumber()) {
this->freeColumnInfoList(&columnInfoList);
THROW_EXCEPTION_WITH_STR(
env, "Expected Integer as type of Column type", NULL)
return;
}
containerInfo[i].name = Util::strdup(
key.As<Napi::String>().Utf8Value().c_str());
containerInfo[i].type = value1.As<Napi::Number>().Int32Value();
if (colInfo.Length() == 3) {
options = colInfo.Get("2");
if (!options.IsNumber()) {
this->freeColumnInfoList(&columnInfoList);
THROW_EXCEPTION_WITH_STR(env,
"Expected Integer as type of Column options", NULL)
return;
}
containerInfo[i].options = options.As<Napi::Number>().Int32Value();
}
if (colInfo.Length() > 3) {
this->freeColumnInfoList(&columnInfoList);
THROW_EXCEPTION_WITH_STR(env,
"Expected at most 3 elements for ColumnInfo property", NULL)
return;
}
}
// Free current stored ColumnInfo list
if (mContainerInfo.columnInfoList) {
// Free memory of columns name
for (int i = 0; i < static_cast<int>(mContainerInfo.columnCount); i++) {
delete[] mContainerInfo.columnInfoList[i].name;
}
delete[] mContainerInfo.columnInfoList;
}
mContainerInfo.columnCount = columnInfoList.size;
mContainerInfo.columnInfoList = NULL;
if (columnInfoList.size == 0 || columnInfoList.columnInfo == NULL) {
return;
}
GSColumnInfo *tmpColumnInfoList = NULL;
try {
tmpColumnInfoList = new GSColumnInfo[columnInfoList.size]();
} catch (std::bad_alloc&) {
THROW_EXCEPTION_WITH_STR(env, "Memory allocation error", NULL)
return;
}
// Copy memory of GSColumnInfo list
memcpy(tmpColumnInfoList, columnInfoList.columnInfo,
columnInfoList.size * sizeof(GSColumnInfo));
// Copy memory of columns name
for (int i = 0; i < static_cast<int>(columnInfoList.size); i++) {
if (columnInfoList.columnInfo[i].name) {
try {
tmpColumnInfoList[i].name = Util::strdup(
columnInfoList.columnInfo[i].name);
} catch (std::bad_alloc&) {
delete[] tmpColumnInfoList;
tmpColumnInfoList = NULL;
THROW_EXCEPTION_WITH_STR(env, "Memory allocation error", NULL)
return;
}
} else {
tmpColumnInfoList[i].name = NULL;
}
}
mContainerInfo.columnInfoList = tmpColumnInfoList;
this->freeColumnInfoList(&columnInfoList);
}
Napi::Value ContainerInfo::getColumnInfoList(
const Napi::CallbackInfo &info) {
mColumnInfoList.columnInfo =
const_cast<GSColumnInfo*>(mContainerInfo.columnInfoList);
mColumnInfoList.size = mContainerInfo.columnCount;
ColumnInfoList columnInfoList = mColumnInfoList;
Napi::Env env = info.Env();
Napi::Array returnWrapper = Napi::Array::New(env);
size_t len = columnInfoList.size;
Napi::Array obj;
if (len > 0) {
obj = Napi::Array::New(env, len);
for (int i = 0; i < static_cast<int>(len); i++) {
Napi::Array prop = Napi::Array::New(env);
prop.Set("0",
Napi::String::New(env, columnInfoList.columnInfo[i].name));
prop.Set("1",
Napi::Number::New(env, columnInfoList.columnInfo[i].type));
prop.Set("2",
Napi::Number::New(env,
columnInfoList.columnInfo[i].options));
returnWrapper.Set(i, prop);
}
}
return returnWrapper;
}
void ContainerInfo::freeColumnInfoList(
ColumnInfoList *columnInfoList) {
size_t len = columnInfoList->size;
if (columnInfoList->columnInfo) {
for (int i = 0; i < static_cast<int>(len); i++) {
delete[] columnInfoList->columnInfo[i].name;
}
delete[] columnInfoList->columnInfo;
}
}
void ContainerInfo::setExpiration(const Napi::CallbackInfo &info,
const Napi::Value &value) {
ExpirationInfo* expiration = Napi::ObjectWrap<ExpirationInfo>::Unwrap(
value.As<Napi::Object>());
if (mContainerInfo.timeSeriesProperties != NULL) {
delete mContainerInfo.timeSeriesProperties;
mContainerInfo.timeSeriesProperties = NULL;
}
if (expiration) {
GSTimeSeriesProperties* ts;
try {
ts = new GSTimeSeriesProperties();
} catch (std::bad_alloc&) {
THROW_EXCEPTION_WITH_STR(
info.Env(), "Memory allocation error", NULL)
return;
}
memcpy(ts, expiration->gs_ts(), sizeof(GSTimeSeriesProperties));
mContainerInfo.timeSeriesProperties = ts;
}
}
Napi::Value ContainerInfo::getExpiration(
const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
int time;
GSTimeUnit unit;
int division_count;
if (mContainerInfo.timeSeriesProperties != NULL) {
time = mContainerInfo.timeSeriesProperties->rowExpirationTime;
unit = mContainerInfo.timeSeriesProperties->rowExpirationTimeUnit;
division_count =
mContainerInfo.timeSeriesProperties->expirationDivisionCount;
// Create new ExpirationInfo object
Napi::EscapableHandleScope scope(env);
return scope.Escape(ExpirationInfo::constructor.New( {
Napi::Number::New(env, time), Napi::Number::New(env, unit),
Napi::Number::New(env, division_count) })).ToObject();
} else {
return env.Null();
}
}
/**
* @brief Get all information of Container
* @return A pointer which store all information of Container
*/
GSContainerInfo* ContainerInfo::gs_info() {
return &mContainerInfo;
}
} // namespace griddb