| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. |
| 3 | |
| 4 | #pragma once |
| 5 | |
| 6 | #include <cstdint> |
| 7 | #include "utility.h" |
| 8 | |
| 9 | namespace FASTER { |
| 10 | namespace core { |
| 11 | |
| 12 | /// Hash of a key is 8 bytes, compatible with hash bucket entry. |
| 13 | struct KeyHash { |
| 14 | KeyHash() |
| 15 | : control_{ 0 } { |
| 16 | } |
| 17 | explicit KeyHash(uint64_t code) |
| 18 | : control_{ code } { |
| 19 | } |
| 20 | KeyHash(const KeyHash& other) |
| 21 | : control_{ other.control_ } { |
| 22 | } |
| 23 | |
| 24 | KeyHash& operator=(const KeyHash& other) { |
| 25 | control_ = other.control_; |
| 26 | } |
| 27 | |
| 28 | /// Truncate the key hash's address to get the page_index into a hash table of specified size. |
| 29 | inline uint64_t idx(uint64_t size) const { |
| 30 | assert(Utility::IsPowerOfTwo(size)); |
| 31 | return address_ & (size - 1); |
| 32 | } |
| 33 | |
| 34 | /// The tag (14 bits) serves as a discriminator inside a hash bucket. (Hash buckets use 2 bits |
| 35 | /// for control and 48 bits for log-structured store offset; the remaining 14 bits discriminate |
| 36 | /// between different key hashes stored in the same bucket.) |
| 37 | inline uint16_t tag() const { |
| 38 | return static_cast<uint16_t>(tag_); |
| 39 | } |
| 40 | |
| 41 | private: |
| 42 | union { |
| 43 | struct { |
| 44 | uint64_t address_ : 48; |
| 45 | uint64_t tag_ : 14; |
| 46 | uint64_t not_used_ : 2; |
| 47 | }; |
| 48 | uint64_t control_; |
| 49 | }; |
| 50 | }; |
| 51 | static_assert(sizeof(KeyHash) == 8, "sizeof(KeyHash) != 8" ); |
| 52 | |
| 53 | } |
| 54 | } // namespace FASTER::core |
| 55 | |