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     for (size_t i = 0; i < cloudGid.size(); i+= MAX_VALUE_SIZE) {
249         size_t end = std::min(i + MAX_VALUE_SIZE, cloudGid.size());
250         QueryObjNode operateNode;
251         operateNode.operFlag = QueryObjType::OR;
252         operateNode.type = QueryValueType::VALUE_TYPE_NULL;
253         queryObjNodes_.emplace_back(operateNode);
254 
255         QueryObjNode objNode;
256         objNode.operFlag = QueryObjType::IN;
257         objNode.fieldName = CloudDbConstant::GID_FIELD;
258         objNode.type = QueryValueType::VALUE_TYPE_STRING;
259         std::vector<std::string> subCloudGid(cloudGid.begin() + i, cloudGid.begin() + end);
260         for (const auto &gid : subCloudGid) {
261             if (gid.empty()) {
262                 continue;
263             }
264             FieldValue fieldValue;
265             fieldValue.stringValue = gid;
266             objNode.fieldValue.emplace_back(fieldValue);
267         }
268         queryObjNodes_.emplace_back(objNode);
269     }
270 }
271 
272 namespace {
DeSerializeVersion1Data(uint32_t version,Parcel & parcel,std::string & tableName,std::set<Key> & keys)273 int DeSerializeVersion1Data(uint32_t version, Parcel &parcel, std::string &tableName, std::set<Key> &keys)
274 {
275     if (version >= QUERY_SYNC_OBJECT_VERSION_1) {
276         uint32_t isTblNameExist = 0;
277         (void)parcel.ReadUInt32(isTblNameExist);
278         if (isTblNameExist) {
279             (void)parcel.ReadString(tableName);
280         }
281         uint32_t keysSize = 0;
282         (void)parcel.ReadUInt32(keysSize);
283         if (keysSize > DBConstant::MAX_INKEYS_SIZE) {
284             return -E_PARSE_FAIL;
285         }
286         for (uint32_t i = 0; i < keysSize; ++i) {
287             Key key;
288             (void)parcel.ReadVector(key);
289             keys.emplace(key);
290         }
291     }
292     return E_OK;
293 }
294 }
295 
DeSerializeData(Parcel & parcel,QuerySyncObject & queryObj)296 int QuerySyncObject::DeSerializeData(Parcel &parcel, QuerySyncObject &queryObj)
297 {
298     std::string magic;
299     (void)parcel.ReadString(magic);
300     if (magic != MAGIC) {
301         return -E_INVALID_ARGS;
302     }
303 
304     ObjContext context;
305     (void)parcel.ReadUInt32(context.version);
306     if (context.version > QUERY_SYNC_OBJECT_VERSION_CURRENT) {
307         LOGE("Parcel version and deserialize version not matched! ver=%u", context.version);
308         return -E_VERSION_NOT_SUPPORT;
309     }
310 
311     (void)parcel.ReadVectorChar(context.prefixKey);
312     (void)parcel.ReadString(context.suggestIndex);
313 
314     uint32_t nodesSize = 0;
315     (void)parcel.ReadUInt32(nodesSize);
316     parcel.EightByteAlign();
317     // Due to historical reasons, the limit of query node size was incorrectly set to MAX_QUERY_NODE_SIZE + 1
318     if (parcel.IsError() || nodesSize > MAX_QUERY_NODE_SIZE + 1) { // almost success
319         return -E_INVALID_ARGS;
320     }
321     for (size_t i = 0; i < nodesSize; i++) {
322         QueryObjNode node;
323         int errCode = DeSerializeDataObjNode(parcel, node);
324         if (errCode != E_OK) {
325             return errCode;
326         }
327         context.queryObjNodes.emplace_back(node);
328     }
329 
330     // QUERY_SYNC_OBJECT_VERSION_1 added.
331     std::string tableName;
332     std::set<Key> keys;
333     int errCode = DeSerializeVersion1Data(context.version, parcel, tableName, keys);
334     if (errCode != E_OK) {
335         return errCode;
336     }  // QUERY_SYNC_OBJECT_VERSION_1 end.
337 
338     if (parcel.IsError()) { // almost success
339         return -E_INVALID_ARGS;
340     }
341     queryObj = QuerySyncObject(context.queryObjNodes, context.prefixKey, keys);
342     if (!tableName.empty()) {
343         queryObj.SetTableName(tableName);
344     }
345     return E_OK;
346 }
347 
CalculateLen() const348 uint32_t QuerySyncObject::CalculateLen() const
349 {
350     uint64_t len = Parcel::GetStringLen(MAGIC);
351     len += Parcel::GetUInt32Len(); // version
352     len += Parcel::GetVectorCharLen(prefixKey_);
353     len += Parcel::GetStringLen(suggestIndex_);
354     len += Parcel::GetUInt32Len(); // nodes size
355     len = Parcel::GetEightByteAlign(len);
356     for (const QueryObjNode &node : queryObjNodes_) {
357         if (node.operFlag == QueryObjType::OPER_ILLEGAL) {
358             LOGE("contain illegal operator for query sync!");
359             return 0;
360         }
361         // operflag, fieldName, query value type, value size, union max size, string value
362         len += Parcel::GetUInt32Len();
363         len = Parcel::GetEightByteAlign(len);
364         len += Parcel::GetStringLen(node.fieldName) +
365             Parcel::GetIntLen() + Parcel::GetUInt32Len();
366         for (size_t i = 0; i < node.fieldValue.size(); i++) {
367             len += Parcel::GetInt64Len() + Parcel::GetStringLen(node.fieldValue[i].stringValue);
368         }
369     }
370 
371     // QUERY_SYNC_OBJECT_VERSION_1 added.
372     len += Parcel::GetUInt32Len(); // whether the table name exists.
373     if (isTableNameSpecified_) {
374         len += Parcel::GetStringLen(tableName_);
375     }
376     len += Parcel::GetUInt32Len(); // size of keys_
377     for (const auto &key : keys_) {
378         len += Parcel::GetVectorCharLen(key);
379     }  // QUERY_SYNC_OBJECT_VERSION_1 end.
380 
381     len = Parcel::GetEightByteAlign(len);
382     if (len > INT32_MAX) {
383         return 0;
384     }
385     return static_cast<uint32_t>(len);
386 }
387 
GetRelationTableName() const388 std::string QuerySyncObject::GetRelationTableName() const
389 {
390     if (!isTableNameSpecified_) {
391         return {};
392     }
393     return tableName_;
394 }
395 
GetRelationTableNames() const396 std::vector<std::string> QuerySyncObject::GetRelationTableNames() const
397 {
398     return tables_;
399 }
400 
GetValidStatus() const401 int QuerySyncObject::GetValidStatus() const
402 {
403     return validStatus;
404 }
405 
IsContainQueryNodes() const406 bool QuerySyncObject::IsContainQueryNodes() const
407 {
408     return !queryObjNodes_.empty();
409 }
410 
IsInValueOutOfLimit() const411 bool QuerySyncObject::IsInValueOutOfLimit() const
412 {
413     for (const auto &queryObjNode : queryObjNodes_) {
414         if ((queryObjNode.operFlag == QueryObjType::IN) &&
415             (queryObjNode.fieldValue.size() > DBConstant::MAX_IN_COUNT)) {
416             return false;
417         }
418     }
419     return true;
420 }
421 
GetQuerySyncObject(const DistributedDB::Query & query)422 std::vector<QuerySyncObject> QuerySyncObject::GetQuerySyncObject(const DistributedDB::Query &query)
423 {
424     std::vector<QuerySyncObject> res;
425     const auto &expressions = QueryObject::GetQueryExpressions(query);
426     for (const auto &item : expressions) {
427         res.push_back(QuerySyncObject(item));
428     }
429     return res;
430 }
431 
ParserQueryNodes(const Bytes & bytes,std::vector<QueryNode> & queryNodes)432 int QuerySyncObject::ParserQueryNodes(const Bytes &bytes, std::vector<QueryNode> &queryNodes)
433 {
434     QuerySyncObject tmp;
435     Bytes parcelBytes = bytes;
436     Parcel parcel(parcelBytes.data(), parcelBytes.size());
437     int errCode = DeSerializeData(parcel, tmp);
438     if (errCode != E_OK) {
439         return errCode;
440     }
441     for (const auto &objNode: tmp.queryObjNodes_) {
442         QueryNode node;
443         errCode = TransformToQueryNode(objNode, node);
444         if (errCode != E_OK) {
445             return errCode;
446         }
447         queryNodes.push_back(std::move(node));
448     }
449     return E_OK;
450 }
451 
TransformToQueryNode(const QueryObjNode & objNode,QueryNode & node)452 int QuerySyncObject::TransformToQueryNode(const QueryObjNode &objNode, QueryNode &node)
453 {
454     int errCode = TransformValueToType(objNode, node.fieldValue);
455     if (errCode != E_OK) {
456         LOGE("[Query] transform value to type failed %d", errCode);
457         return errCode;
458     }
459     node.fieldName = objNode.fieldName;
460     return TransformNodeType(objNode, node);
461 }
462 
TransformValueToType(const QueryObjNode & objNode,std::vector<Type> & types)463 int QuerySyncObject::TransformValueToType(const QueryObjNode &objNode, std::vector<Type> &types)
464 {
465     for (const auto &value: objNode.fieldValue) {
466         switch (objNode.type) {
467             case QueryValueType::VALUE_TYPE_STRING:
468                 types.emplace_back(value.stringValue);
469                 break;
470             case QueryValueType::VALUE_TYPE_BOOL:
471                 types.emplace_back(value.boolValue);
472                 break;
473             case QueryValueType::VALUE_TYPE_NULL:
474                 types.emplace_back(Nil());
475                 break;
476             case QueryValueType::VALUE_TYPE_INTEGER:
477             case QueryValueType::VALUE_TYPE_LONG:
478                 types.emplace_back(static_cast<int64_t>(value.integerValue));
479                 break;
480             case QueryValueType::VALUE_TYPE_DOUBLE:
481                 types.emplace_back(value.doubleValue);
482                 break;
483             case QueryValueType::VALUE_TYPE_INVALID:
484                 return -E_INVALID_ARGS;
485         }
486     }
487     return E_OK;
488 }
489 
TransformNodeType(const QueryObjNode & objNode,QueryNode & node)490 int QuerySyncObject::TransformNodeType(const QueryObjNode &objNode, QueryNode &node)
491 {
492     int errCode = E_OK;
493     switch (objNode.operFlag) {
494         case QueryObjType::IN:
495             node.type = QueryNodeType::IN;
496             break;
497         case QueryObjType::OR:
498             node.type = QueryNodeType::OR;
499             break;
500         case QueryObjType::AND:
501             node.type = QueryNodeType::AND;
502             break;
503         case QueryObjType::EQUALTO:
504             node.type = QueryNodeType::EQUAL_TO;
505             break;
506         case QueryObjType::BEGIN_GROUP:
507             node.type = QueryNodeType::BEGIN_GROUP;
508             break;
509         case QueryObjType::END_GROUP:
510             node.type = QueryNodeType::END_GROUP;
511             break;
512         case QueryObjType::IN_KEYS:
513             node.fieldName = CloudDbConstant::CLOUD_KV_FIELD_KEY;
514             node.type = QueryNodeType::IN;
515             break;
516         default:
517             LOGE("[Query] not support type %d", static_cast<int>(objNode.operFlag));
518             errCode = -E_NOT_SUPPORT;
519             node.type = QueryNodeType::ILLEGAL;
520     }
521     return errCode;
522 }
523 } // namespace DistributedDB