1 /*
2  * Copyright (c) 2021 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 SYNCER_PROXY_H
17 #define SYNCER_PROXY_H
18 
19 #include <functional>
20 #include <mutex>
21 #include <map>
22 #include <memory>
23 
24 #include "isyncer.h"
25 
26 namespace DistributedDB {
27 class SyncerProxy : public ISyncer {
28 public:
29     SyncerProxy();
~SyncerProxy()30     ~SyncerProxy() {};
31 
32     // Init the Syncer modules
33     int Initialize(ISyncInterface *syncInterface, bool isNeedActive) override;
34 
35     // Close the syncer
36     int Close(bool isClosedOperation) override;
37 
38     // Sync function.
39     // param devices: The device id list.
40     // param mode: Sync mode, see SyncMode.
41     // param onComplete: The syncer finish callback. set by caller
42     // param onFinalize: will be callback when this Sync Operation finalized.
43     // return a Sync id. It will return a positive value if failed,
44     int Sync(const std::vector<std::string> &devices, int mode,
45         const std::function<void(const std::map<std::string, int> &)> &onComplete,
46         const std::function<void(void)> &onFinalize, bool wait) override;
47 
48     // Sync function. use SyncParma to reduce parameter.
49     int Sync(const SyncParma &param, uint64_t connectionId) override;
50 
51     // Remove the operation, with the given syncId, used to clean resource if sync finished or failed.
52     int RemoveSyncOperation(int syncId) override;
53 
54     int StopSync(uint64_t connectionId) override;
55 
56     // Get The current virtual timestamp
57     uint64_t GetTimestamp() override;
58 
59     // Enable auto sync function
60     void EnableAutoSync(bool enable) override;
61 
62     // delete specified device's watermark
63     int EraseDeviceWaterMark(const std::string &deviceId, bool isNeedHash) override;
64 
65     // delete specified device's and table's watermark
66     int EraseDeviceWaterMark(const std::string &deviceId, bool isNeedHash,
67         const std::string &tableName) override;
68 
69     // Local data changed callback
70     void LocalDataChanged(int notifyEvent) override;
71 
72     // Get manual sync queue size
73     int GetQueuedSyncSize(int *queuedSyncSize) const override;
74 
75     // Set manual sync queue limit
76     int SetQueuedSyncLimit(const int *queuedSyncLimit) override;
77 
78     // Get manual sync queue limit
79     int GetQueuedSyncLimit(int *queuedSyncLimit) const override;
80 
81     // Disable add new manual sync, for rekey
82     int DisableManualSync(void) override;
83 
84     // Enable add new manual sync, for rekey
85     int EnableManualSync(void) override;
86 
87     // Get local deviceId, is hashed
88     int GetLocalIdentity(std::string &outTarget) const override;
89 
90     // Set stale data wipe policy
91     int SetStaleDataWipePolicy(WipePolicy policy) override;
92 
93     // Set Manual Sync retry config
94     int SetSyncRetry(bool isRetry) override;
95 
96     // Set an equal identifier for this database, After this called, send msg to the target will use this identifier
97     int SetEqualIdentifier(const std::string &identifier, const std::vector<std::string> &targets) override;
98 
99     // Dump syncer info
100     void Dump(int fd) override;
101 
102     // Dump syncer basic info
103     SyncerBasicInfo DumpSyncerBasicInfo() override;
104 
105     int RemoteQuery(const std::string &device, const RemoteCondition &condition,
106         uint64_t timeout, uint64_t connectionId, std::shared_ptr<ResultSet> &result) override;
107 
108     int GetSyncDataSize(const std::string &device, size_t &size) const override;
109 
110     int GetHashDeviceId(const std::string &clientId, std::string &hashDevId) const override;
111 
112     int GetWatermarkInfo(const std::string &device, WatermarkInfo &info) override;
113 
114     int UpgradeSchemaVerInMeta() override;
115 
116     void ResetSyncStatus() override;
117 
118     int64_t GetLocalTimeOffset() override;
119 
120     int32_t GetTaskCount() override;
121 private:
122     std::mutex syncerLock_;
123     std::shared_ptr<ISyncer> syncer_;
124 };
125 } // namespace DistributedDB
126 
127 #endif  // SYNCER_PROXY_H
128