2017-07-28 20:55:19 +00:00
|
|
|
// Copyright (c) 2017-present, Facebook, Inc. All rights reserved.
|
2017-07-15 23:03:42 +00:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2017-06-16 21:12:52 +00:00
|
|
|
|
|
|
|
#include "merge_operator.h"
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
2021-08-06 15:26:23 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2017-06-16 21:12:52 +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"
|
2017-07-21 21:42:32 +00:00
|
|
|
#include "utilities/cassandra/format.h"
|
2021-08-06 15:26:23 +00:00
|
|
|
#include "utilities/merge_operators.h"
|
2017-06-16 21:12:52 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2017-06-16 21:12:52 +00:00
|
|
|
namespace cassandra {
|
2021-08-06 15:26:23 +00:00
|
|
|
static std::unordered_map<std::string, OptionTypeInfo>
|
|
|
|
merge_operator_options_info = {
|
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
{"gc_grace_period_in_seconds",
|
|
|
|
{offsetof(struct CassandraOptions, gc_grace_period_in_seconds),
|
|
|
|
OptionType::kUInt32T, OptionVerificationType::kNormal,
|
|
|
|
OptionTypeFlags::kNone}},
|
|
|
|
{"operands_limit",
|
|
|
|
{offsetof(struct CassandraOptions, operands_limit), OptionType::kSizeT,
|
|
|
|
OptionVerificationType::kNormal, OptionTypeFlags::kNone}},
|
|
|
|
#endif // ROCKSDB_LITE
|
|
|
|
};
|
|
|
|
|
|
|
|
CassandraValueMergeOperator::CassandraValueMergeOperator(
|
|
|
|
int32_t gc_grace_period_in_seconds, size_t operands_limit)
|
|
|
|
: options_(gc_grace_period_in_seconds, operands_limit) {
|
|
|
|
RegisterOptions(&options_, &merge_operator_options_info);
|
|
|
|
}
|
2017-06-16 21:12:52 +00:00
|
|
|
|
|
|
|
// Implementation for the merge operation (merges two Cassandra values)
|
|
|
|
bool CassandraValueMergeOperator::FullMergeV2(
|
|
|
|
const MergeOperationInput& merge_in,
|
|
|
|
MergeOperationOutput* merge_out) const {
|
|
|
|
// Clear the *new_value for writing.
|
|
|
|
merge_out->new_value.clear();
|
|
|
|
std::vector<RowValue> row_values;
|
|
|
|
if (merge_in.existing_value) {
|
|
|
|
row_values.push_back(
|
|
|
|
RowValue::Deserialize(merge_in.existing_value->data(),
|
|
|
|
merge_in.existing_value->size()));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& operand : merge_in.operand_list) {
|
|
|
|
row_values.push_back(RowValue::Deserialize(operand.data(), operand.size()));
|
|
|
|
}
|
|
|
|
|
|
|
|
RowValue merged = RowValue::Merge(std::move(row_values));
|
2021-08-06 15:26:23 +00:00
|
|
|
merged = merged.RemoveTombstones(options_.gc_grace_period_in_seconds);
|
2017-06-16 21:12:52 +00:00
|
|
|
merge_out->new_value.reserve(merged.Size());
|
|
|
|
merged.Serialize(&(merge_out->new_value));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CassandraValueMergeOperator::PartialMergeMulti(
|
2018-03-05 21:08:17 +00:00
|
|
|
const Slice& /*key*/, const std::deque<Slice>& operand_list,
|
|
|
|
std::string* new_value, Logger* /*logger*/) const {
|
2017-06-16 21:12:52 +00:00
|
|
|
// Clear the *new_value for writing.
|
|
|
|
assert(new_value);
|
|
|
|
new_value->clear();
|
|
|
|
|
|
|
|
std::vector<RowValue> row_values;
|
|
|
|
for (auto& operand : operand_list) {
|
|
|
|
row_values.push_back(RowValue::Deserialize(operand.data(), operand.size()));
|
|
|
|
}
|
|
|
|
RowValue merged = RowValue::Merge(std::move(row_values));
|
|
|
|
new_value->reserve(merged.Size());
|
|
|
|
merged.Serialize(new_value);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace cassandra
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|