2017-05-10 23:54:35 +02:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-16 01:03:42 +02:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2017-05-10 23:54:35 +02:00
|
|
|
//
|
|
|
|
|
2020-10-08 02:46:50 +02:00
|
|
|
#include "db/blob/blob_log_sequential_reader.h"
|
2017-05-10 23:54:35 +02:00
|
|
|
|
2017-10-27 22:14:34 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
2019-09-16 19:31:27 +02:00
|
|
|
#include "file/random_access_file_reader.h"
|
2017-11-28 20:42:28 +01:00
|
|
|
#include "monitoring/statistics.h"
|
|
|
|
#include "util/stop_watch.h"
|
2017-05-10 23:54:35 +02:00
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2017-05-10 23:54:35 +02:00
|
|
|
|
2020-10-08 02:46:50 +02:00
|
|
|
BlobLogSequentialReader::BlobLogSequentialReader(
|
2020-07-07 02:10:41 +02:00
|
|
|
std::unique_ptr<RandomAccessFileReader>&& file_reader, Env* env,
|
|
|
|
Statistics* statistics)
|
2017-11-28 20:42:28 +01:00
|
|
|
: file_(std::move(file_reader)),
|
|
|
|
env_(env),
|
|
|
|
statistics_(statistics),
|
|
|
|
next_byte_(0) {}
|
2017-10-27 22:14:34 +02:00
|
|
|
|
2020-10-08 02:46:50 +02:00
|
|
|
BlobLogSequentialReader::~BlobLogSequentialReader() = default;
|
|
|
|
|
|
|
|
Status BlobLogSequentialReader::ReadSlice(uint64_t size, Slice* slice,
|
|
|
|
char* buf) {
|
|
|
|
assert(slice);
|
2020-08-27 20:54:43 +02:00
|
|
|
assert(file_);
|
|
|
|
|
2017-11-28 20:42:28 +01:00
|
|
|
StopWatch read_sw(env_, statistics_, BLOB_DB_BLOB_FILE_READ_MICROS);
|
2020-04-30 23:48:51 +02:00
|
|
|
Status s = file_->Read(IOOptions(), next_byte_, static_cast<size_t>(size),
|
|
|
|
slice, buf, nullptr);
|
2017-10-27 22:14:34 +02:00
|
|
|
next_byte_ += size;
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
2017-11-28 20:42:28 +01:00
|
|
|
RecordTick(statistics_, BLOB_DB_BLOB_FILE_BYTES_READ, slice->size());
|
2017-10-27 22:14:34 +02:00
|
|
|
if (slice->size() != size) {
|
|
|
|
return Status::Corruption("EOF reached while reading record");
|
|
|
|
}
|
|
|
|
return s;
|
2017-05-10 23:54:35 +02:00
|
|
|
}
|
|
|
|
|
2020-10-08 02:46:50 +02:00
|
|
|
Status BlobLogSequentialReader::ReadHeader(BlobLogHeader* header) {
|
|
|
|
assert(header);
|
2017-05-10 23:54:35 +02:00
|
|
|
assert(next_byte_ == 0);
|
2020-08-27 20:54:43 +02:00
|
|
|
|
|
|
|
static_assert(BlobLogHeader::kSize <= sizeof(header_buf_),
|
|
|
|
"Buffer is smaller than BlobLogHeader::kSize");
|
|
|
|
|
2018-11-13 21:46:35 +01:00
|
|
|
Status s = ReadSlice(BlobLogHeader::kSize, &buffer_, header_buf_);
|
2017-10-27 22:14:34 +02:00
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
2017-05-10 23:54:35 +02:00
|
|
|
|
2017-10-27 22:14:34 +02:00
|
|
|
if (buffer_.size() != BlobLogHeader::kSize) {
|
|
|
|
return Status::Corruption("EOF reached before file header");
|
2017-05-10 23:54:35 +02:00
|
|
|
}
|
|
|
|
|
2017-10-27 22:14:34 +02:00
|
|
|
return header->DecodeFrom(buffer_);
|
2017-05-10 23:54:35 +02:00
|
|
|
}
|
|
|
|
|
2020-10-08 02:46:50 +02:00
|
|
|
Status BlobLogSequentialReader::ReadRecord(BlobLogRecord* record,
|
|
|
|
ReadLevel level,
|
|
|
|
uint64_t* blob_offset) {
|
|
|
|
assert(record);
|
2020-08-27 20:54:43 +02:00
|
|
|
static_assert(BlobLogRecord::kHeaderSize <= sizeof(header_buf_),
|
|
|
|
"Buffer is smaller than BlobLogRecord::kHeaderSize");
|
|
|
|
|
2018-11-13 21:46:35 +01:00
|
|
|
Status s = ReadSlice(BlobLogRecord::kHeaderSize, &buffer_, header_buf_);
|
2017-10-27 22:14:34 +02:00
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
2017-05-10 23:54:35 +02:00
|
|
|
if (buffer_.size() != BlobLogRecord::kHeaderSize) {
|
2017-10-27 22:14:34 +02:00
|
|
|
return Status::Corruption("EOF reached before record header");
|
2017-05-10 23:54:35 +02:00
|
|
|
}
|
|
|
|
|
2017-10-27 22:14:34 +02:00
|
|
|
s = record->DecodeHeaderFrom(buffer_);
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
2017-05-23 19:30:04 +02:00
|
|
|
}
|
2017-05-10 23:54:35 +02:00
|
|
|
|
2017-10-27 22:14:34 +02:00
|
|
|
uint64_t kb_size = record->key_size + record->value_size;
|
2017-08-11 21:30:02 +02:00
|
|
|
if (blob_offset != nullptr) {
|
2017-10-27 22:14:34 +02:00
|
|
|
*blob_offset = next_byte_ + record->key_size;
|
2017-08-11 21:30:02 +02:00
|
|
|
}
|
2017-10-27 22:14:34 +02:00
|
|
|
|
2017-05-10 23:54:35 +02:00
|
|
|
switch (level) {
|
2017-10-17 21:11:52 +02:00
|
|
|
case kReadHeader:
|
2017-05-10 23:54:35 +02:00
|
|
|
next_byte_ += kb_size;
|
2017-10-27 22:14:34 +02:00
|
|
|
break;
|
2017-05-10 23:54:35 +02:00
|
|
|
|
2017-10-17 21:11:52 +02:00
|
|
|
case kReadHeaderKey:
|
2018-11-13 21:46:35 +01:00
|
|
|
record->key_buf.reset(new char[record->key_size]);
|
|
|
|
s = ReadSlice(record->key_size, &record->key, record->key_buf.get());
|
2017-10-27 22:14:34 +02:00
|
|
|
next_byte_ += record->value_size;
|
|
|
|
break;
|
2017-05-10 23:54:35 +02:00
|
|
|
|
2017-10-17 21:11:52 +02:00
|
|
|
case kReadHeaderKeyBlob:
|
2018-11-13 21:46:35 +01:00
|
|
|
record->key_buf.reset(new char[record->key_size]);
|
|
|
|
s = ReadSlice(record->key_size, &record->key, record->key_buf.get());
|
2017-10-27 22:14:34 +02:00
|
|
|
if (s.ok()) {
|
2018-11-13 21:46:35 +01:00
|
|
|
record->value_buf.reset(new char[record->value_size]);
|
|
|
|
s = ReadSlice(record->value_size, &record->value,
|
|
|
|
record->value_buf.get());
|
2017-05-10 23:54:35 +02:00
|
|
|
}
|
2017-10-27 22:14:34 +02:00
|
|
|
if (s.ok()) {
|
|
|
|
s = record->CheckBlobCRC();
|
2017-05-10 23:54:35 +02:00
|
|
|
}
|
2017-10-27 22:14:34 +02:00
|
|
|
break;
|
2017-05-10 23:54:35 +02:00
|
|
|
}
|
2017-10-27 22:14:34 +02:00
|
|
|
return s;
|
2017-05-10 23:54:35 +02:00
|
|
|
}
|
|
|
|
|
2020-10-08 02:46:50 +02:00
|
|
|
Status BlobLogSequentialReader::ReadFooter(BlobLogFooter* footer) {
|
|
|
|
assert(footer);
|
2020-08-27 20:54:43 +02:00
|
|
|
static_assert(BlobLogFooter::kSize <= sizeof(header_buf_),
|
|
|
|
"Buffer is smaller than BlobLogFooter::kSize");
|
|
|
|
|
|
|
|
Status s = ReadSlice(BlobLogFooter::kSize, &buffer_, header_buf_);
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer_.size() != BlobLogFooter::kSize) {
|
|
|
|
return Status::Corruption("EOF reached before file footer");
|
|
|
|
}
|
|
|
|
|
|
|
|
return footer->DecodeFrom(buffer_);
|
|
|
|
}
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|