xref: /aosp_15_r20/external/icing/icing/index/integer-section-indexing-handler.h (revision 8b6cd535a057e39b3b86660c4aa06c99747c2136)
1 // Copyright (C) 2022 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_INTEGER_SECTION_INDEXING_HANDLER_H_
16 #define ICING_INDEX_INTEGER_SECTION_INDEXING_HANDLER_H_
17 
18 #include <cstdint>
19 #include <memory>
20 
21 #include "icing/text_classifier/lib3/utils/base/status.h"
22 #include "icing/text_classifier/lib3/utils/base/statusor.h"
23 #include "icing/index/data-indexing-handler.h"
24 #include "icing/index/numeric/numeric-index.h"
25 #include "icing/store/document-id.h"
26 #include "icing/util/clock.h"
27 #include "icing/util/tokenized-document.h"
28 
29 namespace icing {
30 namespace lib {
31 
32 class IntegerSectionIndexingHandler : public DataIndexingHandler {
33  public:
34   // Creates an IntegerSectionIndexingHandler instance which does not take
35   // ownership of any input components. All pointers must refer to valid objects
36   // that outlive the created IntegerSectionIndexingHandler instance.
37   //
38   // Returns:
39   //   - An IntegerSectionIndexingHandler instance on success
40   //   - FAILED_PRECONDITION_ERROR if any of the input pointer is null
41   static libtextclassifier3::StatusOr<
42       std::unique_ptr<IntegerSectionIndexingHandler>>
43   Create(const Clock* clock, NumericIndex<int64_t>* integer_index);
44 
45   ~IntegerSectionIndexingHandler() override = default;
46 
47   // Handles the integer indexing process: add hits into the integer index for
48   // all contents in tokenized_document.integer_sections.
49   //
50   // Parameter old_document_id is unused since there is no need to migrate data
51   // from old_document_id to (new) document_id.
52   //
53   // Returns:
54   //   - OK on success.
55   //   - INVALID_ARGUMENT_ERROR if document_id is invalid OR document_id is less
56   //     than or equal to the document_id of a previously indexed document in
57   //     non recovery mode.
58   //   - Any NumericIndex<int64_t>::Editor errors.
59   libtextclassifier3::Status Handle(
60       const TokenizedDocument& tokenized_document, DocumentId document_id,
61       DocumentId /*old_document_id*/ _, bool recovery_mode,
62       PutDocumentStatsProto* put_document_stats) override;
63 
64  private:
IntegerSectionIndexingHandler(const Clock * clock,NumericIndex<int64_t> * integer_index)65   explicit IntegerSectionIndexingHandler(const Clock* clock,
66                                          NumericIndex<int64_t>* integer_index)
67       : DataIndexingHandler(clock), integer_index_(*integer_index) {}
68 
69   NumericIndex<int64_t>& integer_index_;  // Does not own.
70 };
71 
72 }  // namespace lib
73 }  // namespace icing
74 
75 #endif  // ICING_INDEX_INTEGER_SECTION_INDEXING_HANDLER_H_
76