1 
2 /*
3  * Copyright (c) 2021 Huawei Device Co., Ltd.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef I_SYNCER_H
18 #define I_SYNCER_H
19 
20 #include <functional>
21 #include <map>
22 #include <memory>
23 #include <mutex>
24 
25 #include "distributeddb/result_set.h"
26 #include "isync_interface.h"
27 #include "types_export.h"
28 #include "query_sync_object.h"
29 #include "store_types.h"
30 
31 namespace DistributedDB {
32 struct SyncerBasicInfo {
33     bool isSyncActive = false;
34     bool isAutoSync = false;
35 };
36 class ISyncer {
37 public:
38     struct SyncParma {
39         std::vector<std::string> devices;
40         std::function<void(const std::map<std::string, int> &devicesMap)> onComplete;
41         SyncStatusCallback relationOnComplete;
42         std::function<void(void)> onFinalize;
43         int mode = 0;
44         bool wait = false;
45         bool isQuerySync = false;
46         QuerySyncObject syncQuery;
47     };
48 
~ISyncer()49     virtual ~ISyncer() {};
50 
51     // Init the Syncer modules
52     virtual int Initialize(ISyncInterface *syncInterface, bool isNeedActive) = 0;
53 
54     // Close
55     virtual int Close(bool isClosedOperation) = 0;
56 
57     // Sync function.
58     // param devices: The device id list.
59     // param mode: Sync mode, see SyncMode.
60     // param onComplete: The syncer finish callback. set by caller
61     // param onFinalize: will be callback when this Sync Operation finalized.
62     // return a Sync id. It will return a positive value if failed,
63     virtual int Sync(const std::vector<std::string> &devices, int mode,
64         const std::function<void(const std::map<std::string, int> &)> &onComplete,
65         const std::function<void(void)> &onFinalize, bool wait) = 0;
66 
67     // Sync function. use SyncParma to reduce parameter.
68     virtual int Sync(const SyncParma &param, uint64_t connectionId) = 0;
69 
70     // Remove the operation, with the given syncId, used to clean resource if sync finished or failed.
71     virtual int RemoveSyncOperation(int syncId) = 0;
72 
73     virtual int StopSync(uint64_t connectionId) = 0;
74 
75     // Get The current virtual timestamp
76     virtual uint64_t GetTimestamp() = 0;
77 
78     // Enable auto sync function
79     virtual void EnableAutoSync(bool enable) = 0;
80 
81     // delete specified device's watermark
82     virtual int EraseDeviceWaterMark(const std::string &deviceId, bool isNeedHash) = 0;
83 
84     // delete specified device's and table's watermark
85     virtual int EraseDeviceWaterMark(const std::string &deviceId, bool isNeedHash,
86         const std::string &tableName) = 0;
87 
88     // Local data changed callback
89     virtual void LocalDataChanged(int notifyEvent) = 0;
90 
91     // Get manual sync queue size
92     virtual int GetQueuedSyncSize(int *queuedSyncSize) const = 0;
93 
94     // Set manual sync queue limit
95     virtual int SetQueuedSyncLimit(const int *queuedSyncLimit) = 0;
96 
97     // Get manual sync queue limit
98     virtual int GetQueuedSyncLimit(int *queuedSyncLimit) const = 0;
99 
100     // Disable add new manual sync, for rekey
101     virtual int DisableManualSync(void) = 0;
102 
103     // Enable add new manual sync, for rekey
104     virtual int EnableManualSync(void) = 0;
105 
106     // Get local deviceId, is hashed
107     virtual int GetLocalIdentity(std::string &outTarget) const = 0;
108 
109     // Set stale data wipe policy
110     virtual int SetStaleDataWipePolicy(WipePolicy policy) = 0;
111 
112     // Set Manual Sync retry config
113     virtual int SetSyncRetry(bool isRetry) = 0;
114 
115     // Set an equal identifier for this database, After this called, send msg to the target will use this identifier
116     virtual int SetEqualIdentifier(const std::string &identifier, const std::vector<std::string> &targets) = 0;
117 
118     virtual void Dump(int fd) = 0;
119 
120     virtual SyncerBasicInfo DumpSyncerBasicInfo() = 0;
121 
122     virtual int RemoteQuery(const std::string &device, const RemoteCondition &condition,
123         uint64_t timeout, uint64_t connectionId, std::shared_ptr<ResultSet> &result) = 0;
124 
125     virtual int GetSyncDataSize(const std::string &device, size_t &size) const = 0;
126 
127     virtual int GetHashDeviceId(const std::string &clientId, std::string &hashDevId) const = 0;
128 
129     virtual int GetWatermarkInfo(const std::string &device, WatermarkInfo &info) = 0;
130 
131     virtual int UpgradeSchemaVerInMeta() = 0;
132 
133     virtual void ResetSyncStatus() = 0;
134 
135     virtual int64_t GetLocalTimeOffset() = 0;
136 
137     virtual int32_t GetTaskCount() = 0;
138 };
139 } // namespace DistributedDB
140 
141 #endif  // I_SYNCER_H
142