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 "rdb_errno.h"
27 #include "rdb_helper.h"
28 #include "rdb_open_callback.h"
29 #include "rdb_store_config.h"
30 
31 namespace OHOS {
32 namespace Notification {
33 struct NotificationRdbConfig {
34     std::string dbPath { NotificationConstant::NOTIFICATION_RDB_PATH };
35     std::string dbName { NotificationConstant::NOTIFICATION_RDB_NAME };
36     std::string tableName { NotificationConstant::NOTIFICATION_RDB_TABLE_NAME };
37     std::string journalMode { NotificationConstant::NOTIFICATION_JOURNAL_MODE };
38     std::string syncMode { NotificationConstant::NOTIFICATION_SYNC_MODE };
39     int32_t version { NotificationConstant::NOTIFICATION_RDB_VERSION };
40 };
41 class RdbStoreDataCallBackNotificationStorage : public NativeRdb::RdbOpenCallback {
42 public:
43 
44     RdbStoreDataCallBackNotificationStorage(const NotificationRdbConfig &notificationRdbConfig);
45 
46     virtual ~RdbStoreDataCallBackNotificationStorage();
47 
48     int32_t OnCreate(NativeRdb::RdbStore &rdbStore) override;
49 
50     int32_t OnUpgrade(NativeRdb::RdbStore &rdbStore, int32_t oldVersion, int32_t newVersion) override;
51 
52     int32_t OnDowngrade(NativeRdb::RdbStore &rdbStore, int currentVersion, int targetVersion) override;
53 
54     int32_t OnOpen(NativeRdb::RdbStore &rdbStore) override;
55 
56     int32_t onCorruption(std::string databaseFile) override;
57 private:
58     NotificationRdbConfig notificationRdbConfig_;
59     bool hasTableInit_ = false;
60 };
61 
62 /**
63  * @class NotificationDataMgr
64  * Notification Data Manager.
65  */
66 class NotificationDataMgr {
67 public:
68 
69     NotificationDataMgr(const NotificationRdbConfig &notificationRdbConfig);
70 
71     int32_t Init();
72 
73     int32_t Destroy();
74 
75     /**
76      * @brief Insert data in DB.
77      * @param key The data Key.
78      * @param userId Optional, Indicate which table to insert data.
79      * @return Returns ERR_OK on success, others on failure.
80      */
81     int32_t InsertData(const std::string &key, const std::string &value, const int32_t &userId = -1);
82 
83     /**
84      * @brief Insert data in DB.
85      * @param key The data Key.
86      * @param userId Optional, Indicate which table to insert data.
87      * @return Returns ERR_OK on success, others on failure.
88      */
89     int32_t InsertData(const std::string &key, const std::vector<uint8_t> &value, const int32_t &userId = -1);
90 
91     /**
92      * @brief Insert batch data in DB.
93      * @param key The data Key.
94      * @param userId Optional, Indicate which table to insert data.
95      * @return Returns ERR_OK on success, others on failure.
96      */
97     int32_t InsertBatchData(const std::unordered_map<std::string, std::string> &values, const int32_t &userId = -1);
98 
99     /**
100      * @brief Delete data in DB.
101      * @param key The data Key.
102      * @param userId Optional, Indicate which table to delete data.
103      * @return Returns ERR_OK on success, others on failure.
104      */
105     int32_t DeleteData(const std::string &key, const int32_t &userId = -1);
106 
107     /**
108      * @brief Delete batch data in DB.
109      * @param key The data Key.
110      * @param userId Optional, Indicate which table to delete data.
111      * @return Returns ERR_OK on success, others on failure.
112      */
113     int32_t DeleteBathchData(const std::vector<std::string> &keys, const int32_t &userId = -1);
114 
115     /**
116      * @brief Query data from DB.
117      * @param userId Optional, Indicate which table to query data.
118      * @return Returns ERR_OK on success, others on failure.
119      */
120     int32_t QueryData(const std::string &key, std::string &value, const int32_t &userId = -1);
121 
122     /**
123      * @brief Query data from DB.
124      * @param userId Optional, Indicate which table to query data.
125      * @return Returns ERR_OK on success, others on failure.
126      */
127     int32_t QueryData(const std::string &key, std::vector<uint8_t> &value, const int32_t &userId = -1);
128 
129     /**
130      * @brief Query data begin whith key in DB.
131      * @param userId Optional, Indicate which table to query data.
132      * @return Returns ERR_OK on success, others on failure.
133      */
134     int32_t QueryDataBeginWithKey(const std::string &key, std::unordered_map<std::string, std::string> &values,
135         const int32_t &userId = -1);
136 
137     /**
138      * @brief Query all data in DB.
139      * @param userId Optional, Indicate which table to query data.
140      * @return Returns ERR_OK on success, others on failure.
141      */
142     int32_t QueryAllData(std::unordered_map<std::string, std::string> &values, const int32_t &userId = -1);
143 
144     /**
145      * @brief Delete the special user-table in DB.
146      * @param userId Optional, Indicate which table to delete.
147      * @return Returns ERR_OK on success, others on failure.
148      */
149     int32_t DropUserTable(const int32_t userId);
150 
151 private:
152     int32_t GetUserTableName(const int32_t &userId, std::string &tableName);
153     std::vector<std::string> GenerateOperatedTables(const int32_t &userId);
154     int32_t DeleteData(const std::string tableName, const std::string key, int32_t &rowId);
155     int32_t QueryData(const std::string tableName, const std::string key, std::string &value);
156     int32_t QueryData(const std::string tableName, const std::string key, std::vector<uint8_t> &value);
157     int32_t QueryDataBeginWithKey(const std::string tableName, const std::string key,
158         std::unordered_map<std::string, std::string> &values);
159     int32_t QueryAllData(const std::string tableName, std::unordered_map<std::string, std::string> &datas);
160     int32_t InitCreatedTables();
161     int32_t RestoreForMasterSlaver();
162 
163 private:
164     NotificationRdbConfig notificationRdbConfig_;
165     std::shared_ptr<NativeRdb::RdbStore> rdbStore_;
166     mutable std::mutex rdbStorePtrMutex_;
167     std::set<std::string> createdTables_;
168     mutable std::mutex createdTableMutex_;
169 };
170 } // namespace Notification
171 } // namespace OHOS
172 
173 #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_NOTIFICATION_RDB_DATA_MGR_H
174