max.cc 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. using ROCKSDB_NAMESPACE::Logger;
  10. using ROCKSDB_NAMESPACE::MergeOperator;
  11. using ROCKSDB_NAMESPACE::Slice;
  12. namespace { // anonymous namespace
  13. // Merge operator that picks the maximum operand, Comparison is based on
  14. // Slice::compare
  15. class MaxOperator : public MergeOperator {
  16. public:
  17. bool FullMergeV2(const MergeOperationInput& merge_in,
  18. MergeOperationOutput* merge_out) const override {
  19. Slice& max = merge_out->existing_operand;
  20. if (merge_in.existing_value) {
  21. max = Slice(merge_in.existing_value->data(),
  22. merge_in.existing_value->size());
  23. } else if (max.data() == nullptr) {
  24. max = Slice();
  25. }
  26. for (const auto& op : merge_in.operand_list) {
  27. if (max.compare(op) < 0) {
  28. max = op;
  29. }
  30. }
  31. return true;
  32. }
  33. bool PartialMerge(const Slice& /*key*/, const Slice& left_operand,
  34. const Slice& right_operand, std::string* new_value,
  35. Logger* /*logger*/) const override {
  36. if (left_operand.compare(right_operand) >= 0) {
  37. new_value->assign(left_operand.data(), left_operand.size());
  38. } else {
  39. new_value->assign(right_operand.data(), right_operand.size());
  40. }
  41. return true;
  42. }
  43. bool PartialMergeMulti(const Slice& /*key*/,
  44. const std::deque<Slice>& operand_list,
  45. std::string* new_value,
  46. Logger* /*logger*/) const override {
  47. Slice max;
  48. for (const auto& operand : operand_list) {
  49. if (max.compare(operand) < 0) {
  50. max = operand;
  51. }
  52. }
  53. new_value->assign(max.data(), max.size());
  54. return true;
  55. }
  56. const char* Name() const override { return "MaxOperator"; }
  57. };
  58. } // end of anonymous namespace
  59. namespace ROCKSDB_NAMESPACE {
  60. std::shared_ptr<MergeOperator> MergeOperators::CreateMaxOperator() {
  61. return std::make_shared<MaxOperator>();
  62. }
  63. } // namespace ROCKSDB_NAMESPACE