merge_operator.cc 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright (c) 2017-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. #include "merge_operator.h"
  6. #include <cassert>
  7. #include <memory>
  8. #include "rocksdb/merge_operator.h"
  9. #include "rocksdb/slice.h"
  10. #include "rocksdb/utilities/options_type.h"
  11. #include "utilities/cassandra/format.h"
  12. #include "utilities/merge_operators.h"
  13. namespace ROCKSDB_NAMESPACE::cassandra {
  14. static std::unordered_map<std::string, OptionTypeInfo>
  15. merge_operator_options_info = {
  16. {"gc_grace_period_in_seconds",
  17. {offsetof(struct CassandraOptions, gc_grace_period_in_seconds),
  18. OptionType::kUInt32T, OptionVerificationType::kNormal,
  19. OptionTypeFlags::kNone}},
  20. {"operands_limit",
  21. {offsetof(struct CassandraOptions, operands_limit), OptionType::kSizeT,
  22. OptionVerificationType::kNormal, OptionTypeFlags::kNone}},
  23. };
  24. CassandraValueMergeOperator::CassandraValueMergeOperator(
  25. int32_t gc_grace_period_in_seconds, size_t operands_limit)
  26. : options_(gc_grace_period_in_seconds, operands_limit) {
  27. RegisterOptions(&options_, &merge_operator_options_info);
  28. }
  29. // Implementation for the merge operation (merges two Cassandra values)
  30. bool CassandraValueMergeOperator::FullMergeV2(
  31. const MergeOperationInput& merge_in,
  32. MergeOperationOutput* merge_out) const {
  33. // Clear the *new_value for writing.
  34. merge_out->new_value.clear();
  35. std::vector<RowValue> row_values;
  36. if (merge_in.existing_value) {
  37. row_values.push_back(RowValue::Deserialize(
  38. merge_in.existing_value->data(), merge_in.existing_value->size()));
  39. }
  40. for (auto& operand : merge_in.operand_list) {
  41. row_values.push_back(RowValue::Deserialize(operand.data(), operand.size()));
  42. }
  43. RowValue merged = RowValue::Merge(std::move(row_values));
  44. merged = merged.RemoveTombstones(options_.gc_grace_period_in_seconds);
  45. merge_out->new_value.reserve(merged.Size());
  46. merged.Serialize(&(merge_out->new_value));
  47. return true;
  48. }
  49. bool CassandraValueMergeOperator::PartialMergeMulti(
  50. const Slice& /*key*/, const std::deque<Slice>& operand_list,
  51. std::string* new_value, Logger* /*logger*/) const {
  52. // Clear the *new_value for writing.
  53. assert(new_value);
  54. new_value->clear();
  55. std::vector<RowValue> row_values;
  56. for (auto& operand : operand_list) {
  57. row_values.push_back(RowValue::Deserialize(operand.data(), operand.size()));
  58. }
  59. RowValue merged = RowValue::Merge(std::move(row_values));
  60. new_value->reserve(merged.Size());
  61. merged.Serialize(new_value);
  62. return true;
  63. }
  64. } // namespace ROCKSDB_NAMESPACE::cassandra