/Users/deen/code/yugabyte-db/src/yb/rocksdb/db/transaction_log_impl.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
2 | | // This source code is licensed under the BSD-style license found in the |
3 | | // LICENSE file in the root directory of this source tree. An additional grant |
4 | | // of patent rights can be found in the PATENTS file in the same directory. |
5 | | // |
6 | | // The following only applies to changes made to this file as part of YugaByte development. |
7 | | // |
8 | | // Portions Copyright (c) YugaByte, Inc. |
9 | | // |
10 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
11 | | // in compliance with the License. You may obtain a copy of the License at |
12 | | // |
13 | | // http://www.apache.org/licenses/LICENSE-2.0 |
14 | | // |
15 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
16 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
17 | | // or implied. See the License for the specific language governing permissions and limitations |
18 | | // under the License. |
19 | | // |
20 | | |
21 | | #ifndef ROCKSDB_LITE |
22 | | #pragma once |
23 | | #include <vector> |
24 | | |
25 | | #include "yb/rocksdb/env.h" |
26 | | #include "yb/rocksdb/options.h" |
27 | | #include "yb/rocksdb/types.h" |
28 | | #include "yb/rocksdb/transaction_log.h" |
29 | | #include "yb/rocksdb/db/version_set.h" |
30 | | #include "yb/rocksdb/db/log_reader.h" |
31 | | #include "yb/rocksdb/db/filename.h" |
32 | | #include "yb/rocksdb/port/port.h" |
33 | | |
34 | | namespace rocksdb { |
35 | | |
36 | | class LogFileImpl : public LogFile { |
37 | | public: |
38 | | LogFileImpl(uint64_t logNum, WalFileType logType, SequenceNumber startSeq, |
39 | | uint64_t sizeBytes) : |
40 | | logNumber_(logNum), |
41 | | type_(logType), |
42 | | startSequence_(startSeq), |
43 | 305 | sizeFileBytes_(sizeBytes) { |
44 | 305 | } |
45 | | |
46 | 44 | std::string PathName() const override { |
47 | 44 | if (type_ == kArchivedLogFile) { |
48 | 28 | return ArchivedLogFileName("", logNumber_); |
49 | 28 | } |
50 | 16 | return LogFileName("", logNumber_); |
51 | 44 | } |
52 | | |
53 | 1.38k | uint64_t LogNumber() const override { return logNumber_; } |
54 | | |
55 | 278 | WalFileType Type() const override { return type_; } |
56 | | |
57 | 79 | SequenceNumber StartSequence() const override { return startSequence_; } |
58 | | |
59 | 7 | uint64_t SizeFileBytes() const override { return sizeFileBytes_; } |
60 | | |
61 | 438 | bool operator < (const LogFile& that) const { |
62 | 438 | return LogNumber() < that.LogNumber(); |
63 | 438 | } |
64 | | |
65 | | private: |
66 | | uint64_t logNumber_; |
67 | | WalFileType type_; |
68 | | SequenceNumber startSequence_; |
69 | | uint64_t sizeFileBytes_; |
70 | | |
71 | | }; |
72 | | |
73 | | class TransactionLogIteratorImpl : public TransactionLogIterator { |
74 | | public: |
75 | | TransactionLogIteratorImpl( |
76 | | const std::string& dir, const DBOptions* options, |
77 | | const TransactionLogIterator::ReadOptions& read_options, |
78 | | const EnvOptions& soptions, const SequenceNumber seqNum, |
79 | | std::unique_ptr<VectorLogPtr> files, VersionSet const* const versions); |
80 | | |
81 | | virtual bool Valid() override; |
82 | | |
83 | | virtual void Next() override; |
84 | | |
85 | | virtual Status status() override; |
86 | | |
87 | | virtual BatchResult GetBatch() override; |
88 | | |
89 | | private: |
90 | | const std::string& dir_; |
91 | | const DBOptions* options_; |
92 | | const TransactionLogIterator::ReadOptions read_options_; |
93 | | const EnvOptions& soptions_; |
94 | | SequenceNumber startingSequenceNumber_; |
95 | | std::unique_ptr<VectorLogPtr> files_; |
96 | | bool started_; |
97 | | bool isValid_; // not valid when it starts of. |
98 | | Status currentStatus_; |
99 | | size_t currentFileIndex_; |
100 | | std::unique_ptr<WriteBatch> currentBatch_; |
101 | | unique_ptr<log::Reader> currentLogReader_; |
102 | | Status OpenLogFile(const LogFile* logFile, |
103 | | unique_ptr<SequentialFileReader>* file); |
104 | | |
105 | | struct LogReporter : public log::Reader::Reporter { |
106 | | Env* env; |
107 | | Logger* info_log; |
108 | 0 | virtual void Corruption(size_t bytes, const Status& s) override { |
109 | 0 | RLOG(InfoLogLevel::ERROR_LEVEL, info_log, |
110 | 0 | "dropping %" ROCKSDB_PRIszt " bytes; %s", bytes, |
111 | 0 | s.ToString().c_str()); |
112 | 0 | } |
113 | 15 | virtual void Info(const char* s) { |
114 | 15 | RLOG(InfoLogLevel::INFO_LEVEL, info_log, "%s", s); |
115 | 15 | } |
116 | | } reporter_; |
117 | | |
118 | | SequenceNumber currentBatchSeq_; // sequence number at start of current batch |
119 | | SequenceNumber currentLastSeq_; // last sequence in the current batch |
120 | | // Used only to get latest seq. num |
121 | | // TODO(icanadi) can this be just a callback? |
122 | | VersionSet const* const versions_; |
123 | | |
124 | | // Reads from transaction log only if the writebatch record has been written |
125 | | bool RestrictedRead(Slice* record, std::string* scratch); |
126 | | // Seeks to startingSequenceNumber reading from startFileIndex in files_. |
127 | | // If strict is set,then must get a batch starting with startingSequenceNumber |
128 | | void SeekToStartSequence(uint64_t startFileIndex = 0, bool strict = false); |
129 | | // Implementation of Next. SeekToStartSequence calls it internally with |
130 | | // internal=true to let it find next entry even if it has to jump gaps because |
131 | | // the iterator may start off from the first available entry but promises to |
132 | | // be continuous after that |
133 | | void NextImpl(bool internal = false); |
134 | | // Check if batch is expected, else return false |
135 | | bool IsBatchExpected(const WriteBatch* batch, SequenceNumber expectedSeq); |
136 | | // Update current batch if a continuous batch is found, else return false |
137 | | void UpdateCurrentWriteBatch(const Slice& record); |
138 | | Status OpenLogReader(const LogFile* file); |
139 | | }; |
140 | | } // namespace rocksdb |
141 | | #endif // ROCKSDB_LITE |