aboutsummaryrefslogtreecommitdiff
path: root/icing/index/iterator/doc-hit-info-iterator-not.cc
blob: 38b1ded87a9c68d1872c39c9511643fe423d68f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright (C) 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "icing/index/iterator/doc-hit-info-iterator-not.h"

#include <cstdint>

#include "icing/text_classifier/lib3/utils/base/status.h"
#include "icing/absl_ports/canonical_errors.h"
#include "icing/absl_ports/str_cat.h"
#include "icing/index/hit/doc-hit-info.h"
#include "icing/index/iterator/doc-hit-info-iterator-all-document-id.h"
#include "icing/schema/section.h"
#include "icing/store/document-id.h"

namespace icing {
namespace lib {

DocHitInfoIteratorNot::DocHitInfoIteratorNot(
    std::unique_ptr<DocHitInfoIterator> to_be_excluded,
    DocumentId document_id_limit)
    : to_be_excluded_(std::move(to_be_excluded)),
      all_document_id_iterator_(
          DocHitInfoIteratorAllDocumentId(document_id_limit)) {}

libtextclassifier3::Status DocHitInfoIteratorNot::Advance() {
  while (all_document_id_iterator_.Advance().ok()) {
    if (all_document_id_iterator_.doc_hit_info().document_id() <
        to_be_excluded_->doc_hit_info().document_id()) {
      // Since DocumentIds are returned from DocHitInfoIterators in decreasing
      // order, we have passed the last NOT result if we're smaller than its
      // DocumentId. Advance the NOT result if so.
      to_be_excluded_->Advance().IgnoreError();
    }

    if (all_document_id_iterator_.doc_hit_info().document_id() ==
        to_be_excluded_->doc_hit_info().document_id()) {
      // This is a NOT result, skip and Advance to the next result.
      continue;
    }

    // No errors, we've found a valid result
    doc_hit_info_ = all_document_id_iterator_.doc_hit_info();
    return libtextclassifier3::Status::OK;
  }

  // Didn't find a hit, return with error
  doc_hit_info_ = DocHitInfo(kInvalidDocumentId);
  return absl_ports::ResourceExhaustedError("No more DocHitInfos in iterator");
}

libtextclassifier3::StatusOr<DocHitInfoIterator::TrimmedNode>
DocHitInfoIteratorNot::TrimRightMostNode() && {
  // Don't generate suggestion if the last operator is NOT.
  return absl_ports::InvalidArgumentError(
      "Cannot generate suggestion if the last term is NOT operator.");
}

int32_t DocHitInfoIteratorNot::GetNumBlocksInspected() const {
  return to_be_excluded_->GetNumBlocksInspected() +
         all_document_id_iterator_.GetNumBlocksInspected();
}

int32_t DocHitInfoIteratorNot::GetNumLeafAdvanceCalls() const {
  return to_be_excluded_->GetNumLeafAdvanceCalls() +
         all_document_id_iterator_.GetNumLeafAdvanceCalls();
}

std::string DocHitInfoIteratorNot::ToString() const {
  return absl_ports::StrCat("(NOT ", to_be_excluded_->ToString(), ")");
}

}  // namespace lib
}  // namespace icing