179a732c7Sopenharmony_ci/*
279a732c7Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
379a732c7Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
479a732c7Sopenharmony_ci * you may not use this file except in compliance with the License.
579a732c7Sopenharmony_ci * You may obtain a copy of the License at
679a732c7Sopenharmony_ci *
779a732c7Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
879a732c7Sopenharmony_ci *
979a732c7Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1079a732c7Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1179a732c7Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1279a732c7Sopenharmony_ci * See the License for the specific language governing permissions and
1379a732c7Sopenharmony_ci * limitations under the License.
1479a732c7Sopenharmony_ci */
1579a732c7Sopenharmony_ci
1679a732c7Sopenharmony_ci#ifndef OHOS_DM_KV_ADAPTER_H
1779a732c7Sopenharmony_ci#define OHOS_DM_KV_ADAPTER_H
1879a732c7Sopenharmony_ci
1979a732c7Sopenharmony_ci#include <atomic>
2079a732c7Sopenharmony_ci#include <condition_variable>
2179a732c7Sopenharmony_ci#include <map>
2279a732c7Sopenharmony_ci#include <memory>
2379a732c7Sopenharmony_ci#include <string>
2479a732c7Sopenharmony_ci#include <unordered_set>
2579a732c7Sopenharmony_ci#include <vector>
2679a732c7Sopenharmony_ci
2779a732c7Sopenharmony_ci#include "dm_kv_info.h"
2879a732c7Sopenharmony_ci#include "distributed_kv_data_manager.h"
2979a732c7Sopenharmony_ci#include "kvstore_death_recipient.h"
3079a732c7Sopenharmony_ci#include "kvstore_observer.h"
3179a732c7Sopenharmony_ci
3279a732c7Sopenharmony_cinamespace OHOS {
3379a732c7Sopenharmony_cinamespace DistributedHardware {
3479a732c7Sopenharmony_ciclass KVAdapter : public DistributedKv::KvStoreDeathRecipient, public std::enable_shared_from_this<KVAdapter> {
3579a732c7Sopenharmony_cipublic:
3679a732c7Sopenharmony_ci    KVAdapter() = default;
3779a732c7Sopenharmony_ci    virtual ~KVAdapter() = default;
3879a732c7Sopenharmony_ci    int32_t Init();
3979a732c7Sopenharmony_ci    void UnInit();
4079a732c7Sopenharmony_ci    int32_t ReInit();
4179a732c7Sopenharmony_ci    int32_t Put(const std::string &key, const std::string &value);
4279a732c7Sopenharmony_ci    int32_t Get(const std::string &key, std::string &value);
4379a732c7Sopenharmony_ci    int32_t DeleteKvStore();
4479a732c7Sopenharmony_ci    int32_t DeleteByAppId(const std::string &appId, const std::string &prefix);
4579a732c7Sopenharmony_ci    int32_t DeleteBatch(const std::vector<std::string> &keys);
4679a732c7Sopenharmony_ci    void OnRemoteDied() override;
4779a732c7Sopenharmony_ci
4879a732c7Sopenharmony_ciprivate:
4979a732c7Sopenharmony_ci    DistributedKv::Status GetLocalKvStorePtr();
5079a732c7Sopenharmony_ci    void RegisterKvStoreDeathListener();
5179a732c7Sopenharmony_ci    void UnregisterKvStoreDeathListener();
5279a732c7Sopenharmony_ci
5379a732c7Sopenharmony_ciprivate:
5479a732c7Sopenharmony_ci    DistributedKv::AppId appId_;
5579a732c7Sopenharmony_ci    DistributedKv::StoreId storeId_;
5679a732c7Sopenharmony_ci    DistributedKv::DistributedKvDataManager kvDataMgr_;
5779a732c7Sopenharmony_ci    DistributedKv::DataType dataType_ = DistributedKv::DataType::TYPE_STATICS;
5879a732c7Sopenharmony_ci    std::shared_ptr<DistributedKv::SingleKvStore> kvStorePtr_ = nullptr;
5979a732c7Sopenharmony_ci    std::mutex kvAdapterMutex_;
6079a732c7Sopenharmony_ci    std::atomic<bool> isInited_ = false;
6179a732c7Sopenharmony_ci};
6279a732c7Sopenharmony_ci} // namespace DistributedHardware
6379a732c7Sopenharmony_ci} // namespace OHOS
6479a732c7Sopenharmony_ci#endif // OHOS_DM_KV_ADAPTER_H
65