xref: /aosp_15_r20/external/icing/icing/join/qualified-id-join-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_JOIN_QUALIFIED_ID_JOIN_INDEXING_HANDLER_H_
16 #define ICING_JOIN_QUALIFIED_ID_JOIN_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/join/qualified-id-join-index.h"
24 #include "icing/proto/logging.pb.h"
25 #include "icing/store/document-id.h"
26 #include "icing/store/document-store.h"
27 #include "icing/util/clock.h"
28 #include "icing/util/tokenized-document.h"
29 
30 namespace icing {
31 namespace lib {
32 
33 class QualifiedIdJoinIndexingHandler : public DataIndexingHandler {
34  public:
35   // Creates a QualifiedIdJoinIndexingHandler instance which does not take
36   // ownership of any input components. All pointers must refer to valid objects
37   // that outlive the created QualifiedIdJoinIndexingHandler instance.
38   //
39   // Returns:
40   //   - A QualifiedIdJoinIndexingHandler instance on success
41   //   - FAILED_PRECONDITION_ERROR if any of the input pointer is null
42   static libtextclassifier3::StatusOr<
43       std::unique_ptr<QualifiedIdJoinIndexingHandler>>
44   Create(const Clock* clock, const DocumentStore* doc_store,
45          QualifiedIdJoinIndex* qualified_id_join_index);
46 
47   ~QualifiedIdJoinIndexingHandler() override = default;
48 
49   // Handles the joinable qualified id data indexing process: add data into the
50   // qualified id join index.
51   //
52   // old_document_id:
53   // - It is only used in QualifiedIdJoinIndexImplV3: if we update a parent
54   //   document, then the existing join data for the parent document should be
55   //   migrated from old_document_id to (new) document_id.
56   // - For other join index versions, since we store (namespace id,
57   //   fingerprint(uri)) or the raw qualified id string for the parent, there is
58   //   no need to migrate.
59   //
60   /// Returns:
61   //   - OK on success.
62   //   - INVALID_ARGUMENT_ERROR if document_id is invalid OR document_id is less
63   //     than or equal to the document_id of a previously indexed document in
64   //     non recovery mode.
65   //   - INTERNAL_ERROR if any other errors occur.
66   //   - Any QualifiedIdJoinIndex errors.
67   libtextclassifier3::Status Handle(
68       const TokenizedDocument& tokenized_document, DocumentId document_id,
69       DocumentId old_document_id, bool recovery_mode,
70       PutDocumentStatsProto* put_document_stats) override;
71 
72  private:
QualifiedIdJoinIndexingHandler(const Clock * clock,const DocumentStore * doc_store,QualifiedIdJoinIndex * qualified_id_join_index)73   explicit QualifiedIdJoinIndexingHandler(
74       const Clock* clock, const DocumentStore* doc_store,
75       QualifiedIdJoinIndex* qualified_id_join_index)
76       : DataIndexingHandler(clock),
77         doc_store_(*doc_store),
78         qualified_id_join_index_(*qualified_id_join_index) {}
79 
80   // TODO(b/275121148): deprecate v1, v2 after rollout v3.
81 
82   // Helper function to handle indexing for QualfiedIdJoinIndexImplV1.
83   libtextclassifier3::Status HandleV1(
84       const TokenizedDocument& tokenized_document, DocumentId document_id);
85 
86   // Helper function to handle indexing for QualfiedIdJoinIndexImplV2.
87   libtextclassifier3::Status HandleV2(
88       const TokenizedDocument& tokenized_document, DocumentId document_id);
89 
90   // Helper function to handle indexing for QualfiedIdJoinIndexImplV3.
91   libtextclassifier3::Status HandleV3(
92       const TokenizedDocument& tokenized_document, DocumentId document_id,
93       DocumentId old_document_id);
94 
95   const DocumentStore& doc_store_;                 // Does not own.
96   QualifiedIdJoinIndex& qualified_id_join_index_;  // Does not own.
97 };
98 
99 }  // namespace lib
100 }  // namespace icing
101 
102 #endif  // ICING_JOIN_QUALIFIED_ID_JOIN_INDEXING_HANDLER_H_
103