|
ADS
Algorithms and Data Structures
|
The persistence of the static structures of ADS, beyond the book: a structure built once (a succinct index, a filter, a minimal perfect hash function, a graph in CSR form) is written to a file, a stream or a buffer by store(), and read back by restore(), or opened in place by restoreMapped(), which maps the file [see MappedFile.h] and makes the large arrays of the structure views over its bytes, with no copy and no parsing, so that an index of any size is opened in \( O( 1 ) \) time and its pages are read from the disk at their first access. More...
#include "ADS/Portable.h"#include "ADS/external/MappedFile.h"#include <array>#include <bit>#include <concepts>#include <cstddef>#include <cstdint>#include <filesystem>#include <initializer_list>#include <iosfwd>#include <span>#include <string>#include <string_view>#include <type_traits>#include <utility>#include <vector>#include "ADS/external/impl/Persistence.tpp"Go to the source code of this file.
Classes | |
| class | ADS::PersistWriter |
| the archive that writes the members of a structure [see store()] More... | |
| class | ADS::PersistReader |
| the archive that reads the members of a structure [see restore()] More... | |
Namespaces | |
| namespace | ADS |
Concepts | |
| concept | ADS::Persistent |
| a structure that store() writes and restore() reads: it names itself, lists its members in persist(), and is made empty by its default constructor or, if it has none, by its persistEmpty() | |
Enumerations | |
| enum class | ADS::ByteOrder : std::uint8_t { ADS::ByteOrder::little = 1 , ADS::ByteOrder::big = 2 , ADS::ByteOrder::native = std::endian::native == std::endian::big ? 2 : 1 } |
| the byte order of the payload of a file More... | |
Functions | |
| template<Persistent T> | |
| void | ADS::store (const T &x, std::ostream &s, ByteOrder o=ByteOrder::native) |
| the structure \( x \) written to the stream \( s \), its payload in the order \( o \) | |
| template<Persistent T> | |
| void | ADS::store (const T &x, const std::filesystem::path &p, ByteOrder o=ByteOrder::native) |
| the structure \( x \) written to the file \( p \), made or replaced, its payload in the order \( o \) | |
| template<Persistent T> | |
| std::vector< std::byte > | ADS::store (const T &x, ByteOrder o=ByteOrder::native) |
| the bytes of the structure \( x \), as in a file, its payload in the order \( o \) | |
| template<Persistent T> | |
| T | ADS::restore (std::istream &s) |
| the structure read from the stream \( s \), its checksum checked | |
| template<Persistent T> | |
| T | ADS::restore (std::span< const std::byte > b) |
| the structure read from the bytes \( b \), its checksum checked | |
| template<Persistent T> | |
| T | ADS::restore (const std::filesystem::path &p) |
| the structure read from the file \( p \), its checksum checked | |
| template<Persistent T> | |
| T | ADS::restoreMapped (const std::filesystem::path &p, bool verify=false) |
| the structure of the file \( p \) mapped in memory, its arrays views over the mapping (copies if the file is of the other byte order, or the array is not on the allocator of the views), its checksum checked if \( \mathit{verify} \); the mapping lives as long as a view of it | |
The persistence of the static structures of ADS, beyond the book: a structure built once (a succinct index, a filter, a minimal perfect hash function, a graph in CSR form) is written to a file, a stream or a buffer by store(), and read back by restore(), or opened in place by restoreMapped(), which maps the file [see MappedFile.h] and makes the large arrays of the structure views over its bytes, with no copy and no parsing, so that an index of any size is opened in \( O( 1 ) \) time and its pages are read from the disk at their first access.
A structure is Persistent when it names itself, persistName (with a persistVersion, 1 if absent, raised when its layout changes, the oldest version that it still reads, persistOldestVersion, equal to persistVersion if absent, and a persistTag() of its parameters, 0 if absent), and lists its members in one member template persist( A & a ), called with the archive that writes them and with the one that reads them (A::loading tells which): a( x , y , ... ) writes or reads the members in order, and a.require( c , what ), on a reading, rejects a file whose members break an invariant \( c \) of the structure. The same member serves the archives of cereal [see interop/cereal/Cereal.h, included by a program that uses them].
The format, version 1, is
The checksum is that of the rounds of xxHash64 (Collet, https://github.com/Cyan4973/xxHash) on four lanes of 64 bits, in the order of the words of the file, at about one cycle a word. A restore() computes it while it reads, and rejects a file whose checksum differs; restoreMapped() checks the magic numbers, the sizes of the header, of the payload and of the trailer (in \( O( 1 ) \) time, which rejects a truncated file) and the invariants that the structures require, but computes the checksum only if asked, since that reads the whole file. A file that is not of ADS, holds another structure or another version of it, is truncated, has a wrong checksum or breaks an invariant throws PersistError with a message that says which, and no member is ever read past the end of the payload.
The contracts [see Contract.h]: none beyond those of the structures; the errors of the files are exceptions, not contracts, since a file is an input of the program. The functions are defined in Persistence.tpp.