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_JOIN_JOIN_PROCESSOR_H_ 16 #define ICING_JOIN_JOIN_PROCESSOR_H_ 17 18 #include <cstdint> 19 #include <memory> 20 #include <string> 21 #include <string_view> 22 #include <unordered_set> 23 #include <vector> 24 25 #include "icing/text_classifier/lib3/utils/base/status.h" 26 #include "icing/text_classifier/lib3/utils/base/statusor.h" 27 #include "icing/join/join-children-fetcher.h" 28 #include "icing/join/qualified-id-join-index.h" 29 #include "icing/proto/search.pb.h" 30 #include "icing/schema/schema-store.h" 31 #include "icing/scoring/scored-document-hit.h" 32 #include "icing/store/document-id.h" 33 #include "icing/store/document-store.h" 34 35 namespace icing { 36 namespace lib { 37 38 class JoinProcessor { 39 public: 40 static constexpr std::string_view kQualifiedIdExpr = "this.qualifiedId()"; 41 JoinProcessor(const DocumentStore * doc_store,const SchemaStore * schema_store,const QualifiedIdJoinIndex * qualified_id_join_index,int64_t current_time_ms)42 explicit JoinProcessor(const DocumentStore* doc_store, 43 const SchemaStore* schema_store, 44 const QualifiedIdJoinIndex* qualified_id_join_index, 45 int64_t current_time_ms) 46 : doc_store_(doc_store), 47 schema_store_(schema_store), 48 qualified_id_join_index_(qualified_id_join_index), 49 current_time_ms_(current_time_ms) {} 50 51 // Gets a JoinChildrenFetcher used to fetch all children documents by a parent 52 // document id. 53 // 54 // Returns: 55 // std::unique_ptr<JoinChildrenFetcher> instance on success. 56 // UNIMPLEMENTED_ERROR if the join type specified by join_spec is not 57 // supported. 58 libtextclassifier3::StatusOr<std::unique_ptr<JoinChildrenFetcher>> 59 GetChildrenFetcher( 60 const JoinSpecProto& join_spec, 61 std::vector<ScoredDocumentHit>&& child_scored_document_hits); 62 63 libtextclassifier3::StatusOr<std::vector<JoinedScoredDocumentHit>> Join( 64 const JoinSpecProto& join_spec, 65 std::vector<ScoredDocumentHit>&& parent_scored_document_hits, 66 const JoinChildrenFetcher& join_children_fetcher); 67 68 // Gets all child documents to delete, propagated from the given deleted 69 // documents. 70 // 71 // Returns: 72 // - On success, a set of child document ids to delete. 73 // - Any other errors. 74 libtextclassifier3::StatusOr<std::unordered_set<DocumentId>> 75 GetPropagatedChildDocumentsToDelete( 76 const std::unordered_set<DocumentId>& deleted_document_ids); 77 78 private: 79 // TODO(b/275121148): deprecate v1, v2 after rollout v3. 80 81 // Helper function to construct JoinChildrenFetcher for 82 // QualfiedIdJoinIndexImplV1. 83 // 84 // Note: JoinChildrenFetcherImplDeprecated will be returned. 85 libtextclassifier3::StatusOr<std::unique_ptr<JoinChildrenFetcher>> 86 GetChildrenFetcherV1( 87 const JoinSpecProto& join_spec, 88 std::vector<ScoredDocumentHit>&& child_scored_document_hits); 89 90 // Helper function to construct JoinChildrenFetcher for 91 // QualfiedIdJoinIndexImplV2. 92 // 93 // Note: JoinChildrenFetcherImplDeprecated will be returned. 94 libtextclassifier3::StatusOr<std::unique_ptr<JoinChildrenFetcher>> 95 GetChildrenFetcherV2( 96 const JoinSpecProto& join_spec, 97 std::vector<ScoredDocumentHit>&& child_scored_document_hits); 98 99 // Fetches referenced document id of the given document under the given 100 // property path. 101 // 102 // TODO(b/256022027): validate joinable property (and its upper-level) should 103 // not have REPEATED cardinality. 104 // 105 // Returns: 106 // - A valid referenced document id on success 107 // - kInvalidDocumentId if the given document is not found, doesn't have 108 // qualified id joinable type for the given property_path, or doesn't have 109 // joinable value (an optional property) 110 // - Any other QualifiedIdJoinIndex errors 111 libtextclassifier3::StatusOr<DocumentId> FetchReferencedQualifiedId( 112 const DocumentId& document_id, const std::string& property_path) const; 113 114 const DocumentStore* doc_store_; // Does not own. 115 const SchemaStore* schema_store_; // Does not own. 116 const QualifiedIdJoinIndex* qualified_id_join_index_; // Does not own. 117 int64_t current_time_ms_; 118 }; 119 120 } // namespace lib 121 } // namespace icing 122 123 #endif // ICING_JOIN_JOIN_PROCESSOR_H_ 124