xref: /aosp_15_r20/external/icing/icing/index/iterator/doc-hit-info-iterator-not.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_NOT_H_
16 #define ICING_INDEX_ITERATOR_DOC_HIT_INFO_ITERATOR_NOT_H_
17 
18 #include <cstdint>
19 #include <memory>
20 #include <string>
21 
22 #include "icing/text_classifier/lib3/utils/base/status.h"
23 #include "icing/index/iterator/doc-hit-info-iterator-all-document-id.h"
24 #include "icing/index/iterator/doc-hit-info-iterator.h"
25 #include "icing/store/document-id.h"
26 
27 namespace icing {
28 namespace lib {
29 
30 // Iterator that will return all documents that are *not* specified by the
31 // to_be_excluded_iterator.
32 //
33 // NOTE: doc_hit_info_.hit_section_ids_mask() is meaningless for this iterator.
34 // When this iterator produces a result, it's because the Document was not
35 // present in the to_be_excluded_iterator. There is no concept of the Document
36 // having been chosen because it's term was in a specific section. Since we
37 // don't know anything about the sections for the Document, the
38 // doc_hit_info.hit_section_ids_mask() is always kSectionIdMaskNone.
39 class DocHitInfoIteratorNot : public DocHitInfoIterator {
40  public:
41   // to_be_excluded_iterator: The results of this iterator will be excluded
42   //     from this iterator's results.
43   // document_id_limit: The DocumentId that represents the most recently added
44   //     Document to the DocumentStore
45   explicit DocHitInfoIteratorNot(
46       std::unique_ptr<DocHitInfoIterator> to_be_excluded_iterator,
47       const DocumentId document_id_limit);
48 
49   libtextclassifier3::Status Advance() override;
50 
51   // The NOT operator is not suppose to be trimmed.
52   // We shouldn't generate suggestion for the last term if the last term belongs
53   // to NOT operator.
54   libtextclassifier3::StatusOr<TrimmedNode> TrimRightMostNode() && override;
55 
56   void MapChildren(const ChildrenMapper& mapper) override;
57 
GetCallStats()58   CallStats GetCallStats() const override {
59     return to_be_excluded_->GetCallStats() +
60            all_document_id_iterator_.GetCallStats();
61   }
62 
63   std::string ToString() const override;
64 
65  private:
66   std::unique_ptr<DocHitInfoIterator> to_be_excluded_;
67   DocHitInfoIteratorAllDocumentId all_document_id_iterator_;
68 };
69 
70 }  // namespace lib
71 }  // namespace icing
72 
73 #endif  // ICING_INDEX_ITERATOR_DOC_HIT_INFO_ITERATOR_NOT_H_
74