• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 OHOS_INSIGHT_RDB_INTENT_DATA_MGR_H
17 #define OHOS_INSIGHT_RDB_INTENT_DATA_MGR_H
18 
19 #include <vector>
20 #include <mutex>
21 #include <string>
22 #include <singleton.h>
23 #include <unordered_map>
24 #include "rdb_errno.h"
25 #include "rdb_helper.h"
26 #include "rdb_open_callback.h"
27 #include "rdb_store_config.h"
28 
29 namespace OHOS {
30 namespace AbilityRuntime {
31 namespace {
32 constexpr static const char *INTENT_NAME = "/insight_intent.db";
33 constexpr const char* INTENT_BACK_UP_RDB_NAME = "intent-backup.db";
34 constexpr const char* INTENT_TABLE_NAME = "insight_intent_table";
35 constexpr static const char *INTENT_PATH = "/data/service/el1/public/database/insight_intent";
36 constexpr static int32_t INTENT_VERSION = 1;
37 } // namespace
38 
39 struct IntentRdbConfig {
40     int32_t version{ INTENT_VERSION };
41     std::string dbPath{ INTENT_PATH };
42     std::string dbName{ INTENT_NAME };
43     std::string tableName {INTENT_TABLE_NAME};
44 };
45 
46 class IntentRdbOpenCallback : public NativeRdb::RdbOpenCallback {
47 public:
48     IntentRdbOpenCallback(const IntentRdbConfig &intentRdbConfig);
49     virtual ~IntentRdbOpenCallback() = default;
50     int32_t OnCreate(NativeRdb::RdbStore &rdbStore) override;
51     int32_t OnUpgrade(NativeRdb::RdbStore &rdbStore, int currentVersion, int targetVersion) override;
52     int32_t OnDowngrade(NativeRdb::RdbStore &rdbStore, int currentVersion, int targetVersion) override;
53     int32_t OnOpen(NativeRdb::RdbStore &rdbStore) override;
54     int32_t onCorruption(std::string databaseFile) override;
55 
56 private:
57     IntentRdbConfig intentRdbConfig_;
58 };
59 
60 /**
61  * @class InsightIntentRdbDataMgr
62  * INtent Data Manager Storage.
63  */
64 class InsightIntentRdbDataMgr : public std::enable_shared_from_this<InsightIntentRdbDataMgr> {
65     DECLARE_DELAYED_SINGLETON(InsightIntentRdbDataMgr)
66 public:
67     bool InsertData(const std::string &key, const std::string &value);
68 
69     bool UpdateData(const std::string &key, const std::string &value);
70 
71     bool DeleteData(const std::string &key);
72     bool DeleteDataBeginWithKey(const std::string &key);
73 
74     bool QueryData(const std::string &key, std::string &value);
75     bool QueryDataBeginWithKey(const std::string &key, std::unordered_map<std::string, std::string> &datas);
76     bool QueryAllData(std::unordered_map<std::string, std::string> &datas);
77 
78 private:
79     bool IsIntentRdbLoaded();
80     void BackupRdb();
81     std::shared_ptr<NativeRdb::RdbStore> GetRdbStore();
82     int32_t InsertWithRetry(std::shared_ptr<NativeRdb::RdbStore> rdbStore, int64_t &rowId,
83         const NativeRdb::ValuesBucket &valuesBucket);
84     bool IsRetryErrCode(int32_t errCode);
85     void DelayCloseRdbStore();
86 
87     std::shared_ptr<NativeRdb::RdbStore> rdbStore_;
88     std::mutex rdbStoreMutex_;
89     IntentRdbConfig intentRdbConfig_;
90 };
91 } // namespace AbilityRuntime
92 } // namespace OHOS
93 
94 #endif // OHOS_INSIGHT_RDB_INTENT_DATA_MGR_H