• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_JOIN_CHILDREN_FETCHER_H_
16 #define ICING_JOIN_JOIN_CHILDREN_FETCHER_H_
17 
18 #include <unordered_map>
19 #include <vector>
20 
21 #include "icing/text_classifier/lib3/utils/base/statusor.h"
22 #include "icing/proto/search.pb.h"
23 #include "icing/scoring/scored-document-hit.h"
24 #include "icing/store/document-id.h"
25 
26 namespace icing {
27 namespace lib {
28 
29 // A class that provides the GetChildren method for joins to fetch all children
30 // documents given a parent document id.
31 //
32 // Internally, the class maintains a map for each joinable value type that
33 // groups children according to the joinable values. Currently we only support
34 // QUALIFIED_ID joining, in which the joinable value type is document id.
35 class JoinChildrenFetcher {
36  public:
JoinChildrenFetcher(const JoinSpecProto & join_spec,std::unordered_map<DocumentId,std::vector<ScoredDocumentHit>> && map_joinable_qualified_id)37   explicit JoinChildrenFetcher(
38       const JoinSpecProto& join_spec,
39       std::unordered_map<DocumentId, std::vector<ScoredDocumentHit>>&&
40           map_joinable_qualified_id)
41       : join_spec_(join_spec),
42         map_joinable_qualified_id_(std::move(map_joinable_qualified_id)) {}
43 
44   // Get a vector of children ScoredDocumentHit by parent document id.
45   //
46   // TODO(b/256022027): Implement property value joins with types of string and
47   // int. In these cases, GetChildren should look up joinable cache to fetch
48   // joinable property value of the given parent_doc_id according to
49   // join_spec_.parent_property_expression, and then fetch children by the
50   // corresponding map in this class using the joinable property value.
51   //
52   // Returns:
53   //   The vector of results on success.
54   //   UNIMPLEMENTED_ERROR if the join type specified by join_spec is not
55   //   supported.
56   libtextclassifier3::StatusOr<std::vector<ScoredDocumentHit>> GetChildren(
57       DocumentId parent_doc_id) const;
58 
59  private:
60   static constexpr std::string_view kQualifiedIdExpr = "this.qualifiedId()";
61 
62   const JoinSpecProto& join_spec_;  // Does not own!
63 
64   // The map that groups children by qualified id used to support QualifiedId
65   // joining. The joining type is document id.
66   std::unordered_map<DocumentId, std::vector<ScoredDocumentHit>>
67       map_joinable_qualified_id_;
68 };
69 
70 }  // namespace lib
71 }  // namespace icing
72 
73 #endif  // ICING_JOIN_JOIN_CHILDREN_FETCHER_H_
74