ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
MMappedUUIDHashTable Class Reference

Persistent open-addressing hash table mapping UUIDs to integers. More...

#include "MMappedUUIDHashTable.h"

Collaboration diagram for MMappedUUIDHashTable:

Classes

struct  value_t
 One slot in the hash table: a UUID key and its associated integer value. More...
struct  table_t
 On-disk layout of the hash table stored in the mmap file. More...

Public Member Functions

 MMappedUUIDHashTable (const char *filename, bool read_only, uint64_t magic)
 Open (or create) the mmap-backed hash table.
 ~MMappedUUIDHashTable ()
 Sync and unmap the file.
bool uncleanShutdown () const
 Whether this file still had FLAG_DIRTY set when it was opened, i.e.
unsigned long operator[] (pg_uuid_t u) const
 Look up the integer index for UUID u.
std::pair< unsigned long, bool > add (pg_uuid_t u)
 Insert UUID u, assigning it the next available integer.
std::pair< unsigned long, bool > publish (pg_uuid_t u, unsigned long value)
 Insert UUID u with a caller-chosen value.
unsigned long nextValue () const
 The value the next add would assign.
unsigned long capacity () const
 The number of slots the table currently has.
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.
unsigned long nbElements () const
 Return the number of UUID→integer pairs currently stored.
void sync ()
 Flush the backing region to its file (MappedRegion::sync()).
void flush ()
 Force the backing file to stable storage (MappedRegion::flush()).

Static Public Attributes

static constexpr unsigned long NOTHING =static_cast<unsigned long>(-1)
 Sentinel returned by operator[]() when the UUID is not present.
static constexpr uint32_t FLAG_DIRTY = 1u
 Bit of table_t::flags set while the file is open for writing.

Private Member Functions

unsigned long hash (pg_uuid_t u) const
 Compute the starting slot index for UUID u.
unsigned long find (pg_uuid_t u) const
 Find the slot index of u, or NOTHING if absent.
void grow ()
 Double the table capacity and rehash all existing entries.

Private Attributes

MappedRegion region
 Backing storage (shared mmap, or heap buffer).
table_t * table
 Typed view of region.base().
std::string path_
 Backing file, for the atomic rehash in grow().
bool read_only_ = false
 Mapped read-only: the header must not be written.
bool unclean_ = false
 The previous run left the dirty bit set.

Static Private Attributes

static constexpr unsigned STARTING_LOG_SIZE =16
 Initial log2 capacity (65 536 slots).
static constexpr double MAXIMUM_LOAD_FACTOR =.5
 Rehash when this fraction of slots is occupied.

Detailed Description

Persistent open-addressing hash table mapping UUIDs to integers.

Definition at line 41 of file MMappedUUIDHashTable.h.

Constructor & Destructor Documentation

◆ MMappedUUIDHashTable()

MMappedUUIDHashTable::MMappedUUIDHashTable ( const char * filename,
bool read_only,
uint64_t magic )

Open (or create) the mmap-backed hash table.

Parameters
filenamePath to the backing file (created if absent).
read_onlyIf true, map the file read-only (no new entries can be inserted).
magicExpected magic value for format validation.

Definition at line 34 of file MMappedUUIDHashTable.cpp.

Here is the call graph for this function:

◆ ~MMappedUUIDHashTable()

MMappedUUIDHashTable::~MMappedUUIDHashTable ( )

Sync and unmap the file.

Definition at line 119 of file MMappedUUIDHashTable.cpp.

Member Function Documentation

◆ add()

std::pair< unsigned long, bool > MMappedUUIDHashTable::add ( pg_uuid_t u)

Insert UUID u, assigning it the next available integer.

If u is already present the existing value is returned without modification.

Parameters
uUUID to insert.
Returns
A pair {value, inserted} where inserted is true if a new entry was created.

Definition at line 144 of file MMappedUUIDHashTable.cpp.

Here is the call graph for this function:

◆ capacity()

unsigned long MMappedUUIDHashTable::capacity ( ) const
inline

The number of slots the table currently has.

Definition at line 206 of file MMappedUUIDHashTable.h.

◆ find()

unsigned long MMappedUUIDHashTable::find ( pg_uuid_t u) const
private

Find the slot index of u, or NOTHING if absent.

Parameters
uUUID to look up.
Returns
Slot index, or NOTHING if u is not in the table.

Definition at line 126 of file MMappedUUIDHashTable.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ flush()

void MMappedUUIDHashTable::flush ( )

Force the backing file to stable storage (MappedRegion::flush()).

Definition at line 179 of file MMappedUUIDHashTable.cpp.

Here is the caller graph for this function:

◆ grow()

void MMappedUUIDHashTable::grow ( )
private

Double the table capacity and rehash all existing entries.

Definition at line 86 of file MMappedUUIDHashTable.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ hash()

unsigned long MMappedUUIDHashTable::hash ( pg_uuid_t u) const
inlineprivate

Compute the starting slot index for UUID u.

Reinterprets the first 8 bytes of u as a 64-bit integer and takes it modulo the current capacity.

