-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathRowKeyPredicate.cpp
More file actions
283 lines (262 loc) · 10.6 KB
/
RowKeyPredicate.cpp
File metadata and controls
283 lines (262 loc) · 10.6 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
/*
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 "RowKeyPredicate.h"
namespace griddb {
/**
* @brief Constructor a new RowSet::RowSet object
* @param *predicate Represents the condition that a row key satisfies.
* @param type The type of Row key used as a matching condition
*/
RowKeyPredicate::RowKeyPredicate(GSRowKeyPredicate *predicate, GSType type): mPredicate(predicate), mType(type),
timestamp_output_with_float(false){
}
/**
* @brief Destructor to free resource of RowKeyPredicate object
*/
RowKeyPredicate::~RowKeyPredicate() {
close();
}
/**
* @brief Release RowKeyPredicate resource
*/
void RowKeyPredicate::close() {
if (mPredicate != NULL) {
gsCloseRowKeyPredicate(&mPredicate);
mPredicate = NULL;
}
}
/**
* @brief Get key type
* @return The type of Row key used as a matching condition
*/
GSType RowKeyPredicate::get_key_type() {
return mType;
}
/**
* @brief Get the value of Row key at the start and end position of the range condition
* @param *startField The pointer to a variable to store the value of the Row key at the starting position
* @param *finishField The pointer to a variable to store the value of the Row key at the end position
*/
void RowKeyPredicate::get_range(Field* startField, Field* finishField) {
assert(startField != NULL);
assert(finishField != NULL);
startField->type = -1; // for all versions which do not support GS_TYPE_NULL
finishField->type = -1; // for all versions which do not support GS_TYPE_NULL
GSType key_type = get_key_type();
const GSValue *startKey = NULL;
const GSValue *endKey = NULL;
GSResult ret = gsGetPredicateStartKeyGeneral(mPredicate, &startKey);
if (!GS_SUCCEEDED(ret)) {
throw GSException(mPredicate, ret);
}
if (startKey != NULL) {
startField->type = key_type;
if (startField->type == GS_TYPE_STRING) {
if (startKey->asString) {
try {
Util::strdup(&(startField->value.asString), startKey->asString);
} catch (bad_alloc& ba) {
throw GSException(mPredicate, "Memory allocation error");
}
} else {
startField->value.asString = NULL;
}
} else {
startField->value = *startKey;
}
}
ret = gsGetPredicateFinishKeyGeneral(mPredicate, &endKey);
if (!GS_SUCCEEDED(ret)) {
if (startField->type == GS_TYPE_STRING && startField->value.asString) {
delete[] startField->value.asString;
}
throw GSException(mPredicate, ret);
}
if (endKey != NULL) {
finishField->type = key_type;
if (finishField->type == GS_TYPE_STRING) {
if (endKey->asString) {
try {
Util::strdup(&(finishField->value.asString), endKey->asString);
} catch (bad_alloc& ba) {
if (startField->type == GS_TYPE_STRING && startField->value.asString) {
delete[] startField->value.asString;
}
throw GSException(mPredicate, "Memory allocation error");
}
} else {
finishField->value.asString = NULL;
}
} else {
finishField->value = *endKey;
}
}
}
/**
* @brief Sets the value of Row key as the start and end position of the range conditions
* @param *startKey The pointer to a variable to store the value of the Row key at the starting position
* @param *finishKey The pointer to a variable to store the value of the Row key at the end position
*/
void RowKeyPredicate::set_range(Field* startKey, Field* finishKey) {
assert(startKey != NULL);
assert(finishKey != NULL);
GSType key_type = get_key_type();
GSResult ret;
switch (key_type) {
case GS_TYPE_LONG:
ret = gsSetPredicateStartKeyByLong(mPredicate, (int64_t*)&startKey->value.asLong);
if (!GS_SUCCEEDED(ret)) {
throw GSException(mPredicate, ret);
}
ret = gsSetPredicateFinishKeyByLong(mPredicate, (int64_t *) &finishKey->value.asLong);
if (!GS_SUCCEEDED(ret)) {
throw GSException(mPredicate, ret);
}
break;
case GS_TYPE_INTEGER:
ret = gsSetPredicateStartKeyByInteger(mPredicate, (const int32_t *) &startKey->value.asInteger);
if (!GS_SUCCEEDED(ret)) {
throw GSException(mPredicate, ret);
}
ret = gsSetPredicateFinishKeyByInteger(mPredicate, (const int32_t *)&finishKey->value.asInteger);
if (!GS_SUCCEEDED(ret)) {
throw GSException(mPredicate, ret);
}
break;
case GS_TYPE_STRING:
ret = gsSetPredicateStartKeyByString(mPredicate, startKey->value.asString);
if (!GS_SUCCEEDED(ret)) {
throw GSException(mPredicate, ret);
}
ret = gsSetPredicateFinishKeyByString(mPredicate, finishKey->value.asString);
if (!GS_SUCCEEDED(ret)) {
throw GSException(mPredicate, ret);
}
break;
case GS_TYPE_TIMESTAMP:
ret = gsSetPredicateStartKeyByTimestamp(mPredicate,
(const GSTimestamp *) &(startKey->value.asTimestamp));
if (!GS_SUCCEEDED(ret)) {
throw GSException(mPredicate, ret);
}
ret = gsSetPredicateFinishKeyByTimestamp(mPredicate,
(const GSTimestamp *) &(finishKey->value.asTimestamp));
if (!GS_SUCCEEDED(ret)) {
throw GSException(mPredicate, ret);
}
break;
default:
throw GSException(mPredicate, "Not support type");
break;
}
}
/**
* @brief Adds the value of Row key as one of the elements in the individual condition
* @param *keys The value of Row key to be added as one of the elements in the individual condition
* @param keyCount Number of distinct key
*/
void RowKeyPredicate::set_distinct_keys(const Field *keys, size_t keyCount) {
assert(keys != NULL);
GSType key_type = get_key_type();
GSResult ret;
for (size_t i = 0; i < keyCount; i++) {
const Field* key = keys + i;
assert(key != NULL);
switch (key_type) {
case GS_TYPE_LONG:
ret = gsAddPredicateKeyByLong(mPredicate, key->value.asLong);
if (!GS_SUCCEEDED(ret)) {
throw GSException(mPredicate, ret);
}
break;
case GS_TYPE_INTEGER:
ret = gsAddPredicateKeyByInteger(mPredicate,
key->value.asInteger);
if (!GS_SUCCEEDED(ret)) {
throw GSException(mPredicate, ret);
}
break;
case GS_TYPE_STRING:
ret = gsAddPredicateKeyByString(mPredicate, key->value.asString);
if (!GS_SUCCEEDED(ret)) {
throw GSException(mPredicate, ret);
}
break;
case GS_TYPE_TIMESTAMP:
ret = gsAddPredicateKeyByTimestamp(mPredicate,
key->value.asTimestamp);
if (!GS_SUCCEEDED(ret)) {
throw GSException(mPredicate, ret);
}
break;
default:
throw GSException(mPredicate, "Not support type");
break;
}
}
}
/**
* @brief Get a set of the values of the Row keys that configure the individual condition.
* @param **keys A pointer refers to list of Row key value
* @param *keyCount A pointer stores number of distinct key
*/
void RowKeyPredicate::get_distinct_keys(Field **keys, size_t* keyCount) {
assert(keys != NULL);
assert(keyCount != NULL);
size_t size;
GSType key_type = get_key_type();
GSValue * keyList;
GSResult ret = gsGetPredicateDistinctKeysGeneral(mPredicate, (const GSValue **)&keyList, &size);
if (!GS_SUCCEEDED(ret)) {
throw GSException(mPredicate, ret);
}
*keyCount = size;
Field* keyFields;
try {
keyFields = new Field[size](); //will be free in typemap out
for (int i = 0; i < size; i++) {
keyFields[i].type = key_type;
switch(key_type) {
case GS_TYPE_STRING:
if (keyList[i].asString) {
Util::strdup(&(keyFields[i].value.asString), keyList[i].asString);
} else {
keyFields[i].value.asString = NULL;
}
break;
default:
keyFields[i].value = keyList[i];
break;
}
}
*keys = keyFields;
} catch (bad_alloc& ba) {
if (keyFields) {
for (int i = 0; i < size; i++) {
if (keyFields[i].type == GS_TYPE_STRING && keyFields[i].value.asString) {
delete[] keyFields[i].value.asString;
}
}
delete[] keyFields;
}
throw GSException(mPredicate, "Memory allocation error");
}
}
/**
* @brief Get GSRowKeyPredicate data in RowKeyPredicate object
* @return A pointer stores GSRowKeyPredicate data in RowKeyPredicate object
*/
GSRowKeyPredicate* RowKeyPredicate::gs_ptr() {
return mPredicate;
}
} /* namespace griddb */