1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "query_sync_object.h"
17
18 #include "cloud/cloud_db_constant.h"
19 #include "db_common.h"
20 #include "db_errno.h"
21 #include "log_print.h"
22 #include "version.h"
23
24 namespace DistributedDB {
25 namespace {
26 const std::string MAGIC = "remote query";
27 // Max value size of each QueryObjNode, current is In & NotIn predicate which is 128
28 const int MAX_VALUE_SIZE = 128;
29 const int MAX_QUERY_NODE_SIZE = 256;
30
SerializeDataObjNode(Parcel & parcel,const QueryObjNode & objNode)31 int SerializeDataObjNode(Parcel &parcel, const QueryObjNode &objNode)
32 {
33 if (objNode.operFlag == QueryObjType::OPER_ILLEGAL) {
34 return -E_INVALID_QUERY_FORMAT;
35 }
36 (void)parcel.WriteUInt32(static_cast<uint32_t>(objNode.operFlag));
37 parcel.EightByteAlign();
38 (void)parcel.WriteString(objNode.fieldName);
39 (void)parcel.WriteInt(static_cast<int32_t>(objNode.type));
40 (void)parcel.WriteUInt32(objNode.fieldValue.size());
41
42 for (const FieldValue &value : objNode.fieldValue) {
43 (void)parcel.WriteString(value.stringValue);
44
45 // string may not closely arranged continuously
46 // longValue is maximum length in union
47 (void)parcel.WriteInt64(value.longValue);
48 }
49 if (parcel.IsError()) {
50 return -E_INVALID_ARGS;
51 }
52 return E_OK;
53 }
54
DeSerializeDataObjNode(Parcel & parcel,QueryObjNode & objNode)55 int DeSerializeDataObjNode(Parcel &parcel, QueryObjNode &objNode)
56 {
57 uint32_t readOperFlag = 0;
58 (void)parcel.ReadUInt32(readOperFlag);
59 objNode.operFlag = static_cast<QueryObjType>(readOperFlag);
60 parcel.EightByteAlign();
61
62 (void)parcel.ReadString(objNode.fieldName);
63
64 int readInt = -1;
65 (void)parcel.ReadInt(readInt);
66 objNode.type = static_cast<QueryValueType>(readInt);
67
68 uint32_t valueSize = 0;
69 (void)parcel.ReadUInt32(valueSize);
70 if (parcel.IsError() || valueSize > MAX_VALUE_SIZE) {
71 return -E_INVALID_ARGS;
72 }
73
74 for (size_t i = 0; i < valueSize; i++) {
75 FieldValue value;
76 (void)parcel.ReadString(value.stringValue);
77
78 (void)parcel.ReadInt64(value.longValue);
79 if (parcel.IsError()) {
80 return -E_INVALID_ARGS;
81 }
82 objNode.fieldValue.push_back(value);
83 }
84 return E_OK;
85 }
86 }
87
QuerySyncObject()88 QuerySyncObject::QuerySyncObject()
89 {}
90
QuerySyncObject(const std::list<QueryObjNode> & queryObjNodes,const std::vector<uint8_t> & prefixKey,const std::set<Key> & keys)91 QuerySyncObject::QuerySyncObject(const std::list<QueryObjNode> &queryObjNodes, const std::vector<uint8_t> &prefixKey,
92 const std::set<Key> &keys)
93 : QueryObject(queryObjNodes, prefixKey, keys)
94 {}
95
QuerySyncObject(const Query & query)96 QuerySyncObject::QuerySyncObject(const Query &query)
97 : QueryObject(query)
98 {}
99
QuerySyncObject(const DistributedDB::QueryExpression & expression)100 QuerySyncObject::QuerySyncObject(const DistributedDB::QueryExpression &expression)
101 : QueryObject(expression)
102 {}
103
~QuerySyncObject()104 QuerySyncObject::~QuerySyncObject()
105 {}
106
GetVersion() const107 uint32_t QuerySyncObject::GetVersion() const
108 {
109 uint32_t version = QUERY_SYNC_OBJECT_VERSION_0;
110 if (isTableNameSpecified_ || !keys_.empty()) {
111 version = QUERY_SYNC_OBJECT_VERSION_1;
112 }
113 return version;
114 }
115
GetObjContext(ObjContext & objContext) const116 int QuerySyncObject::GetObjContext(ObjContext &objContext) const
117 {
118 if (!isValid_) {
119 return -E_INVALID_QUERY_FORMAT;
120 }
121 objContext.version = GetVersion();
122 objContext.prefixKey.assign(prefixKey_.begin(), prefixKey_.end());
123 objContext.suggestIndex = suggestIndex_;
124 objContext.queryObjNodes = queryObjNodes_;
125 return E_OK;
126 }
127
CalculateIdentifyLen() const128 uint32_t QuerySyncObject::CalculateIdentifyLen() const
129 {
130 uint64_t len = Parcel::GetVectorCharLen(prefixKey_);
131 for (const QueryObjNode &node : queryObjNodes_) {
132 if (node.operFlag == QueryObjType::LIMIT || node.operFlag == QueryObjType::ORDERBY ||
133 node.operFlag == QueryObjType::SUGGEST_INDEX) {
134 continue;
135 }
136 // operFlag and valueType is int
137 len += Parcel::GetUInt32Len() + Parcel::GetIntLen() + Parcel::GetStringLen(node.fieldName);
138 for (const FieldValue &value : node.fieldValue) {
139 len += Parcel::GetStringLen(value.stringValue) + Parcel::GetInt64Len();
140 }
141 }
142
143 // QUERY_SYNC_OBJECT_VERSION_1 added.
144 len += isTableNameSpecified_ ? Parcel::GetStringLen(tableName_) : 0;
145 for (const auto &key : keys_) {
146 len += Parcel::GetVectorCharLen(key);
147 } // QUERY_SYNC_OBJECT_VERSION_1 end.
148 return len;
149 }
150
GetIdentify() const151 std::string QuerySyncObject::GetIdentify() const
152 {
153 if (!isValid_) {
154 return std::string();
155 }
156 if (!identify_.empty()) {
157 return identify_;
158 }
159 // suggestionIndex is local attribute, do not need to be propagated to remote
160 uint64_t len = CalculateIdentifyLen();
161 std::vector<uint8_t> buff(len, 0); // It will affect the hash result, the default value cannot be modified
162 Parcel parcel(buff.data(), len);
163
164 // The order needs to be consistent, otherwise it will affect the hash result
165 (void)parcel.WriteVectorChar(prefixKey_);
166 for (const QueryObjNode &node : queryObjNodes_) {
167 if (node.operFlag == QueryObjType::LIMIT || node.operFlag == QueryObjType::ORDERBY ||
168 node.operFlag == QueryObjType::SUGGEST_INDEX) {
169 continue;
170 }
171 (void)parcel.WriteUInt32(static_cast<uint32_t>(node.operFlag));
172 (void)parcel.WriteInt(static_cast<int32_t>(node.type));
173 (void)parcel.WriteString(node.fieldName);
174 for (const FieldValue &value : node.fieldValue) {
175 (void)parcel.WriteInt64(value.longValue);
176 (void)parcel.WriteString(value.stringValue);
177 }
178 }
179
180 // QUERY_SYNC_OBJECT_VERSION_1 added.
181 if (isTableNameSpecified_) {
182 (void)parcel.WriteString(tableName_);
183 }
184 for (const auto &key : keys_) {
185 (void)parcel.WriteVectorChar(key);
186 } // QUERY_SYNC_OBJECT_VERSION_1 end.
187
188 std::vector<uint8_t> hashBuff;
189 if (parcel.IsError() || DBCommon::CalcValueHash(buff, hashBuff) != E_OK) {
190 return std::string();
191 }
192 identify_ = DBCommon::VectorToHexString(hashBuff);
193 return identify_;
194 }
195
CalculateParcelLen(uint32_t softWareVersion) const196 uint32_t QuerySyncObject::CalculateParcelLen(uint32_t softWareVersion) const
197 {
198 if (softWareVersion == SOFTWARE_VERSION_CURRENT) {
199 return CalculateLen();
200 }
201 LOGE("current not support!");
202 return 0;
203 }
204
SerializeData(Parcel & parcel,uint32_t softWareVersion)205 int QuerySyncObject::SerializeData(Parcel &parcel, uint32_t softWareVersion)
206 {
207 ObjContext context;
208 int errCode = GetObjContext(context);
209 if (errCode != E_OK) {
210 return errCode;
211 }
212 (void)parcel.WriteString(MAGIC);
213 (void)parcel.WriteUInt32(context.version);
214 (void)parcel.WriteVectorChar(context.prefixKey);
215 (void)parcel.WriteString(context.suggestIndex);
216 (void)parcel.WriteUInt32(context.queryObjNodes.size());
217 parcel.EightByteAlign();
218 if (parcel.IsError()) {
219 return -E_INVALID_ARGS;
220 }
221 for (const QueryObjNode &node : context.queryObjNodes) {
222 errCode = SerializeDataObjNode(parcel, node);
223 if (errCode != E_OK) {
224 return errCode;
225 }
226 }
227
228 // QUERY_SYNC_OBJECT_VERSION_1 added.
229 if (context.version >= QUERY_SYNC_OBJECT_VERSION_1) {
230 (void)parcel.WriteUInt32(static_cast<uint32_t>(isTableNameSpecified_));
231 if (isTableNameSpecified_) {
232 (void)parcel.WriteString(tableName_);
233 }
234 (void)parcel.WriteUInt32(keys_.size());
235 for (const auto &key : keys_) {
236 (void)parcel.WriteVectorChar(key);
237 }
238 } // QUERY_SYNC_OBJECT_VERSION_1 end.
239 parcel.EightByteAlign();
240 if (parcel.IsError()) { // parcel almost success
241 return -E_INVALID_ARGS;
242 }
243 return E_OK;
244 }
245
SetCloudGid(const std::vector<std::string> & cloudGid)246 void QuerySyncObject::SetCloudGid(const std::vector<std::string> &cloudGid)
247 {
248 QueryObjNode objNode;
249 objNode.operFlag = QueryObjType::OR;
250 objNode.type = QueryValueType::VALUE_TYPE_NULL;
251 queryObjNodes_.push_back(objNode);
252 objNode.operFlag = QueryObjType::IN;
253 objNode.fieldName = CloudDbConstant::GID_FIELD;
254 objNode.type = QueryValueType::VALUE_TYPE_STRING;
255 for (const auto &gid : cloudGid) {
256 FieldValue fieldValue;
257 fieldValue.stringValue = gid;
258 objNode.fieldValue.emplace_back(fieldValue);
259 }
260 queryObjNodes_.emplace_back(objNode);
261 }
262
263 namespace {
DeSerializeVersion1Data(uint32_t version,Parcel & parcel,std::string & tableName,std::set<Key> & keys)264 int DeSerializeVersion1Data(uint32_t version, Parcel &parcel, std::string &tableName, std::set<Key> &keys)
265 {
266 if (version >= QUERY_SYNC_OBJECT_VERSION_1) {
267 uint32_t isTblNameExist = 0;
268 (void)parcel.ReadUInt32(isTblNameExist);
269 if (isTblNameExist) {
270 (void)parcel.ReadString(tableName);
271 }
272 uint32_t keysSize = 0;
273 (void)parcel.ReadUInt32(keysSize);
274 if (keysSize > DBConstant::MAX_INKEYS_SIZE) {
275 return -E_PARSE_FAIL;
276 }
277 for (uint32_t i = 0; i < keysSize; ++i) {
278 Key key;
279 (void)parcel.ReadVector(key);
280 keys.emplace(key);
281 }
282 }
283 return E_OK;
284 }
285 }
286
DeSerializeData(Parcel & parcel,QuerySyncObject & queryObj)287 int QuerySyncObject::DeSerializeData(Parcel &parcel, QuerySyncObject &queryObj)
288 {
289 std::string magic;
290 (void)parcel.ReadString(magic);
291 if (magic != MAGIC) {
292 return -E_INVALID_ARGS;
293 }
294
295 ObjContext context;
296 (void)parcel.ReadUInt32(context.version);
297 if (context.version > QUERY_SYNC_OBJECT_VERSION_CURRENT) {
298 LOGE("Parcel version and deserialize version not matched! ver=%u", context.version);
299 return -E_VERSION_NOT_SUPPORT;
300 }
301
302 (void)parcel.ReadVectorChar(context.prefixKey);
303 (void)parcel.ReadString(context.suggestIndex);
304
305 uint32_t nodesSize = 0;
306 (void)parcel.ReadUInt32(nodesSize);
307 parcel.EightByteAlign();
308 // Due to historical reasons, the limit of query node size was incorrectly set to MAX_QUERY_NODE_SIZE + 1
309 if (parcel.IsError() || nodesSize > MAX_QUERY_NODE_SIZE + 1) { // almost success
310 return -E_INVALID_ARGS;
311 }
312 for (size_t i = 0; i < nodesSize; i++) {
313 QueryObjNode node;
314 int errCode = DeSerializeDataObjNode(parcel, node);
315 if (errCode != E_OK) {
316 return errCode;
317 }
318 context.queryObjNodes.emplace_back(node);
319 }
320
321 // QUERY_SYNC_OBJECT_VERSION_1 added.
322 std::string tableName;
323 std::set<Key> keys;
324 int errCode = DeSerializeVersion1Data(context.version, parcel, tableName, keys);
325 if (errCode != E_OK) {
326 return errCode;
327 } // QUERY_SYNC_OBJECT_VERSION_1 end.
328
329 if (parcel.IsError()) { // almost success
330 return -E_INVALID_ARGS;
331 }
332 queryObj = QuerySyncObject(context.queryObjNodes, context.prefixKey, keys);
333 if (!tableName.empty()) {
334 queryObj.SetTableName(tableName);
335 }
336 return E_OK;
337 }
338
CalculateLen() const339 uint32_t QuerySyncObject::CalculateLen() const
340 {
341 uint64_t len = Parcel::GetStringLen(MAGIC);
342 len += Parcel::GetUInt32Len(); // version
343 len += Parcel::GetVectorCharLen(prefixKey_);
344 len += Parcel::GetStringLen(suggestIndex_);
345 len += Parcel::GetUInt32Len(); // nodes size
346 len = Parcel::GetEightByteAlign(len);
347 for (const QueryObjNode &node : queryObjNodes_) {
348 if (node.operFlag == QueryObjType::OPER_ILLEGAL) {
349 LOGE("contain illegal operator for query sync!");
350 return 0;
351 }
352 // operflag, fieldName, query value type, value size, union max size, string value
353 len += Parcel::GetUInt32Len();
354 len = Parcel::GetEightByteAlign(len);
355 len += Parcel::GetStringLen(node.fieldName) +
356 Parcel::GetIntLen() + Parcel::GetUInt32Len();
357 for (size_t i = 0; i < node.fieldValue.size(); i++) {
358 len += Parcel::GetInt64Len() + Parcel::GetStringLen(node.fieldValue[i].stringValue);
359 }
360 }
361
362 // QUERY_SYNC_OBJECT_VERSION_1 added.
363 len += Parcel::GetUInt32Len(); // whether the table name exists.
364 if (isTableNameSpecified_) {
365 len += Parcel::GetStringLen(tableName_);
366 }
367 len += Parcel::GetUInt32Len(); // size of keys_
368 for (const auto &key : keys_) {
369 len += Parcel::GetVectorCharLen(key);
370 } // QUERY_SYNC_OBJECT_VERSION_1 end.
371
372 len = Parcel::GetEightByteAlign(len);
373 if (len > INT32_MAX) {
374 return 0;
375 }
376 return static_cast<uint32_t>(len);
377 }
378
GetRelationTableName() const379 std::string QuerySyncObject::GetRelationTableName() const
380 {
381 if (!isTableNameSpecified_) {
382 return {};
383 }
384 return tableName_;
385 }
386
GetRelationTableNames() const387 std::vector<std::string> QuerySyncObject::GetRelationTableNames() const
388 {
389 return tables_;
390 }
391
GetValidStatus() const392 int QuerySyncObject::GetValidStatus() const
393 {
394 return validStatus;
395 }
396
IsContainQueryNodes() const397 bool QuerySyncObject::IsContainQueryNodes() const
398 {
399 return !queryObjNodes_.empty();
400 }
401
IsInValueOutOfLimit() const402 bool QuerySyncObject::IsInValueOutOfLimit() const
403 {
404 for (const auto &queryObjNode : queryObjNodes_) {
405 if ((queryObjNode.operFlag == QueryObjType::IN) &&
406 (queryObjNode.fieldValue.size() > DBConstant::MAX_IN_COUNT)) {
407 return false;
408 }
409 }
410 return true;
411 }
412
GetQuerySyncObject(const DistributedDB::Query & query)413 std::vector<QuerySyncObject> QuerySyncObject::GetQuerySyncObject(const DistributedDB::Query &query)
414 {
415 std::vector<QuerySyncObject> res;
416 const auto &expressions = QueryObject::GetQueryExpressions(query);
417 for (const auto &item : expressions) {
418 res.push_back(QuerySyncObject(item));
419 }
420 return res;
421 }
422
ParserQueryNodes(const Bytes & bytes,std::vector<QueryNode> & queryNodes)423 int QuerySyncObject::ParserQueryNodes(const Bytes &bytes, std::vector<QueryNode> &queryNodes)
424 {
425 QuerySyncObject tmp;
426 Bytes parcelBytes = bytes;
427 Parcel parcel(parcelBytes.data(), parcelBytes.size());
428 int errCode = DeSerializeData(parcel, tmp);
429 if (errCode != E_OK) {
430 return errCode;
431 }
432 for (const auto &objNode: tmp.queryObjNodes_) {
433 QueryNode node;
434 errCode = TransformToQueryNode(objNode, node);
435 if (errCode != E_OK) {
436 return errCode;
437 }
438 queryNodes.push_back(std::move(node));
439 }
440 return E_OK;
441 }
442
TransformToQueryNode(const QueryObjNode & objNode,QueryNode & node)443 int QuerySyncObject::TransformToQueryNode(const QueryObjNode &objNode, QueryNode &node)
444 {
445 int errCode = TransformValueToType(objNode, node.fieldValue);
446 if (errCode != E_OK) {
447 LOGE("[Query] transform value to type failed %d", errCode);
448 return errCode;
449 }
450 node.fieldName = objNode.fieldName;
451 return TransformNodeType(objNode, node);
452 }
453
TransformValueToType(const QueryObjNode & objNode,std::vector<Type> & types)454 int QuerySyncObject::TransformValueToType(const QueryObjNode &objNode, std::vector<Type> &types)
455 {
456 for (const auto &value: objNode.fieldValue) {
457 switch (objNode.type) {
458 case QueryValueType::VALUE_TYPE_STRING:
459 types.emplace_back(value.stringValue);
460 break;
461 case QueryValueType::VALUE_TYPE_BOOL:
462 types.emplace_back(value.boolValue);
463 break;
464 case QueryValueType::VALUE_TYPE_NULL:
465 types.emplace_back(Nil());
466 break;
467 case QueryValueType::VALUE_TYPE_INTEGER:
468 case QueryValueType::VALUE_TYPE_LONG:
469 types.emplace_back(static_cast<int64_t>(value.integerValue));
470 break;
471 case QueryValueType::VALUE_TYPE_DOUBLE:
472 types.emplace_back(value.doubleValue);
473 break;
474 case QueryValueType::VALUE_TYPE_INVALID:
475 return -E_INVALID_ARGS;
476 }
477 }
478 return E_OK;
479 }
480
TransformNodeType(const QueryObjNode & objNode,QueryNode & node)481 int QuerySyncObject::TransformNodeType(const QueryObjNode &objNode, QueryNode &node)
482 {
483 int errCode = E_OK;
484 switch (objNode.operFlag) {
485 case QueryObjType::IN:
486 node.type = QueryNodeType::IN;
487 break;
488 case QueryObjType::OR:
489 node.type = QueryNodeType::OR;
490 break;
491 case QueryObjType::AND:
492 node.type = QueryNodeType::AND;
493 break;
494 case QueryObjType::EQUALTO:
495 node.type = QueryNodeType::EQUAL_TO;
496 break;
497 case QueryObjType::BEGIN_GROUP:
498 node.type = QueryNodeType::BEGIN_GROUP;
499 break;
500 case QueryObjType::END_GROUP:
501 node.type = QueryNodeType::END_GROUP;
502 break;
503 case QueryObjType::IN_KEYS:
504 node.fieldName = CloudDbConstant::CLOUD_KV_FIELD_KEY;
505 node.type = QueryNodeType::IN;
506 break;
507 default:
508 LOGE("[Query] not support type %d", static_cast<int>(objNode.operFlag));
509 errCode = -E_NOT_SUPPORT;
510 node.type = QueryNodeType::ILLEGAL;
511 }
512 return errCode;
513 }
514 } // namespace DistributedDB