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