1 /*
2  * Copyright (c) 2023-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_DP_KV_ADAPTER_H
17 #define OHOS_DP_KV_ADAPTER_H
18 
19 #include <condition_variable>
20 #include <map>
21 #include <memory>
22 #include <string>
23 #include <unordered_set>
24 #include <vector>
25 
26 #include "distributed_kv_data_manager.h"
27 #include "kvstore_observer.h"
28 #include "types.h"
29 
30 #include "distributed_device_profile_enums.h"
31 #include "ikv_adapter.h"
32 
33 namespace OHOS {
34 namespace DistributedDeviceProfile {
35 class KVAdapter : public IKVAdapter {
36 public:
37     KVAdapter(const std::string &appId, const std::string &storeId,
38         const std::shared_ptr<DistributedKv::KvStoreObserver> &dataChangeListener,
39         const std::shared_ptr<DistributedKv::KvStoreSyncCallback> &syncCompletedListener,
40         const std::shared_ptr<DistributedKv::KvStoreDeathRecipient> &deathListener,
41         DistributedKv::DataType dataType);
42     virtual ~KVAdapter();
43 
44     int32_t Init() override;
45     int32_t UnInit() override;
46     int32_t Put(const std::string& key, const std::string& value) override;
47     int32_t PutBatch(const std::map<std::string, std::string>& values) override;
48     int32_t Delete(const std::string& key) override;
49     int32_t DeleteByPrefix(const std::string& keyPrefix) override;
50     int32_t Get(const std::string& key, std::string& value) override;
51     int32_t GetByPrefix(const std::string& keyPrefix, std::map<std::string, std::string>& values) override;
52     int32_t Sync(const std::vector<std::string>& deviceList, SyncMode syncMode) override;
53     int32_t GetDeviceEntries(const std::string& udid, std::map<std::string, std::string>& values) override;
54     int32_t DeleteBatch(const std::vector<std::string>& keys) override;
55     int32_t DeleteKvStore();
56 
57 private:
58     DistributedKv::Status GetKvStorePtr(DistributedKv::DataType dataType);
59     int32_t DeleteKvStorePtr();
60     int32_t RegisterDataChangeListener();
61     int32_t UnRegisterDataChangeListener();
62     int32_t DeleteDataChangeListener();
63     int32_t RegisterSyncCompletedListener();
64     int32_t UnRegisterSyncCompletedListener();
65     int32_t DeleteSyncCompletedListener();
66     int32_t RegisterDeathListener();
67     int32_t UnRegisterDeathListener();
68     int32_t DeleteDeathListener();
69 
70 private:
71     DistributedKv::AppId appId_;
72     DistributedKv::StoreId storeId_;
73     DistributedKv::DistributedKvDataManager kvDataMgr_;
74     DistributedKv::DataType dataType_;
75     std::shared_ptr<DistributedKv::SingleKvStore> kvStorePtr_ = nullptr;
76     std::shared_ptr<DistributedKv::KvStoreObserver> dataChangeListener_ = nullptr;
77     std::shared_ptr<DistributedKv::KvStoreSyncCallback> syncCompletedListener_ = nullptr;
78     std::shared_ptr<DistributedKv::KvStoreDeathRecipient> deathRecipient_ = nullptr;
79     std::mutex kvAdapterMutex_;
80 };
81 } // namespace DeviceProfile
82 } // namespace OHOS
83 #endif // OHOS_DP_KV_ADAPTER_H
84