1/*
2 * Copyright (c) 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_DM_KV_ADAPTER_H
17#define OHOS_DM_KV_ADAPTER_H
18
19#include <atomic>
20#include <condition_variable>
21#include <map>
22#include <memory>
23#include <string>
24#include <unordered_set>
25#include <vector>
26
27#include "dm_kv_info.h"
28#include "distributed_kv_data_manager.h"
29#include "kvstore_death_recipient.h"
30#include "kvstore_observer.h"
31
32namespace OHOS {
33namespace DistributedHardware {
34class KVAdapter : public DistributedKv::KvStoreDeathRecipient, public std::enable_shared_from_this<KVAdapter> {
35public:
36    KVAdapter() = default;
37    virtual ~KVAdapter() = default;
38    int32_t Init();
39    void UnInit();
40    int32_t ReInit();
41    int32_t Put(const std::string &key, const std::string &value);
42    int32_t Get(const std::string &key, std::string &value);
43    int32_t DeleteKvStore();
44    int32_t DeleteByAppId(const std::string &appId, const std::string &prefix);
45    int32_t DeleteBatch(const std::vector<std::string> &keys);
46    void OnRemoteDied() override;
47
48private:
49    DistributedKv::Status GetLocalKvStorePtr();
50    void RegisterKvStoreDeathListener();
51    void UnregisterKvStoreDeathListener();
52
53private:
54    DistributedKv::AppId appId_;
55    DistributedKv::StoreId storeId_;
56    DistributedKv::DistributedKvDataManager kvDataMgr_;
57    DistributedKv::DataType dataType_ = DistributedKv::DataType::TYPE_STATICS;
58    std::shared_ptr<DistributedKv::SingleKvStore> kvStorePtr_ = nullptr;
59    std::mutex kvAdapterMutex_;
60    std::atomic<bool> isInited_ = false;
61};
62} // namespace DistributedHardware
63} // namespace OHOS
64#endif // OHOS_DM_KV_ADAPTER_H
65