xref: /aosp_15_r20/external/icing/icing/index/iterator/doc-hit-info-iterator-not.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-not.h"
16 
17 #include <cstdint>
18 #include <memory>
19 #include <utility>
20 
21 #include "icing/text_classifier/lib3/utils/base/status.h"
22 #include "icing/absl_ports/canonical_errors.h"
23 #include "icing/absl_ports/str_cat.h"
24 #include "icing/index/hit/doc-hit-info.h"
25 #include "icing/index/iterator/doc-hit-info-iterator-all-document-id.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 
DocHitInfoIteratorNot(std::unique_ptr<DocHitInfoIterator> to_be_excluded,DocumentId document_id_limit)32 DocHitInfoIteratorNot::DocHitInfoIteratorNot(
33     std::unique_ptr<DocHitInfoIterator> to_be_excluded,
34     DocumentId document_id_limit)
35     : to_be_excluded_(std::move(to_be_excluded)),
36       all_document_id_iterator_(
37           DocHitInfoIteratorAllDocumentId(document_id_limit)) {}
38 
Advance()39 libtextclassifier3::Status DocHitInfoIteratorNot::Advance() {
40   while (all_document_id_iterator_.Advance().ok()) {
41     if (all_document_id_iterator_.doc_hit_info().document_id() <
42         to_be_excluded_->doc_hit_info().document_id()) {
43       // Since DocumentIds are returned from DocHitInfoIterators in decreasing
44       // order, we have passed the last NOT result if we're smaller than its
45       // DocumentId. Advance the NOT result if so.
46       to_be_excluded_->Advance().IgnoreError();
47     }
48 
49     if (all_document_id_iterator_.doc_hit_info().document_id() ==
50         to_be_excluded_->doc_hit_info().document_id()) {
51       // This is a NOT result, skip and Advance to the next result.
52       continue;
53     }
54 
55     // No errors, we've found a valid result
56     doc_hit_info_ = all_document_id_iterator_.doc_hit_info();
57     return libtextclassifier3::Status::OK;
58   }
59 
60   // Didn't find a hit, return with error
61   doc_hit_info_ = DocHitInfo(kInvalidDocumentId);
62   return absl_ports::ResourceExhaustedError("No more DocHitInfos in iterator");
63 }
64 
65 libtextclassifier3::StatusOr<DocHitInfoIterator::TrimmedNode>
TrimRightMostNode()66 DocHitInfoIteratorNot::TrimRightMostNode() && {
67   // Don't generate suggestion if the last operator is NOT.
68   return absl_ports::InvalidArgumentError(
69       "Cannot generate suggestion if the last term is NOT operator.");
70 }
71 
MapChildren(const ChildrenMapper & mapper)72 void DocHitInfoIteratorNot::MapChildren(const ChildrenMapper& mapper) {
73   to_be_excluded_ = mapper(std::move(to_be_excluded_));
74 }
75 
ToString() const76 std::string DocHitInfoIteratorNot::ToString() const {
77   return absl_ports::StrCat("(NOT ", to_be_excluded_->ToString(), ")");
78 }
79 
80 }  // namespace lib
81 }  // namespace icing
82