aboutsummaryrefslogtreecommitdiff
path: root/icing/index/integer-section-indexing-handler.cc
blob: 584f028d6fc3d1578676b2f5fa3b00e991d27127 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright (C) 2022 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/integer-section-indexing-handler.h"

#include <cstdint>
#include <memory>

#include "icing/text_classifier/lib3/utils/base/status.h"
#include "icing/text_classifier/lib3/utils/base/statusor.h"
#include "icing/schema/section.h"
#include "icing/store/document-id.h"
#include "icing/util/logging.h"
#include "icing/util/tokenized-document.h"

namespace icing {
namespace lib {

/* static */ libtextclassifier3::StatusOr<
    std::unique_ptr<IntegerSectionIndexingHandler>>
IntegerSectionIndexingHandler::Create(const Clock* clock,
                                      NumericIndex<int64_t>* integer_index) {
  ICING_RETURN_ERROR_IF_NULL(clock);
  ICING_RETURN_ERROR_IF_NULL(integer_index);

  return std::unique_ptr<IntegerSectionIndexingHandler>(
      new IntegerSectionIndexingHandler(clock, integer_index));
}

libtextclassifier3::Status IntegerSectionIndexingHandler::Handle(
    const TokenizedDocument& tokenized_document, DocumentId document_id,
    bool recovery_mode, PutDocumentStatsProto* put_document_stats) {
  // TODO(b/259744228): set integer indexing latency and other stats

  if (!IsDocumentIdValid(document_id)) {
    return absl_ports::InvalidArgumentError(
        IcingStringUtil::StringPrintf("Invalid DocumentId %d", document_id));
  }

  if (integer_index_.last_added_document_id() != kInvalidDocumentId &&
      document_id <= integer_index_.last_added_document_id()) {
    if (recovery_mode) {
      // Skip the document if document_id <= last_added_document_id in recovery
      // mode without returning an error.
      return libtextclassifier3::Status::OK;
    }
    return absl_ports::InvalidArgumentError(IcingStringUtil::StringPrintf(
        "DocumentId %d must be greater than last added document_id %d",
        document_id, integer_index_.last_added_document_id()));
  }
  integer_index_.set_last_added_document_id(document_id);

  libtextclassifier3::Status status;
  // We have to add integer sections into integer index in reverse order because
  // sections are sorted by SectionId in ascending order, but BasicHit should be
  // added in descending order of SectionId (posting list requirement).
  for (auto riter = tokenized_document.integer_sections().rbegin();
       riter != tokenized_document.integer_sections().rend(); ++riter) {
    const Section<int64_t>& section = *riter;
    std::unique_ptr<NumericIndex<int64_t>::Editor> editor = integer_index_.Edit(
        section.metadata.path, document_id, section.metadata.id);

    for (int64_t key : section.content) {
      status = editor->BufferKey(key);
      if (!status.ok()) {
        ICING_LOG(WARNING)
            << "Failed to buffer keys into integer index due to: "
            << status.error_message();
        break;
      }
    }
    if (!status.ok()) {
      break;
    }

    // Add all the seen keys to the integer index.
    status = std::move(*editor).IndexAllBufferedKeys();
    if (!status.ok()) {
      ICING_LOG(WARNING) << "Failed to add keys into integer index due to: "
                         << status.error_message();
      break;
    }
  }

  return status;
}

}  // namespace lib
}  // namespace icing