Module keys

Source
Expand description

Big-endian key encoding for RocksDB with correct lexicographic ordering.

Keys are encoded as entity_id (8B) + space_id (4B) + timestamp (8B) = 20 bytes. All integers use big-endian encoding so that lexicographic byte comparison matches numeric ordering.

Timestamps (i64) use a sign-bit flip (XOR 0x80 on the first byte) so that negative timestamps sort correctly:

  • Without flip: -1 = FF..FF sorts after 1 = 00..01 (wrong)
  • With flip: -1 = 7F..FF sorts before 1 = 80..01 (correct)

§Example

use cvx_storage::keys;

let key = keys::encode_key(42, 0, 1_000_000);
let (entity_id, space_id, timestamp) = keys::decode_key(&key);
assert_eq!(entity_id, 42);
assert_eq!(space_id, 0);
assert_eq!(timestamp, 1_000_000);

Constants§

KEY_SIZE
Total size of an encoded key in bytes.
PREFIX_SIZE
Prefix size for entity_id + space_id (used for prefix bloom filters).

Functions§

decode_key
Decode a 20-byte key back into its components.
decode_timestamp 🔒
Decode a sign-bit-flipped timestamp.
encode_key
Encode a key as 20 big-endian bytes with sign-bit flip on timestamp.
encode_prefix
Encode an entity_id + space_id prefix (12 bytes) for prefix scans.
encode_timestamp 🔒
Encode a timestamp with sign-bit flip for correct lexicographic ordering.