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 <securec.h> 20 21 #include <cinttypes> 22 #include <functional> 23 #include <map> 24 #include <string> 25 #include <variant> 26 #include <vector> 27 28 namespace OHOS::DistributedRdb { 29 enum RdbStatus { 30 RDB_OK, 31 RDB_ERROR, 32 }; 33 34 enum RdbDistributedType { 35 RDB_DEVICE_COLLABORATION = 10, 36 RDB_DISTRIBUTED_TYPE_MAX 37 }; 38 39 struct RdbSyncerParam { 40 std::string bundleName_; 41 std::string hapName_; 42 std::string storeName_; 43 int32_t area_ = 0; 44 int32_t level_ = 0; 45 int32_t type_ = RDB_DEVICE_COLLABORATION; 46 bool isAutoSync_ = false; 47 bool isEncrypt_ = 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 DistributedConfig { 75 bool autoSync = true; 76 }; 77 78 enum Progress { 79 SYNC_BEGIN, 80 SYNC_IN_PROGRESS, 81 SYNC_FINISH, 82 }; 83 84 enum ProgressCode { 85 SUCCESS = 0, 86 UNKNOWN_ERROR, 87 NETWORK_ERROR, 88 CLOUD_DISABLED, 89 LOCKED_BY_OTHERS, 90 RECORD_LIMIT_EXCEEDED, 91 NO_SPACE_FOR_ASSET, 92 }; 93 94 struct Statistic { 95 uint32_t total; 96 uint32_t success; 97 uint32_t failed; 98 uint32_t untreated; 99 }; 100 101 struct TableDetail { 102 Statistic upload; 103 Statistic download; 104 }; 105 106 using TableDetails = std::map<std::string, TableDetail>; 107 108 struct ProgressDetail { 109 int32_t progress; 110 int32_t code; 111 TableDetails details; 112 }; 113 114 using Briefs = std::map<std::string, int>; 115 using Details = std::map<std::string, ProgressDetail>; 116 using AsyncBrief = std::function<void(const Briefs&)>; 117 using AsyncDetail = std::function<void(Details &&)>; 118 119 using SyncResult = Briefs; 120 using SyncCallback = AsyncBrief; 121 122 enum RdbPredicateOperator { 123 EQUAL_TO, 124 NOT_EQUAL_TO, 125 AND, 126 OR, 127 ORDER_BY, 128 LIMIT, 129 OPERATOR_MAX 130 }; 131 132 struct RdbPredicateOperation { 133 RdbPredicateOperator operator_; 134 std::string field_; 135 std::vector<std::string> values_; 136 }; 137 138 struct PredicatesMemo { AddOperationPredicatesMemo139 inline void AddOperation(const RdbPredicateOperator op, const std::string& field, 140 const std::string& value) 141 { 142 operations_.push_back({ op, field, { value } }); 143 } AddOperationPredicatesMemo144 inline void AddOperation(const RdbPredicateOperator op, const std::string& field, 145 const std::vector<std::string>& values) 146 { 147 operations_.push_back({ op, field, values }); 148 } 149 150 std::vector<std::string> tables_; 151 std::vector<std::string> devices_; 152 std::vector<RdbPredicateOperation> operations_; 153 }; 154 155 struct Date { DateDate156 Date() {} DateDate157 Date(int64_t date) : date(date) {} 158 operator double() const 159 { 160 return static_cast<double>(date); 161 } 162 int64_t date; 163 }; 164 165 enum SubscribeMode { 166 REMOTE, 167 CLOUD, 168 CLOUD_DETAIL, 169 LOCAL, 170 LOCAL_SHARED, 171 SUBSCRIBE_MODE_MAX 172 }; 173 174 struct SubscribeOption { 175 SubscribeMode mode; 176 std::string event; 177 }; 178 179 struct Origin { 180 enum OriginType : int32_t { 181 ORIGIN_LOCAL, 182 ORIGIN_NEARBY, 183 ORIGIN_CLOUD, 184 ORIGIN_ALL, 185 ORIGIN_BUTT, 186 }; 187 enum DataType : int32_t { 188 BASIC_DATA, 189 ASSET_DATA, 190 TYPE_BUTT, 191 }; 192 int32_t origin = ORIGIN_ALL; 193 int32_t dataType = BASIC_DATA; 194 // origin is ORIGIN_LOCAL, the id is empty 195 // origin is ORIGIN_NEARBY, the id is networkId; 196 // origin is ORIGIN_CLOUD, the id is the cloud account id 197 std::vector<std::string> id; 198 std::string store; 199 }; 200 201 class RdbStoreObserver { 202 public: 203 enum ChangeType : int32_t { 204 CHG_TYPE_INSERT = 0, 205 CHG_TYPE_UPDATE, 206 CHG_TYPE_DELETE, 207 CHG_TYPE_BUTT 208 }; ~RdbStoreObserver()209 virtual ~RdbStoreObserver() {}; 210 using PrimaryKey = std::variant<std::monostate, std::string, int64_t, double>; 211 using ChangeInfo = std::map<std::string, std::vector<PrimaryKey>[CHG_TYPE_BUTT]>; 212 using PrimaryFields = std::map<std::string, std::string>; 213 virtual void OnChange(const std::vector<std::string> &devices) = 0; // networkid OnChange(const Origin & origin,const PrimaryFields & fields,ChangeInfo && changeInfo)214 virtual void OnChange(const Origin &origin, const PrimaryFields &fields, ChangeInfo &&changeInfo) 215 { 216 OnChange(origin.id); 217 }; OnChange()218 virtual void OnChange() {}; 219 }; 220 221 struct DropOption { 222 }; 223 } // namespace OHOS::DistributedRdb 224 #endif 225