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 #include "syncer_proxy.h"
17 
18 #include "syncer_factory.h"
19 #include "db_errno.h"
20 #include "log_print.h"
21 
22 namespace DistributedDB {
SyncerProxy()23 SyncerProxy::SyncerProxy()
24     : syncer_(nullptr)
25 {
26 }
27 
Initialize(ISyncInterface * syncInterface,bool isNeedActive)28 int SyncerProxy::Initialize(ISyncInterface *syncInterface, bool isNeedActive)
29 {
30     if (syncInterface == nullptr) {
31         return -E_INVALID_ARGS;
32     }
33 
34     int interfaceType = syncInterface->GetInterfaceType();
35     {
36         std::lock_guard<std::mutex> lock(syncerLock_);
37         if (syncer_ == nullptr) {
38             syncer_ = SyncerFactory::GetSyncer(interfaceType);
39         }
40     }
41     if (syncer_ == nullptr) {
42         LOGF("syncer create failed! invalid interface type %d", interfaceType);
43         return -E_OUT_OF_MEMORY;
44     }
45 
46     return syncer_->Initialize(syncInterface, isNeedActive);
47 }
48 
Close(bool isClosedOperation)49 int SyncerProxy::Close(bool isClosedOperation)
50 {
51     if (syncer_ == nullptr) {
52         return -E_NOT_INIT;
53     }
54     return syncer_->Close(isClosedOperation);
55 }
56 
Sync(const std::vector<std::string> & devices,int mode,const std::function<void (const std::map<std::string,int> &)> & onComplete,const std::function<void (void)> & onFinalize,bool wait)57 int SyncerProxy::Sync(const std::vector<std::string> &devices, int mode,
58     const std::function<void(const std::map<std::string, int> &)> &onComplete,
59     const std::function<void(void)> &onFinalize, bool wait)
60 {
61     if (syncer_ == nullptr) { // LCOV_EXCL_BR_LINE
62         return -E_NOT_INIT;
63     }
64     return syncer_->Sync(devices, mode, onComplete, onFinalize, wait);
65 }
66 
Sync(const SyncParma & parma,uint64_t connectionId)67 int SyncerProxy::Sync(const SyncParma &parma, uint64_t connectionId)
68 {
69     if (syncer_ == nullptr) {
70         return -E_NOT_INIT;
71     }
72     return syncer_->Sync(parma, connectionId);
73 }
74 
RemoveSyncOperation(int syncId)75 int SyncerProxy::RemoveSyncOperation(int syncId)
76 {
77     if (syncer_ == nullptr) { // LCOV_EXCL_BR_LINE
78         return -E_NOT_INIT;
79     }
80     return syncer_->RemoveSyncOperation(syncId);
81 }
82 
StopSync(uint64_t connectionId)83 int SyncerProxy::StopSync(uint64_t connectionId)
84 {
85     if (syncer_ == nullptr) {
86         return -E_NOT_INIT;
87     }
88     return syncer_->StopSync(connectionId);
89 }
90 
GetTimestamp()91 uint64_t SyncerProxy::GetTimestamp()
92 {
93     if (syncer_ == nullptr) {
94         return SyncerFactory::GetSyncer(ISyncInterface::SYNC_SVD)->GetTimestamp();
95     }
96     return syncer_->GetTimestamp();
97 }
98 
EnableAutoSync(bool enable)99 void SyncerProxy::EnableAutoSync(bool enable)
100 {
101     if (syncer_ == nullptr) {
102         return;
103     }
104     syncer_->EnableAutoSync(enable);
105 }
106 
EraseDeviceWaterMark(const std::string & deviceId,bool isNeedHash)107 int SyncerProxy::EraseDeviceWaterMark(const std::string &deviceId, bool isNeedHash)
108 {
109     return EraseDeviceWaterMark(deviceId, isNeedHash, "");
110 }
111 
EraseDeviceWaterMark(const std::string & deviceId,bool isNeedHash,const std::string & tableName)112 int SyncerProxy::EraseDeviceWaterMark(const std::string &deviceId, bool isNeedHash,
113     const std::string &tableName)
114 {
115     if (syncer_ == nullptr) {
116         LOGE("[SyncerProxy] Syncer no init, unknown rule to erase waterMark!");
117         return -E_NOT_INIT;
118     }
119     return syncer_->EraseDeviceWaterMark(deviceId, isNeedHash, tableName);
120 }
121 
LocalDataChanged(int notifyEvent)122 void SyncerProxy::LocalDataChanged(int notifyEvent)
123 {
124     if (syncer_ == nullptr) {
125         return;
126     }
127     syncer_->LocalDataChanged(notifyEvent);
128 }
129 
GetQueuedSyncSize(int * queuedSyncSize) const130 int SyncerProxy::GetQueuedSyncSize(int *queuedSyncSize) const
131 {
132     if (syncer_ == nullptr) {
133         return -E_NOT_INIT;
134     }
135     return syncer_->GetQueuedSyncSize(queuedSyncSize);
136 }
137 
SetQueuedSyncLimit(const int * queuedSyncLimit)138 int SyncerProxy::SetQueuedSyncLimit(const int *queuedSyncLimit)
139 {
140     if (syncer_ == nullptr) {
141         return -E_NOT_INIT;
142     }
143     return syncer_->SetQueuedSyncLimit(queuedSyncLimit);
144 }
145 
GetQueuedSyncLimit(int * queuedSyncLimit) const146 int SyncerProxy::GetQueuedSyncLimit(int *queuedSyncLimit) const
147 {
148     if (syncer_ == nullptr) {
149         return -E_NOT_INIT;
150     }
151     return syncer_->GetQueuedSyncLimit(queuedSyncLimit);
152 }
153 
DisableManualSync(void)154 int SyncerProxy::DisableManualSync(void)
155 {
156     if (syncer_ == nullptr) {
157         return -E_NOT_INIT;
158     }
159     return syncer_->DisableManualSync();
160 }
161 
EnableManualSync(void)162 int SyncerProxy::EnableManualSync(void)
163 {
164     if (syncer_ == nullptr) {
165         return -E_NOT_INIT;
166     }
167     return syncer_->EnableManualSync();
168 }
169 
GetLocalIdentity(std::string & outTarget) const170 int SyncerProxy::GetLocalIdentity(std::string &outTarget) const
171 {
172     if (syncer_ == nullptr) {
173         return -E_NOT_INIT;
174     }
175     return syncer_->GetLocalIdentity(outTarget);
176 }
177 
SetStaleDataWipePolicy(WipePolicy policy)178 int SyncerProxy::SetStaleDataWipePolicy(WipePolicy policy)
179 {
180     if (syncer_ == nullptr) {
181         return -E_NOT_INIT;
182     }
183     return syncer_->SetStaleDataWipePolicy(policy);
184 }
185 
SetSyncRetry(bool isRetry)186 int SyncerProxy::SetSyncRetry(bool isRetry)
187 {
188     if (syncer_ == nullptr) {
189         return -E_NOT_INIT;
190     }
191     return syncer_->SetSyncRetry(isRetry);
192 }
193 
SetEqualIdentifier(const std::string & identifier,const std::vector<std::string> & targets)194 int SyncerProxy::SetEqualIdentifier(const std::string &identifier, const std::vector<std::string> &targets)
195 {
196     if (syncer_ == nullptr) {
197         return -E_NOT_INIT;
198     }
199     return syncer_->SetEqualIdentifier(identifier, targets);
200 }
201 
Dump(int fd)202 void SyncerProxy::Dump(int fd)
203 {
204     if (syncer_ == nullptr) {
205         return;
206     }
207     return syncer_->Dump(fd);
208 }
209 
DumpSyncerBasicInfo()210 SyncerBasicInfo SyncerProxy::DumpSyncerBasicInfo()
211 {
212     if (syncer_ == nullptr) {
213         return SyncerBasicInfo { false, false };
214     }
215     return syncer_->DumpSyncerBasicInfo();
216 }
217 
RemoteQuery(const std::string & device,const RemoteCondition & condition,uint64_t timeout,uint64_t connectionId,std::shared_ptr<ResultSet> & result)218 int SyncerProxy::RemoteQuery(const std::string &device, const RemoteCondition &condition,
219     uint64_t timeout, uint64_t connectionId, std::shared_ptr<ResultSet> &result)
220 {
221     if (syncer_ == nullptr) {
222         return -E_NOT_INIT;
223     }
224     return syncer_->RemoteQuery(device, condition, timeout, connectionId, result);
225 }
226 
GetSyncDataSize(const std::string & device,size_t & size) const227 int SyncerProxy::GetSyncDataSize(const std::string &device, size_t &size) const
228 {
229     if (syncer_ == nullptr) {
230         return -E_NOT_INIT;
231     }
232     return syncer_->GetSyncDataSize(device, size);
233 }
234 
GetHashDeviceId(const std::string & clientId,std::string & hashDevId) const235 int SyncerProxy::GetHashDeviceId(const std::string &clientId, std::string &hashDevId) const
236 {
237     if (syncer_ == nullptr) {
238         return -E_NOT_INIT;
239     }
240     return syncer_->GetHashDeviceId(clientId, hashDevId);
241 }
242 
GetWatermarkInfo(const std::string & device,WatermarkInfo & info)243 int SyncerProxy::GetWatermarkInfo(const std::string &device, WatermarkInfo &info)
244 {
245     if (syncer_ == nullptr) {
246         return -E_NOT_INIT;
247     }
248     return syncer_->GetWatermarkInfo(device, info);
249 }
250 
UpgradeSchemaVerInMeta()251 int SyncerProxy::UpgradeSchemaVerInMeta()
252 {
253     if (syncer_ == nullptr) {
254         return -E_NOT_INIT;
255     }
256     return syncer_->UpgradeSchemaVerInMeta();
257 }
258 
ResetSyncStatus()259 void SyncerProxy::ResetSyncStatus()
260 {
261     if (syncer_ == nullptr) {
262         return;
263     }
264     syncer_->ResetSyncStatus();
265 }
266 
GetLocalTimeOffset()267 int64_t SyncerProxy::GetLocalTimeOffset()
268 {
269     if (syncer_ == nullptr) {
270         LOGW("[SyncerProxy] get local time offset without syncer");
271         return 0;
272     }
273     return syncer_->GetLocalTimeOffset();
274 }
275 
GetTaskCount()276 int32_t SyncerProxy::GetTaskCount()
277 {
278     if (syncer_ == nullptr) {
279         LOGW("[SyncerProxy] get task count without syncer");
280         return 0;
281     }
282     return syncer_->GetTaskCount();
283 }
284 } // namespace DistributedDB
285