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 MULTI_VER_VACUUM_EXECUTOR_STUB_H
17 #define MULTI_VER_VACUUM_EXECUTOR_STUB_H
18 
19 #include <atomic>
20 #include "multi_ver_vacuum_executor.h"
21 
22 namespace DistributedDB {
23 struct DbScale {
24     uint8_t left = 1;
25     uint8_t right = 1;
26     uint8_t vacuumNeed = 1;
27     uint8_t shadow = 1;
28 };
29 
30 class MultiVerVacuumExecutorStub : public MultiVerVacuumExecutor {
31 public:
32     // Total Time: (3 + 2L + 2LT + LTS + 2R + RT) Multiple timeCostEachCall(In Millisecond)
33     MultiVerVacuumExecutorStub(const DbScale &inScale, int timeCostEachCall);
34     ~MultiVerVacuumExecutorStub();
35 
36     bool IsTransactionOccupied();
37 
38     int GetVacuumAbleCommits(std::list<MultiVerCommitInfo> &leftBranchCommits,
39         std::list<MultiVerCommitInfo> &rightBranchCommits) const;
40     int GetVacuumNeedRecordsByVersion(uint64_t version, std::list<MultiVerRecordInfo> &vacuumNeedRecords);
41     int GetShadowRecordsOfClearTypeRecord(uint64_t version, const std::vector<uint8_t> &hashKey,
42         std::list<MultiVerRecordInfo> &shadowRecords);
43     int GetShadowRecordsOfNonClearTypeRecord(uint64_t version, const std::vector<uint8_t> &hashKey,
44         std::list<MultiVerRecordInfo> &shadowRecords);
45 
46     int StartTransactionForVacuum();
47     int CommitTransactionForVacuum();
48     int RollBackTransactionForVacuum();
49 
50     int DeleteRecordTotally(uint64_t version, const std::vector<uint8_t> &hashKey);
51     int MarkRecordAsVacuumDone(uint64_t version, const std::vector<uint8_t> &hashKey);
52     int MarkCommitAsVacuumDone(const std::vector<uint8_t> &commitId);
53 private:
54     DbScale dbScale_;
55     int timeCostEachCall_;
56     std::atomic<bool> transactionOccupied_;
57 };
58 }
59 
60 #endif // MULTI_VER_VACUUM_EXECUTOR_STUB_H