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 "multi_ver_vacuum_executor_stub.h"
17 #include <thread>
18 #include "db_errno.h"
19 
20 using namespace DistributedDB;
21 
MultiVerVacuumExecutorStub(const DbScale & inScale,int timeCostEachCall)22 MultiVerVacuumExecutorStub::MultiVerVacuumExecutorStub(const DbScale &inScale, int timeCostEachCall)
23     : dbScale_(inScale), timeCostEachCall_(timeCostEachCall), transactionOccupied_(false)
24 {
25 }
26 
~MultiVerVacuumExecutorStub()27 MultiVerVacuumExecutorStub::~MultiVerVacuumExecutorStub()
28 {
29 }
30 
IsTransactionOccupied()31 bool MultiVerVacuumExecutorStub::IsTransactionOccupied()
32 {
33     return transactionOccupied_;
34 }
35 
GetVacuumAbleCommits(std::list<MultiVerCommitInfo> & leftBranchCommits,std::list<MultiVerCommitInfo> & rightBranchCommits) const36 int MultiVerVacuumExecutorStub::GetVacuumAbleCommits(std::list<MultiVerCommitInfo> &leftBranchCommits,
37     std::list<MultiVerCommitInfo> &rightBranchCommits) const
38 {
39     std::this_thread::sleep_for(std::chrono::milliseconds(timeCostEachCall_));
40     for (uint8_t i = dbScale_.left + dbScale_.right; i > dbScale_.right; i--) {
41         MultiVerCommitInfo commit;
42         commit.version = i;
43         commit.commitId.push_back(i);
44         leftBranchCommits.push_back(commit);
45     }
46     for (uint8_t i = dbScale_.right; i > 0; i--) {
47         MultiVerCommitInfo commit;
48         commit.version = i;
49         commit.commitId.push_back(i);
50         rightBranchCommits.push_back(commit);
51     }
52     return E_OK;
53 }
54 
GetVacuumNeedRecordsByVersion(uint64_t version,std::list<MultiVerRecordInfo> & vacuumNeedRecords)55 int MultiVerVacuumExecutorStub::GetVacuumNeedRecordsByVersion(uint64_t version,
56     std::list<MultiVerRecordInfo> &vacuumNeedRecords)
57 {
58     std::this_thread::sleep_for(std::chrono::milliseconds(timeCostEachCall_));
59     for (uint8_t i = dbScale_.vacuumNeed; i > 0; i--) {
60         MultiVerRecordInfo record;
61         record.type = RecordType::VALID;
62         record.version = version;
63         record.hashKey.push_back(i);
64         vacuumNeedRecords.push_back(record);
65     }
66     return E_OK;
67 }
68 
GetShadowRecordsOfClearTypeRecord(uint64_t version,const std::vector<uint8_t> & hashKey,std::list<MultiVerRecordInfo> & shadowRecords)69 int MultiVerVacuumExecutorStub::GetShadowRecordsOfClearTypeRecord(uint64_t version,
70     const std::vector<uint8_t> &hashKey, std::list<MultiVerRecordInfo> &shadowRecords)
71 {
72     std::this_thread::sleep_for(std::chrono::milliseconds(timeCostEachCall_));
73     return E_OK;
74 }
75 
GetShadowRecordsOfNonClearTypeRecord(uint64_t version,const std::vector<uint8_t> & hashKey,std::list<MultiVerRecordInfo> & shadowRecords)76 int MultiVerVacuumExecutorStub::GetShadowRecordsOfNonClearTypeRecord(uint64_t version,
77     const std::vector<uint8_t> &hashKey, std::list<MultiVerRecordInfo> &shadowRecords)
78 {
79     std::this_thread::sleep_for(std::chrono::milliseconds(timeCostEachCall_));
80     for (uint8_t i = dbScale_.shadow; i > 0; i--) {
81         MultiVerRecordInfo record;
82         record.type = RecordType::VALID;
83         record.version = i;
84         record.hashKey = hashKey;
85         shadowRecords.push_back(record);
86     }
87     return E_OK;
88 }
89 
StartTransactionForVacuum()90 int MultiVerVacuumExecutorStub::StartTransactionForVacuum()
91 {
92     std::this_thread::sleep_for(std::chrono::milliseconds(timeCostEachCall_));
93     transactionOccupied_ = true;
94     return E_OK;
95 }
96 
CommitTransactionForVacuum()97 int MultiVerVacuumExecutorStub::CommitTransactionForVacuum()
98 {
99     std::this_thread::sleep_for(std::chrono::milliseconds(timeCostEachCall_));
100     transactionOccupied_ = false;
101     return E_OK;
102 }
103 
RollBackTransactionForVacuum()104 int MultiVerVacuumExecutorStub::RollBackTransactionForVacuum()
105 {
106     std::this_thread::sleep_for(std::chrono::milliseconds(timeCostEachCall_));
107     transactionOccupied_ = false;
108     return E_OK;
109 }
110 
DeleteRecordTotally(uint64_t version,const std::vector<uint8_t> & hashKey)111 int MultiVerVacuumExecutorStub::DeleteRecordTotally(uint64_t version, const std::vector<uint8_t> &hashKey)
112 {
113     std::this_thread::sleep_for(std::chrono::milliseconds(timeCostEachCall_));
114     return E_OK;
115 }
116 
MarkRecordAsVacuumDone(uint64_t version,const std::vector<uint8_t> & hashKey)117 int MultiVerVacuumExecutorStub::MarkRecordAsVacuumDone(uint64_t version, const std::vector<uint8_t> &hashKey)
118 {
119     std::this_thread::sleep_for(std::chrono::milliseconds(timeCostEachCall_));
120     return E_OK;
121 }
122 
MarkCommitAsVacuumDone(const std::vector<uint8_t> & commitId)123 int MultiVerVacuumExecutorStub::MarkCommitAsVacuumDone(const std::vector<uint8_t> &commitId)
124 {
125     std::this_thread::sleep_for(std::chrono::milliseconds(timeCostEachCall_));
126     return E_OK;
127 }
128