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 OHOS_DISTRIBUTED_DATA_SERVICE_KVDB_SERVICE_IMPL_H 17 #define OHOS_DISTRIBUTED_DATA_SERVICE_KVDB_SERVICE_IMPL_H 18 #include <set> 19 #include <vector> 20 21 #include "concurrent_map.h" 22 #include "device_matrix.h" 23 #include "kv_store_nb_delegate.h" 24 #include "kvdb_service_stub.h" 25 #include "kvstore_sync_manager.h" 26 #include "metadata/store_meta_data.h" 27 #include "metadata/store_meta_data_local.h" 28 #include "metadata/strategy_meta_data.h" 29 #include "ref_count.h" 30 #include "store_cache.h" 31 namespace OHOS::DistributedKv { 32 class API_EXPORT KVDBServiceImpl final : public KVDBServiceStub { 33 public: 34 using DBLaunchParam = DistributedDB::AutoLaunchParam; 35 API_EXPORT KVDBServiceImpl(); 36 virtual ~KVDBServiceImpl(); 37 Status GetStoreIds(const AppId &appId, std::vector<StoreId> &storeIds) override; 38 Status BeforeCreate(const AppId &appId, const StoreId &storeId, const Options &options) override; 39 Status AfterCreate(const AppId &appId, const StoreId &storeId, const Options &options, 40 const std::vector<uint8_t> &password) override; 41 Status Delete(const AppId &appId, const StoreId &storeId) override; 42 Status Sync(const AppId &appId, const StoreId &storeId, const SyncInfo &syncInfo) override; 43 Status RegisterSyncCallback(const AppId &appId, sptr<IKvStoreSyncCallback> callback) override; 44 Status UnregisterSyncCallback(const AppId &appId) override; 45 Status SetSyncParam(const AppId &appId, const StoreId &storeId, const KvSyncParam &syncParam) override; 46 Status GetSyncParam(const AppId &appId, const StoreId &storeId, KvSyncParam &syncParam) override; 47 Status EnableCapability(const AppId &appId, const StoreId &storeId) override; 48 Status DisableCapability(const AppId &appId, const StoreId &storeId) override; 49 Status SetCapability(const AppId &appId, const StoreId &storeId, const std::vector<std::string> &local, 50 const std::vector<std::string> &remote) override; 51 Status AddSubscribeInfo(const AppId &appId, const StoreId &storeId, const SyncInfo &syncInfo) override; 52 Status RmvSubscribeInfo(const AppId &appId, const StoreId &storeId, const SyncInfo &syncInfo) override; 53 Status Subscribe(const AppId &appId, const StoreId &storeId, sptr<IKvStoreObserver> observer) override; 54 Status Unsubscribe(const AppId &appId, const StoreId &storeId, sptr<IKvStoreObserver> observer) override; 55 Status GetBackupPassword(const AppId &appId, const StoreId &storeId, std::vector<uint8_t> &password) override; 56 DevBrief GetLocalDevice() override; 57 std::vector<DevBrief> GetRemoteDevices() override; 58 59 int32_t OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, const std::string &appId) override; 60 int32_t ResolveAutoLaunch(const std::string &identifier, DBLaunchParam ¶m) override; 61 int32_t OnUserChange(uint32_t code, const std::string &user, const std::string &account) override; 62 int32_t OnReady(const std::string &device) override; 63 64 private: 65 using StoreMetaData = OHOS::DistributedData::StoreMetaData; 66 using StrategyMeta = OHOS::DistributedData::StrategyMeta; 67 using StoreMetaDataLocal = OHOS::DistributedData::StoreMetaDataLocal; 68 using DBStore = DistributedDB::KvStoreNbDelegate; 69 using DBManager = DistributedDB::KvStoreDelegateManager; 70 using SyncEnd = KvStoreSyncManager::SyncEnd; 71 using DBResult = std::map<std::string, DistributedDB::DBStatus>; 72 using DBStatus = DistributedDB::DBStatus; 73 using DBMode = DistributedDB::SyncMode; 74 enum SyncAction { 75 ACTION_SYNC, 76 ACTION_SUBSCRIBE, 77 ACTION_UNSUBSCRIBE, 78 }; 79 struct SyncAgent { 80 pid_t pid_ = 0; 81 AppId appId_; 82 sptr<IKvStoreSyncCallback> callback_; 83 std::map<std::string, uint32_t> delayTimes_; 84 std::map<std::string, std::shared_ptr<StoreCache::Observers>> observers_; 85 void ReInit(pid_t pid, const AppId &appId); 86 }; 87 class Factory { 88 public: 89 Factory(); 90 ~Factory(); 91 }; 92 93 void AddOptions(const Options &options, StoreMetaData &metaData); 94 StoreMetaData GetStoreMetaData(const AppId &appId, const StoreId &storeId); 95 StrategyMeta GetStrategyMeta(const AppId &appId, const StoreId &storeId); 96 int32_t GetInstIndex(uint32_t tokenId, const AppId &appId); 97 Status DoSync(const StoreMetaData &meta, const SyncInfo &info, const SyncEnd &complete, int32_t type); 98 Status DoComplete(const StoreMetaData &meta, const SyncInfo &info, RefCount refCount, const DBResult &dbResult); 99 uint32_t GetSyncDelayTime(uint32_t delay, const StoreId &storeId); 100 Status ConvertDbStatus(DBStatus status) const; 101 DBMode ConvertDBMode(SyncMode syncMode) const; 102 std::vector<std::string> ConvertDevices(const std::vector<std::string> &deviceIds) const; 103 std::shared_ptr<StoreCache::Observers> GetObservers(uint32_t tokenId, const std::string &storeId); 104 void SaveLocalMetaData(const Options &options, const StoreMetaData &metaData); 105 static Factory factory_; 106 ConcurrentMap<uint32_t, SyncAgent> syncAgents_; 107 StoreCache storeCache_; 108 }; 109 } // namespace OHOS::DistributedKv 110 #endif // OHOS_DISTRIBUTED_DATA_SERVICE_KVDB_SERVICE_IMPL_H 111