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