xref: /aosp_15_r20/external/icing/icing/index/numeric/doc-hit-info-iterator-numeric.h (revision 8b6cd535a057e39b3b86660c4aa06c99747c2136)
1 // Copyright (C) 2022 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef ICING_INDEX_NUMERIC_DOC_HIT_INFO_ITERATOR_NUMERIC_H_
16 #define ICING_INDEX_NUMERIC_DOC_HIT_INFO_ITERATOR_NUMERIC_H_
17 
18 #include <memory>
19 #include <string>
20 #include <vector>
21 
22 #include "icing/text_classifier/lib3/utils/base/status.h"
23 #include "icing/absl_ports/canonical_errors.h"
24 #include "icing/index/iterator/doc-hit-info-iterator.h"
25 #include "icing/index/numeric/numeric-index.h"
26 #include "icing/util/status-macros.h"
27 
28 namespace icing {
29 namespace lib {
30 
31 template <typename T>
32 class DocHitInfoIteratorNumeric : public DocHitInfoLeafIterator {
33  public:
DocHitInfoIteratorNumeric(std::unique_ptr<typename NumericIndex<T>::Iterator> numeric_index_iter)34   explicit DocHitInfoIteratorNumeric(
35       std::unique_ptr<typename NumericIndex<T>::Iterator> numeric_index_iter)
36       : numeric_index_iter_(std::move(numeric_index_iter)) {}
37 
Advance()38   libtextclassifier3::Status Advance() override {
39     // If the query property path doesn't exist (i.e. the storage doesn't
40     // exist), then numeric_index_iter_ will be nullptr.
41     if (numeric_index_iter_ == nullptr) {
42       return absl_ports::ResourceExhaustedError("End of iterator");
43     }
44 
45     ICING_RETURN_IF_ERROR(numeric_index_iter_->Advance());
46 
47     doc_hit_info_ = numeric_index_iter_->GetDocHitInfo();
48     return libtextclassifier3::Status::OK;
49   }
50 
TrimRightMostNode()51   libtextclassifier3::StatusOr<TrimmedNode> TrimRightMostNode() && override {
52     return absl_ports::InvalidArgumentError(
53         "Cannot generate suggestion if the last term is numeric operator.");
54   }
55 
GetCallStats()56   CallStats GetCallStats() const override {
57     if (numeric_index_iter_ == nullptr) {
58       return CallStats();
59     }
60 
61     return CallStats(/*num_leaf_advance_calls_lite_index_in=*/0,
62                      /*num_leaf_advance_calls_main_index_in=*/0,
63                      /*num_leaf_advance_calls_integer_index_in=*/
64                      numeric_index_iter_->GetNumAdvanceCalls(),
65                      /*num_leaf_advance_calls_no_index_in=*/0,
66                      /*num_blocks_inspected_in=*/
67                      numeric_index_iter_->GetNumBlocksInspected());
68   }
69 
ToString()70   std::string ToString() const override { return "test"; }
71 
72   void PopulateMatchedTermsStats(
73       std::vector<TermMatchInfo>* matched_terms_stats,
74       SectionIdMask filtering_section_mask = kSectionIdMaskAll) const override {
75     // For numeric hit iterator, this should do nothing since there is no term.
76   }
77 
78  private:
79   std::unique_ptr<typename NumericIndex<T>::Iterator> numeric_index_iter_;
80 };
81 
82 }  // namespace lib
83 }  // namespace icing
84 
85 #endif  // ICING_INDEX_NUMERIC_DOC_HIT_INFO_ITERATOR_NUMERIC_H_
86