2011-03-19 06:37:00 +08:00
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
|
|
|
|
#ifndef STORAGE_LEVELDB_DB_LOG_READER_H_
|
|
|
|
#define STORAGE_LEVELDB_DB_LOG_READER_H_
|
|
|
|
|
2020-04-30 03:59:39 +08:00
|
|
|
#include <cstdint>
|
2011-05-21 10:17:43 +08:00
|
|
|
|
2011-03-19 06:37:00 +08:00
|
|
|
#include "db/log_format.h"
|
2011-03-31 02:35:40 +08:00
|
|
|
#include "leveldb/slice.h"
|
|
|
|
#include "leveldb/status.h"
|
2011-03-19 06:37:00 +08:00
|
|
|
|
|
|
|
namespace leveldb {
|
|
|
|
|
|
|
|
class SequentialFile;
|
|
|
|
|
|
|
|
namespace log {
|
|
|
|
|
|
|
|
class Reader {
|
|
|
|
public:
|
|
|
|
// Interface for reporting errors.
|
|
|
|
class Reporter {
|
|
|
|
public:
|
|
|
|
virtual ~Reporter();
|
|
|
|
|
2021-04-16 15:30:59 +08:00
|
|
|
// Some corruption was detected. "bytes" is the approximate number
|
2011-03-19 06:37:00 +08:00
|
|
|
// of bytes dropped due to the corruption.
|
|
|
|
virtual void Corruption(size_t bytes, const Status& status) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create a reader that will return log records from "*file".
|
|
|
|
// "*file" must remain live while this Reader is in use.
|
|
|
|
//
|
2018-04-11 07:18:06 +08:00
|
|
|
// If "reporter" is non-null, it is notified whenever some data is
|
2011-03-19 06:37:00 +08:00
|
|
|
// dropped due to a detected corruption. "*reporter" must remain
|
|
|
|
// live while this Reader is in use.
|
|
|
|
//
|
|
|
|
// If "checksum" is true, verify checksums if available.
|
2011-05-21 10:17:43 +08:00
|
|
|
//
|
|
|
|
// The Reader will start reading at the first record located at physical
|
|
|
|
// position >= initial_offset within the file.
|
|
|
|
Reader(SequentialFile* file, Reporter* reporter, bool checksum,
|
|
|
|
uint64_t initial_offset);
|
2011-03-19 06:37:00 +08:00
|
|
|
|
2019-05-04 00:31:18 +08:00
|
|
|
Reader(const Reader&) = delete;
|
|
|
|
Reader& operator=(const Reader&) = delete;
|
|
|
|
|
2011-03-19 06:37:00 +08:00
|
|
|
~Reader();
|
|
|
|
|
|
|
|
// Read the next record into *record. Returns true if read
|
|
|
|
// successfully, false if we hit end of the input. May use
|
|
|
|
// "*scratch" as temporary storage. The contents filled in *record
|
|
|
|
// will only be valid until the next mutating operation on this
|
|
|
|
// reader or the next mutation to *scratch.
|
|
|
|
bool ReadRecord(Slice* record, std::string* scratch);
|
|
|
|
|
2011-05-21 10:17:43 +08:00
|
|
|
// Returns the physical offset of the last record returned by ReadRecord.
|
|
|
|
//
|
|
|
|
// Undefined before the first call to ReadRecord.
|
|
|
|
uint64_t LastRecordOffset();
|
|
|
|
|
2011-03-19 06:37:00 +08:00
|
|
|
private:
|
|
|
|
// Extend record types with the following special values
|
|
|
|
enum {
|
|
|
|
kEof = kMaxRecordType + 1,
|
2011-05-21 10:17:43 +08:00
|
|
|
// Returned whenever we find an invalid physical record.
|
|
|
|
// Currently there are three situations in which this happens:
|
|
|
|
// * The record has an invalid CRC (ReadPhysicalRecord reports a drop)
|
|
|
|
// * The record is a 0-length record (No drop is reported)
|
|
|
|
// * The record is below constructor's initial_offset (No drop is reported)
|
2011-03-19 06:37:00 +08:00
|
|
|
kBadRecord = kMaxRecordType + 2
|
|
|
|
};
|
|
|
|
|
2011-05-21 10:17:43 +08:00
|
|
|
// Skips all blocks that are completely before "initial_offset_".
|
|
|
|
//
|
|
|
|
// Returns true on success. Handles reporting.
|
|
|
|
bool SkipToInitialBlock();
|
|
|
|
|
2011-03-19 06:37:00 +08:00
|
|
|
// Return type, or one of the preceding special values
|
|
|
|
unsigned int ReadPhysicalRecord(Slice* result);
|
2011-05-21 10:17:43 +08:00
|
|
|
|
|
|
|
// Reports dropped bytes to the reporter.
|
|
|
|
// buffer_ must be updated to remove the dropped bytes prior to invocation.
|
2014-09-17 05:19:52 +08:00
|
|
|
void ReportCorruption(uint64_t bytes, const char* reason);
|
|
|
|
void ReportDrop(uint64_t bytes, const Status& reason);
|
2011-03-19 06:37:00 +08:00
|
|
|
|
2019-05-04 00:31:18 +08:00
|
|
|
SequentialFile* const file_;
|
|
|
|
Reporter* const reporter_;
|
|
|
|
bool const checksum_;
|
|
|
|
char* const backing_store_;
|
|
|
|
Slice buffer_;
|
|
|
|
bool eof_; // Last Read() indicated EOF by returning < kBlockSize
|
|
|
|
|
|
|
|
// Offset of the last record returned by ReadRecord.
|
|
|
|
uint64_t last_record_offset_;
|
|
|
|
// Offset of the first location past the end of buffer_.
|
|
|
|
uint64_t end_of_buffer_offset_;
|
|
|
|
|
|
|
|
// Offset at which to start looking for the first record to return
|
|
|
|
uint64_t const initial_offset_;
|
|
|
|
|
|
|
|
// True if we are resynchronizing after a seek (initial_offset_ > 0). In
|
|
|
|
// particular, a run of kMiddleType and kLastType records can be silently
|
|
|
|
// skipped in this mode
|
|
|
|
bool resyncing_;
|
2011-03-19 06:37:00 +08:00
|
|
|
};
|
|
|
|
|
2011-11-01 01:22:06 +08:00
|
|
|
} // namespace log
|
|
|
|
} // namespace leveldb
|
2011-03-19 06:37:00 +08:00
|
|
|
|
|
|
|
#endif // STORAGE_LEVELDB_DB_LOG_READER_H_
|