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