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_DOC_JOIN_INFO 16 #define ICING_JOIN_DOC_JOIN_INFO 17 18 #include <cstdint> 19 #include <limits> 20 21 #include "icing/schema/joinable-property.h" 22 #include "icing/store/document-id.h" 23 24 namespace icing { 25 namespace lib { 26 27 // DocJoinInfo is composed of document_id and joinable_property_id. 28 class DocJoinInfo { 29 public: 30 // The datatype used to encode DocJoinInfo information: the document_id and 31 // joinable_property_id. 32 using Value = uint32_t; 33 34 static_assert(kDocumentIdBits + kJoinablePropertyIdBits <= sizeof(Value) * 8, 35 "Cannot encode document id and joinable property id in " 36 "DocJoinInfo::Value"); 37 38 // All bits of kInvalidValue are 1, and it contains: 39 // - 0b1 for 4 unused bits. 40 // - kInvalidDocumentId (2^22-1). 41 // - JoinablePropertyId 2^6-1 (valid), which is ok because kInvalidDocumentId 42 // has already invalidated the value. In fact, we currently use all 2^6 43 // joinable property ids and there is no "invalid joinable property id", so 44 // it doesn't matter what JoinablePropertyId we set for kInvalidValue. 45 static constexpr Value kInvalidValue = std::numeric_limits<Value>::max(); 46 47 explicit DocJoinInfo(DocumentId document_id, 48 JoinablePropertyId joinable_property_id); 49 value_(value)50 explicit DocJoinInfo(Value value = kInvalidValue) : value_(value) {} 51 is_valid()52 bool is_valid() const { return value_ != kInvalidValue; } value()53 Value value() const { return value_; } 54 DocumentId document_id() const; 55 JoinablePropertyId joinable_property_id() const; 56 57 private: 58 // Value bits layout: 4 unused + 22 document_id + 6 joinable_property_id. 59 Value value_; 60 } __attribute__((packed)); 61 static_assert(sizeof(DocJoinInfo) == 4, ""); 62 63 } // namespace lib 64 } // namespace icing 65 66 #endif // ICING_JOIN_DOC_JOIN_INFO 67