提交 fbd60f9b 编写于 作者: Z ZhaoMing

Fix variable naming for LazyBuffer

上级 5a647c99
......@@ -140,7 +140,7 @@ using TMap = std::unordered_map<std::string, T>;
template<class T>
using STMap = std::unordered_map<std::string, std::shared_ptr<T>>;
class WorkerSeparateHelper : public SeparateHelper, public LazyBufferController {
class WorkerSeparateHelper : public SeparateHelper, public LazyBufferState {
public:
void destroy(LazyBuffer* /*buffer*/) const override {}
......@@ -148,7 +148,7 @@ class WorkerSeparateHelper : public SeparateHelper, public LazyBufferController
Status fetch_buffer(LazyBuffer* buffer) const override {
return inplace_decode_callback_(inplace_decode_arg_, buffer,
get_rep(buffer));
get_context(buffer));
}
void TransToCombined(const Slice& user_key, uint64_t sequence,
......@@ -156,7 +156,7 @@ class WorkerSeparateHelper : public SeparateHelper, public LazyBufferController
auto s = value.fetch();
if (s.ok()) {
uint64_t file_number =
SeparateHelper::DecodeFileNumber(value.get_slice());
SeparateHelper::DecodeFileNumber(value.slice());
value.reset(this, {reinterpret_cast<uint64_t>(user_key.data()),
user_key.size(), sequence, 0},
Slice::Invalid(), file_number);
......@@ -165,10 +165,10 @@ class WorkerSeparateHelper : public SeparateHelper, public LazyBufferController
}
}
WorkerSeparateHelper(DependenceMap* dependence_map, void* inplace_decode_arg,
Status (*inplace_decode_callback)(void* arg,
LazyBuffer* buffer,
LazyBufferRep* rep))
WorkerSeparateHelper(
DependenceMap* dependence_map, void* inplace_decode_arg,
Status (*inplace_decode_callback)(void* arg, LazyBuffer* buffer,
LazyBufferContext* rep))
: dependence_map_(dependence_map),
inplace_decode_arg_(inplace_decode_arg),
inplace_decode_callback_(inplace_decode_callback) {}
......@@ -176,7 +176,7 @@ class WorkerSeparateHelper : public SeparateHelper, public LazyBufferController
DependenceMap* dependence_map_;
void* inplace_decode_arg_;
Status (*inplace_decode_callback_)(void* arg, LazyBuffer* buffer,
LazyBufferRep* rep);
LazyBufferContext* rep);
};
std::function<CompactionWorkerResult()>
......@@ -526,7 +526,7 @@ std::string RemoteCompactionDispatcher::Worker::DoCompaction(
c_style_new_iterator.arg = &new_iterator;
c_style_new_iterator.callback = c_style_callback(new_iterator);
auto separate_inplace_decode = [&](LazyBuffer* buffer, LazyBufferRep* rep) {
auto separate_inplace_decode = [&](LazyBuffer* buffer, LazyBufferContext* rep) {
Slice user_key(reinterpret_cast<const char*>(rep->data[0]), rep->data[1]);
uint64_t sequence = rep->data[2];
uint64_t file_number = buffer->file_number();
......
......@@ -1350,7 +1350,7 @@ void CompactionJob::ProcessKeyValueCompaction(SubcompactionState* sub_compact) {
if (!status.ok()) {
break;
}
for (const auto& data_elmt : {key, value.get_slice()}) {
for (const auto& data_elmt : {key, value.slice()}) {
size_t data_end_offset = data_begin_offset + data_elmt.size();
while (sample_begin_offset_iter != sample_begin_offsets.cend() &&
*sample_begin_offset_iter < data_end_offset) {
......@@ -1646,7 +1646,7 @@ void CompactionJob::ProcessGarbageCollection(SubcompactionState* sub_compact) {
break;
}
uint64_t file_number =
SeparateHelper::DecodeFileNumber(value.get_slice());
SeparateHelper::DecodeFileNumber(value.slice());
auto find = dependence_map.find(file_number);
if (find == dependence_map.end()) {
status = Status::Corruption("Separate value dependence missing");
......
......@@ -749,7 +749,7 @@ void CompactionPicker::InitFilesBeingCompact(
iter->Valid(); iter->Next()) {
LazyBuffer value = iter->value();
if (!value.fetch().ok() ||
!element.Decode(iter->key(), value.get_slice())) {
!element.Decode(iter->key(), value.slice())) {
// TODO: log error ?
break;
}
......
......@@ -128,7 +128,7 @@ bool ReadMapElement(MapSstElement& map_element, InternalIterator* iter,
cf_name.c_str(), s.ToString().c_str());
return false;
}
if (!map_element.Decode(iter->key(), value.get_slice())) {
if (!map_element.Decode(iter->key(), value.slice())) {
ROCKS_LOG_BUFFER(
log_buffer,
"[%s] UniversalCompactionPicker MapSstElement Decode fail\n",
......
......@@ -183,7 +183,7 @@ class DBIter final: public Iterator {
status_ = s;
return Slice::Invalid();
}
return value_.get_slice();
return value_.slice();
}
virtual Status status() const override {
if (status_.ok()) {
......
......@@ -626,7 +626,7 @@ class TestPutOperator : public MergeOperator {
if (!Fetch(*merge_in.existing_value, &merge_out->new_value)) {
return true;
}
if (merge_in.existing_value->get_slice() == "corrupted") {
if (merge_in.existing_value->slice() == "corrupted") {
return false;
}
}
......@@ -634,7 +634,7 @@ class TestPutOperator : public MergeOperator {
if (!Fetch(value, &merge_out->new_value)) {
return true;
}
if (value.get_slice() == "corrupted") {
if (value.slice() == "corrupted") {
return false;
}
}
......
......@@ -356,7 +356,7 @@ Status LoadRangeWithDepend(std::vector<RangeWithDepend>& ranges,
if (!s.ok()) {
return s;
}
if (!map_element.Decode(iter->key(), value.get_slice())) {
if (!map_element.Decode(iter->key(), value.slice())) {
return Status::Corruption("Map sst invalid key or value");
}
ranges.emplace_back(map_element);
......
......@@ -393,12 +393,12 @@ class MemTableTombstoneIterator : public MemTableIteratorBase<Slice> {
assert(valid_);
LazyBuffer v = iter_->value();
assert(v.fetch().ok());
return v.fetch().ok() ? v.get_slice() : Slice::Invalid();
return v.fetch().ok() ? v.slice() : Slice::Invalid();
}
};
class MemTableIterator
: public MemTableIteratorBase<LazyBuffer>, public LazyBufferController {
: public MemTableIteratorBase<LazyBuffer>, public LazyBufferState {
using Base = MemTableIteratorBase<LazyBuffer>;
using Base::iter_;
using Base::valid_;
......@@ -411,7 +411,7 @@ class MemTableIterator
void pin_buffer(LazyBuffer* buffer) const override {
if (!value_pinned_ || !iter_->IsValuePinned()) {
LazyBufferController::pin_buffer(buffer);
LazyBufferState::pin_buffer(buffer);
} else {
*buffer = iter_->value();
buffer->pin();
......
......@@ -39,15 +39,15 @@ void MemTableRep::EncodeKeyValue(const Slice& key, const Slice& value,
LazyBuffer MemTableRep::DecodeToLazyValue(const char* key) {
struct SliceControllerImpl : public LazyBufferController {
struct SliceControllerImpl : public LazyBufferState {
void destroy(LazyBuffer* /*buffer*/) const override {}
void pin_buffer(LazyBuffer* /*buffer*/) const override {}
Status fetch_buffer(LazyBuffer* buffer) const override {
auto rep = get_rep(buffer);
const char* k = reinterpret_cast<const char*>(rep->data[0]);
auto context = get_context(buffer);
const char* k = reinterpret_cast<const char*>(context->data[0]);
Slice key_slice = GetLengthPrefixedSlice(k);
set_slice(buffer,
GetLengthPrefixedSlice(key_slice.data() + key_slice.size()));
......
......@@ -28,7 +28,7 @@ bool MergeOperator::FullMergeV2(const MergeOperationInput& merge_in,
if (!Fetch(*merge_in.existing_value, &merge_out->new_value)) {
return true;
}
existing_value = &merge_in.existing_value->get_slice();
existing_value = &merge_in.existing_value->slice();
}
return FullMerge(merge_in.key, existing_value, operand_list_str,
merge_out->new_value.trans_to_string(), merge_in.logger);
......@@ -75,8 +75,8 @@ bool AssociativeMergeOperator::FullMergeV2(
}
temp_value.clear();
const Slice* existing_value_slice =
existing_value == nullptr ? nullptr : &existing_value->get_slice();
if (!Merge(merge_in.key, existing_value_slice, operand.get_slice(),
existing_value == nullptr ? nullptr : &existing_value->slice();
if (!Merge(merge_in.key, existing_value_slice, operand.slice(),
temp_value.trans_to_string(), merge_in.logger)) {
return false;
}
......@@ -98,7 +98,7 @@ bool AssociativeMergeOperator::PartialMerge(const Slice& key,
if (!Fetch(left_operand, new_value) || !Fetch(right_operand, new_value)) {
return true;
}
return Merge(key, &left_operand.get_slice(), right_operand.get_slice(),
return Merge(key, &left_operand.slice(), right_operand.slice(),
new_value->trans_to_string(), logger);
}
......
......@@ -391,7 +391,7 @@ Status TableCache::Get(const ReadOptions& options,
}
// Manual inline MapSstElement::Decode
const char* err_msg = "Map sst invalid link_value";
Slice map_input = map_value.get_slice();
Slice map_input = map_value.slice();
Slice smallest_key;
uint64_t link_count;
uint64_t flags;
......
......@@ -1203,11 +1203,12 @@ Version::Version(ColumnFamilyData* column_family_data, VersionSet* vset,
version_number_(version_number) {}
Status Version::fetch_buffer(LazyBuffer* buffer) const {
auto rep = get_rep(buffer);
Slice user_key(reinterpret_cast<const char*>(rep->data[0]), rep->data[1]);
uint64_t sequence = rep->data[2];
auto context = get_context(buffer);
Slice user_key(reinterpret_cast<const char*>(context->data[0]),
context->data[1]);
uint64_t sequence = context->data[2];
const FileMetaData* file_metadata =
reinterpret_cast<FileMetaData*>(rep->data[3]);
reinterpret_cast<FileMetaData*>(context->data[3]);
bool value_found = false;
SequenceNumber context_seq;
GetContext get_context(cfd_->internal_comparator().user_comparator(), nullptr,
......@@ -1246,7 +1247,7 @@ void Version::TransToCombined(const Slice& user_key, uint64_t sequence,
value.reset(std::move(s));
return;
}
uint64_t file_number = SeparateHelper::DecodeFileNumber(value.get_slice());
uint64_t file_number = SeparateHelper::DecodeFileNumber(value.slice());
auto& dependence_map = storage_info_.dependence_map();
auto find = dependence_map.find(file_number);
if (find == dependence_map.end()) {
......
......@@ -572,7 +572,7 @@ class VersionStorageInfo {
void operator=(const VersionStorageInfo&) = delete;
};
class Version : public SeparateHelper, private LazyBufferController {
class Version : public SeparateHelper, private LazyBufferState {
public:
// Append to *iters a sequence of iterators that will
// yield the contents of this Version when merged together.
......
......@@ -1524,7 +1524,7 @@ class MemTableInserter : public WriteBatch::Handler {
auto s = new_value.fetch();
if (s.ok()) {
bool mem_res =
mem->Add(sequence_, kTypeValue, key, new_value.get_slice());
mem->Add(sequence_, kTypeValue, key, new_value.slice());
if (UNLIKELY(!mem_res)) {
assert(seq_per_batch_);
ret_status = Status::TryAgain("key+seq exists");
......
......@@ -167,7 +167,7 @@ class CompactionFilter {
switch (value_type) {
case ValueType::kValue: {
bool value_changed = false;
bool rv = Filter(level, key, existing_value.get_slice(),
bool rv = Filter(level, key, existing_value.slice(),
new_value->trans_to_string(), &value_changed);
if (rv) {
return Decision::kRemove;
......@@ -175,7 +175,7 @@ class CompactionFilter {
return value_changed ? Decision::kChangeValue : Decision::kKeep;
}
case ValueType::kMergeOperand: {
bool rv = FilterMergeOperand(level, key, existing_value.get_slice());
bool rv = FilterMergeOperand(level, key, existing_value.slice());
return rv ? Decision::kRemove : Decision::kKeep;
}
}
......
......@@ -18,10 +18,12 @@
namespace rocksdb {
// State pattern
class LazyBuffer;
class LazyBufferEditor;
class LazyBufferBuilder;
struct LazyBufferRep {
struct LazyBufferContext {
uint64_t data[4];
};
......@@ -30,9 +32,9 @@ struct LazyBufferCustomizeBuffer {
void* (*uninitialized_resize)(void* handle, size_t size);
};
class LazyBufferController {
class LazyBufferState {
public:
virtual ~LazyBufferController() = default;
virtual ~LazyBufferState() = default;
// Release resource
virtual void destroy(LazyBuffer* buffer) const = 0;
......@@ -46,7 +48,7 @@ class LazyBufferController {
// Save error into buffer
virtual void assign_error(LazyBuffer* buffer, Status&& status) const;
// Pin the buffer, turn the controller into editable
// Pin the buffer, turn the state into editable
virtual void pin_buffer(LazyBuffer* buffer) const;
// Fetch buffer and dump to target, the buffer may be destroyed
......@@ -55,39 +57,39 @@ class LazyBufferController {
// Fetch buffer
virtual Status fetch_buffer(LazyBuffer* buffer) const = 0;
// Use LazyBufferRep as local storage
// Use LazyBufferContext as local storage
// data -> 32 bytes
static const LazyBufferController* light_controller();
static const LazyBufferState* light_state();
// Use LazyBufferRep as buffer
// Use LazyBufferContext as buffer
// data[0] -> handle
// data[1] -> uninitialized_resize call
// data[2 - 3] -> Status
static const LazyBufferController* buffer_controller();
static const LazyBufferState* buffer_state();
// Use LazyBufferRep as string holder
// Use LazyBufferContext as string holder
// data[0] -> string ptr
// data[1] -> is owner
// data[2 - 3] -> Status
static const LazyBufferController* string_controller();
static const LazyBufferState* string_state();
// Use LazyBufferRep as LazuBuffer reference
// Use LazyBufferContext as LazuBuffer reference
// data[0] -> ptr to LazyBuffer
static const LazyBufferController* reference_controller();
static const LazyBufferState* reference_state();
// Use LazyBufferRep as Cleanable
// rep -> Cleanable
static const LazyBufferController* cleanable_controller();
// Use LazyBufferContext as Cleanable
// context -> Cleanable
static const LazyBufferState* cleanable_state();
// Set buffer->slice_ = slice
static void set_slice(LazyBuffer* buffer, const Slice& slice);
// Get &buffer->rep_
static LazyBufferRep* get_rep(LazyBuffer* buffer);
// Get &buffer->context_
static LazyBufferContext* get_context(LazyBuffer* buffer);
};
class LazyBuffer {
friend LazyBufferController;
friend LazyBufferState;
protected:
union {
struct {
......@@ -96,18 +98,18 @@ class LazyBuffer {
};
Slice slice_ = Slice();
};
const LazyBufferController* controller_;
LazyBufferRep rep_;
const LazyBufferState* state_;
LazyBufferContext context_;
uint64_t file_number_;
// Call LazyBufferController::destroy if controller_ not nullptr
// Call LazyBufferState::destroy if state_ not nullptr
void destroy();
// Call LazyBufferController::assign_error if _status not ok
// Call LazyBufferState::assign_error if _status not ok
void assign_error(Status&& _status);
// Fix light_controller local storage
void fix_light_controller(const LazyBuffer& other);
// Fix light_state local storage
void fix_light_state(const LazyBuffer& other);
public:
......@@ -142,9 +144,9 @@ public:
LazyBuffer(const Slice& _slice, Cleanable&& _cleanable,
uint64_t _file_number = uint64_t(-1)) noexcept;
// Init from customize controller
LazyBuffer(const LazyBufferController* _controller,
const LazyBufferRep& _rep, const Slice& _slice = Slice::Invalid(),
// Init from customize state
LazyBuffer(const LazyBufferState* _state, const LazyBufferContext& _context,
const Slice& _slice = Slice::Invalid(),
uint64_t _file_number = uint64_t(-1)) noexcept;
~LazyBuffer() {
......@@ -162,7 +164,7 @@ public:
// Get inner slice
// REQUIRES: valid()
const Slice& get_slice() const { assert(valid()); return slice_; }
const Slice& slice() const { assert(valid()); return slice_; }
// Return a pointer to the beginning of the referenced data
// REQUIRES: valid()
......@@ -249,16 +251,16 @@ public:
void reset(const Slice& _slice, Cleanable&& _cleanable,
uint64_t _file_number = uint64_t(-1));
// Reset to customize controller
void reset(const LazyBufferController* _controller, const LazyBufferRep& _rep,
// Reset to customize state
void reset(const LazyBufferState* _state, const LazyBufferContext& _context,
const Slice& _slice = Slice::Invalid(),
uint64_t _file_number = uint64_t(-1));
// Fetch source and copy it
void assign(const LazyBuffer& _source);
// Trans to editable buffer and get editor
LazyBufferEditor* get_editor();
// Trans to editable buffer and get builder
LazyBufferBuilder* get_builder();
// Trans buffer to string for modification
std::string* trans_to_string();
......@@ -266,7 +268,7 @@ public:
// Return the certain file number of SST, -1 for unknown
uint64_t file_number() const { return file_number_; }
// Pin this buffer, turn the controller into editable
// Pin this buffer, turn the state into editable
void pin();
// Dump buffer to customize buffer
......@@ -282,13 +284,13 @@ public:
Status fetch() const;
// For test
const LazyBufferController* TEST_controller() const { return controller_; }
const LazyBufferState* TEST_state() const { return state_; }
// For test
const LazyBufferRep* TEST_rep() const { return &rep_; }
const LazyBufferContext* TEST_context() const { return &context_; }
};
class LazyBufferEditor : private LazyBuffer {
class LazyBufferBuilder : private LazyBuffer {
public:
char* data() const { return data_; }
using LazyBuffer::size;
......@@ -303,85 +305,85 @@ class LazyBufferEditor : private LazyBuffer {
bool uninitialized_resize(size_t _size);
};
inline void LazyBufferController::set_slice(LazyBuffer* buffer,
const Slice& slice) {
inline void LazyBufferState::set_slice(LazyBuffer* buffer, const Slice& slice) {
buffer->slice_ = slice;
}
inline LazyBufferRep* LazyBufferController::get_rep(LazyBuffer* buffer) {
return &buffer->rep_;
inline LazyBufferContext* LazyBufferState::get_context(LazyBuffer* buffer) {
return &buffer->context_;
}
inline LazyBuffer::LazyBuffer() noexcept
: controller_(LazyBufferController::light_controller()),
rep_{},
: state_(LazyBufferState::light_state()),
context_{},
file_number_(uint64_t(-1)) {}
inline LazyBuffer::LazyBuffer(LazyBuffer&& _buffer) noexcept
: slice_(_buffer.slice_),
controller_(_buffer.controller_),
rep_(_buffer.rep_),
state_(_buffer.state_),
context_(_buffer.context_),
file_number_(_buffer.file_number_) {
if (controller_ == LazyBufferController::light_controller() &&
_buffer.size_ <= sizeof(LazyBufferRep)) {
fix_light_controller(_buffer);
if (state_ == LazyBufferState::light_state() &&
_buffer.size_ <= sizeof(LazyBufferContext)) {
fix_light_state(_buffer);
}
_buffer.slice_ = Slice::Invalid();
_buffer.controller_ = nullptr;
_buffer.state_ = nullptr;
}
inline LazyBuffer::LazyBuffer(const Slice& _slice, bool _copy,
uint64_t _file_number)
: slice_(_slice),
controller_(LazyBufferController::light_controller()),
rep_{},
state_(LazyBufferState::light_state()),
context_{},
file_number_(_file_number) {
assert(_slice.valid());
if (_copy) {
controller_->assign_slice(this, _slice);
state_->assign_slice(this, _slice);
}
}
inline LazyBuffer::LazyBuffer(const Slice& _slice, Cleanable&& _cleanable,
uint64_t _file_number) noexcept
: slice_(_slice),
controller_(LazyBufferController::cleanable_controller()),
rep_{},
state_(LazyBufferState::cleanable_state()),
context_{},
file_number_(_file_number) {
assert(_slice.valid());
static_assert(sizeof _cleanable == sizeof rep_, "");
static_assert(alignof(Cleanable) == alignof(LazyBufferRep), "");
::new(&rep_) Cleanable(std::move(_cleanable));
static_assert(sizeof _cleanable == sizeof context_, "");
static_assert(alignof(Cleanable) == alignof(LazyBufferContext), "");
::new(&context_) Cleanable(std::move(_cleanable));
}
inline LazyBuffer::LazyBuffer(const LazyBufferController* _controller,
const LazyBufferRep& _rep, const Slice& _slice,
inline LazyBuffer::LazyBuffer(const LazyBufferState* _state,
const LazyBufferContext& _context,
const Slice& _slice,
uint64_t _file_number) noexcept
: slice_(_slice),
controller_(_controller),
rep_(_rep),
state_(_state),
context_(_context),
file_number_(_file_number) {
assert(_controller != nullptr);
assert(_state != nullptr);
}
inline void LazyBuffer::destroy() {
if (controller_ != nullptr) {
controller_->destroy(this);
if (state_ != nullptr) {
state_->destroy(this);
}
}
inline void LazyBuffer::assign_error(Status&& _status) {
if (_status.ok()) {
controller_->assign_slice(this, Slice());
state_->assign_slice(this, Slice());
} else {
controller_->assign_error(this, std::move(_status));
state_->assign_error(this, std::move(_status));
assert(!slice_.valid());
}
file_number_ = uint64_t(-1);
}
inline void LazyBuffer::clear() {
controller_->assign_slice(this, Slice());
state_->assign_slice(this, Slice());
assert(size_ == 0);
file_number_ = uint64_t(-1);
}
......@@ -390,15 +392,15 @@ inline void LazyBuffer::reset(LazyBuffer&& _buffer) {
if (this != &_buffer) {
destroy();
slice_ = _buffer.slice_;
controller_ = _buffer.controller_;
rep_ = _buffer.rep_;
state_ = _buffer.state_;
context_ = _buffer.context_;
file_number_ = _buffer.file_number_;
if (controller_ == LazyBufferController::light_controller() &&
_buffer.size_ <= sizeof(LazyBufferRep)) {
fix_light_controller(_buffer);
if (state_ == LazyBufferState::light_state() &&
_buffer.size_ <= sizeof(LazyBufferContext)) {
fix_light_state(_buffer);
}
_buffer.slice_ = Slice::Invalid();
_buffer.controller_ = nullptr;
_buffer.state_ = nullptr;
}
}
......@@ -406,13 +408,13 @@ inline void LazyBuffer::reset(const Slice& _slice, bool _copy,
uint64_t _file_number) {
assert(_slice.valid());
if (_copy) {
controller_->assign_slice(this, _slice);
state_->assign_slice(this, _slice);
assert(slice_ == _slice);
} else {
destroy();
slice_ = _slice;
controller_ = LazyBufferController::light_controller();
rep_ = {};
state_ = LazyBufferState::light_state();
context_ = {};
}
file_number_ = _file_number;
}
......@@ -421,40 +423,40 @@ inline void LazyBuffer::reset(const Slice& _slice, Cleanable&& _cleanable,
uint64_t _file_number) {
assert(_slice.valid());
destroy();
controller_ = LazyBufferController::cleanable_controller();
state_ = LazyBufferState::cleanable_state();
slice_ = _slice;
new(&rep_) Cleanable(std::move(_cleanable));
new(&context_) Cleanable(std::move(_cleanable));
file_number_ = _file_number;
}
inline void LazyBuffer::reset(const LazyBufferController* _controller,
const LazyBufferRep& _rep, const Slice& _slice,
uint64_t _file_number) {
assert(_controller != nullptr);
inline void LazyBuffer::reset(const LazyBufferState* _state,
const LazyBufferContext& _context,
const Slice& _slice, uint64_t _file_number) {
assert(_state != nullptr);
destroy();
slice_ = _slice;
controller_ = _controller;
rep_ = _rep;
state_ = _state;
context_ = _context;
file_number_ = _file_number;
}
inline void LazyBuffer::pin() {
assert(controller_ != nullptr);
return controller_->pin_buffer(this);
assert(state_ != nullptr);
return state_->pin_buffer(this);
}
inline Status LazyBuffer::dump(LazyBuffer& _target) && {
assert(controller_ != nullptr);
inline Status LazyBuffer::dump(LazyBuffer& _target)&& {
assert(state_ != nullptr);
assert(this != &_target);
return controller_->dump_buffer(this, &_target);
return state_->dump_buffer(this, &_target);
}
inline Status LazyBuffer::fetch() const {
assert(controller_ != nullptr);
assert(state_ != nullptr);
if (slice_.valid()) {
return Status::OK();
}
return controller_->fetch_buffer(const_cast<LazyBuffer*>(this));
return state_->fetch_buffer(const_cast<LazyBuffer*>(this));
}
// make a slice reference
......
......@@ -124,7 +124,7 @@ void PatriciaTrieRep::Get(
valvec<char> buffer;
};
class Controller : public LazyBufferController {
class Controller : public LazyBufferState {
public:
virtual void destroy(LazyBuffer* /*buffer*/) const override {}
......
......@@ -140,7 +140,7 @@ public:
template <bool heap_mode>
class PatriciaRepIterator :
public MemTableRep::Iterator,
public LazyBufferController,
public LazyBufferState,
boost::noncopyable {
typedef terark::Patricia::ReaderToken token_t;
......@@ -228,17 +228,17 @@ public:
virtual ~PatriciaRepIterator();
// LazyBufferController override
// LazyBufferState override
virtual void destroy(LazyBuffer* /*buffer*/) const override {}
// LazyBufferController override
// LazyBufferState override
virtual void pin_buffer(LazyBuffer* buffer) const override {
if (!buffer->valid()) {
buffer->reset(GetValue());
}
}
// LazyBufferController override
// LazyBufferState override
Status fetch_buffer(LazyBuffer* buffer) const override {
set_slice(buffer, GetValue());
return Status::OK();
......
......@@ -425,7 +425,7 @@ void BlockBasedTableBuilder::Add(const Slice& key,
r->status = s;
return;
}
const Slice& value = lazy_value.get_slice();
const Slice& value = lazy_value.slice();
ValueType value_type = ExtractValueType(key);
if (r->ignore_key_type || IsValueType(value_type)) {
if (r->props.num_entries > 0) {
......
......@@ -680,7 +680,7 @@ class BlockBasedTableIterator
template <>
class BlockBasedTableIterator<DataBlockIter, LazyBuffer>
: public BlockBasedTableIteratorBase<DataBlockIter, LazyBuffer>,
public LazyBufferController {
public LazyBufferState {
using Base = BlockBasedTableIteratorBase<DataBlockIter, LazyBuffer>;
using Base::block_iter_;
using Base::icomp_;
......
......@@ -88,7 +88,7 @@ void CuckooTableBuilder::Add(const Slice& key, const LazyBuffer& lazy_value) {
status_ = s;
return;
}
const Slice& value = lazy_value.get_slice();
const Slice& value = lazy_value.slice();
if (num_entries_ >= kMaxVectorIdx - 1) {
status_ = Status::NotSupported("Number of keys in a file must be < 2^32-1");
return;
......
......@@ -122,7 +122,7 @@ void PlainTableBuilder::Add(const Slice& key, const LazyBuffer& lazy_value) {
status_ = s;
return;
}
const Slice& value = lazy_value.get_slice();
const Slice& value = lazy_value.slice();
// temp buffer for metadata bytes between key and value.
char meta_bytes_buf[6];
size_t meta_bytes_buf_size = 0;
......
......@@ -382,7 +382,7 @@ void TerarkZipTableBuilder::Add(const Slice& key,
status_ = s;
return;
}
const Slice& value = lazy_value.get_slice();
const Slice& value = lazy_value.slice();
if (table_options_.debugLevel == 3) {
rocksdb::ParsedInternalKey ikey;
rocksdb::ParseInternalKey(key, &ikey);
......@@ -1495,7 +1495,7 @@ TerarkZipTableBuilder::BuilderWriteValues(KeyValueStatus& kvs, std::function<voi
return s;
}
curKey = second_pass_iter_->key();
curVal = lazy_value.get_slice();
curVal = lazy_value.slice();
TERARK_RT_assert(ParseInternalKey(curKey, &pIKey), std::logic_error);
if (debugDumpKeyValue) {
dumpKeyValueFunc(pIKey, curVal);
......@@ -1553,7 +1553,7 @@ TerarkZipTableBuilder::BuilderWriteValues(KeyValueStatus& kvs, std::function<voi
return false;
}
curKey = second_pass_iter_->key();
curVal = lazy_value.get_slice();
curVal = lazy_value.slice();
TERARK_RT_assert(ParseInternalKey(curKey, &pIKey), std::logic_error);
if (debugDumpKeyValue) {
dumpKeyValueFunc(pIKey, curVal);
......
......@@ -238,7 +238,7 @@ public:
template<bool reverse>
class TerarkZipTableIterator :
public TerarkZipTableIndexIterator,
public LazyBufferController {
public LazyBufferState {
protected:
const TableReaderOptions* table_reader_options_;
SequenceNumber global_seqno_;
......
......@@ -255,7 +255,7 @@ class MapSstIterator final : public InternalIterator {
if (!status_.ok()) {
return kInitFirstIterInvalid;
}
Slice map_input = first_level_value_.get_slice();
Slice map_input = first_level_value_.slice();
link_.clear();
largest_key_ = first_level_iter_->key();
uint64_t flags;
......
此差异已折叠。
......@@ -20,55 +20,51 @@ TEST_F(LazyBufferTest, Basic) {
LazyBuffer empty;
ASSERT_OK(empty.fetch());
ASSERT_EQ(empty.get_slice(), "");
ASSERT_EQ(empty.slice(), "");
LazyBuffer abc("abc");
ASSERT_OK(abc.fetch());
ASSERT_EQ(abc.get_slice(), "abc");
ASSERT_EQ(abc.slice(), "abc");
LazyBuffer abc2(std::move(abc));
ASSERT_OK(abc2.fetch());
ASSERT_EQ(abc2.get_slice(), "abc");
ASSERT_EQ(abc2.slice(), "abc");
LazyBuffer abc3 = std::move(abc2);
ASSERT_OK(abc3.fetch());
ASSERT_EQ(abc3.get_slice(), "abc");
ASSERT_EQ(abc3.slice(), "abc");
}
TEST_F(LazyBufferTest, LightColtroller) {
LazyBuffer buffer;
ASSERT_EQ(buffer.TEST_controller(),
LazyBufferController::light_controller());
ASSERT_EQ(buffer.TEST_state(), LazyBufferState::light_state());
std::string string('a', 33);
buffer.reset(string, false);
ASSERT_EQ(buffer.TEST_controller(),
LazyBufferController::light_controller());
ASSERT_EQ(buffer.TEST_state(), LazyBufferState::light_state());
for (auto &c : string) { c = 'b'; };
ASSERT_OK(buffer.fetch());
ASSERT_EQ(buffer.get_slice(), string);
ASSERT_EQ(buffer.slice(), string);
buffer.reset(Slice(string.data(), 32), true);
ASSERT_EQ(buffer.TEST_controller(),
LazyBufferController::light_controller());
ASSERT_EQ(buffer.TEST_state(), LazyBufferState::light_state());
buffer.reset(string, true);
ASSERT_EQ(buffer.TEST_controller(),
LazyBufferController::buffer_controller());
ASSERT_EQ(buffer.TEST_state(), LazyBufferState::buffer_state());
string = "abc";
buffer.reset(string);
for (auto &c : string) { c = 'a'; };
ASSERT_OK(buffer.fetch());
ASSERT_EQ(buffer.get_slice(), "aaa");
ASSERT_EQ(buffer.slice(), "aaa");
buffer.reset(string, true);
for (auto &c : string) { c = 'b'; };
ASSERT_OK(buffer.fetch());
ASSERT_EQ(buffer.get_slice(), "aaa");
ASSERT_EQ(buffer.slice(), "aaa");
}
......@@ -76,24 +72,23 @@ TEST_F(LazyBufferTest, LightColtroller) {
TEST_F(LazyBufferTest, BufferColtroller) {
auto test = [](LazyBuffer& b) {
auto editor = b.get_editor();
ASSERT_FALSE(editor->resize(size_t(-1)));
ASSERT_NOK(editor->fetch());
ASSERT_TRUE(editor->resize(4));
ASSERT_EQ(b.get_slice(), Slice("\0\0\0\0", 4));
::memcpy(editor->data(), "abcd", 4);
ASSERT_TRUE(editor->resize(3));
ASSERT_EQ(b.get_slice(), "abc");
auto builder = b.get_builder();
ASSERT_FALSE(builder->resize(size_t(-1)));
ASSERT_NOK(builder->fetch());
ASSERT_TRUE(builder->resize(4));
ASSERT_EQ(b.slice(), Slice("\0\0\0\0", 4));
::memcpy(builder->data(), "abcd", 4);
ASSERT_TRUE(builder->resize(3));
ASSERT_EQ(b.slice(), "abc");
};
LazyBuffer buffer(size_t(-1));
ASSERT_EQ(buffer.TEST_controller(),
LazyBufferController::buffer_controller());
ASSERT_EQ(buffer.TEST_state(), LazyBufferState::buffer_state());
ASSERT_NOK(buffer.fetch());
test(buffer);
buffer.clear();
ASSERT_EQ(buffer.TEST_controller(),
LazyBufferController::buffer_controller());
ASSERT_EQ(buffer.TEST_state(),
LazyBufferState::buffer_state());
test(buffer);
buffer = LazyBuffer("123");
......@@ -121,30 +116,28 @@ TEST_F(LazyBufferTest, StringColtroller) {
std::string string = "abc";
LazyBuffer buffer(&string);
ASSERT_EQ(buffer.TEST_controller(),
LazyBufferController::string_controller());
ASSERT_EQ(buffer.get_slice(), "abc");
ASSERT_EQ(buffer.TEST_state(), LazyBufferState::string_state());
ASSERT_EQ(buffer.slice(), "abc");
buffer.reset("aaa", true);
buffer.trans_to_string()->append("bbb");
ASSERT_OK(buffer.fetch());
ASSERT_EQ(buffer.get_slice(), "aaabbb");
ASSERT_EQ(buffer.TEST_rep()->data[1], 0);
ASSERT_EQ(buffer.slice(), "aaabbb");
ASSERT_EQ(buffer.TEST_context()->data[1], 0);
buffer.get_editor()->resize(buffer.size() - 1);
ASSERT_EQ(buffer.TEST_controller(),
LazyBufferController::string_controller());
buffer.get_builder()->resize(buffer.size() - 1);
ASSERT_EQ(buffer.TEST_state(), LazyBufferState::string_state());
ASSERT_OK(std::move(buffer).dump(&string));
ASSERT_EQ(string, "aaabb");
buffer.reset(string);
buffer.trans_to_string()->resize(string.size() - 1);
ASSERT_OK(std::move(buffer).dump(&string));
ASSERT_EQ(buffer.TEST_rep()->data[1], 1);
ASSERT_EQ(buffer.TEST_context()->data[1], 1);
ASSERT_EQ(string, "aaab");
buffer.reset(string);
buffer.get_editor()->resize(string.size() - 1);
buffer.get_builder()->resize(string.size() - 1);
ASSERT_OK(std::move(buffer).dump(&string));
ASSERT_EQ(string, "aaa");
......
......@@ -36,7 +36,7 @@ class MaxOperator : public MergeOperator {
max = nullptr;
return true;
}
if (max == nullptr || max->get_slice().compare(op.get_slice()) < 0) {
if (max == nullptr || max->slice().compare(op.slice()) < 0) {
max = &op;
}
}
......@@ -52,7 +52,7 @@ class MaxOperator : public MergeOperator {
if (!Fetch(left_operand, new_value) || !Fetch(right_operand, new_value)) {
return true;
}
if (left_operand.get_slice().compare(right_operand.get_slice()) >= 0) {
if (left_operand.slice().compare(right_operand.slice()) >= 0) {
new_value->assign(left_operand);
} else {
new_value->assign(right_operand);
......@@ -70,8 +70,8 @@ class MaxOperator : public MergeOperator {
if (!Fetch(operand, new_value)) {
return true;
}
if (max.get_slice().compare(operand.get_slice()) < 0) {
max.reset(operand.get_slice());
if (max.slice().compare(operand.slice()) < 0) {
max.reset(operand.slice());
}
}
......
......@@ -263,7 +263,7 @@ Status WritePreparedTxn::RollbackInternal() {
&callback);
assert(s.ok() ? lazy_val.valid() : s.IsNotFound());
if (s.ok()) {
s = rollback_batch_->Put(cf_handle, key, lazy_val.get_slice());
s = rollback_batch_->Put(cf_handle, key, lazy_val.slice());
assert(s.ok());
} else if (s.IsNotFound()) {
// There has been no readable value before txn. By adding a delete we
......
......@@ -386,7 +386,7 @@ Status WriteUnpreparedTxn::RollbackInternal() {
&callback);
if (s.ok()) {
assert(lazy_val.valid());
s = rollback_batch.Put(cf_handle, key, lazy_val.get_slice());
s = rollback_batch.Put(cf_handle, key, lazy_val.slice());
assert(s.ok());
} else if (s.IsNotFound()) {
s = rollback_batch.Delete(cf_handle, key);
......
......@@ -78,7 +78,7 @@ Status WriteUnpreparedTxnDB::RollbackRecoveredTransaction(
&callback);
assert(s.ok() ? lazy_val.valid() : s.IsNotFound());
if (s.ok()) {
s = rollback_batch_->Put(cf_handle, key, lazy_val.get_slice());
s = rollback_batch_->Put(cf_handle, key, lazy_val.slice());
assert(s.ok());
} else if (s.IsNotFound()) {
// There has been no readable value before txn. By adding a delete we
......
......@@ -181,9 +181,9 @@ Status DBWithTTLImpl::StripTS(LazyBuffer* lazy_val) {
return Status::Corruption("Bad timestamp in key-value");
}
// Erasing characters which hold the TS
auto editor = lazy_val->get_editor();
if (!editor->resize(size - kTSLength)) {
return editor->fetch();
auto builder = lazy_val->get_builder();
if (!builder->resize(size - kTSLength)) {
return builder->fetch();
}
return st;
}
......@@ -214,7 +214,7 @@ Status DBWithTTLImpl::Get(const ReadOptions& options,
if (!st.ok()) {
return st;
}
st = SanityCheckTimestamp(value->get_slice());
st = SanityCheckTimestamp(value->slice());
if (!st.ok()) {
return st;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册