• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "account_error_no.h"
23 #ifndef SQLITE_DLCLOSE_ENABLE
24 #include "distributed_kv_data_manager.h"
25 #else
26 #include "database_adapter_loader.h"
27 #endif // SQLITE_DLCLOSE_ENABLE
28 #include "iaccount_info.h"
29 
30 namespace OHOS {
31 namespace AccountSA {
32 #ifndef SQLITE_DLCLOSE_ENABLE
33 struct AccountDataStorageOptions {
34     bool encrypt = false;
35     bool autoSync = false;
36     DistributedKv::SecurityLevel securityLevel = DistributedKv::SecurityLevel::S1;
37     OHOS::DistributedKv::Area area = OHOS::DistributedKv::EL1;
38     std::string baseDir;
39 };
40 #endif // SQLITE_DLCLOSE_ENABLE
41 
42 #ifndef SQLITE_DLCLOSE_ENABLE
43 class AccountDataStorage {
44 public:
45     AccountDataStorage() = delete;
46     AccountDataStorage(const std::string &appId, const std::string &storeId, const AccountDataStorageOptions &options);
47     virtual ~AccountDataStorage();
48     ErrCode LoadAllData(std::map<std::string, std::shared_ptr<IAccountInfo>> &infos);
49     ErrCode AddAccountInfo(const IAccountInfo &iAccountInfo);
50     ErrCode SaveAccountInfo(const IAccountInfo &iAccountInfo);
51     ErrCode LoadDataByLocalFuzzyQuery(std::string subId, std::map<std::string, std::shared_ptr<IAccountInfo>> &infos);
52     void TryTwice(const std::function<DistributedKv::Status()> &func) const;
53     virtual void SaveEntries(std::vector<OHOS::DistributedKv::Entry> allEntries,
54         std::map<std::string, std::shared_ptr<IAccountInfo>> &infos) = 0;
55     ErrCode Close();
56     int DeleteKvStore();
57     ErrCode GetAccountInfoById(const std::string id, IAccountInfo &iAccountInfo);
58     bool IsKeyExists(const std::string keyStr);
59     ErrCode PutValueToKvStore(const std::string &keyStr, const std::string &valueStr);
60     ErrCode GetValueFromKvStore(const std::string &keyStr, std::string &valueStr);
61     ErrCode RemoveValueFromKvStore(const std::string &keyStr);
62     ErrCode MoveData(const std::shared_ptr<AccountDataStorage> &ptr);
63 
64 protected:
65     OHOS::DistributedKv::Status GetEntries(
66         std::string subId, std::vector<OHOS::DistributedKv::Entry> &allEntries) const;
67     OHOS::DistributedKv::Status GetKvStore();
68     bool CheckKvStore();
69     OHOS::DistributedKv::DistributedKvDataManager dataManager_;
70     std::shared_ptr<OHOS::DistributedKv::SingleKvStore> kvStorePtr_;
71     mutable std::mutex kvStorePtrMutex_;
72     OHOS::DistributedKv::AppId appId_;
73     OHOS::DistributedKv::StoreId storeId_;
74     AccountDataStorageOptions options_;
75     std::string baseDir_;
76 };
77 #else
78 class AccountDataStorage {
79 public:
80     AccountDataStorage() = delete;
81     AccountDataStorage(const std::string &appId, const std::string &storeId, const DbAdapterOptions &options);
82     virtual ~AccountDataStorage();
83     ErrCode LoadAllData(std::map<std::string, std::shared_ptr<IAccountInfo>> &infos);
84     ErrCode AddAccountInfo(const IAccountInfo &iAccountInfo);
85     ErrCode SaveAccountInfo(const IAccountInfo &iAccountInfo);
86     ErrCode LoadDataByLocalFuzzyQuery(std::string subId, std::map<std::string, std::shared_ptr<IAccountInfo>> &infos);
87     void TryTwice(const std::function<DbAdapterStatus()> &func) const;
88     virtual void SaveEntries(std::vector<DbAdapterEntry> allEntries,
89         std::map<std::string, std::shared_ptr<IAccountInfo>> &infos) = 0;
90     ErrCode Close();
91     int DeleteKvStore();
92     ErrCode GetAccountInfoById(const std::string id, IAccountInfo &iAccountInfo);
93     bool IsKeyExists(const std::string keyStr);
94     ErrCode PutValueToKvStore(const std::string &keyStr, const std::string &valueStr);
95     ErrCode GetValueFromKvStore(const std::string &keyStr, std::string &valueStr);
96     ErrCode RemoveValueFromKvStore(const std::string &keyStr);
97     ErrCode MoveData(const std::shared_ptr<AccountDataStorage> &ptr);
98 
99 protected:
100     DbAdapterStatus GetEntries(
101         std::string subId, std::vector<DbAdapterEntry> &allEntries) const;
102     DbAdapterStatus GetKvStore();
103     bool CheckKvStore();
104     std::shared_ptr<IDbAdapterDataManager> dataManager_ = nullptr;
105     std::shared_ptr<IDbAdapterSingleStore> kvStorePtr_ = nullptr;
106     mutable std::mutex kvStorePtrMutex_;
107     std::string appId_;
108     std::string storeId_;
109     DbAdapterOptions options_;
110     std::string baseDir_;
111 };
112 #endif // SQLITE_DLCLOSE_ENABLE
113 }  // namespace AccountSA
114 }  // namespace OHOS
115 
116 #endif  // OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_ACCOUNT_DATA_STORAGE_H
117