forked from griddb/python_client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStore.cpp
More file actions
280 lines (243 loc) · 9.91 KB
/
Store.cpp
File metadata and controls
280 lines (243 loc) · 9.91 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
/*
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"
#include "GSException.h"
namespace griddb {
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);
}
/**
* Release all resources created by this gridstore object
*/
void Store::close(GSBool allRelated) {
//
std::map<string, griddb::Container*>::iterator it = mContainerList.begin();
while(it != mContainerList.end()) {
if (it->second) {
delete(it->second);
}
it++;
}
if (mStore != NULL) {
gsCloseGridStore(&mStore, allRelated);
mStore = NULL;
}
}
/**
* Delete container with specified name
*/
void Store::drop_container(const char* name) {
GSResult ret = gsDropContainer(mStore, name);
if (ret != GS_RESULT_OK) {
throw GSException(mStore, ret);
}
}
/**
* Return information object of a specific container
*/
ContainerInfo* Store::get_container_info(const char* name) {
GSContainerInfo containerInfo;
GSChar bExists;
GSResult ret = gsGetContainerInfo(mStore, name, &containerInfo, &bExists);
if (ret != GS_RESULT_OK) {
throw GSException(mStore, ret);
}
if (bExists == false) {
return NULL;
}
return new ContainerInfo(&containerInfo);
}
/**
* Put container. Convert from method gsPutContainerGeneral()
*/
Container* Store::put_container(ContainerInfo* info,
bool modifiable) {
// Get Container information
GSContainerInfo* gsInfo = info->gs_info();
GSContainer* pContainer = NULL;
// Create new gsContainer
GSResult ret = gsPutContainerGeneral(mStore, gsInfo->name, gsInfo, modifiable, &pContainer);
if (ret != GS_RESULT_OK) {
throw GSException(mStore, ret);
}
if (pContainer == NULL) {
return NULL;
}
return new Container(pContainer, gsInfo);
}
/**
* Get container object with corresponding name
*/
Container* Store::get_container(const char* name) {
GSContainer* pContainer;
GSResult ret = gsGetContainerGeneral(mStore, name, &pContainer);
if (ret != GS_RESULT_OK) {
throw GSException(mStore, ret);
}
if (pContainer == NULL) {
//If not found container, return NULL in target language
return NULL;
}
GSContainerInfo containerInfo;
GSChar bExists;
ret = gsGetContainerInfo(mStore, name, &containerInfo, &bExists);
if (ret != GS_RESULT_OK) {
throw GSException(mStore, ret);
}
return new Container(pContainer, &containerInfo);
}
/**
* Query execution and fetch is carried out on a specified arbitrary number of Query, with the request unit enlarged as much as possible.
*/
void Store::fetch_all(GSQuery* const* queryList, size_t queryCount) {
GSResult ret = gsFetchAll(mStore, queryList, queryCount);
if (ret != GS_RESULT_OK) {
throw GSException(mStore, ret);
}
}
/**
* Get Partition controller. Convert from C-API: gsGetPartitionController
*/
PartitionController* Store::partition_info() {
GSPartitionController* partitionController;
GSResult ret = gsGetPartitionController(mStore, &partitionController);
if (ret != GS_RESULT_OK) {
throw GSException(mStore, ret);
}
return new PartitionController(partitionController);
}
/**
* Create row key predicate. Convert from C-API: gsCreateRowKeyPredicate
*/
RowKeyPredicate* Store::create_row_key_predicate(GSType type) {
GSRowKeyPredicate* predicate;
GSResult ret = gsCreateRowKeyPredicate(mStore, type, &predicate);
if (ret != GS_RESULT_OK) {
throw GSException(mStore, ret);
}
return new RowKeyPredicate(predicate, type);
}
/**
* 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.
*/
void Store::multi_put(Row*** listRow, const int *listRowContainerCount,
const char ** listContainerName, size_t containerCount) {
const GSChar *containerName;
GSResult ret;
GSContainerRowEntry * entryList = (GSContainerRowEntry*) malloc (containerCount * sizeof(GSContainerRowEntry));
std::map<string,griddb::Container*>::iterator it;
for (int i= 0; i < containerCount; i++) {
containerName = listContainerName[i];
entryList[i].containerName = containerName;
entryList[i].rowCount = listRowContainerCount[i];
GSRow** listContainerRow = (GSRow**) malloc(listRowContainerCount[i] * sizeof(GSRow*));
string tmpString(containerName);
it = mContainerList.find(tmpString);
Container* pContainer;
if (it != mContainerList.end()) {
//Find it in map
pContainer = it->second;
} else {
//Not find in map
pContainer = this->get_container(containerName);
it = mContainerList.begin();
mContainerList.insert(it, std::pair<string, griddb::Container*>(containerName, pContainer));
}
for (int j = 0; j < listRowContainerCount[i]; j++) {
GSRow* gsrow;
gsCreateRowByContainer(pContainer->getGSContainerPtr(), &gsrow);
listRow[i][j]->set_for_row(gsrow, NULL);
listContainerRow[j] = gsrow;
}
entryList[i].rowList = (void* const*) listContainerRow;
pContainer = NULL;
}
ret = gsPutMultipleContainerRows(mStore, entryList, containerCount);
//Free memory
for (int i = 0; i < containerCount; i++) {
containerName = listContainerName[i];
string tmpString(containerName);
it = mContainerList.find(tmpString);
Container* pContainer;
if (it != mContainerList.end()) {
//Find it in map
pContainer = it->second;
} else {
//Not find in map
pContainer = this->get_container(containerName);
it = mContainerList.begin();
mContainerList.insert(it, std::pair<string, griddb::Container*>(containerName, pContainer));
}
for (int j = 0; j < listRowContainerCount[i]; j++) {
GSRow* gsrow = (GSRow*)entryList[i].rowList[j];
gsCloseRow(&gsrow);
}
free((void*) entryList[i].rowList);
pContainer = NULL;
}
free((void*) entryList);
if (ret != GS_RESULT_OK) {
throw GSException(mStore, ret);
}
}
/**
* muti_get method. Using gsGetMultipleContainerRows C-API
*/
void Store::multi_get(const GSRowKeyPredicateEntry* const * predicateList,
size_t predicateCount, std::vector<griddb::Row*> *listRow, size_t **listRowContainerCount,
char*** listContainerName, size_t* containerCount) {
GSContainerRowEntry *entryList;
GSResult ret = gsGetMultipleContainerRows(mStore, predicateList,
predicateCount, (const GSContainerRowEntry**) &entryList, containerCount);
if (ret != GS_RESULT_OK) {
throw GSException(mStore, ret);
}
//Store list of GsRow.
GSRow*** listGsRow = (GSRow***) malloc(*containerCount * sizeof(GSRow**));
//Memory will be free in typemap out
*listContainerName = (char **) malloc(*containerCount * sizeof(char*));
//Memory will be free in typemap out
*listRowContainerCount = (size_t*) malloc(*containerCount * sizeof(size_t));
std::map<string,griddb::Container*>::iterator it;
GSContainerRowEntry* rowKeyEntry;
for (int i = 0; i< *containerCount; i++) {
//data for each container
rowKeyEntry = &(entryList[i]);
GSContainerInfo containerInfo;
GSChar bExists;
(*listContainerName)[i] = strdup(rowKeyEntry->containerName);
(*listRowContainerCount)[i] = rowKeyEntry->rowCount;
(listGsRow)[i] = (GSRow**) malloc(rowKeyEntry->rowCount * sizeof(GSRow*));
for (int j = 0;j < rowKeyEntry->rowCount; j++){
GSRow* gsrow = (GSRow*) (rowKeyEntry->rowList[j]);
(listGsRow)[i][j] = gsrow;
}
}
//Use this loop to work around the error when call gsGetContainerInfo when process output of gsGetMultipleContainerRows
//Need to be refactored when C-API fix error above.
for (int i = 0; i< *containerCount; i++) {
for (int j = 0; j < (*listRowContainerCount)[i]; j++){
//Dummy row
Row* tmpRow = new Row(1, NULL);
tmpRow->set_from_row((listGsRow)[i][j]);
(*listRow).push_back(tmpRow);
gsCloseRow(&(listGsRow)[i][j]);
}
free((void *) (listGsRow)[i]);
}
free((void *) listGsRow);
}
}