35 fd=open(filename, O_CREAT|(read_only?O_RDONLY:O_RDWR), 0600);
37 throw std::runtime_error(strerror(errno));
39 auto size = lseek(
fd, 0, SEEK_END);
40 lseek(
fd, 0, SEEK_SET);
47 if(ftruncate(
fd, size))
48 throw std::runtime_error(strerror(errno));
51 mmap(size, read_only);
68 PROT_READ|(read_only?0:PROT_WRITE),
72 if(
table == MAP_FAILED)
73 throw std::runtime_error(strerror(errno));
80 std::vector<value_t> elements;
84 elements.push_back(
table->
t[i]);
90 if(ftruncate(
fd, new_size))
91 throw std::runtime_error(strerror(errno));
92 mmap(new_size,
false);
98 for(
const auto &u: elements)
Open-addressing hash table mapping UUIDs to integers, backed by an mmap file.
void set(pg_uuid_t u, unsigned long i)
Store the mapping u → i in the table.
unsigned long hash(pg_uuid_t u) const
Compute the starting slot index for UUID u.
void grow()
Double the table capacity and rehash all existing entries.
int fd
File descriptor of the backing mmap file.
std::pair< unsigned long, bool > add(pg_uuid_t u)
Insert UUID u, assigning it the next available integer.
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.
void mmap(size_t length, bool read_only)
Map length bytes from the backing file (read-write or read-only).
~MMappedUUIDHashTable()
Sync and unmap the file.
void sync()
Flush dirty pages to the backing file with msync().
MMappedUUIDHashTable(const char *filename, bool read_only)
Open (or create) the mmap-backed hash table.
static constexpr unsigned long NOTHING
Sentinel returned by operator[]() when the UUID is not present.
static constexpr double MAXIMUM_LOAD_FACTOR
Rehash when this fraction of slots is occupied.
table_t * table
Pointer to the memory-mapped table header.
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.
static constexpr std::size_t sizeForLogSize(unsigned ls)
Compute the file size required for a table with 2^ls slots.
unsigned log_size
log2 of the number of slots
unsigned long nb_elements
Current number of stored key-value pairs.
unsigned long next_value
Next integer value to assign to a new UUID.
constexpr unsigned long capacity()
Maximum number of slots in the table (2^log_size).
unsigned long value
Associated integer (gate index), or 0 if slot is empty.