• 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_DISTRIBUTED_INCLUDE_DISTRIBUTED_DATABASE_H
17 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_DISTRIBUTED_INCLUDE_DISTRIBUTED_DATABASE_H
18 
19 #include <chrono>
20 #include <mutex>
21 #include <string>
22 #include <vector>
23 
24 #include "distributed_kv_data_manager.h"
25 #include "singleton.h"
26 
27 #include "distributed_database_callback.h"
28 #include "distributed_device_callback.h"
29 #include "distributed_flow_control.h"
30 
31 namespace OHOS {
32 namespace Notification {
33 class DistributedDatabase : private DistributedFlowControl {
34 public:
35     using Entry = DistributedKv::Entry;
36     using DeviceInfo = DistributedKv::DeviceInfo;
37 
38     /**
39      * @brief The constructor.
40      *
41      * @param databaseCb Distributed notification info changed callback object.
42      * @param deviceCb Device connection info changed callback object.
43      */
44     DistributedDatabase(
45         std::shared_ptr<DistributedDatabaseCallback> databaseCb, std::shared_ptr<DistributedDeviceCallback> deviceCb);
46 
47     /**
48      * @brief The deconstructor.
49      */
50     ~DistributedDatabase();
51 
52     /**
53      * @brief Put a key-value to database.
54      *
55      * @param key Indicates the key.
56      * @param value Indicates the value.
57      * @return Whether to put key-value success.
58      */
59     bool PutToDistributedDB(const std::string &key, const std::string &value);
60 
61     /**
62      * @brief Get the value of its key from database.
63      *
64      * @param key Indicates the key.
65      * @param value Indicates the value.
66      * @return Whether to get key-value success.
67      */
68     bool GetFromDistributedDB(const std::string &key, std::string &value);
69 
70     /**
71      * @brief Get all entries which key start with prefixKey.
72      *
73      * @param perfixkey Indicates the prefix to be searched.
74      * @param entries Indicates the entries will be returned in this parameter.
75      * @return Whether to get entries success.
76      */
77     bool GetEntriesFromDistributedDB(const std::string &prefixKey, std::vector<Entry> &entries);
78 
79     /**
80      * @brief Delete a key-value of its key from database.
81      *
82      * @param key Indicates the key.
83      * @return Whether to delete key-value success.
84      */
85     bool DeleteToDistributedDB(const std::string &key);
86 
87     /**
88      * @brief Clear all entries which put by specified device.
89      *
90      * @param deviceId Indicates the id of specified device.
91      * @return Whether to clear device entries success.
92      */
93     bool ClearDataByDevice(const std::string &deviceId);
94 
95     /**
96      * @brief Get local device id.
97      *
98      * @param deviceId Indicates the id of local device.
99      * @return Whether to get device id success.
100      */
101     bool GetLocalDeviceId(std::string &deviceId);
102 
103     /**
104      * @brief Get local device info.
105      *
106      * @param localInfo Indicates the infomation of local device.
107      * @return Whether to get device infomation success.
108      */
109     bool GetLocalDeviceInfo(DeviceInfo &localInfo);
110 
111     /**
112      * @brief Get all devices info on the network.
113      *
114      * @param deviceList Indicates the infomation list of devices.
115      * @return Whether to get devices infomation success.
116      */
117     bool GetDeviceInfoList(std::vector<DeviceInfo> &deviceList);
118 
119     /**
120      * @brief Recreate the database of distributed notification
121      *
122      * @return Whether to recreate the database success.
123      */
124     bool RecreateDistributedDB();
125 
126     bool OnDeviceConnected();
127 
128 private:
129     void GetKvDataManager(void);
130     bool CheckKvDataManager(void);
131     void GetKvStore(void);
132     bool CheckKvStore(void);
133 
134 private:
135     std::mutex mutex_;
136     std::unique_ptr<DistributedKv::DistributedKvDataManager> kvDataManager_;
137     std::shared_ptr<DistributedKv::SingleKvStore> kvStore_;
138     std::shared_ptr<DistributedDatabaseCallback> databaseCb_;
139     std::shared_ptr<DistributedDeviceCallback> deviceCb_;
140 
141     std::string localDeviceId_;
142 
143     DISALLOW_COPY_AND_MOVE(DistributedDatabase);
144 };
145 }  // namespace Notification
146 }  // namespace OHOS
147 
148 #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_DISTRIBUTED_INCLUDE_DISTRIBUTED_DATABASE_H