The first three posts covered reading, scoring and searching. The last one covers writing: the index writer, and a more prosaic question — how much do you actually support?
1. Writer architecture
In one sentence: documents come in, get grouped by term → token, accumulate in a RAM buffer, and flush into a segment when full.
| Design point | Approach | Why |
|---|---|---|
| Grouping | Reusable (termId, tokenIndex) integer sort | Each term’s postings are written contiguously |
| Encoding | One varint stream per term: [docDelta, freq, posDelta*freq] | Matches the Lucene104 format |
| Segment names | Base 36 | Java’s rule; otherwise multi-segment naming collides |
| Deleted bitmap | .liv = header + bits2words(maxDoc) little-endian longs | 1 = live; padding bits past maxDoc must be cleared |
| Merge | Pairwise merge tree (not whole-index reread) | Bounded memory, output still equivalent to a single pass |
| Append | Pre-register source FieldInfos | Preserves field numbers; otherwise they all shift |
2. Acceptance: byte identity
Merge and append correctness has a one-line criterion:
The
forceMerge(1)/ append-then-merge index must be byte-identical to a write done from scratch in one pass.
Measured: at 1M docs, forceMerge(1) produced 950,224,932 bytes, byte-identical to a single-pass write; a 250K append (150K + 100K) merged to 243,181,903 bytes, also identical, with exactly the same document and posting counts.
This standard is far stricter than “searches return the same results” — it forces every intermediate state to be correct.
3. Memory: 7GB → 398MB
The first version kept the whole index in memory before writing: 7.17GB peak at 1M docs — unusable.
After moving to RAM buffer + multiple segments + a merge tree:
| Metric | Before | After | Java |
|---|---|---|---|
| Peak RSS (1M) | 7.17GB | 398MB | 936MB (heap) |
| Index size (1M) | 1,299MB | 971MB | 917MB |
| Indexing speed | 14,976 docs/s | 35,361 docs/s | 33,232 docs/s |
Later work reached 124K docs/s, 278.9MB and 123MB RSS at the 250K scale.
4. Writer traps
| Trap | Symptom | Root cause |
|---|---|---|
| Docs out of order after merge | docs out of order (94726 <= 99991) | Merged doc ids must be compacted and renumbered |
| Java NPE on merged output | collectionStats is null | docCountWithTerms not maintained (index-wide stats) |
| Deleted count off | bits.deleted=968 info.delcount=1000 | .liv padding bits set |
_0.liv missing | File not found | Generation 0 has no suffix; getNextDelGen() starts at 1 |
| Segment name collision | Counter not max+1 | Must parse names as base 36 |
| Append invisible | Only 150K docs | commit() returned early for flush-only runs |
| Merge loses field values | 7 sparse values missing | assign instead of resize on carry-over |
5. Coverage: 59.3% → 100%
Using luceneutil’s real wikimedium.1M.tasks, the port initially supported 6,664 / 11,236 = 59.3%. The missing part was a long tail:
| Missing family | Tasks (approx) | How it was added |
|---|---|---|
| Wildcard | 500 | Automaton / bitset |
| Respell | 500 | DirectSpellChecker port |
| Fuzzy1 / Fuzzy2 | 500 each | Edit-distance trie enumeration + TopTermsBlendedFreq rewrite |
| SpanNear (ordered/unordered) | ~1,036 | Spans machinery |
| IntervalsOrdered | ~536 | Interval iterators ported line by line |
| IntNRQ (numeric ranges) | 500 | points/BKD reader + writer + range query |
After that: 11,236 / 11,236 = 100%.
6. Two methodologies
- A fixture is a claim: it claims “this code path exists”, but it may only cover the first block or the first segment. The rule: grow fixtures until the boundaries appear (multi-block, multi-leaf, multi-segment, with deletes), and make the gate fail rather than silently skip when a fixture stops exercising code.
- Lock invariants with scripts: enum/table alignment,
.livpadding bits, field numbers being meaningful only within one index — each one gets an assertion or a check script.
7. Summary
- Byte identity is the best acceptance test for a writer;
- Memory problems are solved with buffer + segments + merge tree, not with a bigger heap;
- Coverage comes from a gap list and item-by-item closure, not from “it’s about right”;
- Grow fixtures until boundaries appear; make verification prove there are no holes.
Together, the four posts describe the whole method: define correctness with external oracles, prove it by reconciliation, and keep yourself honest with measurement.
Related
- Rewriting Lucene in C++ (Part 1): Byte-Level Compatibility — When Java Reads a C++-Written Index
- Rewriting Lucene in C++ (Part 2): Bit-Exact Scoring — The Floating-Point Traps of BM25
- Implementing Git for Data on DuckDB (Part 3): Branches, diff, pick, merge — The Version Primitives of Data
Get the next essay
New essays on products, business and tech — plus what the experiment's numbers actually say.