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 dirty pages with @c msync().
17 *
18 * Internal helpers:
19 * - @c mmap(): map (or remap) @p length bytes.
20 * - @c grow(): double the capacity and remap.
21 */
22#ifndef MMAPPED_VECTOR_HPP
23#define MMAPPED_VECTOR_HPP
24
25#include "MMappedVector.h"
26
27#include <cassert>
28#include <cerrno>
29#include <cstring>
30#include <new>
31#include <stdexcept>
32#include <vector>
33
34#include <fcntl.h>
35#include <unistd.h>
36
37#include <sys/mman.h>
38
39template <typename T>
40MMappedVector<T>::MMappedVector(const char *filename, bool read_only)
41{
42 fd=open(filename, O_CREAT|(read_only?O_RDONLY:O_RDWR), 0600); // flawfinder: ignore
43 if(fd==-1)
44 throw std::runtime_error(strerror(errno));
45
46 auto length = lseek(fd, 0, SEEK_END);
47 lseek(fd, 0, SEEK_SET);
48
49 bool empty=false;
50
51 if(length==0) {
52 empty=true;
53 length=offsetof(data_t, d)+sizeof(T)*STARTING_CAPACITY;
54 if(ftruncate(fd, length))
55 throw std::runtime_error(strerror(errno));
56 }
57
58 mmap(length, read_only);
60 if(empty) {
61 data->capacity = STARTING_CAPACITY;
62 data->nb_elements = 0;
63 }
64}
65
66template <typename T>
67void MMappedVector<T>::mmap(size_t length, bool read_only)
68{
69 data = reinterpret_cast<data_t *>(::mmap(
70 NULL,
71 length,
72 PROT_READ|(read_only?0:PROT_WRITE),
73 MAP_SHARED,
74 fd,
75 0));
76 if(data == MAP_FAILED)
77 throw std::runtime_error(strerror(errno));
78}
80template <typename T>
82{
83 sync();
84 auto new_capacity = data->capacity*2;
85 munmap(data, offsetof(data_t,d)+sizeof(T)*data->capacity);
87 auto new_length = offsetof(data_t,d)+sizeof(T)*new_capacity;
88 if(ftruncate(fd, new_length))
89 throw std::runtime_error(strerror(errno));
90 mmap(new_length, false);
91
92 data->capacity = new_capacity;
94
95template <typename T>
97{
98 munmap(data, offsetof(data_t,d)+sizeof(T)*data->capacity);
99 close(fd);
100}
101
102template <typename T>
103inline const T &MMappedVector<T>::operator[](unsigned long k) const
104{
105 return data->d[k];
106}
107
108template <typename T>
109inline T &MMappedVector<T>::operator[](unsigned long k)
110{
111 return data->d[k];
112}
114template <typename T>
115void MMappedVector<T>::add(const T &value)
116{
117 if(data->nb_elements == data->capacity)
118 grow();
119
120 data->d[data->nb_elements++] = value;
121}
122
123template <typename T>
125{
126 msync(data, offsetof(data_t,d)+sizeof(T)*data->capacity, MS_SYNC);
127}
128
129#endif /* MMAPPED_VECTOR_HPP */
Append-only vector template backed by a memory-mapped file.
~MMappedVector()
Sync and unmap the file.
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.
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().
MMappedVector(const char *filename, bool read_only)
Open (or create) the mmap-backed vector.
On-disk layout stored at the start of the backing file.