put.cc 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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/merge_operator.h"
  7. #include "rocksdb/slice.h"
  8. #include "utilities/merge_operators.h"
  9. #include "utilities/merge_operators/put_operator.h"
  10. namespace ROCKSDB_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. bool PutOperator::FullMerge(const Slice& /*key*/,
  20. const Slice* /*existing_value*/,
  21. const std::deque<std::string>& operand_sequence,
  22. std::string* new_value, Logger* /*logger*/) const {
  23. // Put basically only looks at the current/latest value
  24. assert(!operand_sequence.empty());
  25. assert(new_value != nullptr);
  26. new_value->assign(operand_sequence.back());
  27. return true;
  28. }
  29. bool PutOperator::PartialMerge(const Slice& /*key*/,
  30. const Slice& /*left_operand*/,
  31. const Slice& right_operand,
  32. std::string* new_value,
  33. Logger* /*logger*/) const {
  34. new_value->assign(right_operand.data(), right_operand.size());
  35. return true;
  36. }
  37. bool PutOperator::PartialMergeMulti(const Slice& /*key*/,
  38. const std::deque<Slice>& operand_list,
  39. std::string* new_value,
  40. Logger* /*logger*/) const {
  41. new_value->assign(operand_list.back().data(), operand_list.back().size());
  42. return true;
  43. }
  44. bool PutOperatorV2::FullMerge(
  45. const Slice& /*key*/, const Slice* /*existing_value*/,
  46. const std::deque<std::string>& /*operand_sequence*/,
  47. std::string* /*new_value*/, Logger* /*logger*/) const {
  48. assert(false);
  49. return false;
  50. }
  51. bool PutOperatorV2::FullMergeV2(const MergeOperationInput& merge_in,
  52. MergeOperationOutput* merge_out) const {
  53. // Put basically only looks at the current/latest value
  54. assert(!merge_in.operand_list.empty());
  55. merge_out->existing_operand = merge_in.operand_list.back();
  56. return true;
  57. }
  58. std::shared_ptr<MergeOperator> MergeOperators::CreateDeprecatedPutOperator() {
  59. return std::make_shared<PutOperator>();
  60. }
  61. std::shared_ptr<MergeOperator> MergeOperators::CreatePutOperator() {
  62. return std::make_shared<PutOperatorV2>();
  63. }
  64. } // namespace ROCKSDB_NAMESPACE