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 AssetOpType {
43     NO_CHANGE = 0,
44     INSERT,
45     DELETE,
46     UPDATE
47 };
48 
49 enum AssetStatus : uint32_t {
50     NORMAL = 0,
51     DOWNLOADING,
52     ABNORMAL,
53     INSERT, // INSERT/DELETE/UPDATE are for client use
54     DELETE,
55     UPDATE,
56     // high 16 bit USE WITH BIT MASK
57     HIDDEN = 0x20000000,
58     DOWNLOAD_WITH_NULL = 0x40000000,
59     UPLOADING = 0x80000000,
60 };
61 
62 struct Asset {
63     uint32_t version = 0;
64     std::string name;
65     std::string assetId;
66     std::string subpath;
67     std::string uri;
68     std::string modifyTime;
69     std::string createTime;
70     std::string size;
71     std::string hash;
72     uint32_t flag = static_cast<uint32_t>(AssetOpType::NO_CHANGE);
73     uint32_t status = static_cast<uint32_t>(AssetStatus::NORMAL);
74     int64_t timestamp = 0;
75     bool operator==(const Asset& asset) const
76     {
77         if (this == &asset) {
78             return true;
79         }
80         // force check all field
81         return (version == asset.version) && (name == asset.name) && (assetId == asset.assetId) &&
82             (subpath == asset.subpath) && (uri == asset.uri) && (modifyTime == asset.modifyTime) &&
83             (createTime == asset.createTime) && (size == asset.size) && (hash == asset.hash) && (flag == asset.flag) &&
84             (status == asset.status) && (timestamp == asset.timestamp);
85     }
86 };
87 using Nil = std::monostate;
88 using Assets = std::vector<Asset>;
89 using Bytes = std::vector<uint8_t>;
90 using Entries = std::map<std::string, std::string>;
91 using Type = std::variant<Nil, int64_t, double, std::string, bool, Bytes, Asset, Assets, Entries>;
92 using VBucket = std::map<std::string, Type>;
93 using GenerateCloudVersionCallback = std::function<std::string(const std::string &originVersion)>;
94 
95 struct Field {
96     std::string colName;
97     int32_t type; // get value from TYPE_INDEX;
98     bool primary = false;
99     bool nullable = true;
100     bool operator==(const Field &comparedField) const
101     {
102         return (colName == comparedField.colName) && (type == comparedField.type) &&
103             (primary == comparedField.primary) && (nullable == comparedField.nullable);
104     }
105 };
106 
107 struct TableSchema {
108     std::string name;
109     std::string sharedTableName; // if table is shared table, its sharedtablename is ""
110     std::vector<Field> fields;
111 };
112 
113 struct DataBaseSchema {
114     std::vector<TableSchema> tables;
115 };
116 
117 enum class CloudQueryType : int64_t {
118     FULL_TABLE = 0, // query full table
119     QUERY_FIELD = 1 // query with some fields
120 };
121 
122 enum class LockAction : uint32_t {
123     NONE = 0,
124     INSERT = 0x1,
125     UPDATE = 0x2,
126     DELETE = 0x4,
127     DOWNLOAD = 0x8
128 };
129 
130 enum CloudSyncState {
131     IDLE = 0,
132     DO_DOWNLOAD,
133     DO_UPLOAD,
134     DO_REPEAT_CHECK,
135     DO_FINISHED
136 };
137 
138 enum CloudSyncEvent {
139     UPLOAD_FINISHED_EVENT,
140     DOWNLOAD_FINISHED_EVENT,
141     ERROR_EVENT,
142     REPEAT_CHECK_EVENT,
143     REPEAT_DOWNLOAD_EVENT,
144     START_SYNC_EVENT,
145     ALL_TASK_FINISHED_EVENT
146 };
147 
148 struct CloudSyncOption {
149     std::vector<std::string> devices;
150     SyncMode mode = SyncMode::SYNC_MODE_CLOUD_MERGE;
151     Query query;
152     int64_t waitTime = 0;
153     bool priorityTask = false;
154     bool compensatedSyncOnly = false;
155     std::vector<std::string> users;
156     bool merge = false;
157     // default, upload insert need lock
158     LockAction lockAction = LockAction::INSERT;
159     std::string prepareTraceId;
160 };
161 
162 enum class QueryNodeType : uint32_t {
163     ILLEGAL = 0,
164     IN = 1,
165     OR = 0x101,
166     AND,
167     EQUAL_TO = 0x201,
168     BEGIN_GROUP = 0x301,
169     END_GROUP
170 };
171 
172 struct QueryNode {
173     QueryNodeType type = QueryNodeType::ILLEGAL;
174     std::string fieldName;
175     std::vector<Type> fieldValue;
176 };
177 
178 struct SqlCondition {
179     std::string sql;  // The sql statement;
180     std::vector<Type> bindArgs;  // The bind args.
181     bool readOnly = false;
182 };
183 
184 enum class RecordStatus {
185     WAIT_COMPENSATED_SYNC,
186     NORMAL
187 };
188 
189 enum class LockStatus : uint32_t {
190     UNLOCK = 0,
191     UNLOCKING,
192     LOCK,
193     LOCK_CHANGE,
194     BUTT,
195 };
196 
197 struct CloudSyncConfig {
198     int32_t maxUploadCount = 30;             // default max upload 30 records
199     int32_t maxUploadSize  = 1024 * 512 * 3; // default max upload 1024 * 512 * 3 = 1.5m
200     int32_t maxRetryConflictTimes = -1;      // default max retry -1 is unlimited retry times
201 };
202 } // namespace DistributedDB
203 #endif // CLOUD_STORE_TYPE_H