Parameters
uUUID to hash.
Returns
Slot index in [0, capacity).

Definition at line 116 of file MMappedUUIDHashTable.h.

Here is the caller graph for this function:

◆ nbElements()

unsigned long MMappedUUIDHashTable::nbElements ( ) const
inline

Return the number of UUID→integer pairs currently stored.

Returns
Element count.

Definition at line 226 of file MMappedUUIDHashTable.h.

◆ nextValue()

unsigned long MMappedUUIDHashTable::nextValue ( ) const
inline

The value the next add would assign.

Kept equal to the number of gate records by MMappedCircuit.

Definition at line 201 of file MMappedUUIDHashTable.h.

◆ operator[]()

unsigned long MMappedUUIDHashTable::operator[] ( pg_uuid_t u) const

Look up the integer index for UUID u.

Parameters
uThe UUID to look up.
Returns
The associated integer, or NOTHING if u is absent.

Definition at line 137 of file MMappedUUIDHashTable.cpp.

Here is the call graph for this function:

◆ publish()

std::pair< unsigned long, bool > MMappedUUIDHashTable::publish ( pg_uuid_t u,
unsigned long value )

Insert UUID u with a caller-chosen value.

Lets the caller build the record value indexes before the key that points at it becomes visible, so that a process killed between the two leaves an unreferenced record rather than a mapping entry pointing past the end of the record vector – which would shift every later gate for the rest of the store's life. The slot's value field is written last, and an aligned 8-byte store is atomic on the supported platforms, so a reader sees either NOTHING or the complete entry.

Parameters
uUUID to insert.
valueValue to associate with it.
Returns
A pair {value, inserted}; inserted is false (and the existing value returned) when u was already there.

Definition at line 152 of file MMappedUUIDHashTable.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ slotKey()

pg_uuid_t MMappedUUIDHashTable::slotKey ( unsigned long k) const
inline

The k-th slot's key; only meaningful when slotValue(k) is not NOTHING.

Definition at line 218 of file MMappedUUIDHashTable.h.

◆ slotValue()

unsigned long MMappedUUIDHashTable::slotValue ( unsigned long k) const
inline

The k-th slot's value, or NOTHING when the slot is empty.

For consistency checks and for the clean-up's mark phase.

Definition at line 212 of file MMappedUUIDHashTable.h.

◆ sync()

void MMappedUUIDHashTable::sync ( )

Flush the backing region to its file (MappedRegion::sync()).

Definition at line 174 of file MMappedUUIDHashTable.cpp.

◆ uncleanShutdown()

bool MMappedUUIDHashTable::uncleanShutdown ( ) const
inline

Whether this file still had FLAG_DIRTY set when it was opened, i.e.

whoever wrote it last did not close it.

Definition at line 157 of file MMappedUUIDHashTable.h.

Member Data Documentation

◆ FLAG_DIRTY

uint32_t MMappedUUIDHashTable::FLAG_DIRTY = 1u
staticconstexpr

Bit of table_t::flags set while the file is open for writing.

Cleared on a clean close, so a file found with it still set was left behind by a process that died (or by an immediate shutdown). Reported by uncleanShutdown; the header layout is unchanged, since the bit lives in what used to be reserved padding.

Definition at line 153 of file MMappedUUIDHashTable.h.

◆ MAXIMUM_LOAD_FACTOR

double MMappedUUIDHashTable::MAXIMUM_LOAD_FACTOR =.5
staticconstexprprivate

Rehash when this fraction of slots is occupied.

Definition at line 106 of file MMappedUUIDHashTable.h.

◆ NOTHING

unsigned long MMappedUUIDHashTable::NOTHING =static_cast<unsigned long>(-1)
staticconstexpr

Sentinel returned by operator[]() when the UUID is not present.

Definition at line 131 of file MMappedUUIDHashTable.h.

◆ path_

std::string MMappedUUIDHashTable::path_
private

Backing file, for the atomic rehash in grow().

Definition at line 99 of file MMappedUUIDHashTable.h.

◆ read_only_

bool MMappedUUIDHashTable::read_only_ = false
private

Mapped read-only: the header must not be written.

Definition at line 100 of file MMappedUUIDHashTable.h.

◆ region

MappedRegion MMappedUUIDHashTable::region
private

Backing storage (shared mmap, or heap buffer).

Definition at line 97 of file MMappedUUIDHashTable.h.

◆ STARTING_LOG_SIZE

unsigned MMappedUUIDHashTable::STARTING_LOG_SIZE =16
staticconstexprprivate

Initial log2 capacity (65 536 slots).

Definition at line 104 of file MMappedUUIDHashTable.h.

◆ table

table_t* MMappedUUIDHashTable::table
private

Typed view of region.base().

Definition at line 98 of file MMappedUUIDHashTable.h.

◆ unclean_

bool MMappedUUIDHashTable::unclean_ = false
private

The previous run left the dirty bit set.

Definition at line 101 of file MMappedUUIDHashTable.h.


The documentation for this class was generated from the following files: