711465ccec
Summary: Added a couple functions to WriteBatchWithIndex to make it easier to query the value of a key including reading pending writes from a batch. (This is needed for transactions). I created write_batch_with_index_internal.h to use to store an internal-only helper function since there wasn't a good place in the existing class hierarchy to store this function (and it didn't seem right to stick this function inside WriteBatchInternal::Rep). Since I needed to access the WriteBatchEntryComparator, I moved some helper classes from write_batch_with_index.cc into write_batch_with_index_internal.h/.cc. WriteBatchIndexEntry, ReadableWriteBatch, and WriteBatchEntryComparator are all unchanged (just moved to a different file(s)). Test Plan: Added new unit tests. Reviewers: rven, yhchiang, sdong, igor Reviewed By: igor Subscribers: dhruba, leveldb Differential Revision: https://reviews.facebook.net/D38037
69 lines
1.9 KiB
C++
69 lines
1.9 KiB
C++
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
|
|
// This source code is licensed under the BSD-style license found in the
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
// of patent rights can be found in the PATENTS file in the same directory.
|
|
//
|
|
#pragma once
|
|
#include "db/dbformat.h"
|
|
#include "rocksdb/slice.h"
|
|
#include <string>
|
|
#include <deque>
|
|
|
|
namespace rocksdb {
|
|
|
|
const std::deque<std::string> empty_operand_list;
|
|
|
|
// The merge context for merging a user key.
|
|
// When doing a Get(), DB will create such a class and pass it when
|
|
// issuing Get() operation to memtables and version_set. The operands
|
|
// will be fetched from the context when issuing partial of full merge.
|
|
class MergeContext {
|
|
public:
|
|
// Clear all the operands
|
|
void Clear() {
|
|
if (operand_list) {
|
|
operand_list->clear();
|
|
}
|
|
}
|
|
// Replace all operands with merge_result, which are expected to be the
|
|
// merge result of them.
|
|
void PushPartialMergeResult(std::string& merge_result) {
|
|
assert (operand_list);
|
|
operand_list->clear();
|
|
operand_list->push_front(std::move(merge_result));
|
|
}
|
|
// Push a merge operand
|
|
void PushOperand(const Slice& operand_slice) {
|
|
Initialize();
|
|
operand_list->push_front(operand_slice.ToString());
|
|
}
|
|
// return total number of operands in the list
|
|
size_t GetNumOperands() const {
|
|
if (!operand_list) {
|
|
return 0;
|
|
}
|
|
return operand_list->size();
|
|
}
|
|
// Get the operand at the index.
|
|
Slice GetOperand(int index) const {
|
|
assert (operand_list);
|
|
return (*operand_list)[index];
|
|
}
|
|
// Return all the operands.
|
|
const std::deque<std::string>& GetOperands() const {
|
|
if (!operand_list) {
|
|
return empty_operand_list;
|
|
}
|
|
return *operand_list;
|
|
}
|
|
private:
|
|
void Initialize() {
|
|
if (!operand_list) {
|
|
operand_list.reset(new std::deque<std::string>());
|
|
}
|
|
}
|
|
std::unique_ptr<std::deque<std::string>> operand_list;
|
|
};
|
|
|
|
} // namespace rocksdb
|