• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2024 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 "ffrt.h"
20 #include <mutex>
21 #include <unordered_map>
22 
23 #include "bms_rdb_config.h"
24 #include "bms_rdb_open_callback.h"
25 #include "rdb_helper.h"
26 #include "single_delayed_task_mgr.h"
27 
28 namespace OHOS {
29 namespace AppExecFwk {
30 class RdbDataManager : public std::enable_shared_from_this<RdbDataManager> {
31 public:
32     RdbDataManager(const BmsRdbConfig &bmsRdbConfig);
33     ~RdbDataManager();
34 
35     static void ClearCache();
36     bool InsertData(const std::string &key, const std::string &value);
37     ErrCode InsertDataWithCode(const std::string &key, const std::string &value);
38     bool InsertData(const NativeRdb::ValuesBucket &valuesBucket);
39     bool UpdateData(const std::string &key, const std::string &value);
40     bool DeleteData(const std::string &key);
41     bool QueryData(const std::string &key, std::string &value);
42     bool QueryAllData(std::map<std::string, std::string> &datas);
43 
44     bool BatchInsert(int64_t &outInsertNum, const std::vector<NativeRdb::ValuesBucket> &valuesBuckets);
45     bool UpdateData(const NativeRdb::ValuesBucket &valuesBucket,
46         const NativeRdb::AbsRdbPredicates &absRdbPredicates);
47     bool DeleteData(const NativeRdb::AbsRdbPredicates &absRdbPredicates);
48     std::shared_ptr<NativeRdb::ResultSet> QueryData(
49         const NativeRdb::AbsRdbPredicates &absRdbPredicates);
50     bool CreateTable();
51     void DelayCloseRdbStore();
52     std::shared_ptr<NativeRdb::ResultSet> QueryByStep(
53         const NativeRdb::AbsRdbPredicates &absRdbPredicates);
54 
55     bool UpdateOrInsertData(
56         const NativeRdb::ValuesBucket &valuesBucket, const NativeRdb::AbsRdbPredicates &absRdbPredicates);
57 
58     bool RdbIntegrityCheckNeedRestore();
59 
60     void CheckSystemSizeAndHisysEvent(const std::string &path, const std::string &fileName);
61 
62     bool CheckIsSatisfyTime();
63 
64     void SendDbErrorEvent(const std::string &dbName, int32_t operationType, int32_t errorCode);
65 private:
66     std::shared_ptr<NativeRdb::RdbStore> GetRdbStore(ErrCode &result);
67     ErrCode GetRdbStoreFromNative();
68     int32_t InsertWithRetry(std::shared_ptr<NativeRdb::RdbStore> rdbStore, int64_t &rowId,
69         const NativeRdb::ValuesBucket &valuesBucket);
70     bool IsRetryErrCode(int32_t errCode);
71     ffrt::mutex &GetRdbRestoreMutex(const std::string &dbName);
72     bool isInitial_ = false;
73     ffrt::mutex rdbMutex_;
74     static ffrt::mutex restoreRdbMapMutex_;
75     static std::unordered_map<std::string, ffrt::mutex> restoreRdbMap_;
76     std::shared_ptr<NativeRdb::RdbStore> rdbStore_;
77 
78     BmsRdbConfig bmsRdbConfig_;
79     std::shared_ptr<SingleDelayedTaskMgr> delayedTaskMgr_;
80 };
81 }  // namespace AppExecFwk
82 }  // namespace OHOS
83 #endif  // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_RDB_DATA_MANAGER_H
84