merging_iterator.h 2.1 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. //
  6. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  7. // Use of this source code is governed by a BSD-style license that can be
  8. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  9. #pragma once
  10. #include "db/dbformat.h"
  11. #include "rocksdb/types.h"
  12. namespace ROCKSDB_NAMESPACE {
  13. class Comparator;
  14. class Env;
  15. class Arena;
  16. template <class TValue>
  17. class InternalIteratorBase;
  18. using InternalIterator = InternalIteratorBase<Slice>;
  19. // Return an iterator that provided the union of the data in
  20. // children[0,n-1]. Takes ownership of the child iterators and
  21. // will delete them when the result iterator is deleted.
  22. //
  23. // The result does no duplicate suppression. I.e., if a particular
  24. // key is present in K child iterators, it will be yielded K times.
  25. //
  26. // REQUIRES: n >= 0
  27. extern InternalIterator* NewMergingIterator(
  28. const InternalKeyComparator* comparator, InternalIterator** children, int n,
  29. Arena* arena = nullptr, bool prefix_seek_mode = false);
  30. class MergingIterator;
  31. // A builder class to build a merging iterator by adding iterators one by one.
  32. class MergeIteratorBuilder {
  33. public:
  34. // comparator: the comparator used in merging comparator
  35. // arena: where the merging iterator needs to be allocated from.
  36. explicit MergeIteratorBuilder(const InternalKeyComparator* comparator,
  37. Arena* arena, bool prefix_seek_mode = false);
  38. ~MergeIteratorBuilder();
  39. // Add iter to the merging iterator.
  40. void AddIterator(InternalIterator* iter);
  41. // Get arena used to build the merging iterator. It is called one a child
  42. // iterator needs to be allocated.
  43. Arena* GetArena() { return arena; }
  44. // Return the result merging iterator.
  45. InternalIterator* Finish();
  46. private:
  47. MergingIterator* merge_iter;
  48. InternalIterator* first_iter;
  49. bool use_merging_iter;
  50. Arena* arena;
  51. };
  52. } // namespace ROCKSDB_NAMESPACE