24#ifndef MMAPPED_UUID_HASH_TABLE_H
25#define MMAPPED_UUID_HASH_TABLE_H
117 return *
reinterpret_cast<unsigned long*
>(&u) % (1 <<
table->log_size);
131static constexpr unsigned long NOTHING=
static_cast<unsigned long>(-1);
202 return table->next_value;
207 return table->capacity();
213 return table->t[k].value;
219 return table->t[k].uuid;
227 return table->nb_elements;
File-backed memory region with two interchangeable backends.
bool read_only_
Mapped read-only: the header must not be written.
unsigned long hash(pg_uuid_t u) const
Compute the starting slot index for UUID u.
std::pair< unsigned long, bool > publish(pg_uuid_t u, unsigned long value)
Insert UUID u with a caller-chosen value.
static constexpr uint32_t FLAG_DIRTY
Bit of table_t::flags set while the file is open for writing.
void grow()
Double the table capacity and rehash all existing entries.
std::pair< unsigned long, bool > add(pg_uuid_t u)
Insert UUID u, assigning it the next available integer.
MMappedUUIDHashTable(const char *filename, bool read_only, uint64_t magic)
Open (or create) the mmap-backed hash table.
bool unclean_
The previous run left the dirty bit set.
static constexpr unsigned STARTING_LOG_SIZE
Initial log2 capacity (65 536 slots).
unsigned long find(pg_uuid_t u) const
Find the slot index of u, or NOTHING if absent.
unsigned long operator[](pg_uuid_t u) const
Look up the integer index for UUID u.
unsigned long capacity() const
The number of slots the table currently has.
unsigned long nbElements() const
Return the number of UUID→integer pairs currently stored.
std::string path_
Backing file, for the atomic rehash in grow().
bool uncleanShutdown() const
Whether this file still had FLAG_DIRTY set when it was opened, i.e.
~MMappedUUIDHashTable()
Sync and unmap the file.
MappedRegion region
Backing storage (shared mmap, or heap buffer).
void sync()
Flush the backing region to its file (MappedRegion::sync()).
unsigned long nextValue() const
The value the next add would assign.
unsigned long slotValue(unsigned long k) const
The k-th slot's value, or NOTHING when the slot is empty.
pg_uuid_t slotKey(unsigned long k) const
The k-th slot's key; only meaningful when slotValue(k) is not NOTHING.
static constexpr unsigned long NOTHING
Sentinel returned by operator[]() when the UUID is not present.
void flush()
Force the backing file to stable storage (MappedRegion::flush()).
static constexpr double MAXIMUM_LOAD_FACTOR
Rehash when this fraction of slots is occupied.
table_t * table
Typed view of region.base().
Core types, constants, and utilities shared across ProvSQL.
On-disk layout of the hash table stored in the mmap file.
static constexpr unsigned logSizeForSize(std::size_t size)
Compute the log2 of the slot count from the file size.
value_t t[]
Flexible array of hash-table slots.
uint32_t flags
Bit 0: opened for writing and not closed since.
static constexpr std::size_t sizeForLogSize(unsigned ls)
Compute the file size required for a table with 2^ls slots.
uint64_t magic
File-type identifier.
unsigned log_size
log2 of the number of slots
uint16_t elem_size
sizeof(value_t) at write time
unsigned long nb_elements
Current number of stored key-value pairs.
unsigned long next_value
Next integer value to assign to a new UUID.
uint16_t version
Format version (currently 1).
constexpr unsigned long capacity()
Maximum number of slots in the table (2^log_size).
One slot in the hash table: a UUID key and its associated integer value.
unsigned long value
Associated integer (gate index), or 0 if slot is empty.