21#ifndef MMAPPED_VECTOR_HPP
22#define MMAPPED_VECTOR_HPP
43 auto length =
region.openFile(filename, read_only);
44 bool empty = (length == 0);
55 data->magic = magic_value;
57 data->elem_size =
static_cast<uint16_t
>(
sizeof(T));
60 data->nb_elements = 0;
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)");
75 auto new_capacity =
data->capacity*2;
78 data->capacity = new_capacity;
102 if(
data->nb_elements ==
data->capacity)
105 data->d[
data->nb_elements++] = value;
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.