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)
 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.
 
void sync ()
 Flush dirty pages to the backing file with msync().
 

Private Member Functions

void mmap (size_t length, bool read_only)
 Map length bytes from the backing file.
 
void grow ()
 Double the backing file and remap.
 
void set (pg_uuid_t u, unsigned long i)
 Unused overload kept for interface compatibility.
 

Private Attributes

int fd
 File descriptor of the backing mmap file.
 
data_tdata
 Pointer to the memory-mapped data header.
 

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 36 of file MMappedVector.h.

Constructor & Destructor Documentation

◆ MMappedVector()

template<typename T >
MMappedVector< T >::MMappedVector ( const char *  filename,
bool  read_only 
)

Open (or create) the mmap-backed vector.

Parameters
filenamePath to the backing file (created with STARTING_CAPACITY slots if absent).
read_onlyIf true, map the file read-only.

Definition at line 40 of file MMappedVector.hpp.

◆ ~MMappedVector()

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

Sync and unmap the file.

Definition at line 96 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 115 of file MMappedVector.hpp.

Here is the caller graph for this function:

◆ grow()

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

Double the backing file and remap.

Definition at line 81 of file MMappedVector.hpp.

◆ mmap()

template<typename T >
void MMappedVector< T >::mmap ( size_t  length,
bool  read_only 
)
private

Map length bytes from the backing file.

Parameters
lengthNumber of bytes to map.
read_onlyIf true, map read-only.

Definition at line 67 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 108 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 109 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 103 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.

◆ sync()

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

Flush dirty pages to the backing file with msync().

Definition at line 124 of file MMappedVector.hpp.

Here is the caller graph for this function:

Member Data Documentation

◆ data

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

Pointer to the memory-mapped data header.

Definition at line 49 of file MMappedVector.h.

◆ fd

template<typename T >
int MMappedVector< T >::fd
private

File descriptor of the backing mmap file.

Definition at line 48 of file MMappedVector.h.

◆ STARTING_CAPACITY

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

Initial number of element slots allocated.

Definition at line 52 of file MMappedVector.h.


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