2013-05-10 17:40:10 +00:00
|
|
|
/**
|
2013-10-05 05:32:05 +00:00
|
|
|
* A MergeOperator for rocksdb that implements string append.
|
2013-05-10 17:40:10 +00:00
|
|
|
* @author Deon Nicholas (dnicholas@fb.com)
|
|
|
|
* Copyright 2013 Facebook
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "stringappend.h"
|
|
|
|
|
|
|
|
#include <assert.h>
|
2013-06-11 18:19:49 +00:00
|
|
|
|
2021-08-06 15:26:23 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2013-08-23 15:38:13 +00:00
|
|
|
#include "rocksdb/merge_operator.h"
|
2021-08-06 15:26:23 +00:00
|
|
|
#include "rocksdb/slice.h"
|
|
|
|
#include "rocksdb/utilities/options_type.h"
|
2013-05-10 17:40:10 +00:00
|
|
|
#include "utilities/merge_operators.h"
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2021-08-06 15:26:23 +00:00
|
|
|
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
|
2013-06-14 23:44:39 +00:00
|
|
|
// Constructor: also specify the delimiter character.
|
2013-05-10 17:40:10 +00:00
|
|
|
StringAppendOperator::StringAppendOperator(char delim_char)
|
2021-08-06 15:26:23 +00:00
|
|
|
: delim_(1, delim_char) {
|
|
|
|
RegisterOptions("Delimiter", &delim_, &stringappend_merge_type_info);
|
|
|
|
}
|
2021-08-02 23:49:54 +00:00
|
|
|
|
|
|
|
StringAppendOperator::StringAppendOperator(const std::string& delim)
|
2021-08-06 15:26:23 +00:00
|
|
|
: delim_(delim) {
|
|
|
|
RegisterOptions("Delimiter", &delim_, &stringappend_merge_type_info);
|
|
|
|
}
|
2013-05-10 17:40:10 +00:00
|
|
|
|
|
|
|
// Implementation for the merge operation (concatenates two strings)
|
2018-03-05 21:08:17 +00:00
|
|
|
bool StringAppendOperator::Merge(const Slice& /*key*/,
|
2013-05-10 17:40:10 +00:00
|
|
|
const Slice* existing_value,
|
2018-03-05 21:08:17 +00:00
|
|
|
const Slice& value, std::string* new_value,
|
|
|
|
Logger* /*logger*/) const {
|
2013-05-10 17:40:10 +00:00
|
|
|
// 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.
|
2021-08-02 23:49:54 +00:00
|
|
|
new_value->reserve(existing_value->size() + delim_.size() + value.size());
|
|
|
|
new_value->assign(existing_value->data(), existing_value->size());
|
|
|
|
new_value->append(delim_);
|
2013-05-10 17:40:10 +00:00
|
|
|
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
2013-08-06 03:14:32 +00:00
|
|
|
|
|
|
|
return true;
|
2013-05-10 17:40:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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
2013-08-16 00:13:07 +00:00
|
|
|
std::shared_ptr<MergeOperator> MergeOperators::CreateStringAppendOperator() {
|
|
|
|
return std::make_shared<StringAppendOperator>(',');
|
|
|
|
}
|
|
|
|
|
2018-03-08 23:59:51 +00:00
|
|
|
std::shared_ptr<MergeOperator> MergeOperators::CreateStringAppendOperator(char delim_char) {
|
|
|
|
return std::make_shared<StringAppendOperator>(delim_char);
|
|
|
|
}
|
|
|
|
|
2021-08-02 23:49:54 +00:00
|
|
|
std::shared_ptr<MergeOperator> MergeOperators::CreateStringAppendOperator(
|
|
|
|
const std::string& delim) {
|
|
|
|
return std::make_shared<StringAppendOperator>(delim);
|
|
|
|
}
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|