ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
MMappedVector< T > Class Template Reference

Append-only, mmap-backed vector of elements of type T. More...

#include "MMappedVector.h"

Collaboration diagram for MMappedVector< T >:

Classes

struct  data_t
 On-disk layout stored at the start of the backing file. More...

Public Member Functions

 MMappedVector (const char *filename, bool read_only, uint64_t magic, uint16_t version=1)
 Open (or create) the mmap-backed vector.
 ~MMappedVector ()
 Sync and unmap the file.
const T & operator[] (unsigned long k) const
 Read-only element access by index.
T & operator[] (unsigned long k)
 Read-write element access by index.
void add (const T &value)
 Append an element to the end of the vector.
unsigned long nbElements () const
 Return the number of elements currently stored.
uint16_t version () const
 Return the format version stamped in the file's header.
void setVersion (uint16_t v)
 Stamp v into the header, declaring the file upgraded.
void sync ()
 Flush the backing region to its file (MappedRegion::sync()).
void flush ()
 Force the backing file to stable storage (MappedRegion::flush()).
bool uncleanShutdown () const
 Whether this file still had FLAG_DIRTY set when it was opened, i.e.

Static Public Attributes

static constexpr uint32_t FLAG_DIRTY = 1u
 Bit of data_t::flags set while the file is open for writing.

Private Member Functions

void grow ()
 Double the backing region and refresh data.
void set (pg_uuid_t u, unsigned long i)
 Unused overload kept for interface compatibility.

Private Attributes

MappedRegion region
 Backing storage (shared mmap, or heap buffer).
data_t * data
 Typed view of region.base().
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_CAPACITY =(1u << 16)
 Initial number of element slots allocated.

Detailed Description

template<typename T>
class MMappedVector< T >

Append-only, mmap-backed vector of elements of type T.

Template Parameters
TThe element type. Must be trivially copyable.

Definition at line 39 of file MMappedVector.h.

Constructor & Destructor Documentation

◆ MMappedVector()

template<typename T>
MMappedVector< T >::MMappedVector ( const char * filename,
bool read_only,
uint64_t magic,
uint16_t version = 1 )

Open (or create) the mmap-backed vector.

A file created here is stamped with version; an existing file is accepted when its stamp is at most version, so a build that understands version n keeps reading the files an older one wrote. The caller reads the stamp back with version() to decide how to interpret fields whose meaning changed.

Parameters
filenamePath to the backing file (created with STARTING_CAPACITY slots if absent).
read_onlyIf true, map the file read-only.
magicExpected magic value for format validation.
versionHighest format version this build writes and reads.

Definition at line 41 of file MMappedVector.hpp.

Here is the call graph for this function:

◆ ~MMappedVector()

template<typename T>
MMappedVector< T >::~MMappedVector ( )

Sync and unmap the file.

Definition at line 91 of file MMappedVector.hpp.

Member Function Documentation

◆ add()

template<typename T>
void MMappedVector< T >::add ( const T & value)

Append an element to the end of the vector.

Grows the backing file if the current capacity is exhausted.

Parameters
valueElement to append.

Definition at line 111 of file MMappedVector.hpp.

Here is the caller graph for this function:

◆ flush()

template<typename T>
void MMappedVector< T >::flush ( )

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

Definition at line 126 of file MMappedVector.hpp.

Here is the caller graph for this function:

◆ grow()

template<typename T>
void MMappedVector< T >::grow ( )
private

Double the backing region and refresh data.

Definition at line 82 of file MMappedVector.hpp.

◆ nbElements()

template<typename T>
unsigned long MMappedVector< T >::nbElements ( ) const
inline

Return the number of elements currently stored.

Returns
Element count.

Definition at line 120 of file MMappedVector.h.

Here is the caller graph for this function:

◆ operator[]() [1/2]

template<typename T>
T & MMappedVector< T >::operator[] ( unsigned long k)
inline

Read-write element access by index.

Parameters
kZero-based element index.
Returns
Reference to element k.

Definition at line 105 of file MMappedVector.hpp.

◆ operator[]() [2/2]

template<typename T>
const T & MMappedVector< T >::operator[] ( unsigned long k) const
inline

Read-only element access by index.

Parameters
kZero-based element index.
Returns
Const reference to element k.

Definition at line 99 of file MMappedVector.hpp.

◆ set()

template<typename T>
void MMappedVector< T >::set ( pg_uuid_t u,
unsigned long i )
private

Unused overload kept for interface compatibility.

Parameters
uIgnored UUID parameter.
iIgnored integer parameter.
Here is the call graph for this function:

◆ setVersion()

template<typename T>
void MMappedVector< T >::setVersion ( uint16_t v)
inline

Stamp v into the header, declaring the file upgraded.

Only a pass that has rewritten every record to the new version's conventions may call this.

Definition at line 133 of file MMappedVector.h.

◆ sync()

template<typename T>
void MMappedVector< T >::sync ( )

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

Definition at line 120 of file MMappedVector.hpp.

◆ uncleanShutdown()

template<typename T>
bool MMappedVector< T >::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 156 of file MMappedVector.h.

◆ version()

template<typename T>
uint16_t MMappedVector< T >::version ( ) const
inline

Return the format version stamped in the file's header.

Definition at line 125 of file MMappedVector.h.

Here is the caller graph for this function:

Member Data Documentation

◆ data

template<typename T>
data_t* MMappedVector< T >::data
private

Typed view of region.base().

Definition at line 56 of file MMappedVector.h.

◆ FLAG_DIRTY

template<typename T>
uint32_t MMappedVector< T >::FLAG_DIRTY = 1u
staticconstexpr

Bit of data_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). The header layout is unchanged: the bit lives in what used to be reserved padding, which older builds neither read nor write.

Definition at line 152 of file MMappedVector.h.

◆ read_only_

template<typename T>
bool MMappedVector< T >::read_only_ = false
private

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

Definition at line 57 of file MMappedVector.h.

◆ region

template<typename T>
MappedRegion MMappedVector< T >::region
private

Backing storage (shared mmap, or heap buffer).

Definition at line 55 of file MMappedVector.h.

◆ STARTING_CAPACITY

template<typename T>
unsigned MMappedVector< T >::STARTING_CAPACITY =(1u << 16)
staticconstexprprivate

Initial number of element slots allocated.

Definition at line 61 of file MMappedVector.h.

◆ unclean_

template<typename T>
bool MMappedVector< T >::unclean_ = false
private

The previous run left the dirty bit set.

Definition at line 58 of file MMappedVector.h.


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