1 /*
2  * Copyright (c) 2022-2023 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_DISTRIBUTED_INCLUDE_PREFERENCES_DATABASE_H
17 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_DISTRIBUTED_INCLUDE_PREFERENCES_DATABASE_H
18 
19 #include <mutex>
20 #include <string>
21 #include <vector>
22 
23 #include "distributed_kv_data_manager.h"
24 #include "singleton.h"
25 
26 #include "distributed_database_callback.h"
27 #include "distributed_device_callback.h"
28 #include "distributed_flow_control.h"
29 
30 namespace OHOS {
31 namespace Notification {
32 class DistributedPreferencesDatabase : private DistributedFlowControl {
33 public:
34     using Entry = DistributedKv::Entry;
35 
36     DistributedPreferencesDatabase();
37     virtual ~DistributedPreferencesDatabase();
38 
39     /**
40      * @brief Put a key-value to database.
41      *
42      * @param key Indicates the key.
43      * @param value Indicates the value.
44      * @return Whether to put key-value success.
45      */
46     bool PutToDistributedDB(const std::string &key, const std::string &value);
47 
48     /**
49      * @brief Get the value of its key from database.
50      *
51      * @param key Indicates key.
52      * @param value Indicates value.
53      * @return Whether to get key-value success.
54      */
55     bool GetFromDistributedDB(const std::string &key, std::string &value);
56 
57     /**
58      * @brief Get all entries which key start with prefixKey.
59      *
60      * @param perfixkey Indicates the prefix to be searched.
61      * @param entries Indicates the entries will be returned in this parameter.
62      * @return Whether to get entries success.
63      */
64     bool GetEntriesFromDistributedDB(const std::string &prefixKey, std::vector<Entry> &entries);
65 
66     /**
67      * @brief Delete a key-value of its key from database.
68      *
69      * @param key Indicates the key.
70      * @return Whether to delete key-value success.
71      */
72     bool DeleteToDistributedDB(const std::string &key);
73 
74     /**
75      * @brief Clear all data in database and Delete the database.
76      *
77      * @return Whether to clear database success.
78      */
79     bool ClearDatabase();
80 
81 private:
82     void GetKvDataManager();
83     bool CheckKvDataManager();
84     void GetKvStore();
85     bool CheckKvStore();
86 
87     std::mutex mutex_;
88     std::unique_ptr<DistributedKv::DistributedKvDataManager> kvDataManager_;
89     std::shared_ptr<DistributedKv::SingleKvStore> kvStore_;
90 
91     DISALLOW_COPY_AND_MOVE(DistributedPreferencesDatabase);
92 };
93 }  // namespace Notification
94 }  // namespace OHOS
95 
96 #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_DISTRIBUTED_INCLUDE_PREFERENCES_DATABASE_H