|
|
|
/**
|
|
|
|
* A MergeOperator for rocksdb that implements string append.
|
|
|
|
* @author Deon Nicholas (dnicholas@fb.com)
|
|
|
|
* Copyright 2013 Facebook
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "stringappend.h"
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include "rocksdb/merge_operator.h"
|
|
|
|
#include "rocksdb/slice.h"
|
|
|
|
#include "rocksdb/utilities/options_type.h"
|
|
|
|
#include "utilities/merge_operators.h"
|
|
|
|
|
|
|
|
namespace ROCKSDB_NAMESPACE {
|
|
|
|
namespace {
|
|
|
|
static std::unordered_map<std::string, OptionTypeInfo>
|
|
|
|
stringappend_merge_type_info = {
|
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
{"delimiter",
|
|
|
|
{0, OptionType::kString, OptionVerificationType::kNormal,
|
|
|
|
OptionTypeFlags::kNone}},
|
|
|
|
#endif // ROCKSDB_LITE
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
// Constructor: also specify the delimiter character.
|
|
|
|
StringAppendOperator::StringAppendOperator(char delim_char)
|
|
|
|
: delim_(1, delim_char) {
|
|
|
|
RegisterOptions("Delimiter", &delim_, &stringappend_merge_type_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
StringAppendOperator::StringAppendOperator(const std::string& delim)
|
|
|
|
: delim_(delim) {
|
|
|
|
RegisterOptions("Delimiter", &delim_, &stringappend_merge_type_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implementation for the merge operation (concatenates two strings)
|
|
|
|
bool StringAppendOperator::Merge(const Slice& /*key*/,
|
|
|
|
const Slice* existing_value,
|
|
|
|
const Slice& value, std::string* new_value,
|
|
|
|
Logger* /*logger*/) const {
|
|
|
|
// Clear the *new_value for writing.
|
|
|
|
assert(new_value);
|
|
|
|
new_value->clear();
|
|
|
|
|
|
|
|
if (!existing_value) {
|
|
|
|
// No existing_value. Set *new_value = value
|
|
|
|
new_value->assign(value.data(),value.size());
|
|
|
|
} else {
|
|
|
|
// Generic append (existing_value != null).
|
|
|
|
// Reserve *new_value to correct size, and apply concatenation.
|
|
|
|
new_value->reserve(existing_value->size() + delim_.size() + value.size());
|
|
|
|
new_value->assign(existing_value->data(), existing_value->size());
|
|
|
|
new_value->append(delim_);
|
|
|
|
new_value->append(value.data(), value.size());
|
|
|
|
}
|
[RocksDB] [MergeOperator] The new Merge Interface! Uses merge sequences.
Summary:
Here are the major changes to the Merge Interface. It has been expanded
to handle cases where the MergeOperator is not associative. It does so by stacking
up merge operations while scanning through the key history (i.e.: during Get() or
Compaction), until a valid Put/Delete/end-of-history is encountered; it then
applies all of the merge operations in the correct sequence starting with the
base/sentinel value.
I have also introduced an "AssociativeMerge" function which allows the user to
take advantage of associative merge operations (such as in the case of counters).
The implementation will always attempt to merge the operations/operands themselves
together when they are encountered, and will resort to the "stacking" method if
and only if the "associative-merge" fails.
This implementation is conjectured to allow MergeOperator to handle the general
case, while still providing the user with the ability to take advantage of certain
efficiencies in their own merge-operator / data-structure.
NOTE: This is a preliminary diff. This must still go through a lot of review,
revision, and testing. Feedback welcome!
Test Plan:
-This is a preliminary diff. I have only just begun testing/debugging it.
-I will be testing this with the existing MergeOperator use-cases and unit-tests
(counters, string-append, and redis-lists)
-I will be "desk-checking" and walking through the code with the help gdb.
-I will find a way of stress-testing the new interface / implementation using
db_bench, db_test, merge_test, and/or db_stress.
-I will ensure that my tests cover all cases: Get-Memtable,
Get-Immutable-Memtable, Get-from-Disk, Iterator-Range-Scan, Flush-Memtable-to-L0,
Compaction-L0-L1, Compaction-Ln-L(n+1), Put/Delete found, Put/Delete not-found,
end-of-history, end-of-file, etc.
-A lot of feedback from the reviewers.
Reviewers: haobo, dhruba, zshao, emayanke
Reviewed By: haobo
CC: leveldb
Differential Revision: https://reviews.facebook.net/D11499
11 years ago
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
Benchmarking for Merge Operator
Summary:
Updated db_bench and utilities/merge_operators.h to allow for dynamic benchmarking
of merge operators in db_bench. Added a new test (--benchmarks=mergerandom), which performs
a bunch of random Merge() operations over random keys. Also added a "--merge_operator=" flag
so that the tester can easily benchmark different merge operators. Currently supports
the PutOperator and UInt64Add operator. Support for stringappend or list append may come later.
Test Plan:
1. make db_bench
2. Test the PutOperator (simulating Put) as follows:
./db_bench --benchmarks=fillrandom,readrandom,updaterandom,readrandom,mergerandom,readrandom --merge_operator=put
--threads=2
3. Test the UInt64AddOperator (simulating numeric addition) similarly:
./db_bench --value_size=8 --benchmarks=fillrandom,readrandom,updaterandom,readrandom,mergerandom,readrandom
--merge_operator=uint64add --threads=2
Reviewers: haobo, dhruba, zshao, MarkCallaghan
Reviewed By: haobo
CC: leveldb
Differential Revision: https://reviews.facebook.net/D11535
11 years ago
|
|
|
std::shared_ptr<MergeOperator> MergeOperators::CreateStringAppendOperator() {
|
|
|
|
return std::make_shared<StringAppendOperator>(',');
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<MergeOperator> MergeOperators::CreateStringAppendOperator(char delim_char) {
|
|
|
|
return std::make_shared<StringAppendOperator>(delim_char);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<MergeOperator> MergeOperators::CreateStringAppendOperator(
|
|
|
|
const std::string& delim) {
|
|
|
|
return std::make_shared<StringAppendOperator>(delim);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace ROCKSDB_NAMESPACE
|