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 v2 after rollout v3. 80 81 // Helper function to construct JoinChildrenFetcher for 82 // QualfiedIdJoinIndexImplV2. 83 // 84 // Note: JoinChildrenFetcherImplDeprecated will be returned. 85 libtextclassifier3::StatusOr<std::unique_ptr<JoinChildrenFetcher>> 86 GetChildrenFetcherV2( 87 const JoinSpecProto& join_spec, 88 std::vector<ScoredDocumentHit>&& child_scored_document_hits); 89 90 const DocumentStore* doc_store_; // Does not own. 91 const SchemaStore* schema_store_; // Does not own. 92 const QualifiedIdJoinIndex* qualified_id_join_index_; // Does not own. 93 int64_t current_time_ms_; 94 }; 95 96 } // namespace lib 97 } // namespace icing 98 99 #endif // ICING_JOIN_JOIN_PROCESSOR_H_ 100