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 <string> 23 #include <variant> 24 #include <vector> 25 #include <set> 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 RdbSyncerParam { 57 std::string bundleName_; 58 std::string hapName_; 59 std::string storeName_; 60 std::string customDir_; 61 int32_t area_ = 0; 62 int32_t level_ = 0; 63 int32_t haMode_ = 0; 64 int32_t type_ = RDB_DEVICE_COLLABORATION; 65 uint32_t roleType_ = 0; 66 bool isEncrypt_ = false; 67 bool isAutoClean_ = true; 68 bool isSearchable_ = false; 69 std::vector<uint8_t> password_; 70 std::map<std::string, RdbDebugInfo> infos_; ~RdbSyncerParamRdbSyncerParam71 ~RdbSyncerParam() 72 { 73 password_.assign(password_.size(), 0); 74 }; 75 }; 76 77 struct RdbNotifyConfig { 78 uint32_t delay_ = 0; 79 bool isFull_ = false; 80 }; 81 82 enum SyncMode { 83 PUSH, 84 PULL, 85 PULL_PUSH, 86 TIME_FIRST = 4, 87 NATIVE_FIRST, 88 CLOUD_FIRST, 89 }; 90 91 struct SyncOption { 92 SyncMode mode; 93 bool isBlock; 94 }; 95 96 enum DistributedTableType { 97 DISTRIBUTED_DEVICE = 0, 98 DISTRIBUTED_CLOUD, 99 DISTRIBUTED_SEARCH 100 }; 101 102 struct Reference { 103 std::string sourceTable; 104 std::string targetTable; 105 std::map<std::string, std::string> refFields; 106 }; 107 108 struct DistributedConfig { 109 bool autoSync = true; 110 std::vector<Reference> references = {}; 111 bool isRebuild = false; 112 }; 113 114 enum Progress { 115 SYNC_BEGIN = 0, 116 SYNC_IN_PROGRESS, 117 SYNC_FINISH, 118 }; 119 120 enum ProgressCode { 121 SUCCESS = 0, 122 UNKNOWN_ERROR, 123 NETWORK_ERROR, 124 CLOUD_DISABLED, 125 LOCKED_BY_OTHERS, 126 RECORD_LIMIT_EXCEEDED, 127 NO_SPACE_FOR_ASSET, 128 BLOCKED_BY_NETWORK_STRATEGY, 129 }; 130 131 struct Statistic { 132 uint32_t total; 133 uint32_t success; 134 uint32_t failed; 135 uint32_t untreated; 136 }; 137 138 struct TableDetail { 139 Statistic upload; 140 Statistic download; 141 }; 142 143 using TableDetails = std::map<std::string, TableDetail>; 144 145 struct ProgressDetail { 146 int32_t progress; 147 int32_t code; 148 TableDetails details; 149 }; 150 151 using Briefs = std::map<std::string, int>; 152 using Details = std::map<std::string, ProgressDetail>; 153 using AsyncBrief = std::function<void(const Briefs&)>; 154 using AsyncDetail = std::function<void(Details &&)>; 155 156 using SyncResult = Briefs; 157 using SyncCallback = AsyncBrief; 158 159 enum RdbPredicateOperator { 160 EQUAL_TO, 161 NOT_EQUAL_TO, 162 AND, 163 OR, 164 ORDER_BY, 165 LIMIT, 166 BEGIN_GROUP, 167 END_GROUP, 168 IN, 169 NOT_IN, 170 CONTAIN, 171 BEGIN_WITH, 172 END_WITH, 173 IS_NULL, 174 IS_NOT_NULL, 175 LIKE, 176 GLOB, 177 BETWEEN, 178 NOT_BETWEEN, 179 GREATER_THAN, 180 GREATER_THAN_OR_EQUAL, 181 LESS_THAN, 182 LESS_THAN_OR_EQUAL, 183 DISTINCT, 184 INDEXED_BY, 185 NOT_CONTAINS, 186 NOT_LIKE, 187 OPERATOR_MAX 188 }; 189 190 struct RdbPredicateOperation { 191 RdbPredicateOperator operator_; 192 std::string field_; 193 std::vector<std::string> values_; 194 }; 195 196 struct PredicatesMemo { AddOperationPredicatesMemo197 inline void AddOperation(const RdbPredicateOperator op, const std::string& field, 198 const std::string& value) 199 { 200 operations_.push_back({ op, field, { value } }); 201 } AddOperationPredicatesMemo202 inline void AddOperation(const RdbPredicateOperator op, const std::string& field, 203 const std::vector<std::string>& values) 204 { 205 operations_.push_back({ op, field, values }); 206 } 207 208 std::vector<std::string> tables_; 209 std::vector<std::string> devices_; 210 std::vector<RdbPredicateOperation> operations_; 211 }; 212 213 struct Date { DateDate214 Date() {} DateDate215 Date(int64_t date) : date(date) {} 216 operator double() const 217 { 218 return static_cast<double>(date); 219 } 220 int64_t date; 221 }; 222 223 class DetailProgressObserver { 224 public: ~DetailProgressObserver()225 virtual ~DetailProgressObserver() {}; 226 227 virtual void ProgressNotification(const Details& details) = 0; 228 }; 229 230 enum SubscribeMode { 231 REMOTE, 232 CLOUD, 233 CLOUD_DETAIL, 234 LOCAL, 235 LOCAL_SHARED, 236 LOCAL_DETAIL, 237 SUBSCRIBE_MODE_MAX 238 }; 239 240 struct SubscribeOption { 241 SubscribeMode mode; 242 std::string event; 243 }; 244 245 struct Origin { 246 enum OriginType : int32_t { 247 ORIGIN_LOCAL, 248 ORIGIN_NEARBY, 249 ORIGIN_CLOUD, 250 ORIGIN_ALL, 251 ORIGIN_BUTT, 252 }; 253 enum DataType : int32_t { 254 BASIC_DATA, 255 ASSET_DATA, 256 TYPE_BUTT, 257 }; 258 int32_t origin = ORIGIN_ALL; 259 int32_t dataType = BASIC_DATA; 260 // origin is ORIGIN_LOCAL, the id is empty 261 // origin is ORIGIN_NEARBY, the id is networkId; 262 // origin is ORIGIN_CLOUD, the id is the cloud account id 263 std::vector<std::string> id; 264 std::string store; 265 }; 266 267 class RdbStoreObserver { 268 public: 269 enum ChangeType : int32_t { 270 CHG_TYPE_INSERT = 0, 271 CHG_TYPE_UPDATE, 272 CHG_TYPE_DELETE, 273 CHG_TYPE_BUTT 274 }; ~RdbStoreObserver()275 virtual ~RdbStoreObserver() {}; 276 using PrimaryKey = std::variant<std::monostate, std::string, int64_t, double>; 277 using ChangeInfo = std::map<std::string, std::vector<PrimaryKey>[CHG_TYPE_BUTT]>; 278 using PrimaryFields = std::map<std::string, std::string>; 279 virtual void OnChange(const std::vector<std::string> &devices) = 0; // networkid OnChange(const Origin & origin,const PrimaryFields & fields,ChangeInfo && changeInfo)280 virtual void OnChange(const Origin &origin, const PrimaryFields &fields, ChangeInfo &&changeInfo) 281 { 282 OnChange(origin.id); 283 }; OnChange()284 virtual void OnChange() {}; 285 }; 286 287 struct DropOption { 288 }; 289 290 struct Field { 291 static constexpr const char *CURSOR_FIELD = "#_cursor"; 292 static constexpr const char *ORIGIN_FIELD = "#_origin"; 293 static constexpr const char *DELETED_FLAG_FIELD = "#_deleted_flag"; 294 static constexpr const char *DATA_STATUS_FIELD = "#_data_status"; 295 static constexpr const char *OWNER_FIELD = "#_cloud_owner"; 296 static constexpr const char *PRIVILEGE_FIELD = "#_cloud_privilege"; 297 static constexpr const char *SHARING_RESOURCE_FIELD = "#_sharing_resource_field"; 298 }; 299 300 struct RdbChangeProperties { 301 bool isTrackedDataChange = false; 302 }; 303 304 struct RdbChangedData { 305 std::map<std::string, RdbChangeProperties> tableData; 306 }; 307 308 class SqlObserver { 309 public: 310 struct SqlExecutionInfo { 311 std::vector<std::string> sql_; 312 int64_t totalTime_; 313 int64_t waitTime_; 314 int64_t prepareTime_; 315 int64_t executeTime_; 316 }; 317 virtual ~SqlObserver() = default; 318 virtual void OnStatistic(const SqlExecutionInfo &info) = 0; 319 }; 320 } // namespace OHOS::DistributedRdb 321 #endif 322