21#ifndef MMAPPED_VECTOR_HPP
22#define MMAPPED_VECTOR_HPP
42 uint64_t magic_value, uint16_t
version)
45 auto length =
region.openFile(filename, read_only);
46 bool empty = (length == 0);
57 data->magic = magic_value;
59 data->elem_size =
static_cast<uint16_t
>(
sizeof(T));
62 data->nb_elements = 0;
64 if(
data->magic != magic_value)
65 throw std::runtime_error(
"ProvSQL mmap: wrong file type (magic mismatch)");
67 throw std::runtime_error(
"ProvSQL mmap: unsupported format version "
68 + std::to_string(
data->version));
69 if(
data->elem_size !=
sizeof(T))
70 throw std::runtime_error(
"ProvSQL mmap: element size mismatch (recompile required)");
78 data->
flags |= FLAG_DIRTY;
84 auto new_capacity =
data->capacity*2;
87 data->capacity = new_capacity;
113 if(
data->nb_elements ==
data->capacity)
116 data->d[
data->nb_elements++] = value;
Append-only vector template backed by a memory-mapped file.
uint16_t version() const
Return the format version stamped in the file's header.
~MMappedVector()
Sync and unmap the file.
data_t * data
Typed view of region.base().
void flush()
Force the backing file to stable storage (MappedRegion::flush()).
static constexpr unsigned STARTING_CAPACITY
Initial number of element slots allocated.
bool read_only_
Mapped read-only: the header must not be written.
void add(const T &value)
Append an element to the end of the vector.
static constexpr uint32_t FLAG_DIRTY
MMappedVector(const char *filename, bool read_only, uint64_t magic, uint16_t version=1)
Open (or create) the mmap-backed vector.
void grow()
Double the backing region and refresh data.
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.
uint32_t flags
Bit 0: opened for writing and not closed since.