put.cc 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright (c) 2011-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 <memory>
  6. #include "rocksdb/slice.h"
  7. #include "rocksdb/merge_operator.h"
  8. #include "utilities/merge_operators.h"
  9. using namespace ROCKSDB_NAMESPACE;
  10. namespace { // anonymous namespace
  11. // A merge operator that mimics Put semantics
  12. // Since this merge-operator will not be used in production,
  13. // it is implemented as a non-associative merge operator to illustrate the
  14. // new interface and for testing purposes. (That is, we inherit from
  15. // the MergeOperator class rather than the AssociativeMergeOperator
  16. // which would be simpler in this case).
  17. //
  18. // From the client-perspective, semantics are the same.
  19. class PutOperator : public MergeOperator {
  20. public:
  21. bool FullMerge(const Slice& /*key*/, const Slice* /*existing_value*/,
  22. const std::deque<std::string>& operand_sequence,
  23. std::string* new_value, Logger* /*logger*/) const override {
  24. // Put basically only looks at the current/latest value
  25. assert(!operand_sequence.empty());
  26. assert(new_value != nullptr);
  27. new_value->assign(operand_sequence.back());
  28. return true;
  29. }
  30. bool PartialMerge(const Slice& /*key*/, const Slice& /*left_operand*/,
  31. const Slice& right_operand, std::string* new_value,
  32. Logger* /*logger*/) const override {
  33. new_value->assign(right_operand.data(), right_operand.size());
  34. return true;
  35. }
  36. using MergeOperator::PartialMergeMulti;
  37. bool PartialMergeMulti(const Slice& /*key*/,
  38. const std::deque<Slice>& operand_list,
  39. std::string* new_value,
  40. Logger* /*logger*/) const override {
  41. new_value->assign(operand_list.back().data(), operand_list.back().size());
  42. return true;
  43. }
  44. const char* Name() const override { return "PutOperator"; }
  45. };
  46. class PutOperatorV2 : public PutOperator {
  47. bool FullMerge(const Slice& /*key*/, const Slice* /*existing_value*/,
  48. const std::deque<std::string>& /*operand_sequence*/,
  49. std::string* /*new_value*/,
  50. Logger* /*logger*/) const override {
  51. assert(false);
  52. return false;
  53. }
  54. bool FullMergeV2(const MergeOperationInput& merge_in,
  55. MergeOperationOutput* merge_out) const override {
  56. // Put basically only looks at the current/latest value
  57. assert(!merge_in.operand_list.empty());
  58. merge_out->existing_operand = merge_in.operand_list.back();
  59. return true;
  60. }
  61. };
  62. } // end of anonymous namespace
  63. namespace ROCKSDB_NAMESPACE {
  64. std::shared_ptr<MergeOperator> MergeOperators::CreateDeprecatedPutOperator() {
  65. return std::make_shared<PutOperator>();
  66. }
  67. std::shared_ptr<MergeOperator> MergeOperators::CreatePutOperator() {
  68. return std::make_shared<PutOperatorV2>();
  69. }
  70. } // namespace ROCKSDB_NAMESPACE