#include "stringappend2.h"
#include <memory>
#include <assert.h>
#include "rocksdb/slice.h"
#include "rocksdb/merge_operator.h"
#include "utilities/merge_operators.h"
namespace
rocksdb {
StringAppendTESTOperator::StringAppendTESTOperator(
char
delim_char)
: delim_(delim_char) {
}
bool
StringAppendTESTOperator::FullMerge(
const
Slice& key,
const
Slice* existing_value,
const
std::deque<std::string>& operands,
std::string* new_value,
Logger* logger)
const
{
assert
(new_value);
new_value->clear();
int
numBytes = 0;
for
(
auto
it = operands.begin(); it != operands.end(); ++it) {
numBytes += it->size() + 1;
}
bool
printDelim =
false
;
if
(existing_value) {
new_value->reserve(numBytes + existing_value->size());
new_value->append(existing_value->data(), existing_value->size());
printDelim =
true
;
}
else
if
(numBytes) {
new_value->reserve(numBytes-1);
}
for
(
auto
it = operands.begin(); it != operands.end(); ++it) {
if
(printDelim) {
new_value->append(1,delim_);
}
new_value->append(*it);
printDelim =
true
;
}
return
true
;
}
bool
StringAppendTESTOperator::PartialMerge(
const
Slice& key,
const
Slice& left_operand,
const
Slice& right_operand,
std::string* new_value,
Logger* logger)
const
{
return
false
;
}
bool
StringAppendTESTOperator::_AssocPartialMerge(
const
Slice& key,
const
Slice& left_operand,
const
Slice& right_operand,
std::string* new_value,
Logger* logger)
const
{
assert
(new_value);
new_value->clear();
new_value->reserve(left_operand.size() + 1 + right_operand.size());
new_value->assign(left_operand.data(), left_operand.size());
new_value->append(1,delim_);
new_value->append(right_operand.data(), right_operand.size());
return
true
;
}
const
char
* StringAppendTESTOperator::Name()
const
{
return
"StringAppendTESTOperator"
;
}
std::shared_ptr<MergeOperator>
MergeOperators::CreateStringAppendTESTOperator() {
return
std::make_shared<StringAppendTESTOperator>(
','
);
}
}