xref: /aosp_15_r20/external/icing/icing/index/iterator/doc-hit-info-iterator-section-restrict.h (revision 8b6cd535a057e39b3b86660c4aa06c99747c2136)
1 // Copyright (C) 2019 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_ITERATOR_DOC_HIT_INFO_ITERATOR_SECTION_RESTRICT_H_
16 #define ICING_INDEX_ITERATOR_DOC_HIT_INFO_ITERATOR_SECTION_RESTRICT_H_
17 
18 #include <cstdint>
19 #include <memory>
20 #include <set>
21 #include <string>
22 #include <vector>
23 
24 #include "icing/text_classifier/lib3/utils/base/status.h"
25 #include "icing/text_classifier/lib3/utils/base/statusor.h"
26 #include "icing/index/iterator/doc-hit-info-iterator.h"
27 #include "icing/index/iterator/section-restrict-data.h"
28 #include "icing/proto/search.pb.h"
29 #include "icing/schema/schema-store.h"
30 #include "icing/schema/section.h"
31 #include "icing/store/document-id.h"
32 #include "icing/store/document-store.h"
33 
34 namespace icing {
35 namespace lib {
36 
37 // A iterator that helps filter for DocHitInfos whose term was in a section
38 // named target_section.
39 //
40 // NOTE: This is a little different from the DocHitInfoIteratorFilter class.
41 // That class is meant to be applied to the root of a query tree and filter over
42 // all results at the end. This class is more used in the limited scope of a
43 // term or a small group of terms.
44 class DocHitInfoIteratorSectionRestrict : public DocHitInfoLeafIterator {
45  public:
46   // Does not take any ownership, and all pointers must refer to valid objects
47   // that outlive the one constructed.
48   explicit DocHitInfoIteratorSectionRestrict(
49       std::unique_ptr<DocHitInfoIterator> delegate, SectionRestrictData* data);
50 
51   // Methods that apply section restrictions to all DocHitInfoLeafIterator nodes
52   // inside the provided iterator tree, and return the root of the tree
53   // afterwards. These methods do not take any ownership for the raw pointer
54   // parameters, which must refer to valid objects that outlive the iterator
55   // returned.
56   static std::unique_ptr<DocHitInfoIterator> ApplyRestrictions(
57       std::unique_ptr<DocHitInfoIterator> iterator,
58       const DocumentStore* document_store, const SchemaStore* schema_store,
59       std::set<std::string> target_sections, int64_t current_time_ms);
60   static std::unique_ptr<DocHitInfoIterator> ApplyRestrictions(
61       std::unique_ptr<DocHitInfoIterator> iterator,
62       const DocumentStore* document_store, const SchemaStore* schema_store,
63       const SearchSpecProto& search_spec, int64_t current_time_ms);
64   static std::unique_ptr<DocHitInfoIterator> ApplyRestrictions(
65       std::unique_ptr<DocHitInfoIterator> iterator, SectionRestrictData* data);
66 
67   libtextclassifier3::Status Advance() override;
68 
69   libtextclassifier3::StatusOr<TrimmedNode> TrimRightMostNode() && override;
70 
GetCallStats()71   CallStats GetCallStats() const override { return delegate_->GetCallStats(); }
72 
73   std::string ToString() const override;
74 
75   // Note that the DocHitInfoIteratorSectionRestrict can only be applied at
76   // DocHitInfoLeafIterator, which can be a term iterator or another
77   // DocHitInfoIteratorSectionRestrict.
78   //
79   // To filter the matching sections, filtering_section_mask should be set to
80   // doc_hit_info_.hit_section_ids_mask() held in the outermost
81   // DocHitInfoIteratorSectionRestrict, which is equal to the intersection of
82   // all hit_section_ids_mask in the DocHitInfoIteratorSectionRestrict chain,
83   // since for any two section restrict iterators chained together, the outer
84   // one's hit_section_ids_mask is always a subset of the inner one's
85   // hit_section_ids_mask.
86   void PopulateMatchedTermsStats(
87       std::vector<TermMatchInfo>* matched_terms_stats,
88       SectionIdMask filtering_section_mask = kSectionIdMaskAll) const override {
89     if (doc_hit_info_.document_id() == kInvalidDocumentId) {
90       // Current hit isn't valid, return.
91       return;
92     }
93     delegate_->PopulateMatchedTermsStats(
94         matched_terms_stats,
95         /*filtering_section_mask=*/filtering_section_mask &
96             doc_hit_info_.hit_section_ids_mask());
97   }
98 
99  private:
100   std::unique_ptr<DocHitInfoIterator> delegate_;
101   // Does not own.
102   SectionRestrictData* data_;
103 };
104 
105 }  // namespace lib
106 }  // namespace icing
107 
108 #endif  // ICING_INDEX_ITERATOR_DOC_HIT_INFO_ITERATOR_SECTION_RESTRICT_H_
109