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 LOCAL_ONLY, 248 INVALID_TYPE, 249 }; 250 251 /** 252 * @brief Enumeration of database security level. 253 */ 254 enum SecurityLevel : int32_t { 255 INVALID_LABEL = -1, 256 NO_LABEL, 257 S0, 258 S1, 259 S2, 260 S3_EX, 261 S3, 262 S4, 263 }; 264 265 /** 266 * @brief Enumeration of database base directory. 267 */ 268 enum Area : int32_t { 269 EL0, 270 EL1, 271 EL2, 272 EL3, 273 EL4 274 }; 275 276 enum KvControlCmd : int32_t { 277 SET_SYNC_PARAM = 1, 278 GET_SYNC_PARAM, 279 }; 280 281 using KvParam = OHOS::DistributedKv::Blob; 282 283 struct KvSyncParam { 284 uint32_t allowedDelayMs { 0 }; 285 }; 286 287 /** 288 * @brief Device basic information. 289 * 290 * Including device id, name and type. 291 */ 292 struct DeviceInfo { 293 std::string deviceId; 294 std::string deviceName; 295 std::string deviceType; 296 }; 297 298 /** 299 * @brief Device filter strategy. 300 */ 301 enum class DeviceFilterStrategy { 302 FILTER = 0, 303 NO_FILTER = 1, 304 }; 305 306 /** 307 * @brief Indicate how and when to sync data with other device. 308 */ 309 enum PolicyType : uint32_t { 310 /** 311 * Data synchronization within valid time. 312 */ 313 TERM_OF_SYNC_VALIDITY, 314 /** 315 * Data synchronization when device manager module call online operation. 316 */ 317 IMMEDIATE_SYNC_ON_ONLINE, 318 /** 319 * Data synchronization when put, delete or update database. 320 */ 321 IMMEDIATE_SYNC_ON_CHANGE, 322 /** 323 * Data synchronization when device manager module call onready operation. 324 */ 325 IMMEDIATE_SYNC_ON_READY, 326 POLICY_BUTT 327 }; 328 329 /** 330 * @brief Policy Type value. 331 */ 332 struct SyncPolicy { 333 uint32_t type; 334 std::variant<std::monostate, uint32_t> value; 335 }; 336 337 /** 338 * @brief Role Type value. 339 */ 340 enum RoleType : uint32_t { 341 /** 342 * The user has administrative rights. 343 */ 344 OWNER = 0, 345 /** 346 * The user has read-only permission. 347 */ 348 VISITOR, 349 }; 350 351 struct Group { 352 std::string groupDir = ""; 353 std::string groupId = ""; 354 }; 355 356 /** 357 * @brief Provide configuration information for database creation. 358 */ 359 struct Options { 360 /** 361 * Whether to create a database when the database file does not exist. 362 * It is created by default. 363 */ 364 bool createIfMissing = true; 365 /** 366 * Set whether the database file is encrypted. 367 * It is not encrypted by default. 368 */ 369 bool encrypt = false; 370 /** 371 * Data storage disk by default. 372 */ 373 bool persistent = true; 374 /** 375 * Set whether the database file is backed up. 376 * It is back up by default. 377 */ 378 bool backup = true; 379 /** 380 * Set whether the database file is automatically synchronized. 381 * It is not automatically synchronized by default. 382 * 'ohos.permission.DISTRIBUTED_DATASYNC' permission is necessary. 383 */ 384 bool autoSync = false; 385 /** 386 * True if is distributed store, false if is local store 387 */ 388 bool syncable = true; 389 /** 390 * Set Whether rebuild the database. 391 */ 392 bool rebuild = false; 393 /** 394 * Set database security level. 395 */ 396 int32_t securityLevel = INVALID_LABEL; 397 /** 398 * Set database directory area. 399 */ 400 int32_t area = EL1; 401 /** 402 * Set the type of database to be created. 403 * The default is multi-device collaboration database. 404 */ 405 KvStoreType kvStoreType = DEVICE_COLLABORATION; 406 /** 407 * The sync policy vector. 408 */ 409 std::vector<SyncPolicy> policies{ { IMMEDIATE_SYNC_ON_CHANGE } }; 410 /** 411 * Set the value stored in the database. 412 * Schema is not used by default. 413 */ 414 std::string schema = ""; 415 /** 416 * Set database directory hapName. 417 */ 418 std::string hapName = ""; 419 /** 420 * Set database directory baseDir. 421 */ 422 std::string baseDir = ""; 423 /** 424 * Whether the kvstore type is valid. 425 */ IsValidTypeOptions426 inline bool IsValidType() const 427 { 428 return kvStoreType == KvStoreType::DEVICE_COLLABORATION || kvStoreType == KvStoreType::SINGLE_VERSION || 429 kvStoreType == KvStoreType::LOCAL_ONLY; 430 } 431 /** 432 * Get the databaseDir. 433 */ GetDatabaseDirOptions434 inline std::string GetDatabaseDir() const 435 { 436 if (baseDir.empty()) { 437 return group.groupDir; 438 } 439 return !group.groupDir.empty() ? "" : baseDir; 440 } 441 /** 442 * Whether the databaseDir is valid. 443 */ IsPathValidOptions444 inline bool IsPathValid() const 445 { 446 if ((baseDir.empty() && group.groupDir.empty()) || (!baseDir.empty() && !group.groupDir.empty())) { 447 return false; 448 } 449 return true; 450 } 451 Group group; 452 /** 453 * Set database role. 454 */ 455 RoleType role; 456 /** 457 * Whether the sync happend in client. 458 */ 459 bool isClientSync = false; 460 /** 461 * Whether the sync need compress. 462 */ 463 bool isNeedCompress = true; 464 }; 465 466 /** 467 * @brief Provide the user information. 468 */ 469 struct UserInfo { 470 /** 471 * The userId Info. 472 */ 473 std::string userId; 474 475 /** 476 * The userType Info. 477 */ 478 int32_t userType; 479 }; 480 } // namespace DistributedKv 481 } // namespace OHOS 482 #endif // DISTRIBUTED_KVSTORE_TYPES_H 483