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