1 /* 2 * Copyright (c) 2022 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 DISTRIBUTED_RDB_RDB_TYPES_H 17 #define DISTRIBUTED_RDB_RDB_TYPES_H 18 19 #include <cinttypes> 20 #include <functional> 21 #include <map> 22 #include <set> 23 #include <string> 24 #include <variant> 25 #include <vector> 26 27 namespace OHOS::DistributedRdb { 28 enum RdbStatus { 29 RDB_OK, 30 RDB_ERROR, 31 RDB_NO_META, 32 }; 33 34 enum RdbDistributedType { 35 RDB_DEVICE_COLLABORATION = 10, 36 RDB_DISTRIBUTED_TYPE_MAX 37 }; 38 39 struct RdbDebugInfo { 40 struct DebugTime { 41 int64_t sec_ = 0; 42 int64_t nsec_ = 0; 43 }; 44 uint64_t inode_ = 0; 45 uint64_t oldInode_ = 0; 46 DebugTime atime_; 47 DebugTime mtime_; 48 DebugTime ctime_; 49 ssize_t size_ = 0; 50 uint32_t dev_ = 0; 51 uint32_t mode_ = 0; 52 uint32_t uid_ = 0; 53 uint32_t gid_ = 0; 54 }; 55 56 struct RdbDfxInfo { 57 std::string lastOpenTime_; 58 int curUserId_; 59 }; 60 61 struct RdbSyncerParam { 62 std::string bundleName_; 63 std::string hapName_; 64 std::string storeName_; 65 std::string customDir_; 66 int32_t area_ = 0; 67 int32_t level_ = 0; 68 int32_t haMode_ = 0; 69 int32_t type_ = RDB_DEVICE_COLLABORATION; 70 uint32_t roleType_ = 0; 71 bool isEncrypt_ = false; 72 bool isAutoClean_ = true; 73 bool isSearchable_ = false; 74 std::vector<uint8_t> password_; 75 std::map<std::string, RdbDebugInfo> infos_; 76 std::vector<uint32_t> tokenIds_; 77 std::vector<int32_t> uids_; 78 std::string user_; 79 std::vector<std::string> permissionNames_ = {}; 80 bool asyncDownloadAsset_ = false; 81 bool enableCloud_ = true; 82 int32_t subUser_ = 0; 83 RdbDfxInfo dfxInfo_; ~RdbSyncerParamRdbSyncerParam84 ~RdbSyncerParam() 85 { 86 password_.assign(password_.size(), 0); 87 }; 88 }; 89 90 struct RdbNotifyConfig { 91 uint32_t delay_ = 0; 92 bool isFull_ = false; 93 }; 94 95 enum SyncMode { 96 PUSH, 97 PULL, 98 PULL_PUSH, 99 TIME_FIRST = 4, 100 NATIVE_FIRST, 101 CLOUD_FIRST, 102 }; 103 104 struct SyncOption { 105 SyncMode mode; 106 bool isBlock; 107 }; 108 109 enum DistributedTableType { 110 DISTRIBUTED_DEVICE = 0, 111 DISTRIBUTED_CLOUD, 112 DISTRIBUTED_SEARCH 113 }; 114 115 struct RdbStatEvent { 116 uint32_t statType = 0; 117 std::string bundleName = ""; 118 std::string storeName = ""; 119 uint32_t subType = 0; 120 uint32_t costTime = 0; 121 122 bool operator<(const RdbStatEvent &other) const 123 { 124 if (statType != other.statType) { 125 return statType < other.statType; 126 } 127 if (bundleName.size() != other.bundleName.size()) { 128 return bundleName.size() < other.bundleName.size(); 129 } 130 if (bundleName != other.bundleName) { 131 return bundleName < other.bundleName; 132 } 133 if (storeName.size() != other.storeName.size()) { 134 return storeName.size() < other.storeName.size(); 135 } 136 if (storeName != other.storeName) { 137 return storeName < other.storeName; 138 } 139 if (subType != other.subType) { 140 return subType < other.subType; 141 } 142 return costTime < other.costTime; 143 } 144 }; 145 146 struct Reference { 147 std::string sourceTable; 148 std::string targetTable; 149 std::map<std::string, std::string> refFields; 150 }; 151 152 struct DistributedConfig { 153 bool autoSync = true; 154 std::vector<Reference> references = {}; 155 bool isRebuild = false; 156 bool asyncDownloadAsset = false; 157 bool enableCloud = true; 158 }; 159 160 enum Progress { 161 SYNC_BEGIN = 0, 162 SYNC_IN_PROGRESS, 163 SYNC_FINISH, 164 }; 165 166 enum ProgressCode { 167 SUCCESS = 0, 168 UNKNOWN_ERROR, 169 NETWORK_ERROR, 170 CLOUD_DISABLED, 171 LOCKED_BY_OTHERS, 172 RECORD_LIMIT_EXCEEDED, 173 NO_SPACE_FOR_ASSET, 174 BLOCKED_BY_NETWORK_STRATEGY, 175 }; 176 177 struct Statistic { 178 uint32_t total; 179 uint32_t success; 180 uint32_t failed; 181 uint32_t untreated; 182 }; 183 184 struct TableDetail { 185 Statistic upload; 186 Statistic download; 187 }; 188 189 using TableDetails = std::map<std::string, TableDetail>; 190 191 struct ProgressDetail { 192 int32_t progress; 193 int32_t code; 194 TableDetails details; 195 }; 196 197 using Briefs = std::map<std::string, int>; 198 using Details = std::map<std::string, ProgressDetail>; 199 using AsyncBrief = std::function<void(const Briefs &)>; 200 using AsyncDetail = std::function<void(Details &&)>; 201 202 using SyncResult = Briefs; 203 using SyncCallback = AsyncBrief; 204 205 enum RdbPredicateOperator { 206 EQUAL_TO, 207 NOT_EQUAL_TO, 208 AND, 209 OR, 210 ORDER_BY, 211 LIMIT, 212 BEGIN_GROUP, 213 END_GROUP, 214 IN, 215 NOT_IN, 216 CONTAIN, 217 BEGIN_WITH, 218 END_WITH, 219 IS_NULL, 220 IS_NOT_NULL, 221 LIKE, 222 GLOB, 223 BETWEEN, 224 NOT_BETWEEN, 225 GREATER_THAN, 226 GREATER_THAN_OR_EQUAL, 227 LESS_THAN, 228 LESS_THAN_OR_EQUAL, 229 DISTINCT, 230 INDEXED_BY, 231 NOT_CONTAINS, 232 NOT_LIKE, 233 ASSETS_ONLY, 234 OPERATOR_MAX 235 }; 236 237 struct RdbPredicateOperation { 238 RdbPredicateOperator operator_; 239 std::string field_; 240 std::vector<std::string> values_; 241 }; 242 243 struct PredicatesMemo { AddOperationPredicatesMemo244 inline void AddOperation(const RdbPredicateOperator op, const std::string &field, const std::string &value) 245 { 246 operations_.push_back({ op, field, { value } }); 247 } AddOperationPredicatesMemo248 inline void AddOperation( 249 const RdbPredicateOperator op, const std::string &field, const std::vector<std::string> &values) 250 { 251 operations_.push_back({ op, field, values }); 252 } 253 254 std::vector<std::string> tables_; 255 std::vector<std::string> devices_; 256 std::vector<RdbPredicateOperation> operations_; 257 }; 258 259 struct Date { DateDate260 Date() {} DateDate261 Date(int64_t date) : date(date) {} 262 operator double() const 263 { 264 return static_cast<double>(date); 265 } 266 int64_t date; 267 }; 268 269 class DetailProgressObserver { 270 public: ~DetailProgressObserver()271 virtual ~DetailProgressObserver() {}; 272 273 virtual void ProgressNotification(const Details &details) = 0; 274 }; 275 276 enum SubscribeMode { 277 REMOTE, 278 CLOUD, 279 CLOUD_DETAIL, 280 LOCAL, 281 LOCAL_SHARED, 282 LOCAL_DETAIL, 283 SUBSCRIBE_MODE_MAX 284 }; 285 286 struct SubscribeOption { 287 SubscribeMode mode; 288 std::string event; 289 }; 290 291 /** 292 * @brief Indicates the column type. 293 * 294 * Value returned by getColumnType(int) 295 */ 296 enum class ColumnType { 297 /** Indicates the column type is NULL.*/ 298 TYPE_NULL = 0, 299 /** Indicates the column type is INTEGER.*/ 300 TYPE_INTEGER, 301 /** Indicates the column type is FLOAT.*/ 302 TYPE_FLOAT, 303 /** Indicates the column type is STRING.*/ 304 TYPE_STRING, 305 /** Indicates the column type is BLOB.*/ 306 TYPE_BLOB, 307 /** Indicates the column type is ASSET.*/ 308 TYPE_ASSET, 309 /** Indicates the column type is ASSETS.*/ 310 TYPE_ASSETS, 311 /** Indicates the column type is Float32.*/ 312 TYPE_FLOAT32_ARRAY, 313 /** Indicates the column type is BigInt.*/ 314 TYPE_BIGINT 315 }; 316 317 struct Origin { 318 enum OriginType : int32_t { 319 ORIGIN_LOCAL, 320 ORIGIN_NEARBY, 321 ORIGIN_CLOUD, 322 ORIGIN_ALL, 323 ORIGIN_BUTT, 324 }; 325 enum DataType : int32_t { 326 BASIC_DATA, 327 ASSET_DATA, 328 TYPE_BUTT, 329 }; 330 int32_t origin = ORIGIN_ALL; 331 int32_t dataType = BASIC_DATA; 332 // origin is ORIGIN_LOCAL, the id is empty 333 // origin is ORIGIN_NEARBY, the id is networkId; 334 // origin is ORIGIN_CLOUD, the id is the cloud account id 335 std::vector<std::string> id; 336 std::string store; 337 }; 338 339 class RdbStoreObserver { 340 public: 341 enum ChangeType : int32_t { 342 CHG_TYPE_INSERT = 0, 343 CHG_TYPE_UPDATE, 344 CHG_TYPE_DELETE, 345 CHG_TYPE_BUTT 346 }; ~RdbStoreObserver()347 virtual ~RdbStoreObserver() {}; 348 using PrimaryKey = std::variant<std::monostate, std::string, int64_t, double>; 349 using ChangeInfo = std::map<std::string, std::vector<PrimaryKey>[CHG_TYPE_BUTT]>; 350 using PrimaryFields = std::map<std::string, std::string>; 351 virtual void OnChange(const std::vector<std::string> &devices) = 0; // networkid OnChange(const Origin & origin,const PrimaryFields & fields,ChangeInfo && changeInfo)352 virtual void OnChange(const Origin &origin, const PrimaryFields &fields, ChangeInfo &&changeInfo) 353 { 354 OnChange(origin.id); 355 }; OnChange()356 virtual void OnChange() {}; 357 }; 358 359 struct DropOption {}; 360 361 struct Field { 362 static constexpr const char *CURSOR_FIELD = "#_cursor"; 363 static constexpr const char *ORIGIN_FIELD = "#_origin"; 364 static constexpr const char *DELETED_FLAG_FIELD = "#_deleted_flag"; 365 static constexpr const char *DATA_STATUS_FIELD = "#_data_status"; 366 static constexpr const char *OWNER_FIELD = "#_cloud_owner"; 367 static constexpr const char *PRIVILEGE_FIELD = "#_cloud_privilege"; 368 static constexpr const char *SHARING_RESOURCE_FIELD = "#_sharing_resource_field"; 369 }; 370 371 struct RdbChangeProperties { 372 bool isTrackedDataChange = false; 373 bool isP2pSyncDataChange = false; 374 }; 375 376 struct RdbChangedData { 377 std::map<std::string, RdbChangeProperties> tableData; 378 }; 379 380 class SqlObserver { 381 public: 382 struct SqlExecutionInfo { 383 std::vector<std::string> sql_; 384 int64_t totalTime_; 385 int64_t waitTime_; 386 int64_t prepareTime_; 387 int64_t executeTime_; 388 }; 389 virtual ~SqlObserver() = default; 390 virtual void OnStatistic(const SqlExecutionInfo &info) = 0; 391 }; 392 } // namespace OHOS::DistributedRdb 393 #endif