-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathStore.cpp
366 lines (330 loc) · 13.3 KB
/
Store.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
/*
Copyright (c) 2017 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 "Store.h"
namespace griddb {
/**
* @brief Constructor a new Store::Store object
* @param *store A pointer which provides functions to manipulate the entire data managed in one GridDB system.
*/
Store::Store(GSGridStore *store) : mStore(store), timestamp_output_with_float(false) {
}
Store::~Store() {
// allRelated = FALSE, since all container object is managed by Container class
close(GS_FALSE);
}
/**
* @brief Release Store resource
*/
void Store::close(GSBool allRelated) {
// close store
if (mStore != NULL) {
gsCloseGridStore(&mStore, allRelated);
mStore = NULL;
}
}
/**
* @brief Delete container with specified name
* @param *name Container name
*/
void Store::drop_container(const char* name) {
GSResult ret = gsDropContainer(mStore, name);
if (!GS_SUCCEEDED(ret)) {
#ifdef GRIDDB_GO
free((void *)name);
#endif
throw GSException(mStore, ret);
}
}
/**
* @brief Get information object of a specific container
* @param *name Container name
* @return Return a pointer which stores all information of container
*/
ContainerInfo* Store::get_container_info(const char* name) {
GSContainerInfo gsContainerInfo = GS_CONTAINER_INFO_INITIALIZER;
GSChar bExists;
GSResult ret = gsGetContainerInfo(mStore, name, &gsContainerInfo, &bExists);
if (!GS_SUCCEEDED(ret)) {
throw GSException(mStore, ret);
}
if (gsContainerInfo.name == NULL || bExists == false) {
return NULL;
}
try {
ContainerInfo* containerInfo = new ContainerInfo(&gsContainerInfo);
return containerInfo;
} catch (bad_alloc& ba) {
throw GSException(mStore, "Memory allocation error");
}
}
/**
* @brief Creates or update a Container with the specified GSContainerInfo.
* @param *info A pointer which stores all information of container
* @param modifiable Indicates whether the column layout of the existing Container can be modified or not
* @return The pointer to a pointer variable to store Container instance
*/
Container* Store::put_container(ContainerInfo* info,
bool modifiable) {
if (info == NULL) {
throw GSException(mStore, "Invalid input for \"Store::put_container\" method. Argument container info can not be null");
}
// Get Container information
GSContainerInfo* gsInfo = info->gs_info();
GSContainer* pContainer = NULL;
// Create new gsContainer
GSResult ret = gsPutContainerGeneral(mStore, gsInfo->name, gsInfo, modifiable, &pContainer);
if (!GS_SUCCEEDED(ret)) {
throw GSException(mStore, ret);
}
try {
Container* container = new Container(pContainer, gsInfo);
return container;
} catch (bad_alloc& ba) {
gsCloseContainer(&pContainer, GS_FALSE);
throw GSException(mStore, "Memory allocation error");
}
}
/**
* @brief Get container object with corresponding name
* @param *name Container name
* @return The pointer to a pointer variable to store Container instance
*/
Container* Store::get_container(const char* name) {
GSContainer* pContainer;
GSResult ret = gsGetContainerGeneral(mStore, name, &pContainer);
if (!GS_SUCCEEDED(ret)) {
#ifdef GRIDDB_GO
free((void *)name);
#endif
throw GSException(mStore, ret);
}
if (pContainer == NULL) {
//If not found container, return NULL in target language
return NULL;
}
GSContainerInfo containerInfo = GS_CONTAINER_INFO_INITIALIZER;
GSChar bExists;
ret = gsGetContainerInfo(mStore, name, &containerInfo, &bExists);
if (!GS_SUCCEEDED(ret)) {
#ifdef GRIDDB_GO
free((void *)name);
#endif
gsCloseContainer(&pContainer, GS_FALSE);
throw GSException(mStore, ret);
}
try {
Container* container = new Container(pContainer, &containerInfo);
return container;
} catch (bad_alloc& ba) {
#ifdef GRIDDB_GO
free((void *)name);
#endif
gsCloseContainer(&pContainer, GS_FALSE);
throw GSException(mStore, "Memory allocation error");
}
}
/**
* @brief Query execution and fetch is carried out on a specified arbitrary number of Query, with the request unit enlarged as much as possible.
* @param **queryList A list of query
* @param queryCount Number of element in query list
*/
void Store::fetch_all(GSQuery* const* queryList, size_t queryCount) {
GSResult ret = gsFetchAll(mStore, queryList, queryCount);
if (!GS_SUCCEEDED(ret)) {
#ifdef GRIDDB_GO
delete [] queryList;
#endif
throw GSException(mStore, ret);
}
}
/**
* @brief Get Partition controller.
* @return The pointer to a pointer variable to store PartitionController instance
*/
PartitionController* Store::partition_info() {
GSPartitionController* partitionController;
GSResult ret = gsGetPartitionController(mStore, &partitionController);
if (!GS_SUCCEEDED(ret)) {
throw GSException(mStore, ret);
}
try {
PartitionController* partition = new PartitionController(partitionController);
return partition;
} catch (bad_alloc& ba) {
gsClosePartitionController(&partitionController);
throw GSException(mStore, "Memory allocation error");
}
}
/**
* @brief Create row key predicate.
* @param type The type of Row key used as a matching condition
* @return The pointer to a pointer variable to store RowKeyPredicate instance
*/
RowKeyPredicate* Store::create_row_key_predicate(GSType type) {
GSRowKeyPredicate* predicate;
GSResult ret = gsCreateRowKeyPredicate(mStore, type, &predicate);
if (!GS_SUCCEEDED(ret)) {
throw GSException(mStore, ret);
}
try {
RowKeyPredicate* rowKeyPredicate = new RowKeyPredicate(predicate, type);
return rowKeyPredicate;
} catch (bad_alloc& ba) {
gsCloseRowKeyPredicate(&predicate);
throw GSException(mStore, "Memory allocation error");
}
}
/**
* @brief New creation or update operation is carried out on an arbitrary number of rows of multiple Containers, with the request unit enlarged as much as possible.
* @param ***listRow A pointer refers list of row data
* @param *listRowContainerCount A array store number of list row for each container
* @param **listContainerName list container name
* @param containerCount Number of container
*/
void Store::multi_put(GSRow*** listRow, const int *listRowContainerCount,
const char ** listContainerName, size_t containerCount) {
assert(listRowContainerCount != NULL);
assert(listContainerName != NULL);
GSResult ret;
GSContainerRowEntry* entryList;
try {
entryList = new GSContainerRowEntry[containerCount]();
} catch (bad_alloc& ba) {
throw GSException(mStore, "Memory allocation error");
}
for (int i= 0; i < containerCount; i++) {
entryList[i].containerName = listContainerName[i];
entryList[i].rowCount = listRowContainerCount[i];
entryList[i].rowList = (void* const*) listRow[i];
}
ret = gsPutMultipleContainerRows(mStore, entryList, containerCount);
delete[] entryList;
if (!GS_SUCCEEDED(ret)) {
throw GSException(mStore, ret);
}
}
/**
* @brief get multi row from multi container
* @param **predicateList A pointer refers list of the specified condition entry by a container for representing the acquisition conditions for a plurality of containers.
* @param predicateCount Number of predicate list
* @param **entryList A pointer refers to a list of the Row contents entry by a container used when operating collectively a plurality of Rows of a plurality of containers.
* @param *containerCount Number of container
* @param **colNumList A pointer refers to an array stores number of column of each container
* @param ***typeList A pointer refers to an array stores type of column of each container
* @param **orderFromInput A pointer refers to an array stores order of each container for output
*/
void Store::multi_get(const GSRowKeyPredicateEntry* const * predicateList,
size_t predicateCount, GSContainerRowEntry **entryList, size_t* containerCount,
int **colNumList, GSType*** typeList, int **orderFromInput) {
assert(predicateList != NULL);
assert(predicateCount >= 0);
assert(colNumList != NULL);
assert(typeList != NULL);
assert(orderFromInput != NULL);
assert(containerCount != NULL);
*colNumList = NULL;
*typeList = NULL;
*orderFromInput = NULL;
*containerCount = 0;
int length = (int) predicateCount;
try {
// get number of column of rows in each container.
*colNumList = new int[predicateCount](); //will be free in argout
*typeList = new GSType*[predicateCount](); //will be free in argout
*orderFromInput = new int[length](); //will be free in argout
} catch (bad_alloc& ba) {
this->freeMemoryMultiGet(colNumList, typeList, length, orderFromInput);
throw GSException(mStore, "Memory allocation error");
}
bool setNumList = this->setMultiContainerNumList(predicateList,
length, &colNumList, &typeList);
if (!setNumList) {
this->freeMemoryMultiGet(colNumList, typeList, length, orderFromInput);
throw GSException(mStore, "Set multi containers number list and type list error");
}
// Get data for entryList
GSResult ret = gsGetMultipleContainerRows(mStore, predicateList,
predicateCount, (const GSContainerRowEntry**) entryList, containerCount);
if (!GS_SUCCEEDED(ret)) {
this->freeMemoryMultiGet(colNumList, typeList, length, orderFromInput);
#ifdef GRIDDB_GO
for (int i = 0; i < predicateCount; i++) {
delete [] (*predicateList)[i].containerName;
}
delete [] *predicateList;
#endif
throw GSException(mStore, ret);
}
// set data for orderFromInput
for (int i = 0; i < length; i++) {
for (int j = 0; j < length; j++) {
if (strcmp((*predicateList)[i].containerName, (*entryList)[j].containerName) == 0){
(*orderFromInput)[i] = j;
}
}
}
}
/**
* Support free memory in multi_get function when exception happen
*/
void Store::freeMemoryMultiGet(int **colNumList, GSType*** typeList,
int length, int **orderFromInput) {
if (*colNumList) {
delete [] *colNumList;
*colNumList = NULL;
}
if (*typeList) {
for (int i = 0; i < length; i++) {
if ((*typeList)[i]) {
delete[] (*typeList)[i];
}
}
delete [] *typeList;
*typeList = NULL;
}
if (*orderFromInput) {
delete [] *orderFromInput;
*orderFromInput = NULL;
}
}
/**
* Support multi_get function to put data into colNumList and typeList
*/
bool Store::setMultiContainerNumList(const GSRowKeyPredicateEntry* const * predicateList,
int length, int ***colNumList, GSType**** typeList) {
for (int i = 0; i < length; i++) {
Container *tmpContainer;
try {
tmpContainer = this->get_container((*predicateList)[i].containerName);
} catch (GSException e) {
return false;
}
if (tmpContainer == NULL) {
return false;
}
(**colNumList)[i] = tmpContainer->getColumnCount();
try {
//(**typeList)[i] will be freed in freeMemoryMultiGet() function or argout
(**typeList)[i] = new GSType[(**colNumList)[i]]();
} catch (bad_alloc& ba) {
delete tmpContainer;
return false;
}
for (int j = 0; j < (**colNumList)[i]; j++) {
(**typeList)[i][j] = tmpContainer->getGSTypeList()[j];
}
delete tmpContainer;
}
return true;
}
}