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 DB_STATUS_ADAPTER_H
17 #define DB_STATUS_ADAPTER_H
18 
19 #include "db_info_handle.h"
20 #include <mutex>
21 #include "macro_utils.h"
22 
23 namespace DistributedDB {
24 using RemoteDBChangeCallback = std::function<void(const std::string &devInfo, const std::vector<DBInfo> &dbInfos)>;
25 using LocalDBChangeCallback = std::function<void(void)>;
26 using RemoteSupportChangeCallback = std::function<void(const std::string &devInfo)>;
27 class DBStatusAdapter {
28 public:
29     DBStatusAdapter();
30     ~DBStatusAdapter() = default;
31     DISABLE_COPY_ASSIGN_MOVE(DBStatusAdapter);
32 
33     void SetDBInfoHandle(const std::shared_ptr<DBInfoHandle> &dbInfoHandle);
34     bool IsSupport(const std::string &devInfo);
35     int GetLocalDBInfos(std::vector<DBInfo> &dbInfos);
36     void SetDBStatusChangeCallback(const RemoteDBChangeCallback &remote, const LocalDBChangeCallback &local,
37         const RemoteSupportChangeCallback &supportCallback);
38     void NotifyDBInfos(const DeviceInfos &devInfos, const std::vector<DBInfo> &dbInfos);
39     void TargetOffline(const std::string &device);
40     bool IsNeedAutoSync(const std::string &userId, const std::string &appId, const std::string &storeId,
41         const std::string &devInfo);
42     void SetRemoteOptimizeCommunication(const std::string &dev, bool optimize);
43     bool IsSendLabelExchange();
44 private:
45     std::shared_ptr<DBInfoHandle> GetDBInfoHandle() const;
46     bool LoadIntoCache(bool isLocal, const DeviceInfos &devInfos, const std::vector<DBInfo> &dbInfos);
47     void ClearAllCache();
48     void NotifyRemoteOffline();
49 
50     static int GetLocalDeviceId(std::string &deviceId);
51     static bool IsLocalDeviceId(const std::string &deviceId);
52     static bool MergeDBInfos(const std::vector<DBInfo> &srcDbInfos, std::vector<DBInfo> &dstDbInfos);
53     mutable std::mutex handleMutex_;
54     std::shared_ptr<DBInfoHandle> dbInfoHandle_ = nullptr;
55 
56     mutable std::mutex callbackMutex_;
57     RemoteDBChangeCallback remoteCallback_;
58     LocalDBChangeCallback localCallback_;
59     RemoteSupportChangeCallback supportCallback_;
60 
61     mutable std::mutex localInfoMutex_;
62     std::vector<DBInfo> localDBInfos_;
63 
64     mutable std::mutex remoteInfoMutex_;
65     std::map<std::string, std::vector<DBInfo>> remoteDBInfos_; // key: device uuid, value: all db which has notified
66 
67     mutable std::mutex supportMutex_;
68     bool localSendLabelExchange_;
69     bool cacheLocalSendLabelExchange_;
70     std::map<std::string, bool> remoteOptimizeInfo_; // key: device uuid, value: is support notified by user
71 };
72 }
73 #endif // DB_STATUS_ADAPTER_H
74