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