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 SUBSCRIBE_RECORDER_H
17 #define SUBSCRIBE_RECORDER_H
18 
19 #include <map>
20 #include <mutex>
21 #include "db_types.h"
22 #include "query_sync_object.h"
23 #include "store_types.h"
24 
25 namespace DistributedDB {
26 class SubscribeRecorder final {
27 public:
28     SubscribeRecorder() = default;
29     ~SubscribeRecorder() = default;
30 
31     void RecordSubscribe(const DBInfo &dbInfo, const DeviceID &deviceId, const QuerySyncObject &query);
32 
33     void RemoveAllSubscribe();
34 
35     void RemoveRemoteSubscribe(const DeviceID &deviceId);
36 
37     void RemoveRemoteSubscribe(const DBInfo &dbInfo);
38 
39     void RemoveRemoteSubscribe(const DBInfo &dbInfo, const DeviceID &deviceId);
40 
41     void RemoveRemoteSubscribe(const DBInfo &dbInfo, const DeviceID &deviceId, const QuerySyncObject &query);
42 
43     void GetSubscribeQuery(const DBInfo &dbInfo,
44         std::map<std::string, std::vector<QuerySyncObject>> &subscribeQuery) const;
45 private:
46 
47     void RemoveSubscribeQueries(const DBInfo &dbInfo, const DeviceID &deviceId);
48 
49     void RemoveSubscribeQuery(const DBInfo &dbInfo, const DeviceID &deviceId, const std::string &queryId);
50 
51     static bool CheckSameDBInfo(const DBInfo &srcDbInfo, const DBInfo &dtsDbInfo);
52 
53     struct SubscribeEntry {
54         DBInfo dbInfo;
55         std::map<DeviceID, std::vector<QuerySyncObject>> subscribeQuery;
56     };
57     mutable std::mutex subscribeMutex_;
58     std::vector<SubscribeEntry> subscribeCache_;
59 };
60 }
61 #endif // SUBSCRIBE_RECORDER_H
62