ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
MMappedVector.hpp
Go to the documentation of this file.
1/**
2 * @file MMappedVector.hpp
3 * @brief Template implementation of @c MMappedVector<T>.
4 *
5 * Provides the out-of-line definitions of all @c MMappedVector<T>
6 * methods declared in @c MMappedVector.h. This file must be included
7 * by any translation unit that instantiates @c MMappedVector<T> for a
8 * specific @c T.
9 *
10 * Implemented methods:
11 * - @c MMappedVector(): open/create the backing file and map it.
12 * - @c ~MMappedVector(): sync and unmap.
13 * - @c operator[](k) const: read element @p k.
14 * - @c operator[](k): write element @p k.
15 * - @c add(): append one element, growing the file if necessary.
16 * - @c sync(): flush the backing region (@c MappedRegion::sync()).
17 *
18 * Internal helpers:
19 * - @c grow(): double the capacity and remap the backing region.
20 */
21#ifndef MMAPPED_VECTOR_HPP
22#define MMAPPED_VECTOR_HPP
23
24#include "MMappedVector.h"
25
26#include <cassert>
27#include <cerrno>
28#include <cstdint>
29#include <cstring>
30#include <new>
31#include <stdexcept>
32#include <string>
33#include <vector>
34
35#include <fcntl.h>
36#include <unistd.h>
37
38#include <sys/mman.h>
39
40template <typename T>
41MMappedVector<T>::MMappedVector(const char *filename, bool read_only, uint64_t magic_value)
42{
43 auto length = region.openFile(filename, read_only);
44 bool empty = (length == 0);
45
46 if(empty) {
47 length = offsetof(data_t, d) + sizeof(T) * STARTING_CAPACITY;
48 region.resizeFile(length);
49 }
50
51 region.map(length);
52 data = reinterpret_cast<data_t *>(region.base());
53
54 if(empty) {
55 data->magic = magic_value;
56 data->version = 1;
57 data->elem_size = static_cast<uint16_t>(sizeof(T));
58 data->_reserved = 0;
59 data->capacity = STARTING_CAPACITY;
60 data->nb_elements = 0;
61 } else {
62 if(data->magic != magic_value)
63 throw std::runtime_error("ProvSQL mmap: wrong file type (magic mismatch)");
64 if(data->version != 1)
65 throw std::runtime_error("ProvSQL mmap: unsupported format version "
66 + std::to_string(data->version));
67 if(data->elem_size != sizeof(T))
68 throw std::runtime_error("ProvSQL mmap: element size mismatch (recompile required)");
69 }
70}
71
72template <typename T>
74{
75 auto new_capacity = data->capacity*2;
76 region.remap(offsetof(data_t,d)+sizeof(T)*new_capacity);
77 data = reinterpret_cast<data_t *>(region.base());
78 data->capacity = new_capacity;
80
81template <typename T>
86
87template <typename T>
88inline const T &MMappedVector<T>::operator[](unsigned long k) const
89{
90 return data->d[k];
91}
92
93template <typename T>
94inline T &MMappedVector<T>::operator[](unsigned long k)
95{
96 return data->d[k];
97}
98
99template <typename T>
100void MMappedVector<T>::add(const T &value)
101{
102 if(data->nb_elements == data->capacity)
103 grow();
105 data->d[data->nb_elements++] = value;
106}
107
108template <typename T>
110{
111 region.sync();
112}
113
114#endif /* MMAPPED_VECTOR_HPP */
Append-only vector template backed by a memory-mapped file.
~MMappedVector()
Sync and unmap the file.
data_t * data
Typed view of region.base().
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()).
On-disk layout stored at the start of the backing file.