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 <string> 22 23 #include "types_export.h" 24 25 namespace DistributedDB { 26 enum DBStatus { 27 DB_ERROR = -1, 28 OK = 0, 29 BUSY, 30 NOT_FOUND, 31 INVALID_ARGS, 32 TIME_OUT, 33 NOT_SUPPORT, 34 INVALID_PASSWD_OR_CORRUPTED_DB, 35 OVER_MAX_LIMITS, 36 INVALID_FILE, 37 NO_PERMISSION, 38 FILE_ALREADY_EXISTED, 39 SCHEMA_MISMATCH, 40 INVALID_SCHEMA, 41 READ_ONLY, 42 INVALID_VALUE_FIELDS, // invalid put value for json schema. 43 INVALID_FIELD_TYPE, // invalid put value field type for json schema. 44 CONSTRAIN_VIOLATION, // invalid put value constrain for json schema. 45 INVALID_FORMAT, // invalid put value format for json schema. 46 STALE, // new record is staler compared to the same key existed in db. 47 LOCAL_DELETED, // local data is deleted by the unpublish. 48 LOCAL_DEFEAT, // local data defeat the sync data while unpublish. 49 LOCAL_COVERED, // local data is covered by the sync data while unpublish. 50 INVALID_QUERY_FORMAT, 51 INVALID_QUERY_FIELD, 52 PERMISSION_CHECK_FORBID_SYNC, // permission check result , forbid sync. 53 ALREADY_SET, // already set. 54 COMM_FAILURE, // communicator may get some error. 55 EKEYREVOKED_ERROR, // EKEYREVOKED error when operating db file 56 SECURITY_OPTION_CHECK_ERROR, // such as remote device's SecurityOption not equal to local 57 SCHEMA_VIOLATE_VALUE, // Values already exist in dbFile do not match new schema 58 INTERCEPT_DATA_FAIL, // Interceptor push data failed. 59 LOG_OVER_LIMITS, // Log size is over the limits. 60 DISTRIBUTED_SCHEMA_NOT_FOUND, // the sync table is not a relational table 61 DISTRIBUTED_SCHEMA_CHANGED, // the schema was changed 62 MODE_MISMATCH, 63 NOT_ACTIVE, 64 USER_CHANGED, 65 NONEXISTENT, // for row record, pass invalid column name or invalid column index. 66 TYPE_MISMATCH, // for row record, get value with mismatch func. 67 REMOTE_OVER_SIZE, // for remote query, the data is too many, only get part or data. 68 RATE_LIMIT, 69 DATA_HANDLE_ERROR, // remote handle data failed 70 CONSTRAINT, // constraint check failed in sqlite 71 CLOUD_ERROR, // cloud error 72 QUERY_END, // Indicates that query function has queried last data from cloud 73 DB_CLOSED, // db is closed 74 UNSET_ERROR, // something should be set not be set 75 CLOUD_NETWORK_ERROR, // network error in cloud 76 CLOUD_SYNC_UNSET, // not set sync option in cloud 77 CLOUD_FULL_RECORDS, // cloud's record is full 78 CLOUD_LOCK_ERROR, // cloud failed to get sync lock 79 CLOUD_ASSET_SPACE_INSUFFICIENT, // cloud failed to download asset 80 }; 81 82 struct KvStoreConfig { 83 std::string dataDir; 84 }; 85 86 enum PragmaCmd { 87 AUTO_SYNC = 1, 88 SYNC_DEVICES = 2, // this cmd will be removed in the future, don't use it 89 RM_DEVICE_DATA = 3, // this cmd will be removed in the future, don't use it 90 PERFORMANCE_ANALYSIS_GET_REPORT, 91 PERFORMANCE_ANALYSIS_OPEN, 92 PERFORMANCE_ANALYSIS_CLOSE, 93 PERFORMANCE_ANALYSIS_SET_REPORTFILENAME, 94 GET_IDENTIFIER_OF_DEVICE, 95 GET_DEVICE_IDENTIFIER_OF_ENTRY, 96 GET_QUEUED_SYNC_SIZE, 97 SET_QUEUED_SYNC_LIMIT, 98 GET_QUEUED_SYNC_LIMIT, 99 SET_WIPE_POLICY, // set the policy of wipe remote stale data 100 RESULT_SET_CACHE_MODE, // Accept ResultSetCacheMode Type As PragmaData 101 RESULT_SET_CACHE_MAX_SIZE, // Allowed Int Type Range [1,16], Unit MB 102 SET_SYNC_RETRY, 103 SET_MAX_LOG_LIMIT, 104 EXEC_CHECKPOINT, 105 }; 106 107 enum ResolutionPolicyType { 108 AUTO_LAST_WIN = 0, // resolve conflicts by timestamp(default value) 109 CUSTOMER_RESOLUTION = 1 // resolve conflicts by user 110 }; 111 112 enum ObserverMode { 113 OBSERVER_CHANGES_NATIVE = 1, 114 OBSERVER_CHANGES_FOREIGN = 2, 115 OBSERVER_CHANGES_LOCAL_ONLY = 4, 116 }; 117 118 enum SyncMode { 119 SYNC_MODE_PUSH_ONLY, 120 SYNC_MODE_PULL_ONLY, 121 SYNC_MODE_PUSH_PULL, 122 SYNC_MODE_CLOUD_MERGE = 4, 123 SYNC_MODE_CLOUD_FORCE_PUSH, 124 SYNC_MODE_CLOUD_FORCE_PULL, 125 }; 126 127 enum ConflictResolvePolicy { 128 LAST_WIN = 0, 129 DEVICE_COLLABORATION, 130 }; 131 132 struct TableStatus { 133 std::string tableName; 134 DBStatus status; 135 }; 136 137 enum ProcessStatus { 138 PREPARED = 0, 139 PROCESSING = 1, 140 FINISHED = 2, 141 }; 142 143 struct Info { 144 uint32_t batchIndex = 0; 145 uint32_t total = 0; 146 uint32_t successCount = 0; // merge or upload success count 147 uint32_t failCount = 0; 148 }; 149 150 struct TableProcessInfo { 151 ProcessStatus process = PREPARED; 152 Info downLoadInfo; 153 Info upLoadInfo; 154 }; 155 156 struct SyncProcess { 157 ProcessStatus process = PREPARED; 158 DBStatus errCode = OK; 159 std::map<std::string, TableProcessInfo> tableProcess; 160 }; 161 162 using KvStoreCorruptionHandler = std::function<void (const std::string &appId, const std::string &userId, 163 const std::string &storeId)>; 164 using StoreCorruptionHandler = std::function<void (const std::string &appId, const std::string &userId, 165 const std::string &storeId)>; 166 using SyncStatusCallback = std::function<void(const std::map<std::string, std::vector<TableStatus>> &devicesMap)>; 167 168 using SyncProcessCallback = std::function<void(const std::map<std::string, SyncProcess> &process)>; 169 170 struct RemoteCondition { 171 std::string sql; // The sql statement; 172 std::vector<std::string> bindArgs; // The bind args. 173 }; 174 using UpdateKeyCallback = std::function<void (const Key &originKey, Key &newKey)>; 175 } // namespace DistributedDB 176 #endif // KV_STORE_TYPE_H 177