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_IMPL_H
17 #define MULTI_VER_VACUUM_EXECUTOR_IMPL_H
18 
19 #ifndef OMIT_MULTI_VER
20 #include "multi_ver_vacuum_executor.h"
21 #include "multi_ver_natural_store.h"
22 
23 namespace DistributedDB {
24 // All functions will not be concurrently called
25 class MultiVerVacuumExecutorImpl final : public MultiVerVacuumExecutor {
26 public:
27     explicit MultiVerVacuumExecutorImpl(MultiVerNaturalStore *multiKvDB);
28     ~MultiVerVacuumExecutorImpl() override;
29 
30     // Call this always beyond transaction
31     int GetVacuumAbleCommits(std::list<MultiVerCommitInfo> &leftBranchCommits,
32         std::list<MultiVerCommitInfo> &rightBranchCommits) const override;
33 
34     // Call this within or beyond transaction
35     int GetVacuumNeedRecordsByVersion(uint64_t version, std::list<MultiVerRecordInfo> &vacuumNeedRecords) override;
36 
37     // Call this within or beyond transaction
38     int GetShadowRecordsOfClearTypeRecord(uint64_t version, const std::vector<uint8_t> &hashKey,
39         std::list<MultiVerRecordInfo> &shadowRecords) override;
40 
41     // Call this within or beyond transaction
42     int GetShadowRecordsOfNonClearTypeRecord(uint64_t version, const std::vector<uint8_t> &hashKey,
43         std::list<MultiVerRecordInfo> &shadowRecords) override;
44 
45     // Call this before change the database
46     int StartTransactionForVacuum() override;
47 
48     // Call this if nothing error happened, if this itself failed, do not need to call rollback
49     int CommitTransactionForVacuum() override;
50 
51     // Call this if anything wrong happened after start transaction except commit fail
52     int RollBackTransactionForVacuum() override;
53 
54     // Call this always within transaction
55     int DeleteRecordTotally(uint64_t version, const std::vector<uint8_t> &hashKey) override;
56 
57     // Call this always within transaction
58     int MarkRecordAsVacuumDone(uint64_t version, const std::vector<uint8_t> &hashKey) override;
59 
60     // Call this always within transaction
61     int MarkCommitAsVacuumDone(const std::vector<uint8_t> &commitId) override;
62 private:
63     MultiVerStorageExecutor *GetCorrectHandleForUse() const;
64     void ReleaseHandleIfNeed(MultiVerStorageExecutor *inHandle);
65 
66     RecordType GetRecordType(const MultiVerTrimedVersionData &inRecord) const;
67 
68     MultiVerNaturalStore *multiKvDB_;
69     MultiVerStorageExecutor *writeHandle_;
70 };
71 } // namespace DistributedDB
72 
73 #endif // MULTI_VER_VACUUM_EXECUTOR_IMPL_H
74 #endif