aboutsummaryrefslogtreecommitdiff
path: root/icing/index/iterator/doc-hit-info-iterator-or.h
blob: 1e9847db80fa31cb54842339ad52f64b13805b87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright (C) 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef ICING_INDEX_ITERATOR_DOC_HIT_INFO_ITERATOR_OR_H_
#define ICING_INDEX_ITERATOR_DOC_HIT_INFO_ITERATOR_OR_H_

#include <cstdint>
#include <string>

#include "icing/index/iterator/doc-hit-info-iterator.h"

namespace icing {
namespace lib {

// Given n iterators, will decide what the fastest Or-iterator implementation
// will be.
std::unique_ptr<DocHitInfoIterator> CreateOrIterator(
    std::vector<std::unique_ptr<DocHitInfoIterator>> iterators);

// Iterate over a logical OR of two child iterators.
class DocHitInfoIteratorOr : public DocHitInfoIterator {
 public:
  explicit DocHitInfoIteratorOr(std::unique_ptr<DocHitInfoIterator> left_it,
                                std::unique_ptr<DocHitInfoIterator> right_it);

  libtextclassifier3::StatusOr<TrimmedNode> TrimRightMostNode() && override;

  libtextclassifier3::Status Advance() override;

  int32_t GetNumBlocksInspected() const override;

  int32_t GetNumLeafAdvanceCalls() const override;

  std::string ToString() const override;

  void PopulateMatchedTermsStats(
      std::vector<TermMatchInfo> *matched_terms_stats,
      SectionIdMask filtering_section_mask = kSectionIdMaskAll) const override {
    if (doc_hit_info_.document_id() == kInvalidDocumentId) {
      // Current hit isn't valid, return.
      return;
    }
    current_->PopulateMatchedTermsStats(matched_terms_stats,
                                        filtering_section_mask);
    // If equal, then current_ == left_. Combine with results from right_.
    if (left_document_id_ == right_document_id_) {
      right_->PopulateMatchedTermsStats(matched_terms_stats,
                                        filtering_section_mask);
    }
  }

 private:
  std::unique_ptr<DocHitInfoIterator> left_;
  std::unique_ptr<DocHitInfoIterator> right_;
  // Pointer to the chosen iterator that points to the current doc_hit_info_. If
  // both left_ and right_ point to the same docid, then chosen_ == left.
  // chosen_ does not own the iterator it points to.
  DocHitInfoIterator *current_;
  DocumentId left_document_id_ = kMaxDocumentId;
  DocumentId right_document_id_ = kMaxDocumentId;
};

// Iterate over a logical OR of multiple child iterators.
//
// NOTE: DocHitInfoIteratorOr is a faster alternative to OR exactly 2 iterators.
class DocHitInfoIteratorOrNary : public DocHitInfoIterator {
 public:
  explicit DocHitInfoIteratorOrNary(
      std::vector<std::unique_ptr<DocHitInfoIterator>> iterators);

  libtextclassifier3::StatusOr<TrimmedNode> TrimRightMostNode() && override;

  libtextclassifier3::Status Advance() override;

  int32_t GetNumBlocksInspected() const override;

  int32_t GetNumLeafAdvanceCalls() const override;

  std::string ToString() const override;

  void PopulateMatchedTermsStats(
      std::vector<TermMatchInfo> *matched_terms_stats,
      SectionIdMask filtering_section_mask = kSectionIdMaskAll) const override {
    if (doc_hit_info_.document_id() == kInvalidDocumentId) {
      // Current hit isn't valid, return.
      return;
    }
    for (size_t i = 0; i < current_iterators_.size(); i++) {
      current_iterators_.at(i)->PopulateMatchedTermsStats(
          matched_terms_stats, filtering_section_mask);
    }
  }

 private:
  std::vector<std::unique_ptr<DocHitInfoIterator>> iterators_;
  // Pointers to the iterators that point to the current doc_hit_info_.
  // current_iterators_ does not own the iterators it points to.
  std::vector<DocHitInfoIterator *> current_iterators_;
};

}  // namespace lib
}  // namespace icing

#endif  // ICING_INDEX_ITERATOR_DOC_HIT_INFO_ITERATOR_OR_H_