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 #ifndef QUERY_SYNC_OBJECT_H
17 #define QUERY_SYNC_OBJECT_H
18 
19 #include <string>
20 
21 #include "parcel.h"
22 #include "query_object.h"
23 
24 namespace DistributedDB {
25 const uint32_t QUERY_SYNC_OBJECT_VERSION_0 = 0;
26 const uint32_t QUERY_SYNC_OBJECT_VERSION_1 = 1; // Add tableName_ and keys_.
27 const uint32_t QUERY_SYNC_OBJECT_VERSION_CURRENT = QUERY_SYNC_OBJECT_VERSION_1; // always point the last.
28 
29 struct ObjContext {
30     uint32_t version = QUERY_SYNC_OBJECT_VERSION_0; // serialized struct version
31     std::vector<uint8_t> prefixKey{};
32     std::string suggestIndex{};
33     std::list<QueryObjNode> queryObjNodes{};
34     std::vector<Key> keys{};
35 };
36 
37 class QuerySyncObject : public QueryObject {
38 public:
39     QuerySyncObject();
40     QuerySyncObject(const std::list<QueryObjNode> &queryObjNodes, const std::vector<uint8_t> &prefixKey,
41         const std::set<Key> &keys);
42     explicit QuerySyncObject(const Query &query);
43     ~QuerySyncObject() override;
44 
45     std::string GetIdentify() const;
46 
47     int SerializeData(Parcel &parcel, uint32_t softWareVersion);
48     void SetCloudGid(const std::vector<std::string> &cloudGid);
49     // should call Parcel.IsError() to Get result.
50     static int DeSerializeData(Parcel &parcel, QuerySyncObject &queryObj);
51     uint32_t CalculateParcelLen(uint32_t softWareVersion) const;
52 
53     std::string GetRelationTableName() const;
54 
55     std::vector<std::string> GetRelationTableNames() const;
56 
57     int GetValidStatus() const;
58 
59     bool IsContainQueryNodes() const;
60 
61     bool IsInValueOutOfLimit() const;
62 
63     static std::vector<QuerySyncObject> GetQuerySyncObject(const Query &query);
64 
65     static int ParserQueryNodes(const Bytes &bytes, std::vector<QueryNode> &queryNodes);
66 private:
67     explicit QuerySyncObject(const QueryExpression &expression);
68     uint32_t CalculateLen() const;
69     uint32_t CalculateIdentifyLen() const;
70     int GetObjContext(ObjContext &objContext) const;
71     uint32_t GetVersion() const;
72 
73     static int TransformToQueryNode(const QueryObjNode &objNode, QueryNode &node);
74 
75     static int TransformValueToType(const QueryObjNode &objNode, std::vector<Type> &types);
76 
77     static int TransformNodeType(const QueryObjNode &objNode, QueryNode &node);
78 
79     mutable std::string identify_;
80 };
81 }
82 #endif