|
| 1 | +/** |
| 2 | + * A MergeOperator for rocksdb that implements string append. |
| 3 | + * @author Deon Nicholas ([email protected]) |
| 4 | + * Copyright 2013 Facebook |
| 5 | + */ |
| 6 | + |
| 7 | +#include "stringappend.h" |
| 8 | + |
| 9 | +#include <memory> |
| 10 | +#include <assert.h> |
| 11 | + |
| 12 | +#include "rocksdb/slice.h" |
| 13 | +#include "rocksdb/merge_operator.h" |
| 14 | +#include "utilities/merge_operators.h" |
| 15 | + |
| 16 | +namespace rocksdb { |
| 17 | + |
| 18 | +// Constructor: also specify the delimiter character. |
| 19 | +StringAppendOperator::StringAppendOperator(char delim_char) |
| 20 | + : delim_(delim_char) { |
| 21 | +} |
| 22 | + |
| 23 | +// Implementation for the merge operation (concatenates two strings) |
| 24 | +bool StringAppendOperator::Merge(const Slice& key, |
| 25 | + const Slice* existing_value, |
| 26 | + const Slice& value, |
| 27 | + std::string* new_value, |
| 28 | + Logger* logger) const { |
| 29 | + |
| 30 | + // Clear the *new_value for writing. |
| 31 | + assert(new_value); |
| 32 | + new_value->clear(); |
| 33 | + |
| 34 | + if (!existing_value) { |
| 35 | + // No existing_value. Set *new_value = value |
| 36 | + new_value->assign(value.data(),value.size()); |
| 37 | + } else { |
| 38 | + // Generic append (existing_value != null). |
| 39 | + // Reserve *new_value to correct size, and apply concatenation. |
| 40 | + new_value->reserve(existing_value->size() + 1 + value.size()); |
| 41 | + new_value->assign(existing_value->data(),existing_value->size()); |
| 42 | + new_value->append(1,delim_); |
| 43 | + new_value->append(value.data(), value.size()); |
| 44 | + } |
| 45 | + |
| 46 | + return true; |
| 47 | +} |
| 48 | + |
| 49 | +const char* StringAppendOperator::Name() const { |
| 50 | + return "StringAppendOperator"; |
| 51 | +} |
| 52 | + |
| 53 | +std::shared_ptr<MergeOperator> MergeOperators::CreateStringAppendOperator() { |
| 54 | + return std::make_shared<StringAppendOperator>(','); |
| 55 | +} |
| 56 | + |
| 57 | +} // namespace rocksdb |
0 commit comments