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 <string> 20 #include <string_view> 21 #include <vector> 22 23 #include "icing/text_classifier/lib3/utils/base/statusor.h" 24 #include "icing/join/join-children-fetcher.h" 25 #include "icing/join/qualified-id-join-index.h" 26 #include "icing/proto/search.pb.h" 27 #include "icing/schema/schema-store.h" 28 #include "icing/scoring/scored-document-hit.h" 29 #include "icing/store/document-store.h" 30 31 namespace icing { 32 namespace lib { 33 34 class JoinProcessor { 35 public: 36 static constexpr std::string_view kQualifiedIdExpr = "this.qualifiedId()"; 37 JoinProcessor(const DocumentStore * doc_store,const SchemaStore * schema_store,const QualifiedIdJoinIndex * qualified_id_join_index,int64_t current_time_ms)38 explicit JoinProcessor(const DocumentStore* doc_store, 39 const SchemaStore* schema_store, 40 const QualifiedIdJoinIndex* qualified_id_join_index, 41 int64_t current_time_ms) 42 : doc_store_(doc_store), 43 schema_store_(schema_store), 44 qualified_id_join_index_(qualified_id_join_index), 45 current_time_ms_(current_time_ms) {} 46 47 // Get a JoinChildrenFetcher used to fetch all children documents by a parent 48 // document id. 49 // 50 // Returns: 51 // A JoinChildrenFetcher instance on success. 52 // UNIMPLEMENTED_ERROR if the join type specified by join_spec is not 53 // supported. 54 libtextclassifier3::StatusOr<JoinChildrenFetcher> GetChildrenFetcher( 55 const JoinSpecProto& join_spec, 56 std::vector<ScoredDocumentHit>&& child_scored_document_hits); 57 58 libtextclassifier3::StatusOr<std::vector<JoinedScoredDocumentHit>> Join( 59 const JoinSpecProto& join_spec, 60 std::vector<ScoredDocumentHit>&& parent_scored_document_hits, 61 const JoinChildrenFetcher& join_children_fetcher); 62 63 private: 64 // Fetches referenced document id of the given document under the given 65 // property path. 66 // 67 // TODO(b/256022027): validate joinable property (and its upper-level) should 68 // not have REPEATED cardinality. 69 // 70 // Returns: 71 // - A valid referenced document id on success 72 // - kInvalidDocumentId if the given document is not found, doesn't have 73 // qualified id joinable type for the given property_path, or doesn't have 74 // joinable value (an optional property) 75 // - Any other QualifiedIdJoinIndex errors 76 libtextclassifier3::StatusOr<DocumentId> FetchReferencedQualifiedId( 77 const DocumentId& document_id, const std::string& property_path) const; 78 79 const DocumentStore* doc_store_; // Does not own. 80 const SchemaStore* schema_store_; // Does not own. 81 const QualifiedIdJoinIndex* qualified_id_join_index_; // Does not own. 82 int64_t current_time_ms_; 83 }; 84 85 } // namespace lib 86 } // namespace icing 87 88 #endif // ICING_JOIN_JOIN_PROCESSOR_H_ 89