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 asset space is insufficient 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 LOW_VERSION_TARGET, // The target device is a low version device 98 NEED_CORRECT_TARGET_USER, // The target user ID is incorrect and needs to be re-obtained 99 BUTT_STATUS = 27394048 // end of status 100 }; 101 102 struct KvStoreConfig { 103 std::string dataDir; 104 }; 105 106 enum PragmaCmd { 107 AUTO_SYNC = 1, 108 SYNC_DEVICES = 2, // this cmd will be removed in the future, don't use it 109 RM_DEVICE_DATA = 3, // this cmd will be removed in the future, don't use it 110 PERFORMANCE_ANALYSIS_GET_REPORT, 111 PERFORMANCE_ANALYSIS_OPEN, 112 PERFORMANCE_ANALYSIS_CLOSE, 113 PERFORMANCE_ANALYSIS_SET_REPORTFILENAME, 114 GET_IDENTIFIER_OF_DEVICE, 115 GET_DEVICE_IDENTIFIER_OF_ENTRY, 116 GET_QUEUED_SYNC_SIZE, 117 SET_QUEUED_SYNC_LIMIT, 118 GET_QUEUED_SYNC_LIMIT, 119 SET_WIPE_POLICY, // set the policy of wipe remote stale data 120 RESULT_SET_CACHE_MODE, // Accept ResultSetCacheMode Type As PragmaData 121 RESULT_SET_CACHE_MAX_SIZE, // Allowed Int Type Range [1,16], Unit MB 122 SET_SYNC_RETRY, 123 SET_MAX_LOG_LIMIT, 124 EXEC_CHECKPOINT, 125 LOGIC_DELETE_SYNC_DATA, 126 SET_MAX_VALUE_SIZE, 127 }; 128 129 enum ResolutionPolicyType { 130 AUTO_LAST_WIN = 0, // resolve conflicts by timestamp(default value) 131 CUSTOMER_RESOLUTION = 1 // resolve conflicts by user 132 }; 133 134 enum ObserverMode { 135 OBSERVER_CHANGES_NATIVE = 1, 136 OBSERVER_CHANGES_FOREIGN = 2, 137 OBSERVER_CHANGES_LOCAL_ONLY = 4, 138 OBSERVER_CHANGES_CLOUD = 8, 139 // bit mask 140 OBSERVER_CHANGES_BRIEF = 0x100, // notify only device 141 OBSERVER_CHANGES_DETAIL = 0x200, // notify with key 142 OBSERVER_CHANGES_DATA = 0x400 // notify with entry 143 }; 144 145 enum SyncMode { 146 SYNC_MODE_PUSH_ONLY, 147 SYNC_MODE_PULL_ONLY, 148 SYNC_MODE_PUSH_PULL, 149 SYNC_MODE_CLOUD_MERGE = 4, 150 SYNC_MODE_CLOUD_FORCE_PUSH, 151 SYNC_MODE_CLOUD_FORCE_PULL, 152 }; 153 154 enum ConflictResolvePolicy { 155 LAST_WIN = 0, 156 DEVICE_COLLABORATION, 157 }; 158 159 struct TableStatus { 160 std::string tableName; 161 DBStatus status; 162 }; 163 164 enum ProcessStatus { 165 PREPARED = 0, 166 PROCESSING = 1, 167 FINISHED = 2, 168 }; 169 170 enum class CollateType : uint32_t { 171 COLLATE_NONE = 0, 172 COLLATE_NOCASE, 173 COLLATE_RTRIM, 174 COLLATE_BUTT 175 }; 176 177 struct Info { 178 uint32_t batchIndex = 0; 179 uint32_t total = 0; 180 uint32_t successCount = 0; // merge or upload success count 181 uint32_t failCount = 0; 182 uint32_t insertCount = 0; 183 uint32_t updateCount = 0; 184 uint32_t deleteCount = 0; 185 }; 186 187 struct TableProcessInfo { 188 ProcessStatus process = PREPARED; 189 Info downLoadInfo; 190 Info upLoadInfo; 191 }; 192 193 struct SyncProcess { 194 ProcessStatus process = PREPARED; 195 DBStatus errCode = OK; 196 std::map<std::string, TableProcessInfo> tableProcess; 197 }; 198 199 struct DeviceSyncOption { 200 std::vector<std::string> devices; 201 SyncMode mode = SYNC_MODE_PULL_ONLY; 202 Query query; // isQuery must be set to true if the query is set 203 bool isQuery = false; 204 bool isWait = true; 205 bool isRetry = true; 206 }; 207 208 struct DeviceSyncInfo { 209 uint32_t total = 0; 210 uint32_t finishedCount = 0; 211 }; 212 213 struct DeviceSyncProcess { 214 ProcessStatus process = PREPARED; 215 DBStatus errCode = OK; 216 uint32_t syncId; 217 DeviceSyncInfo pullInfo; 218 }; 219 220 using KvStoreCorruptionHandler = std::function<void (const std::string &appId, const std::string &userId, 221 const std::string &storeId)>; 222 using StoreCorruptionHandler = std::function<void (const std::string &appId, const std::string &userId, 223 const std::string &storeId)>; 224 using SyncStatusCallback = std::function<void(const std::map<std::string, std::vector<TableStatus>> &devicesMap)>; 225 226 using SyncProcessCallback = std::function<void(const std::map<std::string, SyncProcess> &process)>; 227 228 using DeviceSyncProcessCallback = std::function<void(const std::map<std::string, DeviceSyncProcess> &processMap)>; 229 230 struct RemoteCondition { 231 std::string sql; // The sql statement; 232 std::vector<std::string> bindArgs; // The bind args. 233 }; 234 235 struct DBInfo { 236 std::string userId; 237 std::string appId; 238 std::string storeId; 239 bool syncDualTupleMode = false; 240 bool isNeedSync = false; 241 }; 242 243 using UpdateKeyCallback = std::function<void (const Key &originKey, Key &newKey)>; 244 245 struct TrackerSchema { 246 std::string tableName; 247 std::set<std::string> extendColNames; 248 std::set<std::string> trackerColNames; 249 bool isForceUpgrade = false; 250 bool isTrackAction = false; 251 }; 252 253 struct TableReferenceProperty { 254 std::string sourceTableName; 255 std::string targetTableName; 256 std::map<std::string, std::string> columns; // key is sourceTable column, value is targetTable column 257 }; 258 259 static constexpr const char *GAUSSDB_RD = "gaussdb_rd"; 260 static constexpr const char *SQLITE = "sqlite"; 261 struct ChangeProperties { 262 bool isTrackedDataChange = false; 263 bool isP2pSyncDataChange = false; 264 bool isKnowledgeDataChange = false; 265 bool isCloudSyncDataChange = false; 266 }; 267 268 enum IndexType : uint32_t { 269 /** 270 * use btree index type in database 271 */ 272 BTREE = 0, 273 /** 274 * use hash index type in database 275 */ 276 HASH, 277 }; 278 279 struct Rdconfig { 280 bool readOnly = false; 281 IndexType type = BTREE; 282 uint32_t pageSize = 32u; 283 uint32_t cacheSize = 2048u; 284 }; 285 286 struct WatermarkInfo { 287 uint64_t sendMark = 0; // data will be sent which timestamp greater than sendMark 288 uint64_t receiveMark = 0; // data will be sent in remote which timestamp greater than receiveMark 289 }; 290 291 struct DbIdParam { 292 std::string appId; 293 std::string userId; 294 std::string storeId; 295 std::string subUser = ""; 296 int32_t instanceId = 0; 297 }; 298 299 struct DistributedField { 300 std::string colName; 301 bool isP2pSync = false; // device p2p sync with this column when it was true 302 // default generate by local table pk when none field was specified 303 bool isSpecified = false; // local log hashKey will generate by specified field and deal conflict with them 304 }; 305 306 struct DistributedTable { 307 std::string tableName; 308 std::vector<DistributedField> fields; 309 }; 310 311 struct DistributedSchema { 312 uint32_t version = 0; 313 std::vector<DistributedTable> tables; 314 }; 315 316 // Table mode of device data for relational store 317 enum class DistributedTableMode : int { 318 COLLABORATION = 0, // Save all devices data in user table 319 SPLIT_BY_DEVICE // Save device data in each table split by device 320 }; 321 322 enum class DataOperator : uint32_t { 323 UPDATE_TIME = 0x01, 324 RESET_UPLOAD_CLOUD = 0x02 325 }; 326 327 struct DeviceSyncTarget { 328 std::string device; 329 std::string userId; DeviceSyncTargetDeviceSyncTarget330 DeviceSyncTarget(std::string device, std::string userId) : device(std::move(device)), userId(std::move(userId)) {} 331 bool operator<(const DeviceSyncTarget& other) const 332 { 333 if (device == other.device) { 334 return userId < other.userId; 335 } 336 return device < other.device; 337 } 338 }; 339 } // namespace DistributedDB 340 #endif // KV_STORE_TYPE_H 341