1 /*
2 * Copyright (c) 2023 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 OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_GENERAL_VALUE_H
17 #define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_GENERAL_VALUE_H
18 #include <functional>
19 #include <map>
20 #include <memory>
21 #include <string>
22 #include <variant>
23 #include <vector>
24
25 #include "error/general_error.h"
26 #include "traits.h"
27 namespace OHOS::DistributedData {
28 enum GenProgress {
29 SYNC_BEGIN,
30 SYNC_IN_PROGRESS,
31 SYNC_FINISH,
32 };
33
34 enum SyncTriggerMode {
35 MODE_DEFAULT = 0,
36 MODE_PUSH,
37 MODE_ONLINE,
38 MODE_UNLOCK,
39 MODE_BROADCASTER,
40 MODE_CONSISTENCY,
41 };
42
43 struct GenStatistic {
44 uint32_t total;
45 uint32_t success;
46 uint32_t failed;
47 uint32_t untreated;
48 };
49
50 struct GenTableDetail {
51 GenStatistic upload;
52 GenStatistic download;
53 };
54
55 struct GenProgressDetail {
56 int32_t progress;
57 int32_t code;
58 int32_t dbCode;
59 uint64_t changeCount = 0;
60 std::map<std::string, GenTableDetail> details;
61 };
62
63 struct Asset {
64 enum Status : int32_t {
65 STATUS_UNKNOWN,
66 STATUS_NORMAL,
67 STATUS_INSERT,
68 STATUS_UPDATE,
69 STATUS_DELETE,
70 STATUS_ABNORMAL,
71 STATUS_DOWNLOADING,
72 STATUS_BUTT
73 };
74
75 static constexpr uint64_t NO_EXPIRES_TIME = 0;
76 uint32_t version = 0;
77 uint32_t status = STATUS_UNKNOWN;
78 uint64_t expiresTime = NO_EXPIRES_TIME;
79 std::string id;
80 std::string name;
81 std::string uri;
82 std::string createTime;
83 std::string modifyTime;
84 std::string size;
85 std::string hash;
86 std::string path;
87 };
88
89 struct Reference {
90 std::string sourceTable;
91 std::string targetTable;
92 std::map<std::string, std::string> refFields;
93 };
94
95 struct SyncParam {
96 int32_t mode;
97 int32_t wait;
98 bool isCompensation = false;
99 int32_t triggerMode = MODE_DEFAULT;
100 std::string prepareTraceId;
101 int32_t user;
102 };
103
104 enum SyncStage : int8_t {
105 PREPARE = 0,
106 START,
107 END
108 };
109
110 struct ReportParam {
111 int32_t user = 0;
112 std::string bundleName;
113 std::string prepareTraceId;
114 SyncStage syncStage = SyncStage::PREPARE;
115 int32_t errCode = 0;
116 };
117
118 using Assets = std::vector<Asset>;
119 using Bytes = std::vector<uint8_t>;
120 using Relations = std::map<std::string, std::string>;
121 using Value = std::variant<std::monostate, int64_t, double, std::string, bool, Bytes, Asset, Assets, Relations>;
122 using Values = std::vector<Value>;
123 using VBucket = std::map<std::string, Value>;
124 using VBuckets = std::vector<VBucket>;
125 using GenDetails = std::map<std::string, GenProgressDetail>;
126 using GenAsync = std::function<void(const GenDetails &details)>;
127 template<typename T>
128 inline constexpr size_t TYPE_INDEX = Traits::variant_index_of_v<T, Value>;
129
130 inline constexpr size_t TYPE_MAX = Traits::variant_size_of_v<Value>;
131
132 enum class QueryOperation : uint32_t {
133 ILLEGAL = 0,
134 IN = 1,
135 OR = 0x101,
136 AND,
137 EQUAL_TO = 0x201,
138 BEGIN_GROUP = 0x301,
139 END_GROUP
140 };
141
142 struct QueryNode {
143 QueryOperation op = QueryOperation::ILLEGAL;
144 std::string fieldName;
145 std::vector<Value> fieldValue;
146 };
147 using QueryNodes = std::vector<QueryNode>;
148
149 struct GenQuery {
150 virtual ~GenQuery() = default;
151 virtual bool IsEqual(uint64_t tid) = 0;
152 virtual std::vector<std::string> GetTables() = 0;
SetQueryNodesGenQuery153 virtual void SetQueryNodes(const std::string& tableName, QueryNodes&& nodes) {};
GetQueryNodesGenQuery154 virtual QueryNodes GetQueryNodes(const std::string& tableName)
155 {
156 return {};
157 };
158
159 template<typename T>
QueryInterfaceGenQuery160 int32_t QueryInterface(T *&query)
161 {
162 if (!IsEqual(T::TYPE_ID)) {
163 return E_INVALID_ARGS;
164 }
165 query = static_cast<T *>(this);
166 return E_OK;
167 };
168 };
169
170 template<typename T, typename O>
GetItem(T && input,O & output)171 bool GetItem(T &&input, O &output)
172 {
173 return false;
174 }
175
176 template<typename T, typename O, typename First, typename... Rest>
GetItem(T && input,O & output)177 bool GetItem(T &&input, O &output)
178 {
179 auto val = Traits::get_if<First>(&input);
180 if (val != nullptr) {
181 output = std::move(*val);
182 return true;
183 }
184 return GetItem<T, O, Rest...>(std::move(input), output);
185 }
186
187 template<typename T, typename... Types>
Convert(T && input,std::variant<Types...> & output)188 bool Convert(T &&input, std::variant<Types...> &output)
189 {
190 return GetItem<T, decltype(output), Types...>(std::move(input), output);
191 }
192 } // namespace OHOS::DistributedData
193 #endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_GENERAL_VALUE_H
194