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 // Parameter old_document_id is unused since there is no need to migrate data 56 // from old_document_id to (new) document_id. 57 // 58 /// Returns: 59 // - OK on success 60 // - RESOURCE_EXHAUSTED_ERROR if the index is full and can't add anymore 61 // content. 62 // - INTERNAL_ERROR if any other errors occur. 63 // - Any main/lite index errors. 64 libtextclassifier3::Status Handle(const TokenizedDocument& tokenized_document, 65 DocumentId document_id, 66 DocumentId /*old_document_id*/ _, 67 PutDocumentStatsProto* put_document_stats); 68 69 private: StringSectionIndexingHandler(const Normalizer * normalizer,Index * index)70 explicit StringSectionIndexingHandler(const Normalizer* normalizer, 71 Index* index) 72 : normalizer_(*normalizer), index_(*index) {} 73 74 const Normalizer& normalizer_; // Does not own. 75 Index& index_; // Does not own. 76 }; 77 78 } // namespace lib 79 } // namespace icing 80 81 #endif // ICING_INDEX_STRING_SECTION_INDEXING_HANDLER_H_ 82