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
|
|
|
//
|
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
|
|
|
|
#include "utilities/blob_db/blob_log_reader.h"
|
|
|
|
|
2017-10-27 20:14:34 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2017-05-10 21:54:35 +00:00
|
|
|
#include "util/file_reader_writer.h"
|
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
namespace blob_db {
|
|
|
|
|
|
|
|
Reader::Reader(std::shared_ptr<Logger> info_log,
|
|
|
|
unique_ptr<SequentialFileReader>&& _file)
|
2017-10-27 20:14:34 +00:00
|
|
|
: info_log_(info_log), file_(std::move(_file)), buffer_(), next_byte_(0) {}
|
|
|
|
|
|
|
|
Status Reader::ReadSlice(uint64_t size, Slice* slice, std::string* buf) {
|
|
|
|
buf->reserve(size);
|
|
|
|
Status s = file_->Read(size, slice, &(*buf)[0]);
|
|
|
|
next_byte_ += size;
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
if (slice->size() != size) {
|
|
|
|
return Status::Corruption("EOF reached while reading record");
|
|
|
|
}
|
|
|
|
return s;
|
2017-05-10 21:54:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Status Reader::ReadHeader(BlobLogHeader* header) {
|
|
|
|
assert(file_.get() != nullptr);
|
|
|
|
assert(next_byte_ == 0);
|
2017-10-27 20:14:34 +00:00
|
|
|
Status s = ReadSlice(BlobLogHeader::kSize, &buffer_, &backing_store_);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
Status Reader::ReadRecord(BlobLogRecord* record, ReadLevel level,
|
2017-08-11 19:30:02 +00:00
|
|
|
uint64_t* blob_offset) {
|
2017-10-27 20:14:34 +00:00
|
|
|
Status s = ReadSlice(BlobLogRecord::kHeaderSize, &buffer_, &backing_store_);
|
|
|
|
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-10-27 20:14:34 +00:00
|
|
|
file_->Skip(record->key_size + record->value_size);
|
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:
|
2017-10-27 20:14:34 +00:00
|
|
|
s = ReadSlice(record->key_size, &record->key, &record->key_buf);
|
|
|
|
file_->Skip(record->value_size);
|
|
|
|
next_byte_ += record->value_size;
|
|
|
|
break;
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2017-10-17 19:11:52 +00:00
|
|
|
case kReadHeaderKeyBlob:
|
2017-10-27 20:14:34 +00:00
|
|
|
s = ReadSlice(record->key_size, &record->key, &record->key_buf);
|
|
|
|
if (s.ok()) {
|
|
|
|
s = ReadSlice(record->value_size, &record->value, &record->value_buf);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace blob_db
|
|
|
|
} // namespace rocksdb
|
|
|
|
#endif // ROCKSDB_LITE
|