1 /* 2 * Copyright (c) 2021-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 OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_ACCOUNT_DATA_STORAGE_H 17 #define OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_ACCOUNT_DATA_STORAGE_H 18 19 #include <string> 20 #include <map> 21 22 #include "distributed_kv_data_manager.h" 23 #include "account_error_no.h" 24 #include "iaccount_info.h" 25 26 namespace OHOS { 27 namespace AccountSA { 28 struct AccountDataStorageOptions { 29 bool autoSync = false; 30 DistributedKv::SecurityLevel securityLevel = DistributedKv::SecurityLevel::S1; 31 OHOS::DistributedKv::Area area = OHOS::DistributedKv::EL1; 32 std::string baseDir; 33 }; 34 35 class AccountDataStorage { 36 public: 37 AccountDataStorage() = delete; 38 AccountDataStorage(const std::string &appId, const std::string &storeId, const AccountDataStorageOptions &options); 39 virtual ~AccountDataStorage(); 40 ErrCode LoadAllData(std::map<std::string, std::shared_ptr<IAccountInfo>> &infos); 41 ErrCode AddAccountInfo(const IAccountInfo &iAccountInfo); 42 ErrCode SaveAccountInfo(const IAccountInfo &iAccountInfo); 43 ErrCode LoadDataByLocalFuzzyQuery(std::string subId, std::map<std::string, std::shared_ptr<IAccountInfo>> &infos); 44 void TryTwice(const std::function<DistributedKv::Status()> &func) const; 45 virtual void SaveEntries(std::vector<OHOS::DistributedKv::Entry> allEntries, 46 std::map<std::string, std::shared_ptr<IAccountInfo>> &infos) = 0; 47 int DeleteKvStore(); 48 ErrCode GetAccountInfoById(const std::string id, IAccountInfo &iAccountInfo); 49 bool IsKeyExists(const std::string keyStr); 50 ErrCode PutValueToKvStore(const std::string &keyStr, const std::string &valueStr); 51 ErrCode GetValueFromKvStore(const std::string &keyStr, std::string &valueStr); 52 ErrCode RemoveValueFromKvStore(const std::string &keyStr); 53 54 protected: 55 OHOS::DistributedKv::Status GetEntries( 56 std::string subId, std::vector<OHOS::DistributedKv::Entry> &allEntries) const; 57 OHOS::DistributedKv::Status GetKvStore(); 58 bool CheckKvStore(); 59 OHOS::DistributedKv::DistributedKvDataManager dataManager_; 60 std::shared_ptr<OHOS::DistributedKv::SingleKvStore> kvStorePtr_; 61 mutable std::mutex kvStorePtrMutex_; 62 OHOS::DistributedKv::AppId appId_; 63 OHOS::DistributedKv::StoreId storeId_; 64 AccountDataStorageOptions options_; 65 std::string baseDir_; 66 }; 67 } // namespace AccountSA 68 } // namespace OHOS 69 70 #endif // OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_ACCOUNT_DATA_STORAGE_H 71