1 /*
2  * Copyright (c) 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 FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_RDB_DATA_MANAGER_H
17 #define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_RDB_DATA_MANAGER_H
18 
19 #include <mutex>
20 
21 #include "bms_rdb_config.h"
22 #include "bms_rdb_open_callback.h"
23 #include "rdb_helper.h"
24 
25 namespace OHOS {
26 namespace AppExecFwk {
27 class RdbDataManager : public std::enable_shared_from_this<RdbDataManager> {
28 public:
29     RdbDataManager(const BmsRdbConfig &bmsRdbConfig);
30     ~RdbDataManager();
31 
32     static void ClearCache();
33     bool InsertData(const std::string &key, const std::string &value);
34     bool InsertData(const NativeRdb::ValuesBucket &valuesBucket);
35     bool UpdateData(const std::string &key, const std::string &value);
36     bool DeleteData(const std::string &key);
37     bool QueryData(const std::string &key, std::string &value);
38     bool QueryAllData(std::map<std::string, std::string> &datas);
39 
40     bool BatchInsert(int64_t &outInsertNum, const std::vector<NativeRdb::ValuesBucket> &valuesBuckets);
41     bool UpdateData(const NativeRdb::ValuesBucket &valuesBucket,
42         const NativeRdb::AbsRdbPredicates &absRdbPredicates);
43     bool DeleteData(const NativeRdb::AbsRdbPredicates &absRdbPredicates);
44     std::shared_ptr<NativeRdb::ResultSet> QueryData(
45         const NativeRdb::AbsRdbPredicates &absRdbPredicates);
46     bool CreateTable();
47     void DelayCloseRdbStore();
48     std::shared_ptr<NativeRdb::ResultSet> QueryByStep(
49         const NativeRdb::AbsRdbPredicates &absRdbPredicates);
50 
51     bool UpdateOrInsertData(
52         const NativeRdb::ValuesBucket &valuesBucket, const NativeRdb::AbsRdbPredicates &absRdbPredicates);
53 
54     void BackupRdb();
55 
56     void CheckSystemSizeAndHisysEvent(const std::string &path, const std::string &fileName);
57 private:
58     std::shared_ptr<NativeRdb::RdbStore> GetRdbStore();
59     int32_t InsertWithRetry(std::shared_ptr<NativeRdb::RdbStore> rdbStore, int64_t &rowId,
60         const NativeRdb::ValuesBucket &valuesBucket);
61     bool IsRetryErrCode(int32_t errCode);
62     std::mutex rdbMutex_;
63     std::shared_ptr<NativeRdb::RdbStore> rdbStore_;
64     bool isInitial_ = false;
65 
66     static std::mutex restoreRdbMutex_;
67 
68     BmsRdbConfig bmsRdbConfig_;
69 };
70 }  // namespace AppExecFwk
71 }  // namespace OHOS
72 #endif  // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_RDB_DATA_MANAGER_H
73