xref: /aosp_15_r20/external/icing/icing/index/property-existence-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_PROPERTY_EXISTENCE_INDEXING_HANDLER_H_
16 #define ICING_INDEX_PROPERTY_EXISTENCE_INDEXING_HANDLER_H_
17 
18 #include <memory>
19 #include <string_view>
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/index.h"
24 #include "icing/proto/logging.pb.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 inline constexpr std::string_view kPropertyExistenceTokenPrefix =
33     "\xFF_HAS_\xFF";
34 
35 // This class is meant to be owned by TermIndexingHandler. Instead of using this
36 // handler directly, callers should use TermIndexingHandler to index documents.
37 //
38 // This handler will not check or set last_added_document_id of the index, and
39 // it will not merge or sort the lite index either.
40 class PropertyExistenceIndexingHandler {
41  public:
42   // Creates a PropertyExistenceIndexingHandler instance which does not take
43   // ownership of any input components. All pointers must refer to valid objects
44   // that outlive the created PropertyExistenceIndexingHandler instance.
45   //
46   // Returns:
47   //   - A PropertyExistenceIndexingHandler instance on success
48   //   - FAILED_PRECONDITION_ERROR if any of the input pointer is null
49   static libtextclassifier3::StatusOr<
50       std::unique_ptr<PropertyExistenceIndexingHandler>>
51   Create(const Clock* clock, Index* index);
52 
53   ~PropertyExistenceIndexingHandler() = default;
54 
55   // Handles the property existence indexing process: add hits for metadata
56   // tokens used to index property existence.
57   //
58   // For example, if the passed in document has string properties "propA",
59   // "propB" and "propC.propD", and document property "propC", this handler will
60   // add the following metadata token to the index.
61   // - kPropertyExistenceTokenPrefix + "propA"
62   // - kPropertyExistenceTokenPrefix + "propB"
63   // - kPropertyExistenceTokenPrefix + "propC"
64   // - kPropertyExistenceTokenPrefix + "propC.propD"
65   //
66   // Parameter old_document_id is unused since there is no need to migrate data
67   // from old_document_id to (new) document_id.
68   //
69   /// Returns:
70   //   - OK on success
71   //   - RESOURCE_EXHAUSTED_ERROR if the index is full and can't add anymore
72   //     content.
73   //   - INTERNAL_ERROR if any other errors occur.
74   libtextclassifier3::Status Handle(const TokenizedDocument& tokenized_document,
75                                     DocumentId document_id,
76                                     DocumentId /*old_document_id*/ _,
77                                     PutDocumentStatsProto* put_document_stats);
78 
79  private:
PropertyExistenceIndexingHandler(const Clock & clock,Index * index)80   explicit PropertyExistenceIndexingHandler(const Clock& clock, Index* index)
81       : clock_(clock), index_(*index) {}
82 
83   const Clock& clock_;  // Does not own.
84   Index& index_;        // Does not own.
85 };
86 
87 }  // namespace lib
88 }  // namespace icing
89 
90 #endif  // ICING_INDEX_PROPERTY_EXISTENCE_INDEXING_HANDLER_H_
91