• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 KV_STORE_TYPE_H
17 #define KV_STORE_TYPE_H
18 
19 #include <functional>
20 #include <map>
21 #include <set>
22 #include <string>
23 
24 #include "query.h"
25 #include "types_export.h"
26 
27 namespace DistributedDB {
28 enum DBStatus {
29     OK = 0,
30     DB_ERROR = 27328512, // DBStatus in [27328512, 27394048)
31     BUSY,
32     NOT_FOUND,
33     INVALID_ARGS,
34     TIME_OUT,
35     NOT_SUPPORT,
36     INVALID_PASSWD_OR_CORRUPTED_DB,
37     OVER_MAX_LIMITS,
38     INVALID_FILE,
39     NO_PERMISSION,
40     FILE_ALREADY_EXISTED,
41     SCHEMA_MISMATCH,
42     INVALID_SCHEMA,
43     READ_ONLY,
44     INVALID_VALUE_FIELDS, // invalid put value for json schema.
45     INVALID_FIELD_TYPE, // invalid put value field type for json schema.
46     CONSTRAIN_VIOLATION, // invalid put value constrain for json schema.
47     INVALID_FORMAT, // invalid put value format for json schema.
48     STALE, // new record is staler compared to the same key existed in db.
49     LOCAL_DELETED, // local data is deleted by the unpublish.
50     LOCAL_DEFEAT, // local data defeat the sync data while unpublish.
51     LOCAL_COVERED, // local data is covered by the sync data while unpublish.
52     INVALID_QUERY_FORMAT,
53     INVALID_QUERY_FIELD,
54     PERMISSION_CHECK_FORBID_SYNC, // permission check result , forbid sync.
55     ALREADY_SET, // already set.
56     COMM_FAILURE, // communicator may get some error.
57     EKEYREVOKED_ERROR, // EKEYREVOKED error when operating db file
58     SECURITY_OPTION_CHECK_ERROR, // such as remote device's SecurityOption not equal to local
59     SCHEMA_VIOLATE_VALUE, // Values already exist in dbFile do not match new schema
60     INTERCEPT_DATA_FAIL, // Interceptor push data failed.
61     LOG_OVER_LIMITS, // Log size is over the limits.
62     DISTRIBUTED_SCHEMA_NOT_FOUND, // the sync table is not a relational table
63     DISTRIBUTED_SCHEMA_CHANGED, // the schema was changed
64     MODE_MISMATCH,
65     NOT_ACTIVE,
66     USER_CHANGED,
67     NONEXISTENT,  // for row record, pass invalid column name or invalid column index.
68     TYPE_MISMATCH,  // for row record, get value with mismatch func.
69     REMOTE_OVER_SIZE, // for remote query, the data is too many, only get part or data.
70     RATE_LIMIT,
71     DATA_HANDLE_ERROR, // remote handle data failed
72     CONSTRAINT, // constraint check failed in sqlite
73     CLOUD_ERROR, // cloud error
74     QUERY_END, // Indicates that query function has queried last data from cloud
75     DB_CLOSED, // db is closed
76     UNSET_ERROR, // something should be set not be set
77     CLOUD_NETWORK_ERROR, // network error in cloud
78     CLOUD_SYNC_UNSET, // not set sync option in cloud
79     CLOUD_FULL_RECORDS, // cloud's record is full
80     CLOUD_LOCK_ERROR, // cloud failed to get sync lock
81     CLOUD_ASSET_SPACE_INSUFFICIENT, // cloud failed to download asset
82     PROPERTY_CHANGED, // reference property changed
83     CLOUD_VERSION_CONFLICT, // cloud failed to update version
84     CLOUD_RECORD_EXIST_CONFLICT, // this error happen in Download/BatchInsert/BatchUpdate
85     REMOVE_ASSETS_FAIL, // remove local assets failed
86     WITH_INVENTORY_DATA, // inventory data exists when setTracker for the first time
87     WAIT_COMPENSATED_SYNC, // need to do compensated sync
88     CLOUD_SYNC_TASK_MERGED, // sync task is merged
89     CLOUD_RECORD_NOT_FOUND, // this error happen in BatchUpdate/BatchDelete
90     CLOUD_RECORD_ALREADY_EXISTED, // this error happen in BatchInsert
91     SQLITE_CANT_OPEN, // the sqlite cannot open
92     LOCAL_ASSET_NOT_FOUND, // file manager miss local assets
93     ASSET_NOT_FOUND_FOR_DOWN_ONLY, // assets miss for asset only
94     CLOUD_DISABLED, // the cloud switch has been turned off
95     DISTRIBUTED_FIELD_DECREASE, // sync fewer specified columns than last time
96     SKIP_ASSET, // workaround status for contact app assets download failure, need to ignore these failures
97     BUTT_STATUS = 27394048 // end of status
98 };
99 
100 struct KvStoreConfig {
101     std::string dataDir;
102 };
103 
104 enum PragmaCmd {
105     AUTO_SYNC = 1,
106     SYNC_DEVICES = 2, // this cmd will be removed in the future, don't use it
107     RM_DEVICE_DATA = 3, // this cmd will be removed in the future, don't use it
108     PERFORMANCE_ANALYSIS_GET_REPORT,
109     PERFORMANCE_ANALYSIS_OPEN,
110     PERFORMANCE_ANALYSIS_CLOSE,
111     PERFORMANCE_ANALYSIS_SET_REPORTFILENAME,
112     GET_IDENTIFIER_OF_DEVICE,
113     GET_DEVICE_IDENTIFIER_OF_ENTRY,
114     GET_QUEUED_SYNC_SIZE,
115     SET_QUEUED_SYNC_LIMIT,
116     GET_QUEUED_SYNC_LIMIT,
117     SET_WIPE_POLICY,  // set the policy of wipe remote stale data
118     RESULT_SET_CACHE_MODE, // Accept ResultSetCacheMode Type As PragmaData
119     RESULT_SET_CACHE_MAX_SIZE, // Allowed Int Type Range [1,16], Unit MB
120     SET_SYNC_RETRY,
121     SET_MAX_LOG_LIMIT,
122     EXEC_CHECKPOINT,
123     LOGIC_DELETE_SYNC_DATA,
124     SET_MAX_VALUE_SIZE,
125 };
126 
127 enum ResolutionPolicyType {
128     AUTO_LAST_WIN = 0,      // resolve conflicts by timestamp(default value)
129     CUSTOMER_RESOLUTION = 1 // resolve conflicts by user
130 };
131 
132 enum ObserverMode {
133     OBSERVER_CHANGES_NATIVE = 1,
134     OBSERVER_CHANGES_FOREIGN = 2,
135     OBSERVER_CHANGES_LOCAL_ONLY = 4,
136     OBSERVER_CHANGES_CLOUD = 8,
137     // bit mask
138     OBSERVER_CHANGES_BRIEF = 0x100,  // notify only device
139     OBSERVER_CHANGES_DETAIL = 0x200, // notify with key
140     OBSERVER_CHANGES_DATA = 0x400    // notify with entry
141 };
142 
143 enum SyncMode {
144     SYNC_MODE_PUSH_ONLY,
145     SYNC_MODE_PULL_ONLY,
146     SYNC_MODE_PUSH_PULL,
147     SYNC_MODE_CLOUD_MERGE = 4,
148     SYNC_MODE_CLOUD_FORCE_PUSH,
149     SYNC_MODE_CLOUD_FORCE_PULL,
150 };
151 
152 enum ConflictResolvePolicy {
153     LAST_WIN = 0,
154     DEVICE_COLLABORATION,
155 };
156 
157 struct TableStatus {
158     std::string tableName;
159     DBStatus status;
160 };
161 
162 enum ProcessStatus {
163     PREPARED = 0,
164     PROCESSING = 1,
165     FINISHED = 2,
166 };
167 
168 enum class CollateType : uint32_t {
169     COLLATE_NONE = 0,
170     COLLATE_NOCASE,
171     COLLATE_RTRIM,
172     COLLATE_BUTT
173 };
174 
175 struct Info {
176     uint32_t batchIndex = 0;
177     uint32_t total = 0;
178     uint32_t successCount = 0; // merge or upload success count
179     uint32_t failCount = 0;
180     uint32_t insertCount = 0;
181     uint32_t updateCount = 0;
182     uint32_t deleteCount = 0;
183 };
184 
185 struct TableProcessInfo {
186     ProcessStatus process = PREPARED;
187     Info downLoadInfo;
188     Info upLoadInfo;
189 };
190 
191 struct SyncProcess {
192     ProcessStatus process = PREPARED;
193     DBStatus errCode = OK;
194     std::map<std::string, TableProcessInfo> tableProcess;
195 };
196 
197 struct DeviceSyncOption {
198     std::vector<std::string> devices;
199     SyncMode mode = SYNC_MODE_PULL_ONLY;
200     Query query; // isQuery must be set to true if the query is set
201     bool isQuery = false;
202     bool isWait = true;
203 };
204 
205 struct DeviceSyncInfo {
206     uint32_t total = 0;
207     uint32_t finishedCount = 0;
208 };
209 
210 struct DeviceSyncProcess {
211     ProcessStatus process = PREPARED;
212     DBStatus errCode = OK;
213     uint32_t syncId;
214     DeviceSyncInfo pullInfo;
215 };
216 
217 using KvStoreCorruptionHandler = std::function<void (const std::string &appId, const std::string &userId,
218     const std::string &storeId)>;
219 using StoreCorruptionHandler = std::function<void (const std::string &appId, const std::string &userId,
220     const std::string &storeId)>;
221 using SyncStatusCallback = std::function<void(const std::map<std::string, std::vector<TableStatus>> &devicesMap)>;
222 
223 using SyncProcessCallback = std::function<void(const std::map<std::string, SyncProcess> &process)>;
224 
225 using DeviceSyncProcessCallback = std::function<void(const std::map<std::string, DeviceSyncProcess> &processMap)>;
226 
227 struct RemoteCondition {
228     std::string sql;  // The sql statement;
229     std::vector<std::string> bindArgs;  // The bind args.
230 };
231 
232 struct DBInfo {
233     std::string userId;
234     std::string appId;
235     std::string storeId;
236     bool syncDualTupleMode = false;
237     bool isNeedSync = false;
238 };
239 
240 using UpdateKeyCallback = std::function<void (const Key &originKey, Key &newKey)>;
241 
242 struct TrackerSchema {
243     std::string tableName;
244     std::set<std::string> extendColNames;
245     std::set<std::string> trackerColNames;
246     bool isForceUpgrade = false;
247     bool isTrackAction = false;
248 };
249 
250 struct TableReferenceProperty {
251     std::string sourceTableName;
252     std::string targetTableName;
253     std::map<std::string, std::string> columns; // key is sourceTable column, value is targetTable column
254 };
255 
256 static constexpr const char *GAUSSDB_RD = "gaussdb_rd";
257 static constexpr const char *SQLITE = "sqlite";
258 struct ChangeProperties {
259     bool isTrackedDataChange = false;
260     bool isP2pSyncDataChange = false;
261 };
262 
263 enum IndexType : uint32_t {
264     /**
265      * use btree index type in database
266     */
267     BTREE = 0,
268     /**
269      * use hash index type in database
270     */
271     HASH,
272 };
273 
274 struct Rdconfig {
275     bool readOnly = false;
276     IndexType type = BTREE;
277     uint32_t pageSize = 32u;
278     uint32_t cacheSize = 2048u;
279 };
280 
281 struct WatermarkInfo {
282     uint64_t sendMark = 0; // data will be sent which timestamp greater than sendMark
283     uint64_t receiveMark = 0; // data will be sent in remote which timestamp greater than receiveMark
284 };
285 
286 struct DbIdParam {
287     std::string appId;
288     std::string userId;
289     std::string storeId;
290     std::string subUser = "";
291     int32_t instanceId = 0;
292 };
293 
294 struct DistributedField {
295     std::string colName;
296     bool isP2pSync = false; // device p2p sync with this column when it was true
297     // default generate by local table pk when none field was specified
298     bool isSpecified = false; // local log hashKey will generate by specified field and deal conflict with them
299 };
300 
301 struct DistributedTable {
302     std::string tableName;
303     std::vector<DistributedField> fields;
304 };
305 
306 struct DistributedSchema {
307     uint32_t version = 0;
308     std::vector<DistributedTable> tables;
309 };
310 
311 // Table mode of device data for relational store
312 enum class DistributedTableMode : int {
313     COLLABORATION = 0, // Save all devices data in user table
314     SPLIT_BY_DEVICE // Save device data in each table split by device
315 };
316 } // namespace DistributedDB
317 #endif // KV_STORE_TYPE_H
318