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