xref: /aosp_15_r20/external/icing/icing/index/term-indexing-handler.h (revision 8b6cd535a057e39b3b86660c4aa06c99747c2136)
1 // Copyright (C) 2023 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_TERM_INDEXING_HANDLER_H_
16 #define ICING_INDEX_TERM_INDEXING_HANDLER_H_
17 
18 #include <memory>
19 #include <utility>
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/index.h"
25 #include "icing/index/property-existence-indexing-handler.h"
26 #include "icing/index/string-section-indexing-handler.h"
27 #include "icing/proto/logging.pb.h"
28 #include "icing/store/document-id.h"
29 #include "icing/transform/normalizer.h"
30 #include "icing/util/clock.h"
31 #include "icing/util/tokenized-document.h"
32 
33 namespace icing {
34 namespace lib {
35 
36 class TermIndexingHandler : public DataIndexingHandler {
37  public:
38   // Creates a TermIndexingHandler instance which does not take
39   // ownership of any input components. All pointers must refer to valid objects
40   // that outlive the created TermIndexingHandler instance.
41   //
42   // Returns:
43   //   - A TermIndexingHandler instance on success
44   //   - FAILED_PRECONDITION_ERROR if any of the input pointer is null
45   static libtextclassifier3::StatusOr<std::unique_ptr<TermIndexingHandler>>
46   Create(const Clock* clock, const Normalizer* normalizer, Index* index,
47          bool build_property_existence_metadata_hits);
48 
49   ~TermIndexingHandler() override = default;
50 
51   // Handles term indexing process:
52   // - Checks if document_id > last_added_document_id.
53   // - Updates last_added_document_id to document_id.
54   // - Handles PropertyExistenceIndexingHandler.
55   // - Handles StringSectionIndexingHandler.
56   // - Sorts the lite index if necessary.
57   // - Merges the lite index into the main index if necessary.
58   //
59   // Returns:
60   //   - OK on success
61   //   - INVALID_ARGUMENT_ERROR if document_id is less than or equal to the
62   //     document_id of a previously indexed document in non recovery mode.
63   //   - RESOURCE_EXHAUSTED_ERROR if the index is full and can't add anymore
64   //     content.
65   //   - DATA_LOSS_ERROR if an attempt to merge the index fails and both indices
66   //     are cleared as a result.
67   //   - INTERNAL_ERROR if any other errors occur.
68   //   - Any main/lite index errors.
69   libtextclassifier3::Status Handle(
70       const TokenizedDocument& tokenized_document, DocumentId document_id,
71       DocumentId old_document_id, bool recovery_mode,
72       PutDocumentStatsProto* put_document_stats) override;
73 
74  private:
TermIndexingHandler(const Clock * clock,Index * index,std::unique_ptr<PropertyExistenceIndexingHandler> property_existence_indexing_handler,std::unique_ptr<StringSectionIndexingHandler> string_section_indexing_handler)75   explicit TermIndexingHandler(const Clock* clock, Index* index,
76                                std::unique_ptr<PropertyExistenceIndexingHandler>
77                                    property_existence_indexing_handler,
78                                std::unique_ptr<StringSectionIndexingHandler>
79                                    string_section_indexing_handler)
80       : DataIndexingHandler(clock),
81         index_(*index),
82         property_existence_indexing_handler_(
83             std::move(property_existence_indexing_handler)),
84         string_section_indexing_handler_(
85             std::move(string_section_indexing_handler)) {}
86 
87   Index& index_;  // Does not own.
88 
89   std::unique_ptr<PropertyExistenceIndexingHandler>
90       property_existence_indexing_handler_;  // Nullable
91   std::unique_ptr<StringSectionIndexingHandler>
92       string_section_indexing_handler_;
93 };
94 
95 }  // namespace lib
96 }  // namespace icing
97 
98 #endif  // ICING_INDEX_TERM_INDEXING_HANDLER_H_
99