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 class AccountDataStorage { 29 public: 30 AccountDataStorage() = delete; 31 AccountDataStorage(const std::string &appId, const std::string &storeId, const bool &autoSync); 32 virtual ~AccountDataStorage(); 33 ErrCode LoadAllData(std::map<std::string, std::shared_ptr<IAccountInfo>> &infos); 34 ErrCode AddAccountInfo(const IAccountInfo &iAccountInfo); 35 ErrCode SaveAccountInfo(const IAccountInfo &iAccountInfo); 36 ErrCode LoadDataByLocalFuzzyQuery(std::string subId, std::map<std::string, std::shared_ptr<IAccountInfo>> &infos); 37 void TryTwice(const std::function<DistributedKv::Status()> &func) const; 38 virtual void SaveEntries(std::vector<OHOS::DistributedKv::Entry> allEntries, 39 std::map<std::string, std::shared_ptr<IAccountInfo>> &infos) = 0; 40 int DeleteKvStore(); 41 ErrCode GetAccountInfoById(const std::string id, IAccountInfo &iAccountInfo); 42 bool IsKeyExists(const std::string keyStr); 43 ErrCode PutValueToKvStore(const std::string &keyStr, const std::string &valueStr); 44 ErrCode GetValueFromKvStore(const std::string &keyStr, std::string &valueStr); 45 ErrCode RemoveValueFromKvStore(const std::string &keyStr); 46 47 protected: 48 OHOS::DistributedKv::Status GetEntries( 49 std::string subId, std::vector<OHOS::DistributedKv::Entry> &allEntries) const; 50 OHOS::DistributedKv::Status GetKvStore(); 51 OHOS::DistributedKv::DistributedKvDataManager dataManager_; 52 std::shared_ptr<OHOS::DistributedKv::SingleKvStore> kvStorePtr_; 53 mutable std::mutex kvStorePtrMutex_; 54 bool CheckKvStore(); 55 OHOS::DistributedKv::AppId appId_; 56 OHOS::DistributedKv::StoreId storeId_; 57 bool autoSync_; 58 }; 59 } // namespace AccountSA 60 } // namespace OHOS 61 62 #endif // OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_ACCOUNT_DATA_STORAGE_H 63