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/data-indexing-handler.h" 23 #include "icing/index/index.h" 24 #include "icing/proto/logging.pb.h" 25 #include "icing/store/document-id.h" 26 #include "icing/transform/normalizer.h" 27 #include "icing/util/clock.h" 28 #include "icing/util/tokenized-document.h" 29 30 namespace icing { 31 namespace lib { 32 33 class StringSectionIndexingHandler : public DataIndexingHandler { 34 public: 35 // Creates a StringSectionIndexingHandler instance which does not take 36 // ownership of any input components. All pointers must refer to valid objects 37 // that outlive the created StringSectionIndexingHandler instance. 38 // 39 // Returns: 40 // - A StringSectionIndexingHandler instance on success 41 // - FAILED_PRECONDITION_ERROR if any of the input pointer is null 42 static libtextclassifier3::StatusOr< 43 std::unique_ptr<StringSectionIndexingHandler>> 44 Create(const Clock* clock, const Normalizer* normalizer, Index* index); 45 46 ~StringSectionIndexingHandler() override = default; 47 48 // Handles the string term indexing process: add hits into the lite index for 49 // all contents in tokenized_document.tokenized_string_sections and merge lite 50 // index into main index if necessary. 51 // 52 /// Returns: 53 // - OK on success 54 // - INVALID_ARGUMENT_ERROR if document_id is less than or equal to the 55 // document_id of a previously indexed document in non recovery mode. 56 // - RESOURCE_EXHAUSTED_ERROR if the index is full and can't add anymore 57 // content. 58 // - DATA_LOSS_ERROR if an attempt to merge the index fails and both indices 59 // are cleared as a result. 60 // - INTERNAL_ERROR if any other errors occur. 61 // - Any main/lite index errors. 62 libtextclassifier3::Status Handle( 63 const TokenizedDocument& tokenized_document, DocumentId document_id, 64 bool recovery_mode, PutDocumentStatsProto* put_document_stats) override; 65 66 private: StringSectionIndexingHandler(const Clock * clock,const Normalizer * normalizer,Index * index)67 explicit StringSectionIndexingHandler(const Clock* clock, 68 const Normalizer* normalizer, 69 Index* index) 70 : DataIndexingHandler(clock), normalizer_(*normalizer), index_(*index) {} 71 72 const Normalizer& normalizer_; // Does not own. 73 Index& index_; // Does not own. 74 }; 75 76 } // namespace lib 77 } // namespace icing 78 79 #endif // ICING_INDEX_STRING_SECTION_INDEXING_HANDLER_H_ 80