• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_STRING_SECTION_INDEXING_HANDLER_H_
16 #define ICING_INDEX_STRING_SECTION_INDEXING_HANDLER_H_
17 
18 #include <memory>
19 
20 #include "icing/text_classifier/lib3/utils/base/status.h"
21 #include "icing/text_classifier/lib3/utils/base/statusor.h"
22 #include "icing/index/index.h"
23 #include "icing/proto/logging.pb.h"
24 #include "icing/store/document-id.h"
25 #include "icing/transform/normalizer.h"
26 #include "icing/util/tokenized-document.h"
27 
28 namespace icing {
29 namespace lib {
30 
31 // This class is meant to be owned by TermIndexingHandler. Instead of using this
32 // handler directly, callers should use TermIndexingHandler to index documents.
33 //
34 // This handler will not check or set last_added_document_id of the index, and
35 // it will not merge or sort the lite index either.
36 class StringSectionIndexingHandler {
37  public:
38   // Creates a StringSectionIndexingHandler instance which does not take
39   // ownership of any input components. All pointers must refer to valid objects
40   // that outlive the created StringSectionIndexingHandler instance.
41   //
42   // Returns:
43   //   - A StringSectionIndexingHandler instance on success
44   //   - FAILED_PRECONDITION_ERROR if any of the input pointer is null
45   static libtextclassifier3::StatusOr<
46       std::unique_ptr<StringSectionIndexingHandler>>
47   Create(const Normalizer* normalizer, Index* index);
48 
49   ~StringSectionIndexingHandler() = default;
50 
51   // Handles the string term indexing process: add hits into the lite index for
52   // all contents in tokenized_document.tokenized_string_sections and merge lite
53   // index into main index if necessary.
54   //
55   /// Returns:
56   //   - OK on success
57   //   - RESOURCE_EXHAUSTED_ERROR if the index is full and can't add anymore
58   //     content.
59   //   - INTERNAL_ERROR if any other errors occur.
60   //   - Any main/lite index errors.
61   libtextclassifier3::Status Handle(const TokenizedDocument& tokenized_document,
62                                     DocumentId document_id,
63                                     PutDocumentStatsProto* put_document_stats);
64 
65  private:
StringSectionIndexingHandler(const Normalizer * normalizer,Index * index)66   explicit StringSectionIndexingHandler(const Normalizer* normalizer,
67                                         Index* index)
68       : normalizer_(*normalizer), index_(*index) {}
69 
70   const Normalizer& normalizer_;  // Does not own.
71   Index& index_;                  // Does not own.
72 };
73 
74 }  // namespace lib
75 }  // namespace icing
76 
77 #endif  // ICING_INDEX_STRING_SECTION_INDEXING_HANDLER_H_
78