Build your own vector database
Persistence & segments·25 min

Serialize the index

0 / 1
# grade this stage (from your course directory)
npx theinference test --stage serialize --watch

Write the index out. serializeSegment(index, payloads) produces a Uint8Array following the documented layout: a fixed-width header, then the vector block, then per-layer adjacency, then the length-prefixed payload JSON.

What to build

Your task

Implement serializeSegment

Implement serializeSegment in src/segment.ts. Write the header (magic, version, N, dim, topLayer, entryPoint) as fixed-width little-endian ints, then vectors (float32), levels (i32), adjacency (degree-prefixed u32 ids per node per layer), then the payload block. Wire the serialize subcommand to print the header hex + total byte length.

your_program.sh
./your_program.sh serialize <fixture>/chapters/07-persistence/steps/02-serialize/fixtures/data.json
Fixed offsets make the round-trip exact

Write the header first as fixed-width LE ints, then each block in the documented order. The magic MQDR serializes to the little-endian u32 0x5244514d. Fixed offsets are what let the next step reload the bytes bit-for-bit.

How it's graded

Process mode: stdout_json_match of { headerHex, byteLength } against the frozen reference, atol 0. The header hex (first 24 bytes) catches magic/version/dimension mistakes; the byte length catches a wrong block layout.