fork of https://github.com/oxigraph/rocksdb and https://github.com/facebook/rocksdb for nextgraph and oxigraph
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
135 lines
3.9 KiB
135 lines
3.9 KiB
8 years ago
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
||
7 years ago
|
// 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).
|
||
8 years ago
|
//
|
||
|
|
||
4 years ago
|
#include "db/blob/blob_log_sequential_reader.h"
|
||
8 years ago
|
|
||
7 years ago
|
#include <algorithm>
|
||
|
|
||
5 years ago
|
#include "file/random_access_file_reader.h"
|
||
7 years ago
|
#include "monitoring/statistics.h"
|
||
|
#include "util/stop_watch.h"
|
||
8 years ago
|
|
||
5 years ago
|
namespace ROCKSDB_NAMESPACE {
|
||
8 years ago
|
|
||
4 years ago
|
BlobLogSequentialReader::BlobLogSequentialReader(
|
||
4 years ago
|
std::unique_ptr<RandomAccessFileReader>&& file_reader, Env* env,
|
||
|
Statistics* statistics)
|
||
7 years ago
|
: file_(std::move(file_reader)),
|
||
|
env_(env),
|
||
|
statistics_(statistics),
|
||
|
next_byte_(0) {}
|
||
7 years ago
|
|
||
4 years ago
|
BlobLogSequentialReader::~BlobLogSequentialReader() = default;
|
||
|
|
||
|
Status BlobLogSequentialReader::ReadSlice(uint64_t size, Slice* slice,
|
||
|
char* buf) {
|
||
|
assert(slice);
|
||
4 years ago
|
assert(file_);
|
||
|
|
||
7 years ago
|
StopWatch read_sw(env_, statistics_, BLOB_DB_BLOB_FILE_READ_MICROS);
|
||
5 years ago
|
Status s = file_->Read(IOOptions(), next_byte_, static_cast<size_t>(size),
|
||
|
slice, buf, nullptr);
|
||
7 years ago
|
next_byte_ += size;
|
||
|
if (!s.ok()) {
|
||
|
return s;
|
||
|
}
|
||
7 years ago
|
RecordTick(statistics_, BLOB_DB_BLOB_FILE_BYTES_READ, slice->size());
|
||
7 years ago
|
if (slice->size() != size) {
|
||
|
return Status::Corruption("EOF reached while reading record");
|
||
|
}
|
||
|
return s;
|
||
8 years ago
|
}
|
||
|
|
||
4 years ago
|
Status BlobLogSequentialReader::ReadHeader(BlobLogHeader* header) {
|
||
|
assert(header);
|
||
8 years ago
|
assert(next_byte_ == 0);
|
||
4 years ago
|
|
||
|
static_assert(BlobLogHeader::kSize <= sizeof(header_buf_),
|
||
|
"Buffer is smaller than BlobLogHeader::kSize");
|
||
|
|
||
6 years ago
|
Status s = ReadSlice(BlobLogHeader::kSize, &buffer_, header_buf_);
|
||
7 years ago
|
if (!s.ok()) {
|
||
|
return s;
|
||
|
}
|
||
8 years ago
|
|
||
7 years ago
|
if (buffer_.size() != BlobLogHeader::kSize) {
|
||
|
return Status::Corruption("EOF reached before file header");
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
return header->DecodeFrom(buffer_);
|
||
8 years ago
|
}
|
||
|
|
||
4 years ago
|
Status BlobLogSequentialReader::ReadRecord(BlobLogRecord* record,
|
||
|
ReadLevel level,
|
||
|
uint64_t* blob_offset) {
|
||
|
assert(record);
|
||
4 years ago
|
static_assert(BlobLogRecord::kHeaderSize <= sizeof(header_buf_),
|
||
|
"Buffer is smaller than BlobLogRecord::kHeaderSize");
|
||
|
|
||
6 years ago
|
Status s = ReadSlice(BlobLogRecord::kHeaderSize, &buffer_, header_buf_);
|
||
7 years ago
|
if (!s.ok()) {
|
||
|
return s;
|
||
|
}
|
||
8 years ago
|
if (buffer_.size() != BlobLogRecord::kHeaderSize) {
|
||
7 years ago
|
return Status::Corruption("EOF reached before record header");
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
s = record->DecodeHeaderFrom(buffer_);
|
||
|
if (!s.ok()) {
|
||
|
return s;
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
7 years ago
|
uint64_t kb_size = record->key_size + record->value_size;
|
||
7 years ago
|
if (blob_offset != nullptr) {
|
||
7 years ago
|
*blob_offset = next_byte_ + record->key_size;
|
||
7 years ago
|
}
|
||
7 years ago
|
|
||
8 years ago
|
switch (level) {
|
||
7 years ago
|
case kReadHeader:
|
||
8 years ago
|
next_byte_ += kb_size;
|
||
7 years ago
|
break;
|
||
8 years ago
|
|
||
7 years ago
|
case kReadHeaderKey:
|
||
6 years ago
|
record->key_buf.reset(new char[record->key_size]);
|
||
|
s = ReadSlice(record->key_size, &record->key, record->key_buf.get());
|
||
7 years ago
|
next_byte_ += record->value_size;
|
||
|
break;
|
||
8 years ago
|
|
||
7 years ago
|
case kReadHeaderKeyBlob:
|
||
6 years ago
|
record->key_buf.reset(new char[record->key_size]);
|
||
|
s = ReadSlice(record->key_size, &record->key, record->key_buf.get());
|
||
7 years ago
|
if (s.ok()) {
|
||
6 years ago
|
record->value_buf.reset(new char[record->value_size]);
|
||
|
s = ReadSlice(record->value_size, &record->value,
|
||
|
record->value_buf.get());
|
||
8 years ago
|
}
|
||
7 years ago
|
if (s.ok()) {
|
||
|
s = record->CheckBlobCRC();
|
||
8 years ago
|
}
|
||
7 years ago
|
break;
|
||
8 years ago
|
}
|
||
7 years ago
|
return s;
|
||
8 years ago
|
}
|
||
|
|
||
4 years ago
|
Status BlobLogSequentialReader::ReadFooter(BlobLogFooter* footer) {
|
||
|
assert(footer);
|
||
4 years ago
|
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_);
|
||
|
}
|
||
|
|
||
5 years ago
|
} // namespace ROCKSDB_NAMESPACE
|