• 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 "types_export.h"
25 
26 namespace DistributedDB {
27 enum DBStatus {
28     DB_ERROR = -1,
29     OK = 0,
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 };
86 
87 struct KvStoreConfig {
88     std::string dataDir;
89 };
90 
91 enum PragmaCmd {
92     AUTO_SYNC = 1,
93     SYNC_DEVICES = 2, // this cmd will be removed in the future, don't use it
94     RM_DEVICE_DATA = 3, // this cmd will be removed in the future, don't use it
95     PERFORMANCE_ANALYSIS_GET_REPORT,
96     PERFORMANCE_ANALYSIS_OPEN,
97     PERFORMANCE_ANALYSIS_CLOSE,
98     PERFORMANCE_ANALYSIS_SET_REPORTFILENAME,
99     GET_IDENTIFIER_OF_DEVICE,
100     GET_DEVICE_IDENTIFIER_OF_ENTRY,
101     GET_QUEUED_SYNC_SIZE,
102     SET_QUEUED_SYNC_LIMIT,
103     GET_QUEUED_SYNC_LIMIT,
104     SET_WIPE_POLICY,  // set the policy of wipe remote stale data
105     RESULT_SET_CACHE_MODE, // Accept ResultSetCacheMode Type As PragmaData
106     RESULT_SET_CACHE_MAX_SIZE, // Allowed Int Type Range [1,16], Unit MB
107     SET_SYNC_RETRY,
108     SET_MAX_LOG_LIMIT,
109     EXEC_CHECKPOINT,
110     LOGIC_DELETE_SYNC_DATA,
111 };
112 
113 enum ResolutionPolicyType {
114     AUTO_LAST_WIN = 0,      // resolve conflicts by timestamp(default value)
115     CUSTOMER_RESOLUTION = 1 // resolve conflicts by user
116 };
117 
118 enum ObserverMode {
119     OBSERVER_CHANGES_NATIVE = 1,
120     OBSERVER_CHANGES_FOREIGN = 2,
121     OBSERVER_CHANGES_LOCAL_ONLY = 4,
122 };
123 
124 enum SyncMode {
125     SYNC_MODE_PUSH_ONLY,
126     SYNC_MODE_PULL_ONLY,
127     SYNC_MODE_PUSH_PULL,
128     SYNC_MODE_CLOUD_MERGE = 4,
129     SYNC_MODE_CLOUD_FORCE_PUSH,
130     SYNC_MODE_CLOUD_FORCE_PULL,
131 };
132 
133 enum ConflictResolvePolicy {
134     LAST_WIN = 0,
135     DEVICE_COLLABORATION,
136 };
137 
138 struct TableStatus {
139     std::string tableName;
140     DBStatus status;
141 };
142 
143 enum ProcessStatus {
144     PREPARED = 0,
145     PROCESSING = 1,
146     FINISHED = 2,
147 };
148 
149 enum class CollateType : uint32_t {
150     COLLATE_NONE = 0,
151     COLLATE_NOCASE,
152     COLLATE_RTRIM,
153     COLLATE_BUTT
154 };
155 
156 struct Info {
157     uint32_t batchIndex = 0;
158     uint32_t total = 0;
159     uint32_t successCount = 0; // merge or upload success count
160     uint32_t failCount = 0;
161 };
162 
163 struct TableProcessInfo {
164     ProcessStatus process = PREPARED;
165     Info downLoadInfo;
166     Info upLoadInfo;
167 };
168 
169 struct SyncProcess {
170     ProcessStatus process = PREPARED;
171     DBStatus errCode = OK;
172     std::map<std::string, TableProcessInfo> tableProcess;
173 };
174 
175 using KvStoreCorruptionHandler = std::function<void (const std::string &appId, const std::string &userId,
176     const std::string &storeId)>;
177 using StoreCorruptionHandler = std::function<void (const std::string &appId, const std::string &userId,
178     const std::string &storeId)>;
179 using SyncStatusCallback = std::function<void(const std::map<std::string, std::vector<TableStatus>> &devicesMap)>;
180 
181 using SyncProcessCallback = std::function<void(const std::map<std::string, SyncProcess> &process)>;
182 
183 struct RemoteCondition {
184     std::string sql;  // The sql statement;
185     std::vector<std::string> bindArgs;  // The bind args.
186 };
187 
188 struct DBInfo {
189     std::string userId;
190     std::string appId;
191     std::string storeId;
192     bool syncDualTupleMode = false;
193     bool isNeedSync = false;
194 };
195 
196 using UpdateKeyCallback = std::function<void (const Key &originKey, Key &newKey)>;
197 
198 struct TrackerSchema {
199     std::string tableName;
200     std::string extendColName;
201     std::set<std::string> trackerColNames;
202 };
203 
204 struct TableReferenceProperty {
205     std::string sourceTableName;
206     std::string targetTableName;
207     std::map<std::string, std::string> columns; // key is sourceTable column, value is targetTable column
208 };
209 
210 static constexpr const char *GAUSSDB_RD = "gaussdb_rd";
211 static constexpr const char *SQLITE = "sqlite";
212 struct ChangeProperties {
213     bool isTrackedDataChange = false;
214 };
215 
216 struct Rdconfig {
217     bool readOnly = false;
218 };
219 
220 struct WatermarkInfo {
221     uint64_t sendMark = 0; // data will be sent which timestamp greater than sendMark
222     uint64_t receiveMark = 0; // data will be sent in remote which timestamp greater than receiveMark
223 };
224 } // namespace DistributedDB
225 #endif // KV_STORE_TYPE_H
226