merge_context.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. //
  6. #pragma once
  7. #include <algorithm>
  8. #include <memory>
  9. #include <string>
  10. #include <vector>
  11. #include "rocksdb/db.h"
  12. #include "rocksdb/slice.h"
  13. namespace ROCKSDB_NAMESPACE {
  14. const std::vector<Slice> empty_operand_list;
  15. // The merge context for merging a user key.
  16. // When doing a Get(), DB will create such a class and pass it when
  17. // issuing Get() operation to memtables and version_set. The operands
  18. // will be fetched from the context when issuing partial of full merge.
  19. class MergeContext {
  20. public:
  21. GetMergeOperandsOptions* get_merge_operands_options = nullptr;
  22. // Clear all the operands
  23. void Clear() {
  24. if (operand_list_) {
  25. operand_list_->clear();
  26. copied_operands_->clear();
  27. }
  28. }
  29. // Push a merge operand
  30. void PushOperand(const Slice& operand_slice, bool operand_pinned = false) {
  31. Initialize();
  32. SetDirectionBackward();
  33. if (operand_pinned) {
  34. operand_list_->push_back(operand_slice);
  35. } else {
  36. // We need to have our own copy of the operand since it's not pinned
  37. copied_operands_->emplace_back(
  38. new std::string(operand_slice.data(), operand_slice.size()));
  39. operand_list_->push_back(*copied_operands_->back());
  40. }
  41. }
  42. // Push back a merge operand
  43. void PushOperandBack(const Slice& operand_slice,
  44. bool operand_pinned = false) {
  45. Initialize();
  46. SetDirectionForward();
  47. if (operand_pinned) {
  48. operand_list_->push_back(operand_slice);
  49. } else {
  50. // We need to have our own copy of the operand since it's not pinned
  51. copied_operands_->emplace_back(
  52. new std::string(operand_slice.data(), operand_slice.size()));
  53. operand_list_->push_back(*copied_operands_->back());
  54. }
  55. }
  56. // return total number of operands in the list
  57. size_t GetNumOperands() const {
  58. if (!operand_list_) {
  59. return 0;
  60. }
  61. return operand_list_->size();
  62. }
  63. // Get the operand at the index.
  64. Slice GetOperand(int index) const {
  65. assert(operand_list_);
  66. SetDirectionForward();
  67. return (*operand_list_)[index];
  68. }
  69. // Same as GetOperandsDirectionForward
  70. //
  71. // Note that the returned reference is only good until another call
  72. // to this MergeContext. If the returned value is needed for longer,
  73. // a copy must be made.
  74. const std::vector<Slice>& GetOperands() const {
  75. return GetOperandsDirectionForward();
  76. }
  77. // Return all the operands in the order as they were merged (passed to
  78. // FullMerge or FullMergeV2)
  79. //
  80. // Note that the returned reference is only good until another call
  81. // to this MergeContext. If the returned value is needed for longer,
  82. // a copy must be made.
  83. const std::vector<Slice>& GetOperandsDirectionForward() const {
  84. if (!operand_list_) {
  85. return empty_operand_list;
  86. }
  87. SetDirectionForward();
  88. return *operand_list_;
  89. }
  90. // Return all the operands in the reversed order relative to how they were
  91. // merged (passed to FullMerge or FullMergeV2)
  92. //
  93. // Note that the returned reference is only good until another call
  94. // to this MergeContext. If the returned value is needed for longer,
  95. // a copy must be made.
  96. const std::vector<Slice>& GetOperandsDirectionBackward() const {
  97. if (!operand_list_) {
  98. return empty_operand_list;
  99. }
  100. SetDirectionBackward();
  101. return *operand_list_;
  102. }
  103. private:
  104. void Initialize() {
  105. if (!operand_list_) {
  106. operand_list_.reset(new std::vector<Slice>());
  107. copied_operands_.reset(new std::vector<std::unique_ptr<std::string>>());
  108. }
  109. }
  110. void SetDirectionForward() const {
  111. if (operands_reversed_ == true) {
  112. std::reverse(operand_list_->begin(), operand_list_->end());
  113. operands_reversed_ = false;
  114. }
  115. }
  116. void SetDirectionBackward() const {
  117. if (operands_reversed_ == false) {
  118. std::reverse(operand_list_->begin(), operand_list_->end());
  119. operands_reversed_ = true;
  120. }
  121. }
  122. // List of operands, the order of operands depends on operands_reversed_.
  123. mutable std::unique_ptr<std::vector<Slice>> operand_list_;
  124. // Copy of operands that are not pinned.
  125. std::unique_ptr<std::vector<std::unique_ptr<std::string>>> copied_operands_;
  126. // Reversed means the newest update is ordered first.
  127. mutable bool operands_reversed_ = true;
  128. };
  129. } // namespace ROCKSDB_NAMESPACE