Engineering
Every file can be checked
An embedded database keeps its data in files beside the application, where disks fail and processes are killed in the middle of a write. This is what Hinny does so that damage is found, not believed.
A search index is derived from other data, and it is tempting to treat it as disposable. But rebuilding one means reading and embedding everything again, and an index that quietly returns wrong results is worse than one that is gone. Two rules run through Hinny's storage: a damaged file must be noticed, and a file that cannot be understood must be refused, never guessed at.
Every file says what it is
Every file starts by naming its kind and the version of its format: the manifest, each write-ahead log, each deletion file, each segment, each file of stored text, and the schema. A release reads the formats of the release before it and writes its own; older segments are rewritten when a merge reaches them. A file of a newer format is refused with an error that names both versions, never misread.
The repository keeps a database written by each release, and a test checks that the current code reads it, writes on top of it and merges it.
A checksum on every file
| File | What it holds | Checksum | Checked |
|---|---|---|---|
| Manifest | Which segments make up the index | CRC32 of the whole file | When the index is opened |
| Write-ahead log | Writes that are not in a segment yet | CRC32 of every record; a batch is one record | When the log is replayed |
| Deletion file | Which of a segment's documents are deleted | A header and a CRC32 | When the index is opened |
| Segment | Posting lists, vector codes, attribute columns | CRC32 of the header, and one of every mebibyte after it | The header when the segment is opened; the rest by verify |
| Stored text | The documents' text, compressed in blocks | zstd's checksum in every block | When a block is read |
A batch of writes is one log record under one checksum, so after a crash a batch is there whole or not at all, and a record torn off at the end of the log is ignored on replay. A manifest or a deletion file that fails its checksum is refused: before format 4, damage that still parsed was believed.
Verify reads everything
Segments are memory-mapped, not read through a call that could check them, so a query does not verify checksums as it goes: that would mean reading whole files to answer from a small part of them. Instead, verify on a namespace (ns.verify() in Python, hinny_verify in C) reads every file against its checksums and names the first damaged one. Reading everything is what it costs. Segments from before format 8 have no checksums, and verify counts them as unchecked.
Refusing instead of guessing
The dangerous moment for a damaged index is opening it. An audit of the recovery code found that an unreadable manifest opened as an empty index, whose cleanup then deleted the segments it no longer named. Now opening rejects an unreadable or malformed manifest or deletion file before anything is cleaned up. A missing manifest beside existing segment files requires explicit recovery, and the files are kept. Every log's header is checked before a writer is created.
Failures while writing are handled in the same spirit. A failed append to the log stops the writer until the index is reopened, because appending behind a torn record could lose later records on replay. A deletion bitmap whose save failed stays marked as unsaved. A flush or merge that fails to publish keeps the previous state and its files, to be retried.
Tested by breaking it
| Test | What it breaks | What must hold |
|---|---|---|
| integrity | Single bits in a segment's postings, its header, its checksums and in stored text | Verify finds every one |
| faults | Each of the engine's writes and syncs, failed in turn at every point a workload reaches it: 344 cases, including log appends torn in half | The index reopens to exactly the state after a prefix of the batches, with every batch acknowledged before the last successful sync |
| kills | A writing process, killed with SIGKILL at a random moment while flushes and merges run, 40 times | Every acknowledged batch survives, whole and in order |
| formats | Nothing: it opens a database written by each earlier release | The current code reads it, writes on top of it and merges it |
The tests are checked against the bugs they exist for. Putting back a deletion bug that had just been fixed makes the fault sweep fail, and removing the flush of the log on each write makes the kill test lose acknowledged batches at once.
What it does not promise
- A write is acknowledged once its WAL record reaches the operating system. That survives a process crash; an OS crash or power loss can lose writes made since the last successful
sync()orflush(). - Synchronization relies on the filesystem and storage device honoring sync requests. On non-Unix platforms, directory entries are not synchronized.
- The crash tests kill processes and simulate torn log tails; they are not a hardware power-loss certification.
- From format 7 onward, deletion files are numbered and named in the manifest, so removing one externally causes open to fail. For legacy pre-format-7 segments, a missing deletion file still means no deletions.
- Checksums are verified on file open, when reading stored text blocks, and during
verify(). They are not checked on every query.
How writes, segments and merges fit together is in the docs, and the dated history of this work, commit by commit, is in the dev log.