ReadOnlyKeyValueDB is an immutable IKeyValueDB stored in one continuous block of memory, usually a memory mapped
file. Use it to ship a snapshot of a database, for example reference data, which many processes read without loading
it: opening only checks the header, and nothing is copied into memory.
ReadOnlyCreator writes all keys and values visible in any IKeyValueDBTransaction, together with its commit ulong
and ulongs:
using var tr = sourceDb.StartReadOnlyTransaction();
new ReadOnlyCreator(tr).WriteToFile("data.robtdb");
// or: byte[] data = new ReadOnlyCreator(tr).WriteToArray();
// or: creator.Run(ref memWriter) to write into any MemWriterSetMaxChildren sets the maximum number of keys in a leaf and children of a branch (2 to 127, default 31).
Limits: the whole data must fit into 4GB and one key into 64KB. Keys of one node are also split by size, so long keys only make nodes smaller.
using var db = ReadOnlyKeyValueDB.OpenFile("data.robtdb"); // memory mapped
using var db2 = ReadOnlyKeyValueDB.Create(data); // pins ReadOnlyMemory<byte>
using var db3 = new ReadOnlyKeyValueDB(address, length); // memory owned by the callerThe data must not change while the database is open. ObjectDB and relations work on it unchanged for reading.
Opening reads only the header and footer, so a memory mapped file is read as it is accessed. verifyChecksum: true
(on OpenFile, Create and the constructor) or VerifyChecksum() reads all data first and throws
InvalidDataException when they do not match their checksum, or have none.
- All cursor operations follow
IKeyValueDBCursor.GetKeyIndex,FindKeyIndexand finding a key are O(log n),GetKeyValueCountis O(1). GetValueSpanandGetValueMemorywithoutcopyreturn memory of the data directly. Keys are prefix compressed, so they are copied into the provided buffer unless their node has no common prefix.StartTransactionandStartReadOnlyTransactionreturn the same kind of read only transaction. Every modification,StartWritingTransactionandCreateKvithrowBTDBException.SetCommitUlongandSetUlongare allowed only with the stored value.Compactdoes nothing.- Truncated data and node or value offsets outside the data throw
InvalidDataExceptionwhen the database is opened or the node is read. Other damage is detected only by checking the checksum; without it a damaged key or value reads as other content.
The format is documented in ReadOnlyFormat in
ReadOnlyKeyValueDB.cs. It is a B+tree written bottom up after
the values. Leaves and branches store a common key prefix and key suffixes; branches also store the number of keys
in their children, which makes access by key index O(log n). Since version 2 the data end with the XxHash64 of all
bytes before it; version 1 data, without it, are still read.