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 #include "single_ver_syncer.h"
16
17 #include "db_common.h"
18 #include "single_ver_sync_engine.h"
19
20 namespace DistributedDB {
RemoteDataChanged(const std::string & device)21 void SingleVerSyncer::RemoteDataChanged(const std::string &device)
22 {
23 LOGI("[SingleVerSyncer] device online dev %s", STR_MASK(device));
24 // while remote db is online again, need to do abilitySync
25 static_cast<SingleVerSyncEngine *>(syncEngine_)->SetIsNeedResetAbilitySync(device, true);
26 }
27
RemoteDeviceOffline(const std::string & device)28 void SingleVerSyncer::RemoteDeviceOffline(const std::string &device)
29 {
30 LOGI("[SingleVerSyncer] device offline dev %s", STR_MASK(device));
31 std::string userId = syncInterface_->GetDbProperties().GetStringProp(KvDBProperties::USER_ID, "");
32 std::string appId = syncInterface_->GetDbProperties().GetStringProp(KvDBProperties::APP_ID, "");
33 std::string storeId = syncInterface_->GetDbProperties().GetStringProp(KvDBProperties::STORE_ID, "");
34 RuntimeContext::GetInstance()->NotifyDatabaseStatusChange(userId, appId, storeId, device, false);
35 RefObject::IncObjRef(syncEngine_);
36 ISyncEngine *engine = syncEngine_;
37 ISyncInterface *storage = syncInterface_;
38 storage->IncRefCount();
39 int errCode = RuntimeContext::GetInstance()->ScheduleTask([engine, device, storage]() {
40 static_cast<SingleVerSyncEngine *>(engine)->OfflineHandleByDevice(device);
41 RefObject::DecObjRef(engine);
42 storage->DecRefCount();
43 });
44 if (errCode != E_OK) {
45 LOGW("[SingleVerSyncer][RemoteDeviceOffline] async task failed errCode = %d", errCode);
46 RefObject::DecObjRef(syncEngine_);
47 storage->DecRefCount();
48 }
49 }
50
EraseDeviceWaterMark(const std::string & deviceId,bool isNeedHash)51 int SingleVerSyncer::EraseDeviceWaterMark(const std::string &deviceId, bool isNeedHash)
52 {
53 return EraseDeviceWaterMark(deviceId, isNeedHash, "");
54 }
55
EraseDeviceWaterMark(const std::string & deviceId,bool isNeedHash,const std::string & tableName)56 int SingleVerSyncer::EraseDeviceWaterMark(const std::string &deviceId, bool isNeedHash,
57 const std::string &tableName)
58 {
59 std::shared_ptr<Metadata> metadata;
60 ISyncInterface *storage = nullptr;
61 {
62 std::lock_guard<std::mutex> lock(syncerLock_);
63 if (metadata_ == nullptr || syncInterface_ == nullptr) {
64 return -E_NOT_INIT;
65 }
66 metadata = metadata_;
67 storage = syncInterface_;
68 storage->IncRefCount();
69 }
70 int errCode = metadata->EraseDeviceWaterMark(deviceId, isNeedHash, tableName);
71 storage->DecRefCount();
72 return errCode;
73 }
74
SetStaleDataWipePolicy(WipePolicy policy)75 int SingleVerSyncer::SetStaleDataWipePolicy(WipePolicy policy)
76 {
77 std::lock_guard<std::mutex> lock(syncerLock_);
78 if (closing_) {
79 LOGI("[Syncer] Syncer is closing, return!");
80 return -E_BUSY;
81 }
82 if (syncEngine_ == nullptr) {
83 return -E_NOT_INIT;
84 }
85 int errCode = E_OK;
86 switch (policy) {
87 case RETAIN_STALE_DATA:
88 static_cast<SingleVerSyncEngine *>(syncEngine_)->EnableClearRemoteStaleData(false);
89 break;
90 case WIPE_STALE_DATA:
91 static_cast<SingleVerSyncEngine *>(syncEngine_)->EnableClearRemoteStaleData(true);
92 break;
93 default:
94 errCode = -E_NOT_SUPPORT;
95 break;
96 }
97 return errCode;
98 }
99
CreateSyncEngine()100 ISyncEngine *SingleVerSyncer::CreateSyncEngine()
101 {
102 return new (std::nothrow) SingleVerSyncEngine();
103 }
104
GetHashDeviceId(const std::string & clientId,std::string & hashDevId) const105 int SingleVerSyncer::GetHashDeviceId(const std::string &clientId, std::string &hashDevId) const
106 {
107 std::shared_ptr<Metadata> metadata = nullptr;
108 {
109 std::lock_guard<std::mutex> lock(syncerLock_);
110 if (metadata_ == nullptr) {
111 return -E_BUSY;
112 }
113 metadata = metadata_;
114 }
115 return metadata->GetHashDeviceId(clientId, hashDevId);
116 }
117 }
118