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 "log_print.h"
18 #include "multi_ver_commit.h"
19 
20 namespace DistributedDB {
MultiVerCommit()21 MultiVerCommit::MultiVerCommit()
22     : versionInfo_(0),
23       timestamp_(0),
24       localFlag_(true)
25 {}
26 
~MultiVerCommit()27 MultiVerCommit::~MultiVerCommit()
28 {}
29 
GetCommitVersion() const30 Version MultiVerCommit::GetCommitVersion() const
31 {
32     return versionInfo_;
33 }
34 
SetCommitVersion(const Version & versionInfo)35 void MultiVerCommit::SetCommitVersion(const Version &versionInfo)
36 {
37     versionInfo_ = versionInfo;
38     return;
39 }
40 
GetCommitId() const41 CommitID MultiVerCommit::GetCommitId() const
42 {
43     return commitID_;
44 }
45 
SetCommitId(const CommitID & id)46 void MultiVerCommit::SetCommitId(const CommitID &id)
47 {
48     commitID_ = id;
49     return;
50 }
51 
GetLeftParentId() const52 CommitID MultiVerCommit::GetLeftParentId() const
53 {
54     return leftParentID_;
55 }
56 
SetLeftParentId(const CommitID & id)57 void MultiVerCommit::SetLeftParentId(const CommitID &id)
58 {
59     leftParentID_ = id;
60     return;
61 }
62 
GetRightParentId() const63 CommitID MultiVerCommit::GetRightParentId() const
64 {
65     return rightParentID_;
66 }
67 
SetRightParentId(const CommitID & id)68 void MultiVerCommit::SetRightParentId(const CommitID &id)
69 {
70     rightParentID_ = id;
71     return;
72 }
73 
GetTimestamp() const74 Timestamp MultiVerCommit::GetTimestamp() const
75 {
76     return timestamp_;
77 }
78 
SetTimestamp(Timestamp timestamp)79 void MultiVerCommit::SetTimestamp(Timestamp timestamp)
80 {
81     timestamp_ = timestamp;
82     return;
83 }
84 
GetLocalFlag() const85 bool MultiVerCommit::GetLocalFlag() const
86 {
87     return localFlag_;
88 }
89 
SetLocalFlag(bool localFlag)90 void MultiVerCommit::SetLocalFlag(bool localFlag)
91 {
92     localFlag_ = localFlag;
93     return;
94 }
95 
GetDeviceInfo() const96 DeviceID MultiVerCommit::GetDeviceInfo() const
97 {
98     return deviceInfo_;
99 }
100 
SetDeviceInfo(const DeviceID & deviceInfo)101 void MultiVerCommit::SetDeviceInfo(const DeviceID &deviceInfo)
102 {
103     deviceInfo_ = deviceInfo;
104     return;
105 }
106 
CheckCommit() const107 bool MultiVerCommit::CheckCommit() const
108 {
109     if (commitID_.empty() || commitID_.size() > MAX_COMMIT_ID_LENGTH ||
110         leftParentID_.size() > MAX_COMMIT_ID_LENGTH || rightParentID_.size() > MAX_COMMIT_ID_LENGTH ||
111         deviceInfo_.size() > MAX_COMMIT_DEV_LENGTH) {
112         LOGE("Check commit failed! Error length of commit ID.");
113         return false;
114     }
115     // commitId should not equal to the parentId; the left parent should not equal to the right
116     if (commitID_ == leftParentID_ || commitID_ == rightParentID_ ||
117         (leftParentID_ == rightParentID_ && leftParentID_.size() != 0)) {
118         LOGE("Check commit failed! Wrong commit ID.");
119         return false;
120     }
121     return true;
122 }
123 }
124 #endif