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_STORAGE_EXECUTOR_H
17 #define MULTI_VER_STORAGE_EXECUTOR_H
18 
19 #ifndef OMIT_MULTI_VER
20 #include "storage_executor.h"
21 #include "ikvdb.h"
22 #include "ikvdb_commit_storage.h"
23 #include "ikvdb_multi_ver_data_storage.h"
24 #include "macro_utils.h"
25 #include "multi_ver_kvdata_storage.h"
26 
27 namespace DistributedDB {
28 enum class MultiTransactionType {
29     NORMAL_DATA,
30     ALL_DATA,
31 };
32 
33 class MultiVerStorageExecutor : public StorageExecutor {
34 public:
35     MultiVerStorageExecutor(IKvDB *kvDB, IKvDBMultiVerDataStorage *dataStorage, IKvDBCommitStorage *commitStorage,
36         MultiVerKvDataStorage *kvDataStorage, bool writable);
37     ~MultiVerStorageExecutor() override;
38 
39     // Delete the copy and assign constructors
40     DISABLE_COPY_ASSIGN_MOVE(MultiVerStorageExecutor);
41 
42     int Reset() override;
43 
44     int Put(const Key &key, const Value &value);
45 
46     int Get(const Key &key, Value &value) const;
47 
48     int GetEntries(const Key &keyPrefix, std::vector<Entry> &entries) const;
49 
50     int Delete(const Key &key);
51 
52     int Clear();
53 
54     int PutMetaData(const Key &key, const Value &value);
55 
56     int GetMetaData(const Key &key, Value &value) const;
57 
58     int GetDeviceLatestCommit(std::map<std::string, MultiVerCommitNode> &commitMap) const;
59 
60     int GetCommitTree(const std::map<std::string, MultiVerCommitNode> &commitMap,
61         std::vector<MultiVerCommitNode> &commits) const;
62 
63     bool IsCommitExisted(const MultiVerCommitNode &commit, int &errCode) const;
64 
65     int GetCommitData(const MultiVerCommitNode &commit, std::vector<MultiVerKvEntry *> &entries) const;
66 
67     bool IsValueSliceExisted(const ValueSliceHash &value, int &errCode) const;
68 
69     int GetValueSlice(const ValueSliceHash &hashValue, ValueSlice &sliceValue) const;
70 
71     int PutValueSlice(const ValueSliceHash &hashValue, const ValueSlice &sliceValue, bool isAddCount);
72 
73     int PutCommitData(const MultiVerCommitNode &commit, const std::vector<MultiVerKvEntry *> &entries,
74         const std::string &deviceName);
75 
76     int MergeSyncCommit(const MultiVerCommitNode &commit, const std::vector<MultiVerCommitNode> &commits);
77 
78     int GetDiffEntries(const CommitID &begin, const CommitID &end, MultiVerDiffData &data) const;
79 
80     int StartTransaction(MultiTransactionType type = MultiTransactionType::NORMAL_DATA);
81 
82     int CommitTransaction(MultiTransactionType type = MultiTransactionType::NORMAL_DATA);
83 
84     int RollBackTransaction(MultiTransactionType type = MultiTransactionType::NORMAL_DATA);
85 
86     int InitCurrentReadVersion();
87 
88     void Close();
89 
90     Version GetCurrentReadVersion() const;
91 
92     // Get all the commits with the view of one commit.
93     int GetAllCommitsInTree(std::list<MultiVerCommitNode> &commits) const;
94 
95     // Get all the hash key of one version.
96     int GetEntriesByVersion(Version version, std::list<MultiVerTrimedVersionData> &data) const;
97 
98     // Get all the overwritten record whose version is less than the specified version and tag is less the cleard data.
99     int GetOverwrittenClearTypeEntries(Version clearVersion, std::list<MultiVerTrimedVersionData> &data) const;
100 
101     // Get all the overwritten non-cleared record whose version is less than the specified version.
102     int GetOverwrittenNonClearTypeEntries(Version version, const Key &hashKey,
103         std::list<MultiVerTrimedVersionData> &data) const;
104 
105     // Delete the data whose hash key is equal to the hashKey and version is less than the specified.
106     int DeleteEntriesByHashKey(Version version, const Key &hashKey);
107 
108     // Update the trimmed flag for the hash key with the specified version.
109     int UpdateTrimedFlag(Version version, const Key &hashKey);
110 
111     // Update the trimmed flag for the commit.
112     int UpdateTrimedFlag(const CommitID &commit);
113 
114 private:
115     static void ReleaseMultiVerKvEntries(std::vector<MultiVerKvEntry *> &entries);
116 
117     int GetSliceCount(std::vector<Entry> &&entries, uint32_t &count) const;
118 
119     int PutSliceCount(const Key &sliceKey, uint32_t count) const;
120 
121     int CommitTransaction(const MultiVerCommitNode &multiVerCommit, bool isMerge);
122 
123     int GetResolvedConflictEntries(const MultiVerCommitNode &commitItem, std::vector<MultiVerKvEntry *> &entries) const;
124 
125     int TransferDiffEntries(MultiVerDiffData &data) const;
126 
127     int TransferToUserValue(const Value &savedValue, Value &value) const;
128 
129     int TransferToSavedValue(const Value &value, Value &savedValue);
130 
131     void CommitNotifiedData(const CommitID &commitId);
132 
133     int GetParentCommitId(const CommitID &commitId, CommitID &parentId, Version &curVersion) const;
134 
135     int AllocNewCommitId(CommitID &commitId) const;
136 
137     int FillAndCommitLogEntry(const Version &versionInfo, CommitID &commitId, uint64_t timestamp) const;
138 
139     int FillCommitByForeign(IKvDBCommit *commit, const MultiVerCommitNode &multiVerCommit,
140         const Version &versionInfo, const CommitID &commitId, bool isMerge) const;
141 
142     int FillAndCommitLogEntry(const Version &versionInfo, const MultiVerCommitNode &multiVerCommit,
143         CommitID &commitId, bool isMerge, Timestamp &timestamp) const;
144 
145     int MergeOneCommit(const MultiVerCommitNode &commit);
146 
147     int MergeCommits(const std::vector<MultiVerCommitNode> &commits);
148 
149     int CommitSyncCommits();
150 
151     int StartAllDbTransaction();
152 
153     int TransferToValueObject(const Value &value, MultiVerValueObject &valueObject);
154 
155     int RollBackAllDbTransaction();
156 
157     int CommitAllDbTransaction();
158 
159     int ReInitTransactionVersion(const MultiVerCommitNode &commit);
160 
161     int StartSliceTransaction();
162 
163     int CommitSliceTransaction();
164 
165     int RollbackSliceTransaction();
166 
167     int GetValueSliceInner(const SliceTransaction *sliceTransaction, const ValueSliceHash &hashValue,
168         ValueSlice &sliceValue) const;
169 
170     int PutValueSliceInner(SliceTransaction *sliceTransaction, const ValueSliceHash &hashValue,
171         const ValueSlice &sliceValue, bool isAddCount);
172 
173     int DeleteValueSliceInner(SliceTransaction *sliceTransaction, const ValueSliceHash &hashValue);
174 
175     int AddSliceDataCount(const std::vector<Value> &values);
176 
177     static const int COMMIT_ID_LENGTH = 20;
178     IKvDB *kvDB_;
179     IKvDBMultiVerDataStorage *dataStorage_;
180     IKvDBCommitStorage *commitStorage_;
181     MultiVerKvDataStorage *kvDataStorage_;
182     IKvDBMultiVerTransaction *transaction_;
183     SliceTransaction *sliceTransaction_;
184     Version readVersion_ = 0;
185 };
186 } // namespace DistributedDB
187 
188 #endif // MULTI_VER_STORAGE_EXECUTOR_H
189 #endif