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