How the index takes deletes and inserts while it serves searches. We follow the code: the data, the write paths, and the cleanup that folds everything back into a clean index.
The graph, the sketches and the vectors are flat files. The server reads them through a memory map. So we never edit them.
p belongs to document p.Deletes and inserts are cheap and happen at once. The cleanup pays the bill later.
delete.log.Tombstonesdelta.log. Add the document to the delta.DeltaOne MutableIndex is one version of the index. The server keeps it behind a lock.
Read-only inside
The graph, the sketches, the vectors and ids never change in a version.
Two things move
The tombstones are atomic bits. The delta has its own lock. So a delete or an insert only needs the shared lock on the index.
Replaced whole
A cleanup builds a new value and swaps it in.
A version is a folder named vN. A cleanup writes vN+1.tmp and renames it when it is complete.
index.bingraph, seed lists, term_maxsketches.binrouting sketches, in memory at startvectors.binfull vectors, memory-mappedids.binext_base, then the external id of each position (from v1)delta.log and delete.logonly exist after a write. A missing log means empty.v0
The original build has no ids.bin. The id is the position, and ext_base is the number of documents.
Start-up
from_env loads the highest vN and deletes every .tmp folder.
Writes
Logs go in the folder of the current version.
A delete is a single atomic OR. No lock, and searches read the same words at the same time.
Who deleted first
fetch_or returns the old word, so only one caller sees newly = true. The count stays right.
Room for the delta
The set has n_main + MAX_DELTA bits, so a delta document can be deleted too.
The trigger
count / documents tells the cleanup thread when to start.
The beam search still walks through deleted nodes. It never returns one.
A test before the walk
A dead node is only worth a visit if its score could enter the results. Otherwise the walk stops there.
Nothing to rescore
Dead ids never reach the rescoring, so no vector is read for them.
The log is how a restart gets the bits back.
Internal ids
The log stores internal ids. It only makes sense next to its own version folder.
Answered means saved
The server answers after the append. A crash before it means the client never got a 202.
The graph knows internal ids. Clients know external ids. The server converts at the door.
Sorted ids
Compaction keeps the order, so ids stays sorted and a lookup is a binary search.
Never reused
A deleted id answers 404 for ever. The next insert gets ext_base, never an old id.
ids = [0, 1, 3, 4, 5, 6], ext_base = 7.The delta is small and grows by one document at a time. It needs no graph.
Append only
A document never moves inside the delta. Its local id is its place.
Arc
The cleanup copies the document list by cloning pointers, not vectors.
Bounded
The 50,001st insert gets a 507 until a cleanup empties the delta.
A query only touches the lists of its own terms. The score is the full dot product.
One scratch array
acc has a slot for each possible delta document. Each worker owns one, so no search allocates.
One id space
Delta ids continue after the graph: n_main + local. The tombstones need no translation.
search_all joins the two hit lists, sorts by score and keeps the top k. Both scores are exact, so they compare.An insert is in the log before it can be found. A crash cannot lose an insert that was answered.
Ids follow the log
The id is chosen while we hold the write lock. So the ids and the order of the log always agree.
The price
The file write happens under the lock. Searches on the delta wait for it.
One record is nnz | indices | values | ext_id, 4 bytes each: 8 + 8 × nnz bytes.
Torn record
A crash in the middle of a write leaves half a record. We drop it and truncate the file, so the next append starts clean.
A check on the id
Every record carries its id. A log that does not fit its ext_base is refused, not trusted.
Only the swap needs the write lock on the index.
| Operation | Index lock | Delta lock | Other |
|---|---|---|---|
| Search | read, for the whole search | read | scratch mutex, to take one |
| Insert | read | write, while it logs | – |
| Delete | read | read, to count | one atomic bit |
| Cleanup: heal, compact | read | read, to copy the list | – |
| Cleanup: swap | write | read | – |
RwLock prefers writers. Once the cleanup waits for the write lock, new readers queue behind it.A search holds the read guard from start to end. So it always sees one consistent version.
Sized by version
The visited array has one slot for each document. After a swap the count changes, so an old scratch is replaced.
The swap waits
It cannot take the write lock while a search holds a read guard. Searches in flight finish on the old index.
A thread wakes every 10 seconds. It starts a cleanup when half of the documents are deleted, or when the delta is full.
Writes keep coming while the cleanup runs. The snapshot says what this cleanup covers.
Late writes
Anything that arrives after this point is a late write. The swap copies it in.
Only pointers
We clone the list of Arc, then drop the delta guard at once. Holding it through the heal blocked every insert, and then every search.
Most rows do not. We only touch a live row that points at a deleted node.
Dead rows
A deleted node gets an empty row. Nobody will reach it after the compaction anyway.
Isolated nodes
If a row loses every neighbor and finds no border, it becomes empty.
A dead node still has edges. We follow them until we reach live nodes.
A counter, not a clear
We do not clear an array of n entries for every row. A node is visited if its slot equals the counter.
A budget
max_dfs steps and max_border_length nodes for each row. Too small a budget leaves weak neighbors.
p reads other rows from the old graph and writes only its own row in the copy. That is why rows can run in parallel.Sketches are for routing. Choosing neighbors uses the full vectors, like the build.
Sequential decode
We are already inside a parallel loop, so the decode does not start another one.
Scratch for each split
try_for_each_init gives each rayon split its own visited array and dense buffer.
Local ids
The prune works on positions in ids. We map them back to node ids at the end.
Compaction writes a fresh index into vN+1.tmp. First, every live document gets a new position.
Order kept
Live documents keep their relative order. That keeps ids.bin sorted.
Copy what you can
A sketch depends only on its own vector, so it is copied. Only delta documents need a new one.
Each live delta document is inserted one by one, like an online build.
Each sees the ones before
The search only reaches nodes that are already in the graph.
Single thread
The graph changes at every step, and the order matters.
Seeds translated
Old seeds point to old ids. We map them with new_pos and drop the dead ones.
The last steps of the compaction describe the new version, then write it.
Ids stay unique
A deleted delta document still uses up an id, because ext_base moves by delta.len().
Careful
It refuses to write into a folder that has an index.bin. If nothing is alive, it returns an error.
Reading the files back is slow. It must not happen while everybody waits.
What the load does
It parses the graph and the sketches, replays the logs, builds the delta postings and maps vectors.bin.
One lock, not two
An earlier version dropped the lock to reload, then locked again. A write that landed in between was lost. Now we load first, then one write lock covers the copy and the swap.
Under the write lock, nothing can change. We copy what arrived after the snapshot into the new version.
One batch
The log is opened once and written once, not once for each insert.
Words, not ids
One AND checks 64 documents at a time.
The bits we found use old internal ids. The new version has other positions.
Snapshot documents
They moved in the compaction. old_to_new knows where.
Late inserts
They went to the new delta. Their place there is the old delta place, minus the snapshot size.
Three short steps, then the lock is released.
WillNeed
A new memory map starts cold. We ask the kernel to read it in now, so the first searches do not wait for the disk.
The map survives
The rename changes the folder name, not the file. The map stays valid.
Free later
Dropping a big index takes time. We do it after the lock.
A folder is complete or absent. The old version serves until the swap.
| It stops during | What serves | What happens next |
|---|---|---|
| heal, compact, load | the old version | On an error, a guard removes vN+1.tmp. After a crash, the next start removes it. |
| the copy of late writes | the old version | Same. The late writes are still in the old logs. |
| after the rename, before the swap | after a restart: vN+1 | The highest folder wins. Its logs already hold the late writes. |
| after the swap | vN+1 | New writes go to the logs of vN+1. |
Fourteen tests in correctness_test.rs, grouped by the part they protect.
scripts/check_updates.py and scripts/check_cleanup.py.Things we know and have not fixed yet.
vN after vN+1 is live.