1 /*
2 * Copyright (c) 2022 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 "single_ver_relational_sync_task_context.h"
17 #include "db_common.h"
18
19 #ifdef RELATIONAL_STORE
20 namespace DistributedDB {
SingleVerRelationalSyncTaskContext()21 SingleVerRelationalSyncTaskContext::SingleVerRelationalSyncTaskContext()
22 : SingleVerSyncTaskContext()
23 {}
24
~SingleVerRelationalSyncTaskContext()25 SingleVerRelationalSyncTaskContext::~SingleVerRelationalSyncTaskContext()
26 {
27 }
28
GetQuerySyncId() const29 std::string SingleVerRelationalSyncTaskContext::GetQuerySyncId() const
30 {
31 std::lock_guard<std::mutex> autoLock(querySyncIdMutex_);
32 return querySyncId_;
33 }
34
GetDeleteSyncId() const35 std::string SingleVerRelationalSyncTaskContext::GetDeleteSyncId() const
36 {
37 std::lock_guard<std::mutex> autoLock(deleteSyncIdMutex_);
38 return deleteSyncId_;
39 }
40
Clear()41 void SingleVerRelationalSyncTaskContext::Clear()
42 {
43 {
44 std::lock_guard<std::mutex> autoLock(querySyncIdMutex_);
45 querySyncId_.clear();
46 }
47 {
48 std::lock_guard<std::mutex> autoLock(deleteSyncIdMutex_);
49 deleteSyncId_.clear();
50 }
51 SingleVerSyncTaskContext::Clear();
52 }
53
CopyTargetData(const ISyncTarget * target,const TaskParam & taskParam)54 void SingleVerRelationalSyncTaskContext::CopyTargetData(const ISyncTarget *target, const TaskParam &taskParam)
55 {
56 SingleVerSyncTaskContext::CopyTargetData(target, taskParam);
57 std::string hashTableName;
58 std::string queryId;
59 {
60 std::lock_guard<std::mutex> autoLock(queryMutex_);
61 hashTableName = DBCommon::TransferHashString(query_.GetRelationTableName());
62 queryId = query_.GetIdentify();
63 }
64 std::string hexTableName = DBCommon::TransferStringToHex(hashTableName);
65 {
66 std::lock_guard<std::mutex> autoLock(querySyncIdMutex_);
67 querySyncId_ = hexTableName + queryId; // save as deviceId + hexTableName + queryId
68 }
69 {
70 std::lock_guard<std::mutex> autoLock(deleteSyncIdMutex_);
71 deleteSyncId_ = GetDeviceId() + hexTableName; // save as deviceId + hexTableName
72 }
73 }
74
SetRelationalSyncStrategy(const RelationalSyncStrategy & strategy,bool isSchemaSync)75 void SingleVerRelationalSyncTaskContext::SetRelationalSyncStrategy(const RelationalSyncStrategy &strategy,
76 bool isSchemaSync)
77 {
78 std::lock_guard<std::mutex> autoLock(syncStrategyMutex_);
79 relationalSyncStrategy_ = strategy;
80 isSchemaSync_ = isSchemaSync;
81 }
82
GetSchemaSyncStatus(QuerySyncObject & querySyncObject) const83 std::pair<bool, bool> SingleVerRelationalSyncTaskContext::GetSchemaSyncStatus(QuerySyncObject &querySyncObject) const
84 {
85 std::lock_guard<std::mutex> autoLock(syncStrategyMutex_);
86 auto it = relationalSyncStrategy_.find(querySyncObject.GetRelationTableName());
87 if (it == relationalSyncStrategy_.end()) {
88 return {false, isSchemaSync_};
89 }
90 return {it->second.permitSync, isSchemaSync_};
91 }
92
SchemaChange()93 void SingleVerRelationalSyncTaskContext::SchemaChange()
94 {
95 RelationalSyncStrategy strategy;
96 SetRelationalSyncStrategy(strategy, false);
97 SyncTaskContext::SchemaChange();
98 }
99
IsSchemaCompatible() const100 bool SingleVerRelationalSyncTaskContext::IsSchemaCompatible() const
101 {
102 std::lock_guard<std::mutex> autoLock(syncStrategyMutex_);
103 for (const auto &strategy : relationalSyncStrategy_) {
104 if (!strategy.second.permitSync) {
105 return false;
106 }
107 }
108 return true;
109 }
110 }
111 #endif