• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_NOTIFICATION_RDB_DATA_MGR_H
17 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_NOTIFICATION_RDB_DATA_MGR_H
18 
19 #include <mutex>
20 #include <set>
21 #include <vector>
22 #include <string>
23 #include <map>
24 #include <unordered_map>
25 #include "notification_constant.h"
26 #include "notification_request.h"
27 #include "rdb_errno.h"
28 #include "rdb_helper.h"
29 #include "rdb_open_callback.h"
30 #include "rdb_store_config.h"
31 #include "ffrt.h"
32 
33 namespace OHOS {
34 namespace Notification {
35 struct NotificationRdbConfig {
36     std::string dbPath { NotificationConstant::NOTIFICATION_RDB_PATH };
37     std::string dbName { NotificationConstant::NOTIFICATION_RDB_NAME };
38     std::string tableName { NotificationConstant::NOTIFICATION_RDB_TABLE_NAME };
39     std::string journalMode { NotificationConstant::NOTIFICATION_JOURNAL_MODE };
40     std::string syncMode { NotificationConstant::NOTIFICATION_SYNC_MODE };
41     int32_t version { NotificationConstant::NOTIFICATION_RDB_VERSION };
42 };
43 class RdbStoreDataCallBackNotificationStorage : public NativeRdb::RdbOpenCallback {
44 public:
45 
46     RdbStoreDataCallBackNotificationStorage(const NotificationRdbConfig &notificationRdbConfig);
47 
48     virtual ~RdbStoreDataCallBackNotificationStorage();
49 
50     int32_t OnCreate(NativeRdb::RdbStore &rdbStore) override;
51 
52     int32_t OnUpgrade(NativeRdb::RdbStore &rdbStore, int32_t oldVersion, int32_t newVersion) override;
53 
54     int32_t OnDowngrade(NativeRdb::RdbStore &rdbStore, int currentVersion, int targetVersion) override;
55 
56     int32_t OnOpen(NativeRdb::RdbStore &rdbStore) override;
57 
58     int32_t onCorruption(std::string databaseFile) override;
59 
60     std::set<std::string> GetTableNames(NativeRdb::RdbStore &rdbStore);
61 
62     bool ProcessTable(NativeRdb::RdbStore &rdbStore, const std::string &tableName);
63 
64     bool UpdateRequest(NotificationRequest *request);
65 
66     std::shared_ptr<NativeRdb::AbsSharedResultSet> QueryTable(NativeRdb::RdbStore &rdbStore,
67         const std::string &tableName);
68 
69     bool ProcessResultSet(std::shared_ptr<NativeRdb::AbsSharedResultSet> absSharedResultSet,
70         NativeRdb::RdbStore &rdbStore, const std::string &tableName);
71 
72     bool ProcessRow(std::shared_ptr<NativeRdb::AbsSharedResultSet> absSharedResultSet,
73         NativeRdb::RdbStore &rdbStore, const std::string &tableName);
74 
75     bool GetStringFromResultSet(std::shared_ptr<NativeRdb::AbsSharedResultSet> absSharedResultSet,
76         int columnIndex, std::string &result);
77 
78     bool WriteBackToDatabase(NotificationRequest *request, NotificationBundleOption *bundleOption,
79         NativeRdb::RdbStore &rdbStore, const std::string &tableName);
80 private:
81     NotificationRdbConfig notificationRdbConfig_;
82     bool hasTableInit_ = false;
83 };
84 
85 /**
86  * @class NotificationDataMgr
87  * Notification Data Manager.
88  */
89 class NotificationDataMgr {
90 public:
91 
92     NotificationDataMgr(const NotificationRdbConfig &notificationRdbConfig);
93 
94     int32_t Init();
95 
96     int32_t Destroy();
97 
98     /**
99      * @brief Insert data in DB.
100      * @param key The data Key.
101      * @param userId Optional, Indicate which table to insert data.
102      * @return Returns ERR_OK on success, others on failure.
103      */
104     int32_t InsertData(const std::string &key, const std::string &value, const int32_t &userId = -1);
105 
106     /**
107      * @brief Insert data in DB.
108      * @param key The data Key.
109      * @param userId Optional, Indicate which table to insert data.
110      * @return Returns ERR_OK on success, others on failure.
111      */
112     int32_t InsertData(const std::string &key, const std::vector<uint8_t> &value, const int32_t &userId = -1);
113 
114     /**
115      * @brief Insert batch data in DB.
116      * @param key The data Key.
117      * @param userId Optional, Indicate which table to insert data.
118      * @return Returns ERR_OK on success, others on failure.
119      */
120     int32_t InsertBatchData(const std::unordered_map<std::string, std::string> &values, const int32_t &userId = -1);
121 
122     /**
123      * @brief Delete data in DB.
124      * @param key The data Key.
125      * @param userId Optional, Indicate which table to delete data.
126      * @return Returns ERR_OK on success, others on failure.
127      */
128     int32_t DeleteData(const std::string &key, const int32_t &userId = -1);
129 
130     /**
131      * @brief Delete batch data in DB.
132      * @param key The data Key.
133      * @param userId Optional, Indicate which table to delete data.
134      * @return Returns ERR_OK on success, others on failure.
135      */
136     int32_t DeleteBatchData(const std::vector<std::string> &keys, const int32_t &userId = -1);
137 
138     /**
139      * @brief Query data from DB.
140      * @param userId Optional, Indicate which table to query data.
141      * @return Returns ERR_OK on success, others on failure.
142      */
143     int32_t QueryData(const std::string &key, std::string &value, const int32_t &userId = -1);
144 
145     /**
146      * @brief Query data from DB.
147      * @param userId Optional, Indicate which table to query data.
148      * @return Returns ERR_OK on success, others on failure.
149      */
150     int32_t QueryData(const std::string &key, std::vector<uint8_t> &value, const int32_t &userId = -1);
151 
152     /**
153      * @brief Query data begin whith key in DB.
154      * @param userId Optional, Indicate which table to query data.
155      * @return Returns ERR_OK on success, others on failure.
156      */
157     int32_t QueryDataBeginWithKey(const std::string &key, std::unordered_map<std::string, std::string> &values,
158         const int32_t &userId = -1);
159 
160     /**
161      * @brief Query data Contains whith key in DB.
162      * @param userId Optional, Indicate which table to query data.
163      * @return Returns ERR_OK on success, others on failure.
164      */
165     int32_t QueryDataContainsWithKey(const std::string &key, std::unordered_map<std::string, std::string> &values,
166         const int32_t &userId = -1);
167 
168     /**
169      * @brief Query all data in DB.
170      * @param userId Optional, Indicate which table to query data.
171      * @return Returns ERR_OK on success, others on failure.
172      */
173     int32_t QueryAllData(std::unordered_map<std::string, std::string> &values, const int32_t &userId = -1);
174 
175     /**
176      * @brief Delete the special user-table in DB.
177      * @param userId Optional, Indicate which table to delete.
178      * @return Returns ERR_OK on success, others on failure.
179      */
180     int32_t DropUserTable(const int32_t userId);
181 
182 private:
183     int32_t GetUserTableName(const int32_t &userId, std::string &tableName);
184     std::vector<std::string> GenerateOperatedTables(const int32_t &userId);
185     int32_t DeleteData(const std::string tableName, const std::string key, int32_t &rowId);
186     int32_t QueryData(const std::string tableName, const std::string key, std::string &value);
187     int32_t QueryData(const std::string tableName, const std::string key, std::vector<uint8_t> &value);
188     int32_t QueryDataBeginWithKey(const std::string tableName, const std::string key,
189         std::unordered_map<std::string, std::string> &values);
190     int32_t QueryDataContainsWithKey(const std::string tableName, const std::string key,
191         std::unordered_map<std::string, std::string> &values);
192     int32_t QueryAllData(const std::string tableName, std::unordered_map<std::string, std::string> &datas);
193     int32_t InitCreatedTables();
194     int32_t RestoreForMasterSlaver();
195     void SendUserDataSizeHisysevent();
196     std::uint64_t GetRemainPartitionSize(const std::string &partitionName);
197     std::vector<std::uint64_t> GetFileOrFolderSize(const std::vector<std::string> &paths);
198 
199 private:
200     NotificationRdbConfig notificationRdbConfig_;
201     std::shared_ptr<NativeRdb::RdbStore> rdbStore_;
202     mutable ffrt::mutex rdbStorePtrMutex_;
203     std::set<std::string> createdTables_;
204     mutable ffrt::mutex createdTableMutex_;
205 };
206 } // namespace Notification
207 } // namespace OHOS
208 
209 #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_NOTIFICATION_RDB_DATA_MGR_H
210