aboutsummaryrefslogtreecommitdiff
path: root/icing/index/main/doc-hit-info-iterator-term-main.cc
blob: 8f0d3f53b3e138dc54cbbf25b0e7d7fb1b5d20e5 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// 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.

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

#include <cstdint>
#include <memory>

#include "icing/text_classifier/lib3/utils/base/status.h"
#include "icing/absl_ports/canonical_errors.h"
#include "icing/absl_ports/str_cat.h"
#include "icing/file/posting_list/posting-list-identifier.h"
#include "icing/index/hit/doc-hit-info.h"
#include "icing/index/main/posting-list-hit-accessor.h"
#include "icing/legacy/core/icing-string-util.h"
#include "icing/schema/section.h"
#include "icing/store/document-id.h"
#include "icing/util/logging.h"
#include "icing/util/status-macros.h"

namespace icing {
namespace lib {

namespace {

std::string SectionIdMaskToString(SectionIdMask section_id_mask) {
  std::string mask(kTotalNumSections, '0');
  for (SectionId i = kMaxSectionId; i >= 0; --i) {
    if (section_id_mask & (UINT64_C(1) << i)) {
      mask[kMaxSectionId - i] = '1';
    }
  }
  return mask;
}

}  // namespace

libtextclassifier3::Status DocHitInfoIteratorTermMain::Advance() {
  ++cached_doc_hit_infos_idx_;
  while (posting_list_accessor_ == nullptr ||
         (!all_pages_consumed_ && cached_doc_hit_info_count() == 1)) {
    // If we haven't retrieved any hits before or we've already returned all but
    // the last cached hit, then go get some more!
    // We hold back the last cached hit because it could have more hits on the
    // next posting list in the chain.
    libtextclassifier3::Status status = RetrieveMoreHits();
    if (!status.ok()) {
      if (!absl_ports::IsNotFound(status)) {
        // NOT_FOUND is expected to happen (not every term will be in the main
        // index!). Other errors are worth logging.
        ICING_LOG(ERROR)
            << "Encountered unexpected failure while retrieving  hits "
            << status.error_message();
      }
      return absl_ports::ResourceExhaustedError(
          "No more DocHitInfos in iterator");
    }
  }
  if (cached_doc_hit_infos_idx_ == -1 ||
      cached_doc_hit_infos_idx_ >= cached_doc_hit_infos_.size()) {
    // Nothing more for the iterator to return. Set these members to invalid
    // values.
    doc_hit_info_ = DocHitInfo();
    hit_intersect_section_ids_mask_ = kSectionIdMaskNone;
    return absl_ports::ResourceExhaustedError(
        "No more DocHitInfos in iterator");
  }
  doc_hit_info_ = cached_doc_hit_infos_.at(cached_doc_hit_infos_idx_);
  hit_intersect_section_ids_mask_ = doc_hit_info_.hit_section_ids_mask();
  return libtextclassifier3::Status::OK;
}

libtextclassifier3::StatusOr<DocHitInfoIterator::TrimmedNode>
DocHitInfoIteratorTermMain::TrimRightMostNode() && {
  // Leaf iterator should trim itself.
  DocHitInfoIterator::TrimmedNode node = {nullptr, term_, term_start_index_,
                                          unnormalized_term_length_};
  return node;
}

libtextclassifier3::Status DocHitInfoIteratorTermMainExact::RetrieveMoreHits() {
  DocHitInfo last_doc_hit_info;
  if (!cached_doc_hit_infos_.empty()) {
    last_doc_hit_info = cached_doc_hit_infos_.back();
  }
  cached_doc_hit_infos_idx_ = 0;
  cached_doc_hit_infos_.clear();
  if (last_doc_hit_info.document_id() != kInvalidDocumentId) {
    // Carry over the last hit. It might need to be merged with the first hit of
    // of the next posting list in the chain.
    cached_doc_hit_infos_.push_back(last_doc_hit_info);
  }
  if (posting_list_accessor_ == nullptr) {
    ICING_ASSIGN_OR_RETURN(posting_list_accessor_,
                           main_index_->GetAccessorForExactTerm(term_));
  }

  ICING_ASSIGN_OR_RETURN(std::vector<Hit> hits,
                         posting_list_accessor_->GetNextHitsBatch());
  if (hits.empty()) {
    all_pages_consumed_ = true;
  }
  ++num_blocks_inspected_;
  cached_doc_hit_infos_.reserve(hits.size() + 1);
  cached_hit_term_frequency_.reserve(hits.size() + 1);
  for (const Hit& hit : hits) {
    // Check sections.
    if (((UINT64_C(1) << hit.section_id()) & section_restrict_mask_) == 0) {
      continue;
    }
    // We want exact hits, skip prefix-only hits.
    if (hit.is_prefix_hit()) {
      continue;
    }
    if (cached_doc_hit_infos_.empty() ||
        hit.document_id() != cached_doc_hit_infos_.back().document_id()) {
      cached_doc_hit_infos_.push_back(DocHitInfo(hit.document_id()));
      cached_hit_term_frequency_.push_back(Hit::TermFrequencyArray());
    }
    cached_doc_hit_infos_.back().UpdateSection(hit.section_id());
    cached_hit_term_frequency_.back()[hit.section_id()] = hit.term_frequency();
  }
  return libtextclassifier3::Status::OK;
}

std::string DocHitInfoIteratorTermMainExact::ToString() const {
  return absl_ports::StrCat(SectionIdMaskToString(section_restrict_mask_), ":",
                            term_);
}

libtextclassifier3::Status
DocHitInfoIteratorTermMainPrefix::RetrieveMoreHits() {
  DocHitInfo last_doc_hit_info;
  if (!cached_doc_hit_infos_.empty()) {
    last_doc_hit_info = cached_doc_hit_infos_.back();
  }
  cached_doc_hit_infos_idx_ = 0;
  cached_doc_hit_infos_.clear();
  if (last_doc_hit_info.document_id() != kInvalidDocumentId) {
    // Carry over the last hit. It might need to be merged with the first hit of
    // of the next posting list in the chain.
    cached_doc_hit_infos_.push_back(last_doc_hit_info);
  }

  ++num_blocks_inspected_;
  if (posting_list_accessor_ == nullptr) {
    ICING_ASSIGN_OR_RETURN(MainIndex::GetPrefixAccessorResult result,
                           main_index_->GetAccessorForPrefixTerm(term_));
    posting_list_accessor_ = std::move(result.accessor);
    exact_ = result.exact;
  }
  ICING_ASSIGN_OR_RETURN(std::vector<Hit> hits,
                         posting_list_accessor_->GetNextHitsBatch());
  if (hits.empty()) {
    all_pages_consumed_ = true;
  }
  cached_doc_hit_infos_.reserve(hits.size());
  if (need_hit_term_frequency_) {
    cached_hit_term_frequency_.reserve(hits.size());
  }
  for (const Hit& hit : hits) {
    // Check sections.
    if (((UINT64_C(1) << hit.section_id()) & section_restrict_mask_) == 0) {
      continue;
    }
    // If we only want hits from prefix sections.
    if (!exact_ && !hit.is_in_prefix_section()) {
      continue;
    }
    if (cached_doc_hit_infos_.empty() ||
        hit.document_id() != cached_doc_hit_infos_.back().document_id()) {
      cached_doc_hit_infos_.push_back(DocHitInfo(hit.document_id()));
      if (need_hit_term_frequency_) {
        cached_hit_term_frequency_.push_back(Hit::TermFrequencyArray());
      }
    }
    cached_doc_hit_infos_.back().UpdateSection(hit.section_id());
    if (need_hit_term_frequency_) {
      cached_hit_term_frequency_.back()[hit.section_id()] =
          hit.term_frequency();
    }
  }
  return libtextclassifier3::Status::OK;
}

std::string DocHitInfoIteratorTermMainPrefix::ToString() const {
  return absl_ports::StrCat(SectionIdMaskToString(section_restrict_mask_), ":",
                            term_, "*");
}

}  // namespace lib
}  // namespace icing