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 #include "relational_db_sync_interface.h"
19
20 #ifdef RELATIONAL_STORE
21 namespace DistributedDB {
SingleVerRelationalSyncTaskContext()22 SingleVerRelationalSyncTaskContext::SingleVerRelationalSyncTaskContext()
23 : SingleVerSyncTaskContext()
24 {}
25
~SingleVerRelationalSyncTaskContext()26 SingleVerRelationalSyncTaskContext::~SingleVerRelationalSyncTaskContext()
27 {
28 }
29
GetQuerySyncId() const30 std::string SingleVerRelationalSyncTaskContext::GetQuerySyncId() const
31 {
32 RelationalSchemaObject schemaObj;
33 if (!IsRemoteSupportFieldSync() || GetDistributedSchema(schemaObj) != E_OK) {
34 std::lock_guard<std::mutex> autoLock(querySyncIdMutex_);
35 return querySyncId_;
36 }
37
38 std::string tableName = GetQuery().GetRelationTableName();
39 std::vector<FieldInfo> fieldsInfo = schemaObj.GetSyncFieldInfo(tableName);
40 std::vector<std::string> strFields(fieldsInfo.size());
41 for (const auto &field : fieldsInfo) {
42 strFields.push_back(field.GetFieldName());
43 }
44 std::sort(strFields.begin(), strFields.end());
45 std::string splitFields;
46 for (const auto &strField : strFields) {
47 splitFields += strField;
48 }
49 std::string queryFieldsSyncId = DBCommon::TransferHashString(splitFields);
50 std::lock_guard<std::mutex> autoLock(querySyncIdMutex_);
51 return querySyncId_ + DBCommon::TransferStringToHex(queryFieldsSyncId);
52 }
53
GetDeleteSyncId() const54 std::string SingleVerRelationalSyncTaskContext::GetDeleteSyncId() const
55 {
56 std::lock_guard<std::mutex> autoLock(deleteSyncIdMutex_);
57 return deleteSyncId_;
58 }
59
Clear()60 void SingleVerRelationalSyncTaskContext::Clear()
61 {
62 {
63 std::lock_guard<std::mutex> autoLock(querySyncIdMutex_);
64 querySyncId_.clear();
65 }
66 {
67 std::lock_guard<std::mutex> autoLock(deleteSyncIdMutex_);
68 deleteSyncId_.clear();
69 }
70 SingleVerSyncTaskContext::Clear();
71 }
72
CopyTargetData(const ISyncTarget * target,const TaskParam & taskParam)73 void SingleVerRelationalSyncTaskContext::CopyTargetData(const ISyncTarget *target, const TaskParam &taskParam)
74 {
75 SingleVerSyncTaskContext::CopyTargetData(target, taskParam);
76 std::string hashTableName;
77 std::string queryId;
78 {
79 std::lock_guard<std::mutex> autoLock(queryMutex_);
80 hashTableName = DBCommon::TransferHashString(query_.GetRelationTableName());
81 queryId = query_.GetIdentify();
82 }
83 std::string hexTableName = DBCommon::TransferStringToHex(hashTableName);
84 {
85 std::lock_guard<std::mutex> autoLock(querySyncIdMutex_);
86 querySyncId_ = hexTableName + queryId; // save as deviceId + hexTableName + queryId
87 }
88 {
89 std::lock_guard<std::mutex> autoLock(deleteSyncIdMutex_);
90 deleteSyncId_ = GetDeviceId() + hexTableName; // save as deviceId + hexTableName
91 }
92 }
93
SetRelationalSyncStrategy(const RelationalSyncStrategy & strategy,bool isSchemaSync)94 void SingleVerRelationalSyncTaskContext::SetRelationalSyncStrategy(const RelationalSyncStrategy &strategy,
95 bool isSchemaSync)
96 {
97 std::lock_guard<std::mutex> autoLock(syncStrategyMutex_);
98 relationalSyncStrategy_ = strategy;
99 isSchemaSync_ = isSchemaSync;
100 }
101
GetSchemaSyncStatus(QuerySyncObject & querySyncObject) const102 std::pair<bool, bool> SingleVerRelationalSyncTaskContext::GetSchemaSyncStatus(QuerySyncObject &querySyncObject) const
103 {
104 std::lock_guard<std::mutex> autoLock(syncStrategyMutex_);
105 auto it = relationalSyncStrategy_.find(querySyncObject.GetRelationTableName());
106 if (it == relationalSyncStrategy_.end()) {
107 return {false, isSchemaSync_};
108 }
109 return {it->second.permitSync, isSchemaSync_};
110 }
111
SchemaChange()112 void SingleVerRelationalSyncTaskContext::SchemaChange()
113 {
114 RelationalSyncStrategy strategy;
115 SetRelationalSyncStrategy(strategy, false);
116 SyncTaskContext::SchemaChange();
117 }
118
IsSchemaCompatible() const119 bool SingleVerRelationalSyncTaskContext::IsSchemaCompatible() const
120 {
121 std::lock_guard<std::mutex> autoLock(syncStrategyMutex_);
122 for (const auto &strategy : relationalSyncStrategy_) {
123 if (!strategy.second.permitSync) {
124 return false;
125 }
126 }
127 return true;
128 }
129
IsRemoteSupportFieldSync() const130 bool SingleVerRelationalSyncTaskContext::IsRemoteSupportFieldSync() const
131 {
132 return GetRemoteSoftwareVersion() > SOFTWARE_VERSION_RELEASE_10_0;
133 }
134
GetDistributedSchema(RelationalSchemaObject & schemaObj) const135 int SingleVerRelationalSyncTaskContext::GetDistributedSchema(RelationalSchemaObject &schemaObj) const
136 {
137 auto *relationalDbSyncInterface = static_cast<RelationalDBSyncInterface*>(syncInterface_);
138 if (SyncTaskContext::GetMode() == SyncModeType::RESPONSE_PULL) {
139 return relationalDbSyncInterface->GetRemoteDeviceSchema(GetDeviceId(), schemaObj);
140 }
141 schemaObj = relationalDbSyncInterface->GetSchemaInfo();
142 return E_OK;
143 }
144 }
145 #endif