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 26 namespace OHOS::DistributedRdb { 27 enum RdbStatus { 28 RDB_OK, 29 RDB_ERROR, 30 }; 31 32 enum RdbDistributedType { 33 RDB_DEVICE_COLLABORATION = 10, 34 RDB_DISTRIBUTED_TYPE_MAX 35 }; 36 37 struct RdbSyncerParam { 38 std::string bundleName_; 39 std::string hapName_; 40 std::string storeName_; 41 std::string customDir_; 42 int32_t area_ = 0; 43 int32_t level_ = 0; 44 int32_t type_ = RDB_DEVICE_COLLABORATION; 45 bool isEncrypt_ = false; 46 bool isAutoClean_ = true; 47 bool isSearchable_ = false; 48 std::vector<uint8_t> password_; ~RdbSyncerParamRdbSyncerParam49 ~RdbSyncerParam() 50 { 51 password_.assign(password_.size(), 0); 52 }; 53 }; 54 55 enum SyncMode { 56 PUSH, 57 PULL, 58 PULL_PUSH, 59 TIME_FIRST = 4, 60 NATIVE_FIRST, 61 CLOUD_FIRST, 62 }; 63 64 struct SyncOption { 65 SyncMode mode; 66 bool isBlock; 67 }; 68 69 enum DistributedTableType { 70 DISTRIBUTED_DEVICE = 0, 71 DISTRIBUTED_CLOUD 72 }; 73 74 struct Reference { 75 std::string sourceTable; 76 std::string targetTable; 77 std::map<std::string, std::string> refFields; 78 }; 79 80 struct DistributedConfig { 81 bool autoSync = true; 82 std::vector<Reference> references = {}; 83 }; 84 85 enum Progress { 86 SYNC_BEGIN = 0, 87 SYNC_IN_PROGRESS, 88 SYNC_FINISH, 89 }; 90 91 enum ProgressCode { 92 SUCCESS = 0, 93 UNKNOWN_ERROR, 94 NETWORK_ERROR, 95 CLOUD_DISABLED, 96 LOCKED_BY_OTHERS, 97 RECORD_LIMIT_EXCEEDED, 98 NO_SPACE_FOR_ASSET, 99 }; 100 101 struct Statistic { 102 uint32_t total; 103 uint32_t success; 104 uint32_t failed; 105 uint32_t untreated; 106 }; 107 108 struct TableDetail { 109 Statistic upload; 110 Statistic download; 111 }; 112 113 using TableDetails = std::map<std::string, TableDetail>; 114 115 struct ProgressDetail { 116 int32_t progress; 117 int32_t code; 118 TableDetails details; 119 }; 120 121 using Briefs = std::map<std::string, int>; 122 using Details = std::map<std::string, ProgressDetail>; 123 using AsyncBrief = std::function<void(const Briefs&)>; 124 using AsyncDetail = std::function<void(Details &&)>; 125 126 using SyncResult = Briefs; 127 using SyncCallback = AsyncBrief; 128 129 enum RdbPredicateOperator { 130 EQUAL_TO, 131 NOT_EQUAL_TO, 132 AND, 133 OR, 134 ORDER_BY, 135 LIMIT, 136 BEGIN_GROUP, 137 END_GROUP, 138 IN, 139 NOT_IN, 140 CONTAIN, 141 BEGIN_WITH, 142 END_WITH, 143 IS_NULL, 144 IS_NOT_NULL, 145 LIKE, 146 GLOB, 147 BETWEEN, 148 NOT_BETWEEN, 149 GREATER_THAN, 150 GREATER_THAN_OR_EQUAL, 151 LESS_THAN, 152 LESS_THAN_OR_EQUAL, 153 DISTINCT, 154 INDEXED_BY, 155 OPERATOR_MAX 156 }; 157 158 struct RdbPredicateOperation { 159 RdbPredicateOperator operator_; 160 std::string field_; 161 std::vector<std::string> values_; 162 }; 163 164 struct PredicatesMemo { AddOperationPredicatesMemo165 inline void AddOperation(const RdbPredicateOperator op, const std::string& field, 166 const std::string& value) 167 { 168 operations_.push_back({ op, field, { value } }); 169 } AddOperationPredicatesMemo170 inline void AddOperation(const RdbPredicateOperator op, const std::string& field, 171 const std::vector<std::string>& values) 172 { 173 operations_.push_back({ op, field, values }); 174 } 175 176 std::vector<std::string> tables_; 177 std::vector<std::string> devices_; 178 std::vector<RdbPredicateOperation> operations_; 179 }; 180 181 struct Date { DateDate182 Date() {} DateDate183 Date(int64_t date) : date(date) {} 184 operator double() const 185 { 186 return static_cast<double>(date); 187 } 188 int64_t date; 189 }; 190 191 class DetailProgressObserver { 192 public: ~DetailProgressObserver()193 virtual ~DetailProgressObserver() {}; 194 195 virtual void ProgressNotification(const Details& details) = 0; 196 }; 197 198 enum SubscribeMode { 199 REMOTE, 200 CLOUD, 201 CLOUD_DETAIL, 202 LOCAL, 203 LOCAL_SHARED, 204 SUBSCRIBE_MODE_MAX 205 }; 206 207 struct SubscribeOption { 208 SubscribeMode mode; 209 std::string event; 210 }; 211 212 struct Origin { 213 enum OriginType : int32_t { 214 ORIGIN_LOCAL, 215 ORIGIN_NEARBY, 216 ORIGIN_CLOUD, 217 ORIGIN_ALL, 218 ORIGIN_BUTT, 219 }; 220 enum DataType : int32_t { 221 BASIC_DATA, 222 ASSET_DATA, 223 TYPE_BUTT, 224 }; 225 int32_t origin = ORIGIN_ALL; 226 int32_t dataType = BASIC_DATA; 227 // origin is ORIGIN_LOCAL, the id is empty 228 // origin is ORIGIN_NEARBY, the id is networkId; 229 // origin is ORIGIN_CLOUD, the id is the cloud account id 230 std::vector<std::string> id; 231 std::string store; 232 }; 233 234 class RdbStoreObserver { 235 public: 236 enum ChangeType : int32_t { 237 CHG_TYPE_INSERT = 0, 238 CHG_TYPE_UPDATE, 239 CHG_TYPE_DELETE, 240 CHG_TYPE_BUTT 241 }; ~RdbStoreObserver()242 virtual ~RdbStoreObserver() {}; 243 using PrimaryKey = std::variant<std::monostate, std::string, int64_t, double>; 244 using ChangeInfo = std::map<std::string, std::vector<PrimaryKey>[CHG_TYPE_BUTT]>; 245 using PrimaryFields = std::map<std::string, std::string>; 246 virtual void OnChange(const std::vector<std::string> &devices) = 0; // networkid OnChange(const Origin & origin,const PrimaryFields & fields,ChangeInfo && changeInfo)247 virtual void OnChange(const Origin &origin, const PrimaryFields &fields, ChangeInfo &&changeInfo) 248 { 249 OnChange(origin.id); 250 }; OnChange()251 virtual void OnChange() {}; 252 }; 253 254 struct DropOption { 255 }; 256 257 struct Field { 258 static constexpr const char *CURSOR_FIELD = "#_cursor"; 259 static constexpr const char *ORIGIN_FIELD = "#_origin"; 260 static constexpr const char *DELETED_FLAG_FIELD = "#_deleted_flag"; 261 static constexpr const char *OWNER_FIELD = "#_cloud_owner"; 262 static constexpr const char *PRIVILEGE_FIELD = "#_cloud_privilege"; 263 static constexpr const char *SHARING_RESOURCE_FIELD = "#_sharing_resource_field"; 264 }; 265 266 struct RdbChangeProperties { 267 bool isTrackedDataChange = false; 268 }; 269 270 struct RdbChangedData { 271 std::map<std::string, RdbChangeProperties> tableData; 272 }; 273 } // namespace OHOS::DistributedRdb 274 #endif 275