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