1 // Copyright (C) 2024 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_CHILDREN_FETCHER_IMPL_V3_H_ 16 #define ICING_JOIN_JOIN_CHILDREN_FETCHER_IMPL_V3_H_ 17 18 #include <cstdint> 19 #include <memory> 20 #include <unordered_map> 21 #include <utility> 22 #include <vector> 23 24 #include "icing/text_classifier/lib3/utils/base/statusor.h" 25 #include "icing/join/document-join-id-pair.h" 26 #include "icing/join/join-children-fetcher.h" 27 #include "icing/join/qualified-id-join-index.h" 28 #include "icing/proto/search.pb.h" 29 #include "icing/schema/schema-store.h" 30 #include "icing/scoring/scored-document-hit.h" 31 #include "icing/store/document-id.h" 32 #include "icing/store/document-store.h" 33 34 namespace icing { 35 namespace lib { 36 37 // A class that provides the GetChildren method for joins to fetch all children 38 // documents given a parent document id. 39 // - It does lazy lookup and caches the results after lookup and filter. 40 // - Only QualifiedIdJoinIndexImplV3 will use this class. 41 class JoinChildrenFetcherImplV3 : public JoinChildrenFetcher { 42 public: 43 // Creates JoinChildrenFetcherImplV3. 44 // 45 // Returns: 46 // - A JoinChildrenFetcherImplV3 instance on success. 47 // - FAILED_PRECONDITION_ERROR if any of the input pointer is null 48 // - UNIMPLEMENTED_ERROR if the join type specified by join_spec is not 49 // supported. 50 // - INVALID_ARGUMENT_ERROR if qualified_id_join_index is not v3. 51 static libtextclassifier3::StatusOr< 52 std::unique_ptr<JoinChildrenFetcherImplV3>> 53 Create(const JoinSpecProto& join_spec, const SchemaStore* schema_store, 54 const DocumentStore* doc_store, 55 const QualifiedIdJoinIndex* qualified_id_join_index, 56 int64_t current_time_ms, 57 std::vector<ScoredDocumentHit>&& child_scored_document_hits); 58 59 ~JoinChildrenFetcherImplV3() override = default; 60 61 libtextclassifier3::StatusOr<std::vector<ScoredDocumentHit>> GetChildren( 62 DocumentId parent_doc_id) const override; 63 64 private: JoinChildrenFetcherImplV3(const JoinSpecProto & join_spec,const QualifiedIdJoinIndex * qualified_id_join_index,std::unordered_map<DocumentJoinIdPair,ScoredDocumentHit,DocumentJoinIdPair::Hasher> && child_join_id_pair_to_scored_document_hit_map)65 explicit JoinChildrenFetcherImplV3( 66 const JoinSpecProto& join_spec, 67 const QualifiedIdJoinIndex* qualified_id_join_index, 68 std::unordered_map<DocumentJoinIdPair, ScoredDocumentHit, 69 DocumentJoinIdPair::Hasher>&& 70 child_join_id_pair_to_scored_document_hit_map) 71 : JoinChildrenFetcher(join_spec), 72 qualified_id_join_index_(*qualified_id_join_index), 73 child_join_id_pair_to_scored_document_hit_map_( 74 std::move(child_join_id_pair_to_scored_document_hit_map)) {} 75 76 const QualifiedIdJoinIndex& qualified_id_join_index_; // Does not own. 77 78 // Map the child join id pair to its scored document hit. It will be used to 79 // filter the child join id pairs. See GetChildren() implementation for more 80 // details. 81 std::unordered_map<DocumentJoinIdPair, ScoredDocumentHit, 82 DocumentJoinIdPair::Hasher> 83 child_join_id_pair_to_scored_document_hit_map_; 84 85 // The cache for storing parent document id to its children's scored document 86 // hits. GetChildren() will first check the cache to see if the parent 87 // document id is already in the cache. If not, it will do a lookup and 88 // filter the children (via child_join_id_pair_to_scored_document_hit_map_) 89 // and then store the result in the cache. 90 mutable std::unordered_map<DocumentId, std::vector<ScoredDocumentHit>> 91 cached_parent_to_children_map_; 92 }; 93 94 } // namespace lib 95 } // namespace icing 96 97 #endif // ICING_JOIN_JOIN_CHILDREN_FETCHER_IMPL_V3_H_ 98