• 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     REMOTE_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     BUTT_STATUS = 27394048 // end of status
94 };
95 
96 struct KvStoreConfig {
97     std::string dataDir;
98 };
99 
100 enum PragmaCmd {
101     AUTO_SYNC = 1,
102     SYNC_DEVICES = 2, // this cmd will be removed in the future, don't use it
103     RM_DEVICE_DATA = 3, // this cmd will be removed in the future, don't use it
104     PERFORMANCE_ANALYSIS_GET_REPORT,
105     PERFORMANCE_ANALYSIS_OPEN,
106     PERFORMANCE_ANALYSIS_CLOSE,
107     PERFORMANCE_ANALYSIS_SET_REPORTFILENAME,
108     GET_IDENTIFIER_OF_DEVICE,
109     GET_DEVICE_IDENTIFIER_OF_ENTRY,
110     GET_QUEUED_SYNC_SIZE,
111     SET_QUEUED_SYNC_LIMIT,
112     GET_QUEUED_SYNC_LIMIT,
113     SET_WIPE_POLICY,  // set the policy of wipe remote stale data
114     RESULT_SET_CACHE_MODE, // Accept ResultSetCacheMode Type As PragmaData
115     RESULT_SET_CACHE_MAX_SIZE, // Allowed Int Type Range [1,16], Unit MB
116     SET_SYNC_RETRY,
117     SET_MAX_LOG_LIMIT,
118     EXEC_CHECKPOINT,
119     LOGIC_DELETE_SYNC_DATA,
120     SET_MAX_VALUE_SIZE,
121 };
122 
123 enum ResolutionPolicyType {
124     AUTO_LAST_WIN = 0,      // resolve conflicts by timestamp(default value)
125     CUSTOMER_RESOLUTION = 1 // resolve conflicts by user
126 };
127 
128 enum ObserverMode {
129     OBSERVER_CHANGES_NATIVE = 1,
130     OBSERVER_CHANGES_FOREIGN = 2,
131     OBSERVER_CHANGES_LOCAL_ONLY = 4,
132     OBSERVER_CHANGES_CLOUD = 8,
133     // bit mask
134     OBSERVER_CHANGES_BRIEF = 0x100,  // notify only device
135     OBSERVER_CHANGES_DETAIL = 0x200, // notify with key
136     OBSERVER_CHANGES_DATA = 0x400    // notify with entry
137 };
138 
139 enum SyncMode {
140     SYNC_MODE_PUSH_ONLY,
141     SYNC_MODE_PULL_ONLY,
142     SYNC_MODE_PUSH_PULL,
143     SYNC_MODE_CLOUD_MERGE = 4,
144     SYNC_MODE_CLOUD_FORCE_PUSH,
145     SYNC_MODE_CLOUD_FORCE_PULL,
146 };
147 
148 enum ConflictResolvePolicy {
149     LAST_WIN = 0,
150     DEVICE_COLLABORATION,
151 };
152 
153 struct TableStatus {
154     std::string tableName;
155     DBStatus status;
156 };
157 
158 enum ProcessStatus {
159     PREPARED = 0,
160     PROCESSING = 1,
161     FINISHED = 2,
162 };
163 
164 enum class CollateType : uint32_t {
165     COLLATE_NONE = 0,
166     COLLATE_NOCASE,
167     COLLATE_RTRIM,
168     COLLATE_BUTT
169 };
170 
171 struct Info {
172     uint32_t batchIndex = 0;
173     uint32_t total = 0;
174     uint32_t successCount = 0; // merge or upload success count
175     uint32_t failCount = 0;
176     uint32_t insertCount = 0;
177     uint32_t updateCount = 0;
178     uint32_t deleteCount = 0;
179 };
180 
181 struct TableProcessInfo {
182     ProcessStatus process = PREPARED;
183     Info downLoadInfo;
184     Info upLoadInfo;
185 };
186 
187 struct SyncProcess {
188     ProcessStatus process = PREPARED;
189     DBStatus errCode = OK;
190     std::map<std::string, TableProcessInfo> tableProcess;
191 };
192 
193 struct DeviceSyncOption {
194     std::vector<std::string> devices;
195     SyncMode mode = SYNC_MODE_PULL_ONLY;
196     Query query; // isQuery must be set to true if the query is set
197     bool isQuery = false;
198     bool isWait = true;
199 };
200 
201 struct DeviceSyncInfo {
202     uint32_t total = 0;
203     uint32_t finishedCount = 0;
204 };
205 
206 struct DeviceSyncProcess {
207     ProcessStatus process = PREPARED;
208     DBStatus errCode = OK;
209     uint32_t syncId;
210     DeviceSyncInfo pullInfo;
211 };
212 
213 using KvStoreCorruptionHandler = std::function<void (const std::string &appId, const std::string &userId,
214     const std::string &storeId)>;
215 using StoreCorruptionHandler = std::function<void (const std::string &appId, const std::string &userId,
216     const std::string &storeId)>;
217 using SyncStatusCallback = std::function<void(const std::map<std::string, std::vector<TableStatus>> &devicesMap)>;
218 
219 using SyncProcessCallback = std::function<void(const std::map<std::string, SyncProcess> &process)>;
220 
221 using DeviceSyncProcessCallback = std::function<void(const std::map<std::string, DeviceSyncProcess> &processMap)>;
222 
223 struct RemoteCondition {
224     std::string sql;  // The sql statement;
225     std::vector<std::string> bindArgs;  // The bind args.
226 };
227 
228 struct DBInfo {
229     std::string userId;
230     std::string appId;
231     std::string storeId;
232     bool syncDualTupleMode = false;
233     bool isNeedSync = false;
234 };
235 
236 using UpdateKeyCallback = std::function<void (const Key &originKey, Key &newKey)>;
237 
238 struct TrackerSchema {
239     std::string tableName;
240     std::string extendColName;
241     std::set<std::string> trackerColNames;
242     bool isForceUpgrade = false;
243 };
244 
245 struct TableReferenceProperty {
246     std::string sourceTableName;
247     std::string targetTableName;
248     std::map<std::string, std::string> columns; // key is sourceTable column, value is targetTable column
249 };
250 
251 static constexpr const char *GAUSSDB_RD = "gaussdb_rd";
252 static constexpr const char *SQLITE = "sqlite";
253 struct ChangeProperties {
254     bool isTrackedDataChange = false;
255 };
256 
257 enum IndexType : uint32_t {
258     /**
259      * use btree index type in database
260     */
261     BTREE = 0,
262     /**
263      * use hash index type in database
264     */
265     HASH,
266 };
267 
268 struct Rdconfig {
269     bool readOnly = false;
270     IndexType type = BTREE;
271     uint32_t pageSize = 32u;
272     uint32_t cacheSize = 2048u;
273 };
274 
275 struct WatermarkInfo {
276     uint64_t sendMark = 0; // data will be sent which timestamp greater than sendMark
277     uint64_t receiveMark = 0; // data will be sent in remote which timestamp greater than receiveMark
278 };
279 
280 struct DbIdParam {
281     std::string appId;
282     std::string userId;
283     std::string storeId;
284     std::string subUser = "";
285     int32_t instanceId = 0;
286 };
287 } // namespace DistributedDB
288 #endif // KV_STORE_TYPE_H
289