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