ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
MMappedVector.h
Go to the documentation of this file.
1/**
2 * @file MMappedVector.h
3 * @brief Append-only vector template backed by a memory-mapped file.
4 *
5 * @c MMappedVector<T> provides a @c std::vector-like interface over a
6 * memory-mapped file, enabling the provenance circuit data structures
7 * (@c GateInformation, child UUID lists, extra string data) to survive
8 * PostgreSQL restarts and be shared across processes.
9 *
10 * Design constraints:
11 * - **Append-only**: elements are added with @c add(); existing elements
12 * can be updated in-place via @c operator[]() but cannot be removed.
13 * - **Automatic growth**: when capacity is exhausted the backing file is
14 * grown by a factor of two and remapped.
15 * - @c T must be a trivially copyable type so that it can be stored
16 * directly in the memory-mapped region.
17 *
18 * The implementation is in @c MMappedVector.hpp (included from this header).
19 */
20#ifndef MMAPPED_VECTOR_H
21#define MMAPPED_VECTOR_H
22
23#include <cstddef>
24#include <vector>
25
26extern "C" {
27#include "provsql_utils.h"
28}
29
30/**
31 * @brief Append-only, mmap-backed vector of elements of type @c T.
32 *
33 * @tparam T The element type. Must be trivially copyable.
34 */
35template <typename T>
37/**
38 * @brief On-disk layout stored at the start of the backing file.
39 *
40 * @c d is a flexible array member holding the actual elements.
41 */
42struct data_t {
43 unsigned long nb_elements; ///< Number of elements currently stored
44 unsigned long capacity; ///< Maximum elements before the next grow
45 T d[]; ///< Flexible array of elements
46};
47
48int fd; ///< File descriptor of the backing mmap file
49data_t *data; ///< Pointer to the memory-mapped data header
50
51/** @brief Initial number of element slots allocated. */
52static constexpr unsigned STARTING_CAPACITY=(1u << 16);
53
54/**
55 * @brief Map @p length bytes from the backing file.
56 * @param length Number of bytes to map.
57 * @param read_only If @c true, map read-only.
58 */
59void mmap(size_t length, bool read_only);
60/** @brief Double the backing file and remap. */
61void grow();
62/**
63 * @brief Unused overload kept for interface compatibility.
64 * @param u Ignored UUID parameter.
65 * @param i Ignored integer parameter.
66 */
67void set(pg_uuid_t u, unsigned long i);
68
69public:
70/**
71 * @brief Open (or create) the mmap-backed vector.
72 *
73 * @param filename Path to the backing file (created with
74 * @c STARTING_CAPACITY slots if absent).
75 * @param read_only If @c true, map the file read-only.
76 */
77MMappedVector(const char *filename, bool read_only);
78/** @brief Sync and unmap the file. */
80
81/**
82 * @brief Read-only element access by index.
83 * @param k Zero-based element index.
84 * @return Const reference to element @p k.
85 */
86const T &operator[](unsigned long k) const;
87
88/**
89 * @brief Read-write element access by index.
90 * @param k Zero-based element index.
91 * @return Reference to element @p k.
92 */
93T &operator[](unsigned long k);
94
95/**
96 * @brief Append an element to the end of the vector.
97 *
98 * Grows the backing file if the current capacity is exhausted.
99 *
100 * @param value Element to append.
101 */
102void add(const T& value);
103
104/**
105 * @brief Return the number of elements currently stored.
106 * @return Element count.
107 */
108inline unsigned long nbElements() const {
109 return data->nb_elements;
110}
111
112/** @brief Flush dirty pages to the backing file with @c msync(). */
113void sync();
114};
115
116 #endif /* MMAPPED_VECTOR_H */
Append-only, mmap-backed vector of elements of type T.
void set(pg_uuid_t u, unsigned long i)
Unused overload kept for interface compatibility.
~MMappedVector()
Sync and unmap the file.
data_t * data
Pointer to the memory-mapped data header.
unsigned long nbElements() const
Return the number of elements currently stored.
static constexpr unsigned STARTING_CAPACITY
Initial number of element slots allocated.
void add(const T &value)
Append an element to the end of the vector.
void mmap(size_t length, bool read_only)
Map length bytes from the backing file.
int fd
File descriptor of the backing mmap file.
void grow()
Double the backing file and remap.
const T & operator[](unsigned long k) const
Read-only element access by index.
void sync()
Flush dirty pages to the backing file with msync().
Core types, constants, and utilities shared across ProvSQL.
On-disk layout stored at the start of the backing file.
unsigned long nb_elements
Number of elements currently stored.
T d[]
Flexible array of elements.
unsigned long capacity
Maximum elements before the next grow.
UUID structure.