1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
6 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
7 // Use of this source code is governed by a BSD-style license that can be
8 // found in the LICENSE file. See the AUTHORS file for names of contributors.
14 #include "db/log_format.h"
15 #include "rocksdb/slice.h"
16 #include "rocksdb/status.h"
17 #include "rocksdb/options.h"
21 class SequentialFileReader
;
23 using std::unique_ptr
;
28 * Reader is a general purpose log stream reader implementation. The actual job
29 * of reading from the device is implemented by the SequentialFile interface.
31 * Please see Writer for details on the file and record layout.
35 // Interface for reporting errors.
40 // Some corruption was detected. "size" is the approximate number
41 // of bytes dropped due to the corruption.
42 virtual void Corruption(size_t bytes
, const Status
& status
) = 0;
45 // Create a reader that will return log records from "*file".
46 // "*file" must remain live while this Reader is in use.
48 // If "reporter" is non-nullptr, it is notified whenever some data is
49 // dropped due to a detected corruption. "*reporter" must remain
50 // live while this Reader is in use.
52 // If "checksum" is true, verify checksums if available.
53 Reader(std::shared_ptr
<Logger
> info_log
,
54 // @lint-ignore TXT2 T25377293 Grandfathered in
55 unique_ptr
<SequentialFileReader
>&& file
, Reporter
* reporter
,
56 bool checksum
, uint64_t log_num
);
60 // Read the next record into *record. Returns true if read
61 // successfully, false if we hit end of the input. May use
62 // "*scratch" as temporary storage. The contents filled in *record
63 // will only be valid until the next mutating operation on this
64 // reader or the next mutation to *scratch.
65 bool ReadRecord(Slice
* record
, std::string
* scratch
,
66 WALRecoveryMode wal_recovery_mode
=
67 WALRecoveryMode::kTolerateCorruptedTailRecords
);
69 // Returns the physical offset of the last record returned by ReadRecord.
71 // Undefined before the first call to ReadRecord.
72 uint64_t LastRecordOffset();
74 // returns true if the reader has encountered an eof condition.
79 // when we know more data has been written to the file. we can use this
80 // function to force the reader to look again in the file.
81 // Also aligns the file position indicator to the start of the next block
82 // by reading the rest of the data from the EOF position to the end of the
83 // block that was partially read.
86 SequentialFileReader
* file() { return file_
.get(); }
89 std::shared_ptr
<Logger
> info_log_
;
90 const unique_ptr
<SequentialFileReader
> file_
;
91 Reporter
* const reporter_
;
93 char* const backing_store_
;
95 bool eof_
; // Last Read() indicated EOF by returning < kBlockSize
96 bool read_error_
; // Error occurred while reading from file
98 // Offset of the file position indicator within the last block when an
102 // Offset of the last record returned by ReadRecord.
103 uint64_t last_record_offset_
;
104 // Offset of the first location past the end of buffer_.
105 uint64_t end_of_buffer_offset_
;
107 // which log number this is
108 uint64_t const log_number_
;
110 // Whether this is a recycled log file
113 // Extend record types with the following special values
115 kEof
= kMaxRecordType
+ 1,
116 // Returned whenever we find an invalid physical record.
117 // Currently there are three situations in which this happens:
118 // * The record has an invalid CRC (ReadPhysicalRecord reports a drop)
119 // * The record is a 0-length record (No drop is reported)
120 kBadRecord
= kMaxRecordType
+ 2,
121 // Returned when we fail to read a valid header.
122 kBadHeader
= kMaxRecordType
+ 3,
123 // Returned when we read an old record from a previous user of the log.
124 kOldRecord
= kMaxRecordType
+ 4,
125 // Returned when we get a bad record length
126 kBadRecordLen
= kMaxRecordType
+ 5,
127 // Returned when we get a bad record checksum
128 kBadRecordChecksum
= kMaxRecordType
+ 6,
131 // Return type, or one of the preceding special values
132 unsigned int ReadPhysicalRecord(Slice
* result
, size_t* drop_size
);
135 bool ReadMore(size_t* drop_size
, int *error
);
137 // Reports dropped bytes to the reporter.
138 // buffer_ must be updated to remove the dropped bytes prior to invocation.
139 void ReportCorruption(size_t bytes
, const char* reason
);
140 void ReportDrop(size_t bytes
, const Status
& reason
);
142 // No copying allowed
143 Reader(const Reader
&);
144 void operator=(const Reader
&);
148 } // namespace rocksdb