1 /* 2 * Copyright (c) 2021 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 DISTRIBUTED_KV_DATA_MANAGER_H 17 #define DISTRIBUTED_KV_DATA_MANAGER_H 18 19 #include <functional> 20 #include "kvstore.h" 21 #include "kvstore_death_recipient.h" 22 #include "kvstore_observer.h" 23 #include "single_kvstore.h" 24 #include "types.h" 25 #include "device_status_change_listener.h" 26 27 namespace OHOS { 28 namespace DistributedKv { 29 class DistributedKvDataManager final { 30 public: 31 KVSTORE_API 32 DistributedKvDataManager(); 33 34 KVSTORE_API 35 ~DistributedKvDataManager(); 36 37 // Open kvstore instance with the given storeId, creating it if needed. 38 // It is allowed to open the same kvstore concurrently 39 // multiple times, but only one KvStoreImpl will be created. 40 // Parameters: 41 // options: the config of the kvstore, including encrypt, 42 // create if needed and whether need sync between devices. 43 // appId: the name of the application. 44 // :storeId: the name of the kvstore. 45 // callback: including status and KvStore instance returned by this call. 46 // callback will return: 47 // if Options.createIfMissing is false and kvstore has not been created before, 48 // STORE_NOT_FOUND and nullptr, 49 // if storeId is not valid, INVALID_ARGUMENT and nullptr, 50 // if appId has no permission, PERMISSION_DENIED and nullptr, 51 // otherwise, SUCCESS and the unipue_ptr of kvstore, which client can use to operate kvstore, will be returned. 52 KVSTORE_API Status GetKvStore(const Options &options, const AppId &appId, const StoreId &storeId, 53 std::shared_ptr<KvStore> &kvStore); 54 55 // Open kvstore instance with the given storeId, creating it if needed. 56 // It is allowed to open the same kvstore concurrently 57 // multiple times, but only one KvStoreImpl will be created. 58 // Parameters: 59 // options: the config of the kvstore, including encrypt, 60 // create if needed and whether need sync between devices. 61 // appId: the name of the application. 62 // :storeId: the name of the kvstore. 63 // callback: including status and KvStore instance returned by this call. 64 // callback will return: 65 // if Options.createIfMissing is false and kvstore has not been created before, 66 // STORE_NOT_FOUND and nullptr, 67 // if storeId is not valid, INVALID_ARGUMENT and nullptr, 68 // if appId has no permission, PERMISSION_DENIED and nullptr, 69 // otherwise, SUCCESS and the unipue_ptr of kvstore, which client can use to operate kvstore, will be returned. 70 KVSTORE_API Status GetSingleKvStore(const Options &options, const AppId &appId, const StoreId &storeId, 71 std::shared_ptr<SingleKvStore> &singleKvStore); 72 73 // get all existed kvstore names. 74 KVSTORE_API Status GetAllKvStoreId(const AppId &appId, std::vector<StoreId> &storeIds); 75 76 // WARNING: try to close a KvStore while other thread(s) still using it may cause process crash. 77 // Disconnect kvstore instance from kvstoreimpl with the given storeId, 78 // if all kvstore created for a single kvsotreimpl, kvstoreimpl and resource below will be freed. 79 // before this call, all KvStoreSnapshot must be released firstly, 80 // otherwise this call will fail. 81 // after this call, kvstore and kvstoresnapshot become invalid. 82 // call to it will return nullptr exception. 83 // Parameters: 84 // appId: the name of the application. 85 // storeId: the name of the kvstore. 86 KVSTORE_API Status CloseKvStore(const AppId &appId, const StoreId &storeId); 87 88 // WARNING: try to close a KvStore while other thread(s) still using it may cause process crash. 89 // 90 // Disconnect kvstore instance from kvstoreimpl. 91 // if all kvstore created for a single kvsotreimpl, kvstoreimpl and resource below will be freed. 92 // before this call, all KvStoreResultSet must be released firstly, 93 // otherwise this call will fail. 94 // after this call, kvstore and KvStoreResultSet become invalid. 95 // call to it will return nullptr exception. 96 // Parameters: 97 // appId: the name of the application. 98 // kvStorePtr: the pointer of the kvstore. 99 KVSTORE_API Status CloseKvStore(const AppId &appId, std::shared_ptr<SingleKvStore> &kvStorePtr); 100 101 // WARNING: try to close a KvStore while other thread(s) still using it may cause process crash. 102 // close all opened kvstores for this appId. 103 KVSTORE_API Status CloseAllKvStore(const AppId &appId); 104 105 // delete kvstore file with the given storeId. 106 // client should first close all connections to it and then delete it, 107 // otherwise delete may return error. 108 // after this call, kvstore and kvstoresnapshot become invalid. 109 // call to it will return error. 110 // Parameters: 111 // appId: the name of the application. 112 // storeId: the name of the kvstore. 113 KVSTORE_API Status DeleteKvStore(const AppId &appId, const StoreId &storeId); 114 115 // delete all kvstore. 116 KVSTORE_API Status DeleteAllKvStore(const AppId &appId); 117 118 KVSTORE_API void RegisterKvStoreServiceDeathRecipient(std::shared_ptr<KvStoreDeathRecipient> kvStoreDeathRecipient); 119 120 KVSTORE_API 121 void UnRegisterKvStoreServiceDeathRecipient(std::shared_ptr<KvStoreDeathRecipient> kvStoreDeathRecipient); 122 123 // Subscribe device status change, like online or offline. 124 // Client should override AppDeviceStatusChangeListener and register it by this function, observer->OnDeviceChanged 125 // will be called on remote device status change. 126 // Parameters: 127 // observer: callback for device status change event. 128 // Return: 129 // Status of this subscribe operation. 130 KVSTORE_API Status StartWatchDeviceChange(std::shared_ptr<DeviceStatusChangeListener> observer); 131 132 // Unsubscribe device status change, like online or offline. 133 // client should override AppDeviceStatusChangeListener and register it by calling this function, then 134 // observer->OnDeviceChanged will no longer be called on remote device status change. 135 // Parameters: 136 // observer: callback for device status change event. 137 // Return: 138 // Status of this unsubscribe operation. 139 KVSTORE_API Status StopWatchDeviceChange(std::shared_ptr<DeviceStatusChangeListener> observer); 140 141 // Get all connected devices. 142 // Client can use this method to retrieve all devices that have already connected, 143 // and then call StartWatchDeviceChange to watch device status change later. 144 // Parameters: 145 // deviceInfoList: list of all connected device will be returned by this parameter. 146 // Return: 147 // Status of this get device list operation. 148 KVSTORE_API Status GetDeviceList(std::vector<DeviceInfo> &deviceInfoList, DeviceFilterStrategy strategy); 149 150 // Get device. 151 // Client can use this method to retrieve local device, 152 // Parameters: 153 // localDevice: DeviceInfo will be returned by this parameter. 154 // Return: 155 // Status of this get device operation. 156 KVSTORE_API Status GetLocalDevice(DeviceInfo &localDevice); 157 }; 158 } // namespace DistributedKv 159 } // namespace OHOS 160 #endif // DISTRIBUTED_KV_DATA_MANAGER_H 161