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_STORE_NAMESPACE_FINGERPRINT_IDENTIFIER_H_ 16 #define ICING_STORE_NAMESPACE_FINGERPRINT_IDENTIFIER_H_ 17 18 #include <cstdint> 19 #include <string> 20 #include <string_view> 21 22 #include "icing/text_classifier/lib3/utils/base/statusor.h" 23 #include "icing/store/namespace-id.h" 24 25 namespace icing { 26 namespace lib { 27 28 class NamespaceFingerprintIdentifier { 29 public: 30 static constexpr int kEncodedNamespaceIdLength = 3; 31 static constexpr int kMinEncodedLength = kEncodedNamespaceIdLength + 1; 32 33 static libtextclassifier3::StatusOr<NamespaceFingerprintIdentifier> 34 DecodeFromCString(std::string_view encoded_cstr); 35 NamespaceFingerprintIdentifier()36 explicit NamespaceFingerprintIdentifier() 37 : namespace_id_(0), fingerprint_(0) {} 38 NamespaceFingerprintIdentifier(NamespaceId namespace_id,uint64_t fingerprint)39 explicit NamespaceFingerprintIdentifier(NamespaceId namespace_id, 40 uint64_t fingerprint) 41 : namespace_id_(namespace_id), fingerprint_(fingerprint) {} 42 43 explicit NamespaceFingerprintIdentifier(NamespaceId namespace_id, 44 std::string_view target_str); 45 46 std::string EncodeToCString() const; 47 48 bool operator<(const NamespaceFingerprintIdentifier& other) const { 49 if (namespace_id_ != other.namespace_id_) { 50 return namespace_id_ < other.namespace_id_; 51 } 52 return fingerprint_ < other.fingerprint_; 53 } 54 55 bool operator==(const NamespaceFingerprintIdentifier& other) const { 56 return namespace_id_ == other.namespace_id_ && 57 fingerprint_ == other.fingerprint_; 58 } 59 namespace_id()60 NamespaceId namespace_id() const { return namespace_id_; } fingerprint()61 uint64_t fingerprint() const { return fingerprint_; } 62 63 private: 64 NamespaceId namespace_id_; 65 uint64_t fingerprint_; 66 } __attribute__((packed)); 67 static_assert(sizeof(NamespaceFingerprintIdentifier) == 10, ""); 68 69 } // namespace lib 70 } // namespace icing 71 72 #endif // ICING_STORE_NAMESPACE_FINGERPRINT_IDENTIFIER_H_ 73