|
1 | 1 | # cython: language_level=3 |
2 | 2 | """kh57 encoding and recovery. |
3 | 3 |
|
4 | | -Stub. Algorithm implementation coming later. |
| 4 | +encodes a 57-bit non-negative integer key into a 64-bit compound sort key: |
| 5 | +the top 7 bits carry a level id derived from `bit_length(uniform_hash(key))`, |
| 6 | +the bottom 57 bits carry the original key. the first bit is zero by |
| 7 | +construction, keeping the sign bit free so big-endian encoding preserves |
| 8 | +order even where keys are treated as signed. within a level the original |
| 9 | +key order is preserved; levels are deterministic random subsets of the key |
| 10 | +space, with each higher level holding roughly twice as many keys. |
5 | 11 | """ |
| 12 | + |
| 13 | +from libc.stdint cimport int64_t, uint8_t |
| 14 | + |
| 15 | +from kh57.siphash cimport isiphash64 |
| 16 | + |
| 17 | + |
| 18 | +SALT = b"0123456789abcdef" |
| 19 | + |
| 20 | +cdef unsigned char* _SALT = SALT |
| 21 | + |
| 22 | + |
| 23 | +cpdef int64_t uniform_hash(int64_t key): |
| 24 | + """Uniform, not necessarily cryptographic, hash of a signed 64-bit int. |
| 25 | +
|
| 26 | + uses siphash-2-4 underneath: 64-bit message, 16-byte key (the module |
| 27 | + SALT), 64-bit hash out. |
| 28 | + """ |
| 29 | + return isiphash64(key, _SALT) |
| 30 | + |
| 31 | + |
| 32 | +def _uniform_hash_with_salt(key: int, salt: bytes) -> int: |
| 33 | + """Like `uniform_hash` but with an explicit 16-byte salt. |
| 34 | + |
| 35 | + the top-level `uniform_hash` is intentionally not parametrized: the salt |
| 36 | + determines the level structure, so it must stay constant for a dataset. |
| 37 | + use this only if you need a different (but again constant) salt. |
| 38 | + """ |
| 39 | + if len(salt) != 16: |
| 40 | + raise ValueError("salt must be exactly 16 bytes") |
| 41 | + cdef const uint8_t[:] s = salt |
| 42 | + return isiphash64(key, <uint8_t*>&s[0]) |
| 43 | + |
| 44 | + |
| 45 | +def kh57(key: int) -> int: |
| 46 | + """Encode a 57-bit non-negative integer key into a 64-bit compound key. |
| 47 | + |
| 48 | + Args: |
| 49 | + key: non-negative integer in the range [0, 2^57 - 1]. |
| 50 | + |
| 51 | + Returns: |
| 52 | + the 64-bit encoded key: 7 level bits then 57 key bits. |
| 53 | + |
| 54 | + Raises: |
| 55 | + ValueError: if the key is negative or does not fit in 57 bits. |
| 56 | + """ |
| 57 | + # supports range 0, 1, 2, ..., (2^57 - 2), (2^57 - 1) |
| 58 | + if key < 0: |
| 59 | + raise ValueError("key must be non-negative") |
| 60 | + if key >> 57: |
| 61 | + raise ValueError("key must fit in 57 bits") |
| 62 | + |
| 63 | + some_deterministic_hash = uniform_hash(key) |
| 64 | + |
| 65 | + # Cast to unsigned int |
| 66 | + buffer = some_deterministic_hash.to_bytes(8, "big", signed=True) |
| 67 | + some_deterministic_hash = int.from_bytes(buffer, "big", signed=False) |
| 68 | + |
| 69 | + # Take the most significant bit as the level id. |
| 70 | + # Higher levels hold most of the records; the highest available level |
| 71 | + # holds almost half of them. |
| 72 | + level_idx = some_deterministic_hash.bit_length() |
| 73 | + |
| 74 | + if level_idx: |
| 75 | + # Levels with id N are stored as (N-1) to avoid wasting a bit: |
| 76 | + # 2^6 = 64 needs 7 bits, but level 0 is a near-impossible |
| 77 | + # special case (hash == 0), so the ids can be shifted down safely. |
| 78 | + level_idx -= 1 |
| 79 | + |
| 80 | + # Format: 8 bytes in total |
| 81 | + # +-------+---------------------------------------------------------------+ |
| 82 | + # | 7 bit | . . . . . 57 bits | |
| 83 | + # | level | . . . . . big-endian | |
| 84 | + # | id | . . . . .unsigned integer | |
| 85 | + # +-------+---------------------------------------------------------------+ |
| 86 | + # | 0 . 1 . 2 . 3 . 4 . 5 . 6 . 7 | |
| 87 | + # +-----------------------------------------------------------------------+ |
| 88 | + # Note: the first bit is zero by design, so storage layers that treat |
| 89 | + # the first bit as a sign bit still sort correctly: big-endian |
| 90 | + # encoding preserves the order. |
| 91 | + h = (level_idx << 57) | key |
| 92 | + |
| 93 | + return h |
| 94 | + |
| 95 | + |
| 96 | +def recover(h: int) -> tuple[int, int]: |
| 97 | + """Recover the level id and the original key from a `kh57` encoded key. |
| 98 | + |
| 99 | + Args: |
| 100 | + h: the 64-bit encoded key returned by `kh57`. |
| 101 | + |
| 102 | + Returns: |
| 103 | + (level_id, key) tuple. |
| 104 | + """ |
| 105 | + level_idx = h >> 57 |
| 106 | + mask = (1 << 57) - 1 |
| 107 | + key = h & mask |
| 108 | + return level_idx, key |
0 commit comments