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 #include "combine_status.h"
17 
18 namespace DistributedDB {
UpdateProgressId(uint64_t inProgressId)19 void CombineStatus::UpdateProgressId(uint64_t inProgressId)
20 {
21     progressId_ = inProgressId;
22     hasProgressFlag_ = true;
23 }
24 
GetProgressId() const25 uint64_t CombineStatus::GetProgressId() const
26 {
27     return progressId_;
28 }
29 
CheckProgress()30 bool CombineStatus::CheckProgress()
31 {
32     bool preFlag = hasProgressFlag_;
33     hasProgressFlag_ = false;
34     return preFlag;
35 }
36 
SetFragmentLen(uint32_t inFragLen)37 void CombineStatus::SetFragmentLen(uint32_t inFragLen)
38 {
39     fragmentLen_ = inFragLen;
40 }
41 
SetLastFragmentLen(uint32_t inLastFragLen)42 void CombineStatus::SetLastFragmentLen(uint32_t inLastFragLen)
43 {
44     lastFragmentLen_ = inLastFragLen;
45 }
46 
GetThisFragmentLength(uint16_t inFragNo) const47 uint32_t CombineStatus::GetThisFragmentLength(uint16_t inFragNo) const
48 {
49     // It had already been checked outside that inFragNo smaller than fragmentCount_
50     return ((inFragNo != fragmentCount_ - 1) ? fragmentLen_ : lastFragmentLen_); // subtract by 1 for index
51 }
52 
GetThisFragmentOffset(uint16_t inFragNo) const53 uint32_t CombineStatus::GetThisFragmentOffset(uint16_t inFragNo) const
54 {
55     // It had already been checked outside that inFragNo smaller than fragmentCount_
56     return fragmentLen_ * inFragNo; // It can be guaranteed no overflow will happen by multiply
57 }
58 
SetFragmentCount(uint16_t inFragCount)59 void CombineStatus::SetFragmentCount(uint16_t inFragCount)
60 {
61     fragmentCount_ = inFragCount;
62 }
63 
IsFragNoAlreadyExist(uint16_t inFragNo) const64 bool CombineStatus::IsFragNoAlreadyExist(uint16_t inFragNo) const
65 {
66     return (combinedFragmentNo_.count(inFragNo) != 0);
67 }
68 
CheckInFragmentNo(uint16_t inFragNo)69 void CombineStatus::CheckInFragmentNo(uint16_t inFragNo)
70 {
71     if (inFragNo >= fragmentCount_) {
72         return;
73     }
74     combinedFragmentNo_.insert(inFragNo);
75 }
76 
IsCombineDone() const77 bool CombineStatus::IsCombineDone() const
78 {
79     return (combinedFragmentNo_.size() >= fragmentCount_);
80 }
81 } // namespace DistributedDB
82