• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 CLOUD_STORE_TYPE_H
17 #define CLOUD_STORE_TYPE_H
18 
19 #include <cstdint>
20 #include <functional>
21 #include <map>
22 #include <variant>
23 #include <string>
24 
25 #include "query.h"
26 #include "store_types.h"
27 
28 namespace DistributedDB {
29 enum TableSyncType {
30     DEVICE_COOPERATION = 0,
31     CLOUD_COOPERATION = 1,
32 };
33 
34 enum ClearMode {
35     DEFAULT = 0,        // use for device to device sync
36     FLAG_AND_DATA = 1,  // use for device to cloud sync
37     FLAG_ONLY = 2,
38     CLEAR_SHARED_TABLE = 3,
39     BUTT = 4,
40 };
41 
42 enum class ClearMetaDataMode : uint64_t {
43     CLOUD_WATERMARK = 0x01,  // clear watermark of device to cloud sync
44     BUTT,
45 };
46 
47 struct ClearMetaDataOption {
48     ClearMetaDataMode mode = ClearMetaDataMode::CLOUD_WATERMARK;
49     std::set<std::string> tableNameList;  // an empty set means clearing meta data on all tables
50 };
51 
52 enum class ClearKvMetaOpType : uint64_t {
53     CLEAN_CLOUD_WATERMARK = 0x01,  // clear watermark of device to cloud sync
54 };
55 
56 struct ClearKvMetaDataOption {
57     ClearKvMetaOpType type = ClearKvMetaOpType::CLEAN_CLOUD_WATERMARK;
58 };
59 
60 enum class AssetOpType {
61     NO_CHANGE = 0,
62     INSERT,
63     DELETE,
64     UPDATE
65 };
66 
67 enum AssetStatus : uint32_t {
68     NORMAL = 0,
69     DOWNLOADING,
70     ABNORMAL,
71     INSERT, // INSERT/DELETE/UPDATE are for client use
72     DELETE,
73     UPDATE,
74     // high 16 bit USE WITH BIT MASK
75     HIDDEN = 0x20000000,
76     DOWNLOAD_WITH_NULL = 0x40000000,
77     UPLOADING = 0x80000000,
78 };
79 
80 struct Asset {
81     uint32_t version = 0;
82     std::string name;
83     std::string assetId;
84     std::string subpath;
85     std::string uri;
86     std::string modifyTime;
87     std::string createTime;
88     std::string size;
89     std::string hash;
90     uint32_t flag = static_cast<uint32_t>(AssetOpType::NO_CHANGE);
91     uint32_t status = static_cast<uint32_t>(AssetStatus::NORMAL);
92     int64_t timestamp = 0;
93     bool operator==(const Asset& asset) const
94     {
95         if (this == &asset) {
96             return true;
97         }
98         // force check all field
99         return (version == asset.version) && (name == asset.name) && (assetId == asset.assetId) &&
100             (subpath == asset.subpath) && (uri == asset.uri) && (modifyTime == asset.modifyTime) &&
101             (createTime == asset.createTime) && (size == asset.size) && (hash == asset.hash) && (flag == asset.flag) &&
102             (status == asset.status) && (timestamp == asset.timestamp);
103     }
104 };
105 using Nil = std::monostate;
106 using Assets = std::vector<Asset>;
107 using Bytes = std::vector<uint8_t>;
108 using Entries = std::map<std::string, std::string>;
109 using Type = std::variant<Nil, int64_t, double, std::string, bool, Bytes, Asset, Assets, Entries>;
110 using VBucket = std::map<std::string, Type>;
111 using GenerateCloudVersionCallback = std::function<std::string(const std::string &originVersion)>;
112 
113 struct Field {
114     std::string colName;
115     int32_t type; // get value from TYPE_INDEX;
116     bool primary = false;
117     bool nullable = true;
118     bool operator==(const Field &comparedField) const
119     {
120         return (colName == comparedField.colName) && (type == comparedField.type) &&
121             (primary == comparedField.primary) && (nullable == comparedField.nullable);
122     }
123 };
124 
125 struct TableSchema {
126     std::string name;
127     std::string sharedTableName; // if table is shared table, its sharedtablename is ""
128     std::vector<Field> fields;
129 };
130 
131 struct DataBaseSchema {
132     std::vector<TableSchema> tables;
133 };
134 
135 enum class CloudQueryType : int64_t {
136     FULL_TABLE = 0, // query full table
137     QUERY_FIELD = 1 // query with some fields
138 };
139 
140 enum class LockAction : uint32_t {
141     NONE = 0,
142     INSERT = 0x1,
143     UPDATE = 0x2,
144     DELETE = 0x4,
145     DOWNLOAD = 0x8
146 };
147 
148 enum CloudSyncState {
149     IDLE = 0,
150     DO_DOWNLOAD,
151     DO_UPLOAD,
152     DO_REPEAT_CHECK,
153     DO_FINISHED
154 };
155 
156 enum CloudSyncEvent {
157     UPLOAD_FINISHED_EVENT,
158     DOWNLOAD_FINISHED_EVENT,
159     ERROR_EVENT,
160     REPEAT_CHECK_EVENT,
161     REPEAT_DOWNLOAD_EVENT,
162     START_SYNC_EVENT,
163     ALL_TASK_FINISHED_EVENT
164 };
165 
166 struct CloudSyncOption {
167     std::vector<std::string> devices;
168     SyncMode mode = SyncMode::SYNC_MODE_CLOUD_MERGE;
169     Query query;
170     int64_t waitTime = 0;
171     bool priorityTask = false;
172     int32_t priorityLevel = 0; // [0, 2]
173     bool compensatedSyncOnly = false;
174     std::vector<std::string> users;
175     bool merge = false;
176     // default, upload insert need lock
177     LockAction lockAction = LockAction::INSERT;
178     std::string prepareTraceId;
179     bool asyncDownloadAssets = false;
180 };
181 
182 enum class QueryNodeType : uint32_t {
183     ILLEGAL = 0,
184     IN = 1,
185     OR = 0x101,
186     AND,
187     EQUAL_TO = 0x201,
188     BEGIN_GROUP = 0x301,
189     END_GROUP
190 };
191 
192 struct QueryNode {
193     QueryNodeType type = QueryNodeType::ILLEGAL;
194     std::string fieldName;
195     std::vector<Type> fieldValue;
196 };
197 
198 struct SqlCondition {
199     std::string sql;  // The sql statement;
200     std::vector<Type> bindArgs;  // The bind args.
201     bool readOnly = false;
202 };
203 
204 enum class RecordStatus {
205     WAIT_COMPENSATED_SYNC,
206     NORMAL
207 };
208 
209 enum class LockStatus : uint32_t {
210     UNLOCK = 0,
211     UNLOCKING,
212     LOCK,
213     LOCK_CHANGE,
214     BUTT,
215 };
216 
217 struct CloudSyncConfig {
218     int32_t maxUploadCount = 30;             // default max upload 30 records
219     int32_t maxUploadSize  = 1024 * 512 * 3; // default max upload 1024 * 512 * 3 = 1.5m
220     int32_t maxRetryConflictTimes = -1;      // default max retry -1 is unlimited retry times
221     bool isSupportEncrypt = false;           // default encryption is not supported
222 };
223 
224 struct AsyncDownloadAssetsConfig {
225     uint32_t maxDownloadTask = 1; // valid range in [1, 12] max async download task in process
226     uint32_t maxDownloadAssetsCount = 100; // valid range in [1, 2000] max async download assets count in one batch
227 };
228 } // namespace DistributedDB
229 #endif // CLOUD_STORE_TYPE_H