xref: /aosp_15_r20/external/icing/icing/index/iterator/doc-hit-info-iterator-property-in-document.cc (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 #include "icing/index/iterator/doc-hit-info-iterator-property-in-document.h"
16 
17 #include <memory>
18 #include <string>
19 #include <string_view>
20 #include <utility>
21 
22 #include "icing/text_classifier/lib3/utils/base/status.h"
23 #include "icing/text_classifier/lib3/utils/base/statusor.h"
24 #include "icing/absl_ports/canonical_errors.h"
25 #include "icing/index/hit/doc-hit-info.h"
26 #include "icing/index/iterator/doc-hit-info-iterator.h"
27 #include "icing/store/document-id.h"
28 
29 namespace icing {
30 namespace lib {
31 
DocHitInfoIteratorPropertyInDocument(std::unique_ptr<DocHitInfoIterator> meta_hit_iterator)32 DocHitInfoIteratorPropertyInDocument::DocHitInfoIteratorPropertyInDocument(
33     std::unique_ptr<DocHitInfoIterator> meta_hit_iterator)
34     : meta_hit_iterator_(std::move(meta_hit_iterator)) {}
35 
Advance()36 libtextclassifier3::Status DocHitInfoIteratorPropertyInDocument::Advance() {
37   while (meta_hit_iterator_->Advance().ok()) {
38     // Currently, the metadata hits added by PropertyExistenceIndexingHandler
39     // can only have a section id of 0, so the section mask has to be 1 << 0.
40     if (meta_hit_iterator_->doc_hit_info().hit_section_ids_mask() == (1 << 0)) {
41       doc_hit_info_ = meta_hit_iterator_->doc_hit_info();
42       // Hits returned by "hasProperty" should not be associated with any
43       // section.
44       doc_hit_info_.set_hit_section_ids_mask(/*section_id_mask=*/0);
45       return libtextclassifier3::Status::OK;
46     }
47   }
48 
49   doc_hit_info_ = DocHitInfo(kInvalidDocumentId);
50   return absl_ports::ResourceExhaustedError("No more DocHitInfos in iterator");
51 }
52 
53 libtextclassifier3::StatusOr<DocHitInfoIterator::TrimmedNode>
TrimRightMostNode()54 DocHitInfoIteratorPropertyInDocument::TrimRightMostNode() && {
55   // Don't generate suggestion if the last operator is this custom function.
56   return absl_ports::InvalidArgumentError(
57       "Cannot generate suggestion if the last term is hasProperty().");
58 }
59 
ToString() const60 std::string DocHitInfoIteratorPropertyInDocument::ToString() const {
61   return meta_hit_iterator_->ToString();
62 }
63 
64 }  // namespace lib
65 }  // namespace icing
66