BlobDB: use char array instead of string as buffer (#4662)

Summary:
As pointed out in #4059, we miss use string as buffer for file read. Changing to use char array instead.

Closing #4059
Pull Request resolved: https://github.com/facebook/rocksdb/pull/4662

Differential Revision: D13012998

Pulled By: yiwu-arbug

fbshipit-source-id: 41234ba17c0bccea65bd647e362a0e979152bd1e
main
Yi Wu 6 years ago committed by Facebook Github Bot
parent 4f0fcb78ae
commit 05dab3aacd
  1. 6
      utilities/blob_db/blob_log_format.h
  2. 19
      utilities/blob_db/blob_log_reader.cc
  3. 6
      utilities/blob_db/blob_log_reader.h

@ -10,7 +10,9 @@
#ifndef ROCKSDB_LITE #ifndef ROCKSDB_LITE
#include <limits> #include <limits>
#include <memory>
#include <utility> #include <utility>
#include "rocksdb/options.h" #include "rocksdb/options.h"
#include "rocksdb/slice.h" #include "rocksdb/slice.h"
#include "rocksdb/status.h" #include "rocksdb/status.h"
@ -106,8 +108,8 @@ struct BlobLogRecord {
uint32_t blob_crc = 0; uint32_t blob_crc = 0;
Slice key; Slice key;
Slice value; Slice value;
std::string key_buf; std::unique_ptr<char[]> key_buf;
std::string value_buf; std::unique_ptr<char[]> value_buf;
uint64_t record_size() const { return kHeaderSize + key_size + value_size; } uint64_t record_size() const { return kHeaderSize + key_size + value_size; }

@ -24,10 +24,9 @@ Reader::Reader(unique_ptr<RandomAccessFileReader>&& file_reader, Env* env,
buffer_(), buffer_(),
next_byte_(0) {} next_byte_(0) {}
Status Reader::ReadSlice(uint64_t size, Slice* slice, std::string* buf) { Status Reader::ReadSlice(uint64_t size, Slice* slice, char* buf) {
StopWatch read_sw(env_, statistics_, BLOB_DB_BLOB_FILE_READ_MICROS); StopWatch read_sw(env_, statistics_, BLOB_DB_BLOB_FILE_READ_MICROS);
buf->reserve(static_cast<size_t>(size)); Status s = file_->Read(next_byte_, static_cast<size_t>(size), slice, buf);
Status s = file_->Read(next_byte_, static_cast<size_t>(size), slice, &(*buf)[0]);
next_byte_ += size; next_byte_ += size;
if (!s.ok()) { if (!s.ok()) {
return s; return s;
@ -42,7 +41,7 @@ Status Reader::ReadSlice(uint64_t size, Slice* slice, std::string* buf) {
Status Reader::ReadHeader(BlobLogHeader* header) { Status Reader::ReadHeader(BlobLogHeader* header) {
assert(file_.get() != nullptr); assert(file_.get() != nullptr);
assert(next_byte_ == 0); assert(next_byte_ == 0);
Status s = ReadSlice(BlobLogHeader::kSize, &buffer_, &backing_store_); Status s = ReadSlice(BlobLogHeader::kSize, &buffer_, header_buf_);
if (!s.ok()) { if (!s.ok()) {
return s; return s;
} }
@ -56,7 +55,7 @@ Status Reader::ReadHeader(BlobLogHeader* header) {
Status Reader::ReadRecord(BlobLogRecord* record, ReadLevel level, Status Reader::ReadRecord(BlobLogRecord* record, ReadLevel level,
uint64_t* blob_offset) { uint64_t* blob_offset) {
Status s = ReadSlice(BlobLogRecord::kHeaderSize, &buffer_, &backing_store_); Status s = ReadSlice(BlobLogRecord::kHeaderSize, &buffer_, header_buf_);
if (!s.ok()) { if (!s.ok()) {
return s; return s;
} }
@ -80,14 +79,18 @@ Status Reader::ReadRecord(BlobLogRecord* record, ReadLevel level,
break; break;
case kReadHeaderKey: case kReadHeaderKey:
s = ReadSlice(record->key_size, &record->key, &record->key_buf); record->key_buf.reset(new char[record->key_size]);
s = ReadSlice(record->key_size, &record->key, record->key_buf.get());
next_byte_ += record->value_size; next_byte_ += record->value_size;
break; break;
case kReadHeaderKeyBlob: case kReadHeaderKeyBlob:
s = ReadSlice(record->key_size, &record->key, &record->key_buf); record->key_buf.reset(new char[record->key_size]);
s = ReadSlice(record->key_size, &record->key, record->key_buf.get());
if (s.ok()) { if (s.ok()) {
s = ReadSlice(record->value_size, &record->value, &record->value_buf); record->value_buf.reset(new char[record->value_size]);
s = ReadSlice(record->value_size, &record->value,
record->value_buf.get());
} }
if (s.ok()) { if (s.ok()) {
s = record->CheckBlobCRC(); s = record->CheckBlobCRC();

@ -60,19 +60,19 @@ class Reader {
Status ReadRecord(BlobLogRecord* record, ReadLevel level = kReadHeader, Status ReadRecord(BlobLogRecord* record, ReadLevel level = kReadHeader,
uint64_t* blob_offset = nullptr); uint64_t* blob_offset = nullptr);
Status ReadSlice(uint64_t size, Slice* slice, std::string* buf);
void ResetNextByte() { next_byte_ = 0; } void ResetNextByte() { next_byte_ = 0; }
uint64_t GetNextByte() const { return next_byte_; } uint64_t GetNextByte() const { return next_byte_; }
private: private:
Status ReadSlice(uint64_t size, Slice* slice, char* buf);
const std::unique_ptr<RandomAccessFileReader> file_; const std::unique_ptr<RandomAccessFileReader> file_;
Env* env_; Env* env_;
Statistics* statistics_; Statistics* statistics_;
std::string backing_store_;
Slice buffer_; Slice buffer_;
char header_buf_[BlobLogRecord::kHeaderSize];
// which byte to read next. For asserting proper usage // which byte to read next. For asserting proper usage
uint64_t next_byte_; uint64_t next_byte_;

Loading…
Cancel
Save