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 OMIT_MULTI_VER
17 #include "multi_ver_syncer.h"
18
19 #include <functional>
20 #include <mutex>
21 #include <map>
22
23 #include "multi_ver_sync_engine.h"
24 #include "multi_ver_kvdb_sync_interface.h"
25 #include "log_print.h"
26
27 namespace DistributedDB {
MultiVerSyncer()28 MultiVerSyncer::MultiVerSyncer()
29 : autoSyncEnable_(true)
30 {
31 }
32
~MultiVerSyncer()33 MultiVerSyncer::~MultiVerSyncer()
34 {
35 }
36
EnableAutoSync(bool enable)37 void MultiVerSyncer::EnableAutoSync(bool enable)
38 {
39 LOGD("[Syncer] EnableAutoSync enable = %d", enable);
40 if (autoSyncEnable_ == enable) {
41 return;
42 }
43
44 autoSyncEnable_ = enable;
45 if (!enable || (syncEngine_ == nullptr)) {
46 return;
47 }
48
49 std::vector<std::string> devices;
50 GetOnlineDevices(devices);
51 if (devices.empty()) {
52 return;
53 }
54
55 int errCode = Sync(devices, SyncModeType::AUTO_PULL, nullptr, nullptr, false);
56 if (errCode != E_OK) {
57 LOGE("[Syncer] sync start by EnableAutoSync failed err %d", errCode);
58 }
59 }
60
EraseDeviceWaterMark(const std::string & deviceId,bool isNeedHash)61 int MultiVerSyncer::EraseDeviceWaterMark(const std::string &deviceId, bool isNeedHash)
62 {
63 return EraseDeviceWaterMark(deviceId, isNeedHash, "");
64 }
65
EraseDeviceWaterMark(const std::string & deviceId,bool isNeedHash,const std::string & tableName)66 int MultiVerSyncer::EraseDeviceWaterMark(const std::string &deviceId, bool isNeedHash,
67 const std::string &tableName)
68 {
69 return -E_NOT_SUPPORT;
70 }
71
LocalDataChanged(int notifyEvent)72 void MultiVerSyncer::LocalDataChanged(int notifyEvent)
73 {
74 if (!initialized_) {
75 LOGE("[Syncer] Syncer has not Init");
76 return;
77 }
78
79 if (!autoSyncEnable_) {
80 return;
81 }
82
83 syncEngine_->BroadCastDataChanged();
84 }
85
RemoteDataChanged(const std::string & device)86 void MultiVerSyncer::RemoteDataChanged(const std::string &device)
87 {
88 LOGD("[MultiVerSyncer] Remote data changed or device online dev %s{private}", device.c_str());
89 if (autoSyncEnable_) {
90 std::vector<std::string> devices {device};
91 int errCode = Sync(devices, SyncModeType::AUTO_PULL, nullptr, nullptr, false);
92 if (errCode != E_OK) {
93 LOGE("[MultiVerSyncer] sync start by RemoteDataChanged failed err %d", errCode);
94 }
95 }
96 }
97
RemoteDeviceOffline(const std::string & device)98 void MultiVerSyncer::RemoteDeviceOffline(const std::string &device)
99 {
100 (void) device;
101 }
102
CreateSyncEngine()103 ISyncEngine *MultiVerSyncer::CreateSyncEngine()
104 {
105 return new (std::nothrow) MultiVerSyncEngine();
106 }
107
AddSyncOperation(ISyncEngine * engine,SyncOperation * operation)108 void MultiVerSyncer::AddSyncOperation(ISyncEngine *engine, SyncOperation *operation)
109 {
110 MultiVerKvDBSyncInterface *syncInterface = static_cast<MultiVerKvDBSyncInterface *>(syncInterface_);
111 syncInterface->NotifyStartSyncOperation();
112 GenericSyncer::AddSyncOperation(syncEngine_, operation);
113 }
114
SyncOperationKillCallbackInner(int syncId)115 void MultiVerSyncer::SyncOperationKillCallbackInner(int syncId)
116 {
117 if (syncInterface_ != nullptr) {
118 LOGI("[MultiVerSyncer] Operation on kill id = %d", syncId);
119 MultiVerKvDBSyncInterface *syncInterface = static_cast<MultiVerKvDBSyncInterface *>(syncInterface_);
120 syncInterface->NotifyFinishSyncOperation();
121 }
122 GenericSyncer::SyncOperationKillCallbackInner(syncId);
123 }
124
SetStaleDataWipePolicy(WipePolicy policy)125 int MultiVerSyncer::SetStaleDataWipePolicy(WipePolicy policy)
126 {
127 (void) policy;
128 return -E_NOT_SUPPORT;
129 }
130 } // namespace DistributedDB
131 #endif