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.
107 lines
3.0 KiB
107 lines
3.0 KiB
8 years ago
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
||
8 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
|
//
|
||
|
#ifndef ROCKSDB_LITE
|
||
|
|
||
5 years ago
|
#include "db/blob/blob_log_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
|
namespace blob_db {
|
||
|
|
||
6 years ago
|
Reader::Reader(std::unique_ptr<RandomAccessFileReader>&& file_reader, Env* env,
|
||
7 years ago
|
Statistics* statistics)
|
||
|
: file_(std::move(file_reader)),
|
||
|
env_(env),
|
||
|
statistics_(statistics),
|
||
|
buffer_(),
|
||
|
next_byte_(0) {}
|
||
7 years ago
|
|
||
6 years ago
|
Status Reader::ReadSlice(uint64_t size, Slice* slice, char* buf) {
|
||
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
|
}
|
||
|
|
||
|
Status Reader::ReadHeader(BlobLogHeader* header) {
|
||
|
assert(file_.get() != nullptr);
|
||
|
assert(next_byte_ == 0);
|
||
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
|
}
|
||
|
|
||
|
Status Reader::ReadRecord(BlobLogRecord* record, ReadLevel level,
|
||
7 years ago
|
uint64_t* blob_offset) {
|
||
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
|
}
|
||
|
|
||
|
} // namespace blob_db
|
||
5 years ago
|
} // namespace ROCKSDB_NAMESPACE
|
||
8 years ago
|
#endif // ROCKSDB_LITE
|