max.cc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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/max_operator.h"
  10. namespace ROCKSDB_NAMESPACE {
  11. bool MaxOperator::FullMergeV2(const MergeOperationInput& merge_in,
  12. MergeOperationOutput* merge_out) const {
  13. Slice& max = merge_out->existing_operand;
  14. if (merge_in.existing_value) {
  15. max =
  16. Slice(merge_in.existing_value->data(), merge_in.existing_value->size());
  17. } else if (max.data() == nullptr) {
  18. max = Slice();
  19. }
  20. for (const auto& op : merge_in.operand_list) {
  21. if (max.compare(op) < 0) {
  22. max = op;
  23. }
  24. }
  25. return true;
  26. }
  27. bool MaxOperator::PartialMerge(const Slice& /*key*/, const Slice& left_operand,
  28. const Slice& right_operand,
  29. std::string* new_value,
  30. Logger* /*logger*/) const {
  31. if (left_operand.compare(right_operand) >= 0) {
  32. new_value->assign(left_operand.data(), left_operand.size());
  33. } else {
  34. new_value->assign(right_operand.data(), right_operand.size());
  35. }
  36. return true;
  37. }
  38. bool MaxOperator::PartialMergeMulti(const Slice& /*key*/,
  39. const std::deque<Slice>& operand_list,
  40. std::string* new_value,
  41. Logger* /*logger*/) const {
  42. Slice max;
  43. for (const auto& operand : operand_list) {
  44. if (max.compare(operand) < 0) {
  45. max = operand;
  46. }
  47. }
  48. new_value->assign(max.data(), max.size());
  49. return true;
  50. }
  51. std::shared_ptr<MergeOperator> MergeOperators::CreateMaxOperator() {
  52. return std::make_shared<MaxOperator>();
  53. }
  54. } // namespace ROCKSDB_NAMESPACE