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 <string> 21 #include <string_view> 22 23 #include "icing/text_classifier/lib3/utils/base/status.h" 24 #include "icing/index/iterator/doc-hit-info-iterator.h" 25 #include "icing/schema/schema-store.h" 26 #include "icing/store/document-store.h" 27 28 namespace icing { 29 namespace lib { 30 31 // A iterator that helps filter for DocHitInfos whose term was in a section 32 // named target_section. 33 // 34 // NOTE: This is a little different from the DocHitInfoIteratorFilter class. 35 // That class is meant to be applied to the root of a query tree and filter over 36 // all results at the end. This class is more used in the limited scope of a 37 // term or a small group of terms. 38 class DocHitInfoIteratorSectionRestrict : public DocHitInfoIterator { 39 public: 40 // Does not take any ownership, and all pointers must refer to valid objects 41 // that outlive the one constructed. 42 explicit DocHitInfoIteratorSectionRestrict( 43 std::unique_ptr<DocHitInfoIterator> delegate, 44 const DocumentStore* document_store, const SchemaStore* schema_store, 45 std::string_view target_section); 46 47 libtextclassifier3::Status Advance() override; 48 49 int32_t GetNumBlocksInspected() const override; 50 51 int32_t GetNumLeafAdvanceCalls() const override; 52 53 std::string ToString() const override; 54 55 // Note that the DocHitInfoIteratorSectionRestrict is the only iterator that 56 // should set filtering_section_mask, hence the received 57 // filtering_section_mask is ignored and the filtering_section_mask passed to 58 // the delegate will be set to hit_intersect_section_ids_mask_. This will 59 // allow to filter the matching sections in the delegate. 60 void PopulateMatchedTermsStats( 61 std::vector<TermMatchInfo>* matched_terms_stats, 62 SectionIdMask filtering_section_mask = kSectionIdMaskAll) const override { 63 if (doc_hit_info_.document_id() == kInvalidDocumentId) { 64 // Current hit isn't valid, return. 65 return; 66 } 67 delegate_->PopulateMatchedTermsStats( 68 matched_terms_stats, 69 /*filtering_section_mask=*/hit_intersect_section_ids_mask_); 70 } 71 72 private: 73 std::unique_ptr<DocHitInfoIterator> delegate_; 74 const DocumentStore& document_store_; 75 const SchemaStore& schema_store_; 76 77 // Ensure that this does not outlive the underlying string value. 78 std::string_view target_section_; 79 }; 80 81 } // namespace lib 82 } // namespace icing 83 84 #endif // ICING_INDEX_ITERATOR_DOC_HIT_INFO_ITERATOR_SECTION_RESTRICT_H_ 85