ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Persistence.h File Reference

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

Detailed description

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

  • a header of 128 bytes, little-endian: the magic number \x89 ADS \r \n \x1a \n (which a transfer as text or a truncation to 7 bits breaks), the version of the format (16 bits), the byte order of the payload (8 bits, 1 little and 2 big), the version of the structure (32 bits), its tag (64 bits), and its name (32 bits of length and at most 100 characters);
  • the payload, the members in the order of persist(): a number (an integer of any width, a bool, an enumeration, a float or a double) in 8 bytes; an array of numbers (a std::vector of them) as its size and the bytes of an item in 8 bytes each, then, from the next multiple of 64 bytes of the file, its items in their own width, and 0s up to a multiple of 8; an array of other items as its size and the items; a std::array, a std::pair and a std::string by their items; a nested structure as its version and its members;
  • a trailer of 32 bytes, little-endian: the bytes of the payload, the checksum of the header and of the payload, 8 bytes of 0s, and the magic number ADS-END\n. The payload is in the byte order of the machine that wrote it (or in the one asked of store()), and a reader of the other order swaps the bytes of each number; a file mapped on a machine of its order is read in place, one of the other order is copied. Since an array starts on a multiple of 64 bytes of the file, and a mapping on a page, the views of a mapped file are aligned to the lines of the cache, as the words of a BitVector are.

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.

See also
S. Gog, T. Beller, A. Moffat, M. Petri, From theory to practice: plug and play with succinct data structures, Proc. SEA 2014, LNCS 8504, 326-337, 2014, doi:10.1007/978-3-319-07959-2_28
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa