196279301Sopenharmony_ci/* 296279301Sopenharmony_ci * Copyright (c) 2022-2023 Huawei Device Co., Ltd. 396279301Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 496279301Sopenharmony_ci * you may not use this file except in compliance with the License. 596279301Sopenharmony_ci * You may obtain a copy of the License at 696279301Sopenharmony_ci * 796279301Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 896279301Sopenharmony_ci * 996279301Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1096279301Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1196279301Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1296279301Sopenharmony_ci * See the License for the specific language governing permissions and 1396279301Sopenharmony_ci * limitations under the License. 1496279301Sopenharmony_ci */ 1596279301Sopenharmony_ci 1696279301Sopenharmony_ci#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_DISTRIBUTED_INCLUDE_DISTRIBUTED_DATABASE_H 1796279301Sopenharmony_ci#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_DISTRIBUTED_INCLUDE_DISTRIBUTED_DATABASE_H 1896279301Sopenharmony_ci 1996279301Sopenharmony_ci#include <chrono> 2096279301Sopenharmony_ci#include <mutex> 2196279301Sopenharmony_ci#include <string> 2296279301Sopenharmony_ci#include <vector> 2396279301Sopenharmony_ci 2496279301Sopenharmony_ci#include "distributed_kv_data_manager.h" 2596279301Sopenharmony_ci#include "singleton.h" 2696279301Sopenharmony_ci 2796279301Sopenharmony_ci#include "distributed_database_callback.h" 2896279301Sopenharmony_ci#include "distributed_device_callback.h" 2996279301Sopenharmony_ci#include "distributed_flow_control.h" 3096279301Sopenharmony_ci 3196279301Sopenharmony_cinamespace OHOS { 3296279301Sopenharmony_cinamespace Notification { 3396279301Sopenharmony_ciclass DistributedDatabase : private DistributedFlowControl { 3496279301Sopenharmony_cipublic: 3596279301Sopenharmony_ci using Entry = DistributedKv::Entry; 3696279301Sopenharmony_ci using DeviceInfo = DistributedHardware::DmDeviceInfo; 3796279301Sopenharmony_ci 3896279301Sopenharmony_ci /** 3996279301Sopenharmony_ci * @brief The constructor. 4096279301Sopenharmony_ci * 4196279301Sopenharmony_ci * @param databaseCb Distributed notification info changed callback object. 4296279301Sopenharmony_ci * @param deviceCb Device connection info changed callback object. 4396279301Sopenharmony_ci */ 4496279301Sopenharmony_ci DistributedDatabase( 4596279301Sopenharmony_ci std::shared_ptr<DistributedDatabaseCallback> databaseCb, std::shared_ptr<DistributedDeviceCallback> deviceCb); 4696279301Sopenharmony_ci 4796279301Sopenharmony_ci /** 4896279301Sopenharmony_ci * @brief The deconstructor. 4996279301Sopenharmony_ci */ 5096279301Sopenharmony_ci virtual ~DistributedDatabase(); 5196279301Sopenharmony_ci 5296279301Sopenharmony_ci /** 5396279301Sopenharmony_ci * @brief Put a key-value to database. 5496279301Sopenharmony_ci * 5596279301Sopenharmony_ci * @param key Indicates the key. 5696279301Sopenharmony_ci * @param value Indicates the value. 5796279301Sopenharmony_ci * @return Whether to put key-value success. 5896279301Sopenharmony_ci */ 5996279301Sopenharmony_ci bool PutToDistributedDB(const std::string &key, const std::string &value); 6096279301Sopenharmony_ci 6196279301Sopenharmony_ci /** 6296279301Sopenharmony_ci * @brief Get the value of its key from database. 6396279301Sopenharmony_ci * 6496279301Sopenharmony_ci * @param key Indicates the key. 6596279301Sopenharmony_ci * @param value Indicates the value. 6696279301Sopenharmony_ci * @return Whether to get key-value success. 6796279301Sopenharmony_ci */ 6896279301Sopenharmony_ci bool GetFromDistributedDB(const std::string &key, std::string &value); 6996279301Sopenharmony_ci 7096279301Sopenharmony_ci /** 7196279301Sopenharmony_ci * @brief Get all entries which key start with prefixKey. 7296279301Sopenharmony_ci * 7396279301Sopenharmony_ci * @param perfixkey Indicates the prefix to be searched. 7496279301Sopenharmony_ci * @param entries Indicates the entries will be returned in this parameter. 7596279301Sopenharmony_ci * @return Whether to get entries success. 7696279301Sopenharmony_ci */ 7796279301Sopenharmony_ci bool GetEntriesFromDistributedDB(const std::string &prefixKey, std::vector<Entry> &entries); 7896279301Sopenharmony_ci 7996279301Sopenharmony_ci /** 8096279301Sopenharmony_ci * @brief Delete a key-value of its key from database. 8196279301Sopenharmony_ci * 8296279301Sopenharmony_ci * @param key Indicates the key. 8396279301Sopenharmony_ci * @return Whether to delete key-value success. 8496279301Sopenharmony_ci */ 8596279301Sopenharmony_ci bool DeleteToDistributedDB(const std::string &key); 8696279301Sopenharmony_ci 8796279301Sopenharmony_ci /** 8896279301Sopenharmony_ci * @brief Clear all entries which put by specified device. 8996279301Sopenharmony_ci * 9096279301Sopenharmony_ci * @param deviceId Indicates the id of specified device. 9196279301Sopenharmony_ci * @return Whether to clear device entries success. 9296279301Sopenharmony_ci */ 9396279301Sopenharmony_ci bool ClearDataByDevice(const std::string &deviceId); 9496279301Sopenharmony_ci 9596279301Sopenharmony_ci /** 9696279301Sopenharmony_ci * @brief Get local device id. 9796279301Sopenharmony_ci * 9896279301Sopenharmony_ci * @param deviceId Indicates the id of local device. 9996279301Sopenharmony_ci * @return Whether to get device id success. 10096279301Sopenharmony_ci */ 10196279301Sopenharmony_ci bool GetLocalDeviceId(std::string &deviceId); 10296279301Sopenharmony_ci 10396279301Sopenharmony_ci /** 10496279301Sopenharmony_ci * @brief Get local device info. 10596279301Sopenharmony_ci * 10696279301Sopenharmony_ci * @param localInfo Indicates the infomation of local device. 10796279301Sopenharmony_ci * @return Whether to get device infomation success. 10896279301Sopenharmony_ci */ 10996279301Sopenharmony_ci bool GetLocalDeviceInfo(DeviceInfo &localInfo); 11096279301Sopenharmony_ci 11196279301Sopenharmony_ci /** 11296279301Sopenharmony_ci * @brief Get all devices info on the network. 11396279301Sopenharmony_ci * 11496279301Sopenharmony_ci * @param deviceList Indicates the infomation list of devices. 11596279301Sopenharmony_ci * @return Whether to get devices infomation success. 11696279301Sopenharmony_ci */ 11796279301Sopenharmony_ci bool GetDeviceInfoList(std::vector<DeviceInfo> &deviceList); 11896279301Sopenharmony_ci 11996279301Sopenharmony_ci /** 12096279301Sopenharmony_ci * @brief Recreate the database of distributed notification 12196279301Sopenharmony_ci * 12296279301Sopenharmony_ci * @return Whether to recreate the database success. 12396279301Sopenharmony_ci */ 12496279301Sopenharmony_ci bool RecreateDistributedDB(); 12596279301Sopenharmony_ci 12696279301Sopenharmony_ci bool OnDeviceConnected(); 12796279301Sopenharmony_ci 12896279301Sopenharmony_ciprivate: 12996279301Sopenharmony_ci void GetKvDataManager(); 13096279301Sopenharmony_ci bool CheckKvDataManager(); 13196279301Sopenharmony_ci void GetKvStore(); 13296279301Sopenharmony_ci bool CheckKvStore(); 13396279301Sopenharmony_ci 13496279301Sopenharmony_ci std::mutex mutex_; 13596279301Sopenharmony_ci std::unique_ptr<DistributedKv::DistributedKvDataManager> kvDataManager_; 13696279301Sopenharmony_ci std::shared_ptr<DistributedKv::SingleKvStore> kvStore_; 13796279301Sopenharmony_ci std::shared_ptr<DistributedDatabaseCallback> databaseCb_; 13896279301Sopenharmony_ci std::shared_ptr<DistributedDeviceCallback> deviceCb_; 13996279301Sopenharmony_ci std::shared_ptr<DistributedHardware::DmInitCallback> initCallback_; 14096279301Sopenharmony_ci std::string localDeviceId_; 14196279301Sopenharmony_ci 14296279301Sopenharmony_ci class DeviceInitCallBack : public DistributedHardware::DmInitCallback { 14396279301Sopenharmony_ci public: 14496279301Sopenharmony_ci void OnRemoteDied() override; 14596279301Sopenharmony_ci }; 14696279301Sopenharmony_ci 14796279301Sopenharmony_ci DISALLOW_COPY_AND_MOVE(DistributedDatabase); 14896279301Sopenharmony_ci}; 14996279301Sopenharmony_ci} // namespace Notification 15096279301Sopenharmony_ci} // namespace OHOS 15196279301Sopenharmony_ci#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_DISTRIBUTED_INCLUDE_DISTRIBUTED_DATABASE_H