hashtable module is the core storage layer of RadishDB. It implements a chained hash table with separate chaining for collision resolution, automatic resizing, TTL-aware lookups, and introspection utilities.
Data structures
Entry
A single key-value record stored in the hash table.
Heap-allocated string containing the entry’s key.
Heap-allocated string containing the entry’s value.
Absolute Unix timestamp after which the entry is considered expired. A value of
0 means the entry never expires.Pointer to the next entry in the same bucket chain.
NULL if this is the last entry.HashTable
The top-level hash table container.
Array of pointers to bucket chain heads. Length is
size.Total number of buckets allocated.
Number of live (non-expired) entries in the table.
Cumulative count of resize operations performed on this table.
Info
Snapshot of hash table statistics returned by ht_info.
Number of live keys currently in the table.
Number of live keys that have a non-zero
expires_at value.Number of entries found during the scan that have already passed their expiry time.
Total number of buckets in the table.
Ratio of live keys to total buckets (
keys / buckets).Length of the longest bucket chain found during the scan.
Total number of resize operations performed on this table.
Functions
ht_create
Allocates and initializes a new hash table.
Initial number of buckets. Must be greater than
0. Choose a power of two for best distribution.HashTable, or NULL if allocation fails. You are responsible for freeing this with ht_free.
hash
Computes a hash value for a null-terminated string.
Null-terminated string to hash.
unsigned long hash value. The table internally uses this modulo ht->size to select a bucket.
You rarely need to call
hash directly. It is used internally by ht_set, ht_get, and ht_delete.ht_set
Inserts or updates a key-value pair in the hash table.
The hash table to write to.
Null-terminated key string. The function makes an internal copy.
Null-terminated value string. The function makes an internal copy.
Absolute Unix timestamp for expiry. Pass
0 for no expiry.expires_at are updated in-place. The table resizes automatically when the load factor exceeds the internal threshold.
ht_get
Retrieves the value for a key.
The hash table to read from.
Null-terminated key string to look up.
NULL if the key is not found or has expired. Passive expiry: if the key is found but its expires_at is in the past, the entry is deleted before NULL is returned.
ht_delete
Removes a key from the hash table.
The hash table to delete from.
Null-terminated key string to remove.
1 if the key was found and deleted, 0 if the key was not found.
ht_ttl
Returns the remaining time-to-live for a key in seconds.
The hash table to query.
Null-terminated key string.
| Value | Meaning |
|---|---|
-1 | Key exists and has no expiry (expires_at == 0) |
-2 | Key not found, or key was found but already expired (passive delete occurs) |
N > 0 | Seconds remaining until expiry |
ht_info
Scans the table and returns a point-in-time statistics snapshot.
The hash table to inspect.
Info struct populated by a full table scan. The scan counts live keys, TTL keys, already-expired keys, the longest chain, and computes the load factor.
ht_info performs a full scan of all buckets. On very large tables this has O(n) cost proportional to size + count.ht_resize
Rehashes the table into a new bucket array of the given size.
The hash table to resize.
New number of buckets. Should be a power of two.
ht->resizes is incremented. The table calls this automatically when the load factor grows beyond the internal threshold, but you can trigger it manually to pre-size the table before a bulk insert.
ht_free
Frees all memory owned by the hash table, including all entries, keys, values, and the bucket array.
The hash table to destroy. The pointer itself becomes invalid after this call.