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_MANAGER_H
17 #define OHOS_DM_KV_ADAPTER_MANAGER_H
18 
19 #include <atomic>
20 #include <map>
21 #include <memory>
22 #include <string>
23 
24 #include "dm_single_instance.h"
25 
26 #include "kv_adapter.h"
27 
28 namespace OHOS {
29 namespace DistributedHardware {
30 
31 class KVAdapterManager {
32     DM_DECLARE_SINGLE_INSTANCE_BASE(KVAdapterManager);
33 public:
34     int32_t Init();
35     void UnInit();
36     void ReInit();
37     int32_t PutByAnoyDeviceId(const std::string &key, const DmKVValue &value);
38     int32_t Get(const std::string &key, DmKVValue &value);
39     int32_t DeleteAgedEntry();
40     int32_t AppUnintall(const std::string &appId);
41 
42 private:
43     KVAdapterManager() = default;
44     ~KVAdapterManager() = default;
45     inline bool IsTimeOut(int64_t sourceTime, int64_t targetTime, int64_t timeOut);
46 
47 private:
48     std::shared_ptr<DistributedKv::KvStoreDeathRecipient> deathRecipient_ = nullptr;
49     std::shared_ptr<KVAdapter> kvAdapter_ = nullptr;
50     std::mutex idCacheMapMtx_;
51     std::map<std::string, DmKVValue> idCacheMap_;
52 };
53 } // namespace DistributedHardware
54 } // namespace OHOS
55 #endif // OHOS_DM_KV_ADAPTER_MANAGER_H
56