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 <cstdint>
25#include <vector>
26
27#include "MappedRegion.h"
28
29extern "C" {
30#include "provsql_utils.h"
31}
32
33/**
34 * @brief Append-only, mmap-backed vector of elements of type @c T.
35 *
36 * @tparam T The element type. Must be trivially copyable.
37 */
38template <typename T>
40/**
41 * @brief On-disk layout stored at the start of the backing file.
42 *
43 * @c d is a flexible array member holding the actual elements.
44 */
45struct data_t {
46 uint64_t magic; ///< File-type identifier
47 uint16_t version; ///< Format version (currently 1)
48 uint16_t elem_size; ///< sizeof(T) at write time
49 uint32_t _reserved; ///< Padding to 16-byte boundary, must be 0
50 unsigned long nb_elements; ///< Number of elements currently stored
51 unsigned long capacity; ///< Maximum elements before the next grow
52 T d[]; ///< Flexible array of elements
53};
54
55MappedRegion region; ///< Backing storage (shared mmap, or heap buffer)
56data_t *data; ///< Typed view of @c region.base()
57
58/** @brief Initial number of element slots allocated. */
59static constexpr unsigned STARTING_CAPACITY=(1u << 16);
60
61/** @brief Double the backing region and refresh @c data. */
62void grow();
63/**
64 * @brief Unused overload kept for interface compatibility.
65 * @param u Ignored UUID parameter.
66 * @param i Ignored integer parameter.
67 */
68void set(pg_uuid_t u, unsigned long i);
69
70public:
71/**
72 * @brief Open (or create) the mmap-backed vector.
73 *
74 * @param filename Path to the backing file (created with
75 * @c STARTING_CAPACITY slots if absent).
76 * @param read_only If @c true, map the file read-only.
77 * @param magic Expected magic value for format validation.
78 */
79MMappedVector(const char *filename, bool read_only, uint64_t magic);
80/** @brief Sync and unmap the file. */
82
83/**
84 * @brief Read-only element access by index.
85 * @param k Zero-based element index.
86 * @return Const reference to element @p k.
87 */
88const T &operator[](unsigned long k) const;
89
90/**
91 * @brief Read-write element access by index.
92 * @param k Zero-based element index.
93 * @return Reference to element @p k.
94 */
95T &operator[](unsigned long k);
96
97/**
98 * @brief Append an element to the end of the vector.
99 *
100 * Grows the backing file if the current capacity is exhausted.
101 *
102 * @param value Element to append.
103 */
104void add(const T& value);
105
106/**
107 * @brief Return the number of elements currently stored.
108 * @return Element count.
109 */
110inline unsigned long nbElements() const {
111 return data->nb_elements;
112}
113
114/** @brief Flush the backing region to its file (@c MappedRegion::sync()). */
115void sync();
116};
117
118 #endif /* MMAPPED_VECTOR_H */
File-backed memory region with two interchangeable backends.
void set(pg_uuid_t u, unsigned long i)
Unused overload kept for interface compatibility.
~MMappedVector()
Sync and unmap the file.
data_t * data
Typed view of region.base().
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 grow()
Double the backing region and refresh data.
MMappedVector(const char *filename, bool read_only, uint64_t magic)
Open (or create) the mmap-backed vector.
MappedRegion region
Backing storage (shared mmap, or heap buffer).
const T & operator[](unsigned long k) const
Read-only element access by index.
void sync()
Flush the backing region to its file (MappedRegion::sync()).
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.
uint32_t _reserved
Padding to 16-byte boundary, must be 0.
uint16_t elem_size
sizeof(T) at write time
uint16_t version
Format version (currently 1).
uint64_t magic
File-type identifier.
unsigned long capacity
Maximum elements before the next grow.
UUID structure.