xref: /aosp_15_r20/external/icing/icing/join/document-id-to-join-info.h (revision 8b6cd535a057e39b3b86660c4aa06c99747c2136)
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_DOCUMENT_ID_TO_JOIN_INFO_H_
16 #define ICING_JOIN_DOCUMENT_ID_TO_JOIN_INFO_H_
17 
18 #include <utility>
19 
20 #include "icing/store/document-id.h"
21 
22 namespace icing {
23 namespace lib {
24 
25 // DocumentIdToJoinInfo is composed of document_id and its join info.
26 // - QualifiedId join: join info is the referenced document's namespace_id +
27 //   fingerprint(uri).
28 // - String join: join info is the term id.
29 // - Integer join: join info is the integer.
30 //
31 // DocumentIdToJoinInfo will be stored in posting list.
32 template <typename JoinInfoType>
33 class DocumentIdToJoinInfo {
34  public:
GetInvalid()35   static DocumentIdToJoinInfo<JoinInfoType> GetInvalid() {
36     return DocumentIdToJoinInfo<JoinInfoType>(kInvalidDocumentId,
37                                               JoinInfoType());
38   }
39 
DocumentIdToJoinInfo(DocumentId document_id,JoinInfoType join_info)40   explicit DocumentIdToJoinInfo(DocumentId document_id, JoinInfoType join_info)
41       : document_id_(document_id), join_info_(std::move(join_info)) {}
42 
document_id()43   DocumentId document_id() const { return document_id_; }
join_info()44   const JoinInfoType& join_info() const { return join_info_; }
45 
is_valid()46   bool is_valid() const { return IsDocumentIdValid(document_id_); }
47 
48   bool operator<(const DocumentIdToJoinInfo<JoinInfoType>& other) const {
49     if (document_id_ != other.document_id_) {
50       return document_id_ < other.document_id_;
51     }
52     return join_info_ < other.join_info_;
53   }
54 
55   bool operator==(const DocumentIdToJoinInfo<JoinInfoType>& other) const {
56     return document_id_ == other.document_id_ && join_info_ == other.join_info_;
57   }
58 
59  private:
60   DocumentId document_id_;
61   JoinInfoType join_info_;
62 } __attribute__((packed));
63 
64 }  // namespace lib
65 }  // namespace icing
66 
67 #endif  // ICING_JOIN_DOCUMENT_ID_TO_JOIN_INFO_H_
68