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_ANS_STANDARD_SERVICES_DISTRIBUTED_INCLUDE_DISTRIBUTED_DATABASE_H 17 #define BASE_NOTIFICATION_ANS_STANDARD_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 DistributedDatabase( 38 std::shared_ptr<DistributedDatabaseCallback> databaseCb, std::shared_ptr<DistributedDeviceCallback> deviceCb); 39 ~DistributedDatabase(); 40 41 bool PutToDistributedDB(const std::string &key, const std::string &value); 42 bool GetFromDistributedDB(const std::string &key, std::string &value); 43 bool GetEntriesFromDistributedDB(const std::string &prefixKey, std::vector<Entry> &entries); 44 bool DeleteToDistributedDB(const std::string &key); 45 bool ClearDataByDevice(const std::string &deviceId); 46 bool GetLocalDeviceId(std::string &deviceId); 47 bool GetLocalDeviceInfo(DeviceInfo &localInfo); 48 bool GetDeviceInfoList(std::vector<DeviceInfo> &deviceList); 49 bool RecreateDistributedDB(); 50 51 private: 52 void GetKvDataManager(void); 53 bool CheckKvDataManager(void); 54 void GetKvStore(void); 55 bool CheckKvStore(void); 56 57 private: 58 std::mutex mutex_; 59 std::unique_ptr<DistributedKv::DistributedKvDataManager> kvDataManager_; 60 std::shared_ptr<DistributedKv::SingleKvStore> kvStore_; 61 std::shared_ptr<DistributedDatabaseCallback> databaseCb_; 62 std::shared_ptr<DistributedDeviceCallback> deviceCb_; 63 64 std::string localDeviceId_; 65 66 DISALLOW_COPY_AND_MOVE(DistributedDatabase); 67 }; 68 } // namespace Notification 69 } // namespace OHOS 70 71 #endif /* BASE_NOTIFICATION_ANS_STANDARD_SERVICES_DISTRIBUTED_INCLUDE_DISTRIBUTED_DATABASE_H */