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