1 /* 2 * Copyright (c) 2022-2024 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_delegate_manager.h" 24 #include "kv_store_nb_delegate.h" 25 #include "kvdb_notifier_proxy.h" 26 #include "kvdb_service_stub.h" 27 #include "kvdb_watcher.h" 28 #include "kvstore_sync_manager.h" 29 #include "metadata/meta_data_manager.h" 30 #include "metadata/store_meta_data.h" 31 #include "metadata/store_meta_data_local.h" 32 #include "metadata/strategy_meta_data.h" 33 #include "store/auto_cache.h" 34 #include "store/general_value.h" 35 #include "utils/ref_count.h" 36 namespace OHOS::DistributedKv { 37 class API_EXPORT KVDBServiceImpl final : public KVDBServiceStub { 38 public: 39 using DBLaunchParam = DistributedDB::AutoLaunchParam; 40 using Handler = std::function<void(int, std::map<std::string, std::vector<std::string>> &)>; 41 using RefCount = DistributedData::RefCount; 42 using StoreMetaData = OHOS::DistributedData::StoreMetaData; 43 API_EXPORT KVDBServiceImpl(); 44 virtual ~KVDBServiceImpl(); 45 Status GetStoreIds(const AppId &appId, std::vector<StoreId> &storeIds) override; 46 Status BeforeCreate(const AppId &appId, const StoreId &storeId, const Options &options) override; 47 Status AfterCreate(const AppId &appId, const StoreId &storeId, const Options &options, 48 const std::vector<uint8_t> &password) override; 49 Status Delete(const AppId &appId, const StoreId &storeId) override; 50 Status Close(const AppId &appId, const StoreId &storeId) override; 51 Status CloudSync(const AppId &appId, const StoreId &storeId, const SyncInfo &syncInfo) override; 52 Status Sync(const AppId &appId, const StoreId &storeId, SyncInfo &syncInfo) override; 53 Status RegServiceNotifier(const AppId &appId, sptr<IKVDBNotifier> notifier) override; 54 Status UnregServiceNotifier(const AppId &appId) override; 55 Status SetSyncParam(const AppId &appId, const StoreId &storeId, const KvSyncParam &syncParam) override; 56 Status GetSyncParam(const AppId &appId, const StoreId &storeId, KvSyncParam &syncParam) override; 57 Status EnableCapability(const AppId &appId, const StoreId &storeId) override; 58 Status DisableCapability(const AppId &appId, const StoreId &storeId) override; 59 Status SetCapability(const AppId &appId, const StoreId &storeId, const std::vector<std::string> &local, 60 const std::vector<std::string> &remote) override; 61 Status AddSubscribeInfo(const AppId &appId, const StoreId &storeId, const SyncInfo &syncInfo) override; 62 Status RmvSubscribeInfo(const AppId &appId, const StoreId &storeId, const SyncInfo &syncInfo) override; 63 Status Subscribe(const AppId &appId, const StoreId &storeId, sptr<IKvStoreObserver> observer) override; 64 Status Unsubscribe(const AppId &appId, const StoreId &storeId, sptr<IKvStoreObserver> observer) override; 65 Status GetBackupPassword(const AppId &appId, const StoreId &storeId, std::vector<uint8_t> &password, 66 int32_t passwordType) override; 67 Status NotifyDataChange(const AppId &appId, const StoreId &storeId, uint64_t delay) override; 68 Status PutSwitch(const AppId &appId, const SwitchData &data) override; 69 Status GetSwitch(const AppId &appId, const std::string &networkId, SwitchData &data) override; 70 Status SubscribeSwitchData(const AppId &appId) override; 71 Status UnsubscribeSwitchData(const AppId &appId) override; 72 Status SetConfig(const AppId &appId, const StoreId &storeId, const StoreConfig &storeConfig) override; 73 int32_t OnBind(const BindInfo &bindInfo) override; 74 int32_t OnInitialize() override; 75 int32_t OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, const std::string &appId) override; 76 int32_t ResolveAutoLaunch(const std::string &identifier, DBLaunchParam ¶m) override; 77 int32_t OnUserChange(uint32_t code, const std::string &user, const std::string &account) override; 78 Status RemoveDeviceData(const AppId &appId, const StoreId &storeId, const std::string &device) override; 79 80 private: 81 using StrategyMeta = OHOS::DistributedData::StrategyMeta; 82 using StoreMetaDataLocal = OHOS::DistributedData::StoreMetaDataLocal; 83 using ChangeType = OHOS::DistributedData::DeviceMatrix::ChangeType; 84 using DBStore = DistributedDB::KvStoreNbDelegate; 85 using DBManager = DistributedDB::KvStoreDelegateManager; 86 using SyncEnd = KvStoreSyncManager::SyncEnd; 87 using DBResult = std::map<std::string, DistributedDB::DBStatus>; 88 using DBStatus = DistributedDB::DBStatus; 89 using DBMode = DistributedDB::SyncMode; 90 using Action = OHOS::DistributedData::MetaDataManager::Action; 91 using GeneralError = DistributedData::GeneralError; 92 enum SyncAction { 93 ACTION_SYNC, 94 ACTION_SUBSCRIBE, 95 ACTION_UNSUBSCRIBE, 96 }; 97 struct SyncAgent { 98 pid_t pid_ = 0; 99 int32_t switchesObserverCount_ = 0; 100 bool staticsChanged_ = false; 101 bool dynamicChanged_ = false; 102 AppId appId_; 103 sptr<IKVDBNotifier> notifier_; 104 std::map<std::string, uint32_t> delayTimes_; 105 std::map<std::string, std::set<std::shared_ptr<KVDBWatcher>>> watchers_; 106 void ReInit(pid_t pid, const AppId &appId); 107 }; 108 class Factory { 109 public: 110 Factory(); 111 ~Factory(); 112 private: 113 std::shared_ptr<KVDBServiceImpl> product_; 114 }; 115 116 void Init(); 117 void AddOptions(const Options &options, StoreMetaData &metaData); 118 StoreMetaData GetStoreMetaData(const AppId &appId, const StoreId &storeId); 119 StoreMetaData GetDistributedDataMeta(const std::string &deviceId); 120 StrategyMeta GetStrategyMeta(const AppId &appId, const StoreId &storeId); 121 int32_t GetInstIndex(uint32_t tokenId, const AppId &appId); 122 bool IsNeedMetaSync(const StoreMetaData &meta, const std::vector<std::string> &uuids); 123 Status DoCloudSync(const StoreMetaData &meta, const SyncInfo &syncInfo); 124 void DoCloudSync(bool statics, bool dynamic); 125 Status DoSync(const StoreMetaData &meta, const SyncInfo &info, const SyncEnd &complete, int32_t type); 126 Status DoSyncInOrder(const StoreMetaData &meta, const SyncInfo &info, const SyncEnd &complete, int32_t type); 127 Status DoSyncBegin(const std::vector<std::string> &devices, const StoreMetaData &meta, 128 const SyncInfo &info, const SyncEnd &complete, int32_t type); 129 Status DoComplete(const StoreMetaData &meta, const SyncInfo &info, RefCount refCount, const DBResult &dbResult); 130 uint32_t GetSyncDelayTime(uint32_t delay, const StoreId &storeId); 131 Status ConvertDbStatus(DBStatus status) const; 132 Status ConvertGeneralErr(GeneralError error) const; 133 DBMode ConvertDBMode(SyncMode syncMode) const; 134 ChangeType ConvertType(SyncMode syncMode) const; 135 SwitchState ConvertAction(Action action) const; 136 SyncMode GetSyncMode(bool local, bool remote) const; 137 std::vector<std::string> ConvertDevices(const std::vector<std::string> &deviceIds) const; 138 DistributedData::GeneralStore::SyncMode ConvertGeneralSyncMode(SyncMode syncMode, SyncAction syncAction) const; 139 DBResult HandleGenBriefDetails(const DistributedData::GenDetails &details); 140 ProgressDetail HandleGenDetails(const DistributedData::GenDetails &details); 141 void OnAsyncComplete(uint32_t tokenId, uint64_t seqNum, ProgressDetail &&detail); 142 DistributedData::AutoCache::Watchers GetWatchers(uint32_t tokenId, const std::string &storeId); 143 using SyncResult = std::pair<std::vector<std::string>, std::map<std::string, DBStatus>>; 144 SyncResult ProcessResult(const std::map<std::string, int32_t> &results); 145 void SaveLocalMetaData(const Options &options, const StoreMetaData &metaData); 146 void RegisterKvServiceInfo(); 147 void RegisterHandler(); 148 void DumpKvServiceInfo(int fd, std::map<std::string, std::vector<std::string>> ¶ms); 149 void TryToSync(const StoreMetaData &metaData, bool force = false); 150 bool IsRemoteChange(const StoreMetaData &metaData, const std::string &device); 151 bool IsOHOSType(const std::vector<std::string> &ids); 152 Status ConvertDbStatusNative(DBStatus status); 153 bool CompareTripleIdentifier(const std::string &accountId, const std::string &identifier, 154 const StoreMetaData &storeMeta); 155 static Factory factory_; 156 ConcurrentMap<uint32_t, SyncAgent> syncAgents_; 157 std::shared_ptr<ExecutorPool> executors_; 158 std::atomic_uint64_t syncId_ = 0; 159 static constexpr int32_t OH_OS_TYPE = 10; 160 }; 161 } // namespace OHOS::DistributedKv 162 #endif // OHOS_DISTRIBUTED_DATA_SERVICE_KVDB_SERVICE_IMPL_H