1 /* 2 * Copyright (c) 2021 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_KVSTORE_TYPES_H 17 #define DISTRIBUTED_KVSTORE_TYPES_H 18 19 #include <algorithm> 20 #include <cstdint> 21 #include <string> 22 #include <variant> 23 #include <vector> 24 #include "blob.h" 25 #include "store_errno.h" 26 #include "visibility.h" 27 28 namespace OHOS { 29 namespace DistributedKv { 30 /** 31 * @brief Key set by client, can be any non-empty bytes array, and less than 1024 size. 32 */ 33 using Key = OHOS::DistributedKv::Blob; 34 35 /** 36 * @brief Value set by client, can be any bytes array. 37 */ 38 using Value = OHOS::DistributedKv::Blob; 39 40 /** 41 * @brief User identifier from user-account. 42 */ 43 struct UserId { 44 std::string userId; 45 }; 46 47 /** 48 * @brief App identifier from bms. 49 */ 50 struct API_EXPORT AppId { 51 std::string appId; 52 53 /** 54 * @brief Support appId convert to std::string. 55 */ 56 operator std::string &() noexcept 57 { 58 return appId; 59 } 60 61 /** 62 * @brief Support appId convert to const std::string. 63 */ 64 operator const std::string &() const noexcept 65 { 66 return appId; 67 } 68 69 /** 70 * @brief Check appId. 71 */ IsValidAppId72 inline bool IsValid() const 73 { 74 if (appId.empty() || appId.size() > MAX_APP_ID_LEN) { 75 return false; 76 } 77 int count = 0; 78 auto iter = std::find_if_not(appId.begin(), appId.end(), 79 [&count](char c) { 80 count = (c == SEPARATOR_CHAR) ? (count + 1) : (count >= SEPARATOR_COUNT ? count : 0); 81 return (std::isprint(c) && c != '/'); 82 }); 83 84 return (iter == appId.end()) && (count < SEPARATOR_COUNT); 85 } 86 private: 87 static constexpr int MAX_APP_ID_LEN = 256; 88 static constexpr int SEPARATOR_COUNT = 3; 89 static constexpr char SEPARATOR_CHAR = '#'; 90 }; 91 92 /** 93 * @brief Kvstore name set by client by calling GetKvStore. 94 * 95 * storeId length must be less or equal than 256, 96 * and can not be empty and all space. 97 */ 98 struct API_EXPORT StoreId { 99 std::string storeId; 100 101 /** 102 * @brief Support storeId convert to std::string. 103 */ 104 operator std::string &() noexcept 105 { 106 return storeId; 107 } 108 109 /** 110 * @brief Support storeId convert to const std::string. 111 */ 112 operator const std::string &() const noexcept 113 { 114 return storeId; 115 } 116 117 /** 118 * @brief Operator <. 119 */ 120 bool operator<(const StoreId &id) const noexcept 121 { 122 return this->storeId < id.storeId; 123 } 124 125 /** 126 * @brief Check storeId. 127 */ IsValidStoreId128 inline bool IsValid() const 129 { 130 if (storeId.empty() || storeId.size() > MAX_STORE_ID_LEN) { 131 return false; 132 } 133 auto iter = std::find_if_not(storeId.begin(), storeId.end(), 134 [](char c) { return (std::isdigit(c) || std::isalpha(c) || c == '_'); }); 135 return (iter == storeId.end()); 136 } 137 private: 138 static constexpr int MAX_STORE_ID_LEN = 128; 139 }; 140 141 /** 142 * @brief Identifier unique database. 143 */ 144 struct KvStoreTuple { 145 std::string userId; 146 std::string appId; 147 std::string storeId; 148 }; 149 150 /** 151 * @brief App thread information. 152 */ 153 struct AppThreadInfo { 154 std::int32_t pid; 155 std::int32_t uid; 156 }; 157 158 /** 159 * @brief The type for observer database change. 160 */ 161 enum SubscribeType : uint32_t { 162 /** 163 * Local changes of syncable kv store. 164 */ 165 SUBSCRIBE_TYPE_LOCAL = 1, 166 /** 167 * Synced data changes from remote devices 168 */ 169 SUBSCRIBE_TYPE_REMOTE = 2, 170 /** 171 * Both local changes and synced data changes. 172 */ 173 SUBSCRIBE_TYPE_ALL = 3, 174 }; 175 176 /** 177 * @brief Data is organized by entry definition. 178 */ 179 struct Entry { 180 Key key; 181 Value value; 182 183 static constexpr size_t MAX_KEY_LENGTH = 1024; 184 static constexpr size_t MAX_VALUE_LENGTH = 4 * 1024 * 1024; 185 186 /** 187 * Write blob size and data to memory buffer. 188 * Return error when bufferLeftSize not enough. 189 */ WriteToBufferEntry190 bool WriteToBuffer(uint8_t *&cursorPtr, int &bufferLeftSize) const 191 { 192 return key.WriteToBuffer(cursorPtr, bufferLeftSize) && value.WriteToBuffer(cursorPtr, bufferLeftSize); 193 } 194 195 /** 196 * Read a blob from memory buffer. 197 */ ReadFromBufferEntry198 bool ReadFromBuffer(const uint8_t *&cursorPtr, int &bufferLeftSize) 199 { 200 return key.ReadFromBuffer(cursorPtr, bufferLeftSize) && value.ReadFromBuffer(cursorPtr, bufferLeftSize); 201 } 202 RawSizeEntry203 int RawSize() const 204 { 205 return key.RawSize() + value.RawSize(); 206 } 207 }; 208 209 /** 210 * @brief Indicate how to sync data on sync operation. 211 */ 212 enum SyncMode : int32_t { 213 /** 214 * Sync remote data to local. 215 */ 216 PULL, 217 /** 218 * Sync local data to remote. 219 */ 220 PUSH, 221 /** 222 * Both push and pull. 223 */ 224 PUSH_PULL, 225 }; 226 227 /** 228 * @brief The kvstore type. 229 */ 230 enum KvStoreType : int32_t { 231 /** 232 * Multi-device collaboration. 233 * The data is managed by the dimension of the device, 234 * and there is no conflict. 235 * Support to query data according to the dimension of equipment. 236 */ 237 DEVICE_COLLABORATION, 238 /** 239 * Data is not divided into devices. 240 * Modifying the same key between devices will overwrite. 241 */ 242 SINGLE_VERSION, 243 /** 244 * Not support type. 245 */ 246 MULTI_VERSION, 247 INVALID_TYPE, 248 }; 249 250 /** 251 * @brief Enumeration of database security level. 252 */ 253 enum SecurityLevel : int32_t { 254 NO_LABEL, 255 S0, 256 S1, 257 S2, 258 S3_EX, 259 S3, 260 S4, 261 }; 262 263 /** 264 * @brief Enumeration of database base directory. 265 */ 266 enum Area : int32_t { 267 EL0, 268 EL1, 269 EL2, 270 EL3, 271 EL4 272 }; 273 274 enum KvControlCmd : int32_t { 275 SET_SYNC_PARAM = 1, 276 GET_SYNC_PARAM, 277 }; 278 279 using KvParam = OHOS::DistributedKv::Blob; 280 281 struct KvSyncParam { 282 uint32_t allowedDelayMs { 0 }; 283 }; 284 285 /** 286 * @brief Device basic information. 287 * 288 * Including device id, name and type. 289 */ 290 struct DeviceInfo { 291 std::string deviceId; 292 std::string deviceName; 293 std::string deviceType; 294 }; 295 296 /** 297 * @brief Device filter strategy. 298 */ 299 enum class DeviceFilterStrategy { 300 FILTER = 0, 301 NO_FILTER = 1, 302 }; 303 304 /** 305 * @brief Indicate how and when to sync data with other device. 306 */ 307 enum PolicyType : uint32_t { 308 /** 309 * Data synchronization within valid time. 310 */ 311 TERM_OF_SYNC_VALIDITY, 312 /** 313 * Data synchronization when device manager module call online operation. 314 */ 315 IMMEDIATE_SYNC_ON_ONLINE, 316 /** 317 * Data synchronization when put, delete or update database. 318 */ 319 IMMEDIATE_SYNC_ON_CHANGE, 320 /** 321 * Data synchronization when device manager module call onready operation. 322 */ 323 IMMEDIATE_SYNC_ON_READY, 324 POLICY_BUTT 325 }; 326 327 /** 328 * @brief Policy Type value. 329 */ 330 struct SyncPolicy { 331 uint32_t type; 332 std::variant<std::monostate, uint32_t> value; 333 }; 334 335 /** 336 * @brief Provide configuration information for database creation. 337 */ 338 struct Options { 339 /** 340 * Whether to create a database when the database file does not exist. 341 * It is created by default. 342 */ 343 bool createIfMissing = true; 344 /** 345 * Set whether the database file is encrypted. 346 * It is not encrypted by default. 347 */ 348 bool encrypt = false; 349 /** 350 * Data storage disk by default. 351 */ 352 bool persistent = true; 353 /** 354 * Set whether the database file is backed up. 355 * It is back up by default. 356 */ 357 bool backup = true; 358 /** 359 * Set whether the database file is automatically synchronized. 360 * It is not automatically synchronized by default. 361 * 'ohos.permission.DISTRIBUTED_DATASYNC' permission is necessary. 362 */ 363 bool autoSync = true; 364 /** 365 * True if is distributed store, false if is local store 366 */ 367 bool syncable = true; 368 /** 369 * Set Whether rebuild the database. 370 */ 371 bool rebuild = false; 372 /** 373 * Set database security level. 374 */ 375 int32_t securityLevel = NO_LABEL; 376 /** 377 * Set database directory area. 378 */ 379 int32_t area = EL1; 380 /** 381 * Set the type of database to be created. 382 * The default is multi-device collaboration database. 383 */ 384 KvStoreType kvStoreType = DEVICE_COLLABORATION; 385 /** 386 * The sync policy vector. 387 */ 388 std::vector<SyncPolicy> policies{ { IMMEDIATE_SYNC_ON_CHANGE } }; 389 /** 390 * Set the value stored in the database. 391 * Schema is not used by default. 392 */ 393 std::string schema = ""; 394 /** 395 * Set database directory hapName. 396 */ 397 std::string hapName = ""; 398 /** 399 * Set database directory baseDir. 400 */ 401 std::string baseDir = ""; 402 /** 403 * Whether the kvstore type is valid. 404 */ IsValidTypeOptions405 inline bool IsValidType() const 406 { 407 return kvStoreType == KvStoreType::DEVICE_COLLABORATION || kvStoreType == KvStoreType::SINGLE_VERSION; 408 } 409 }; 410 411 /** 412 * @brief Provide the user information. 413 */ 414 struct UserInfo { 415 /** 416 * The userId Info. 417 */ 418 std::string userId; 419 420 /** 421 * The userType Info. 422 */ 423 int32_t userType; 424 }; 425 } // namespace DistributedKv 426 } // namespace OHOS 427 #endif // DISTRIBUTED_KVSTORE_TYPES_H 428