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