xref: /aosp_15_r20/external/icing/icing/store/namespace-id-fingerprint.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_STORE_NAMESPACE_ID_FINGERPRINT_H_
16 #define ICING_STORE_NAMESPACE_ID_FINGERPRINT_H_
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <functional>
21 #include <string>
22 #include <string_view>
23 
24 #include "icing/text_classifier/lib3/utils/base/statusor.h"
25 #include "icing/store/namespace-id.h"
26 
27 namespace icing {
28 namespace lib {
29 
30 // A wrapper class of namespace id and fingerprint. It is usually used as a
31 // unique identifier of a resource, e.g. document (namespace id +
32 // fingerprint(uri)), corpus (namespace id + fingerprint(schema_type_name)).
33 class NamespaceIdFingerprint {
34  public:
35   struct Hasher {
operatorHasher36     std::size_t operator()(const NamespaceIdFingerprint& nsid_fp) const {
37       return std::hash<NamespaceId>()(nsid_fp.namespace_id_) ^
38              std::hash<uint64_t>()(nsid_fp.fingerprint_);
39     }
40   };
41 
42   static constexpr int kEncodedNamespaceIdLength = 3;
43   static constexpr int kMinEncodedLength = kEncodedNamespaceIdLength + 1;
44 
45   static libtextclassifier3::StatusOr<NamespaceIdFingerprint> DecodeFromCString(
46       std::string_view encoded_cstr);
47 
NamespaceIdFingerprint()48   explicit NamespaceIdFingerprint()
49       : namespace_id_(kInvalidNamespaceId), fingerprint_(0) {}
50 
NamespaceIdFingerprint(NamespaceId namespace_id,uint64_t fingerprint)51   explicit NamespaceIdFingerprint(NamespaceId namespace_id,
52                                   uint64_t fingerprint)
53       : namespace_id_(namespace_id), fingerprint_(fingerprint) {}
54 
55   explicit NamespaceIdFingerprint(NamespaceId namespace_id,
56                                   std::string_view target_str);
57 
58   std::string EncodeToCString() const;
59 
is_valid()60   bool is_valid() const { return namespace_id_ >= 0; }
61 
62   bool operator<(const NamespaceIdFingerprint& other) const {
63     if (namespace_id_ != other.namespace_id_) {
64       return namespace_id_ < other.namespace_id_;
65     }
66     return fingerprint_ < other.fingerprint_;
67   }
68 
69   bool operator==(const NamespaceIdFingerprint& other) const {
70     return namespace_id_ == other.namespace_id_ &&
71            fingerprint_ == other.fingerprint_;
72   }
73 
namespace_id()74   NamespaceId namespace_id() const { return namespace_id_; }
fingerprint()75   uint64_t fingerprint() const { return fingerprint_; }
76 
77  private:
78   NamespaceId namespace_id_;
79   uint64_t fingerprint_;
80 } __attribute__((packed));
81 static_assert(sizeof(NamespaceIdFingerprint) == 10, "");
82 
83 }  // namespace lib
84 }  // namespace icing
85 
86 #endif  // ICING_STORE_NAMESPACE_ID_FINGERPRINT_H_
87