• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(RelationalSyncStrategy & strategy,bool isSchemaSync)75 void SingleVerRelationalSyncTaskContext::SetRelationalSyncStrategy(RelationalSyncStrategy &strategy, bool isSchemaSync)
76 {
77     std::lock_guard<std::mutex> autoLock(syncStrategyMutex_);
78     relationalSyncStrategy_ = strategy;
79     isSchemaSync_ = isSchemaSync;
80 }
81 
GetSchemaSyncStatus(QuerySyncObject & querySyncObject) const82 std::pair<bool, bool> SingleVerRelationalSyncTaskContext::GetSchemaSyncStatus(QuerySyncObject &querySyncObject) const
83 {
84     std::lock_guard<std::mutex> autoLock(syncStrategyMutex_);
85     auto it = relationalSyncStrategy_.find(querySyncObject.GetRelationTableName());
86     if (it == relationalSyncStrategy_.end()) {
87         return {false, isSchemaSync_};
88     }
89     return {it->second.permitSync, isSchemaSync_};
90 }
91 
SchemaChange()92 void SingleVerRelationalSyncTaskContext::SchemaChange()
93 {
94     SetIsNeedResetAbilitySync(true);
95     RelationalSyncStrategy strategy;
96     SetRelationalSyncStrategy(strategy, false);
97 }
98 }
99 #endif