Skip to content

Move an index

An index is a set of files that never change. So you move it by copying it. This page shows the three ways: copy everything, copy one revision, or pack one revision into a single file that you can query in place.

You want to Do
Copy a whole index that nobody writes to Copy
Mirror or sync an index that is in use, or ship exactly one revision Copy one revision
Move the index as one file and query it in place Make a pack
Write on top of a pack Copy the pack into a folder or a bucket first. Packs are read-only.

copyObjects copies between any two stores: a bucket, a folder, memory. With no key list, it copies every file. Do this when no writer is active:

import { copyObjects, S3BlobStore } from "@aiuitech/thread-sdk";
import { FileBlobStore } from "@aiuitech/thread-sdk/node";
const bucket = new S3BlobStore({ endpoint, bucket: "my-bucket", region: "auto", accessKeyId, secretAccessKey });
const laptop = new FileBlobStore("/data/my-index");
await copyObjects(bucket, laptop);
from thread_sdk import FileBlobStore, S3BlobStore, copy_objects
copy_objects(S3BlobStore.r2(account_id, "my-bucket", key_id, secret), FileBlobStore("/data/my-index"))

The folder holds the same layout as the bucket, so rsync, aws s3 sync and rclone work too. With any of these, copy idx/current.json last: it is the pointer that readers follow.

If writers are active during the copy, a plain copy can take the pointer of one revision and the files of another. Copy one revision instead.

A live index holds history: old revisions and files that compaction replaced. A snapshot is the list of files that one revision needs, and nothing else. This is also the safe way to copy or sync an index that is being written.

import { copyObjects, setCurrent, snapshotKeys } from "@aiuitech/thread-sdk";
const index = await thread.open({ store: source });
const revision = index.revision; // take the revision once: the handle can move on
// 1. The files of that revision. They never change, so files that the target has can be skipped.
await copyObjects(source, target, snapshotKeys(revision), { skipExisting: true });
// 2. Then the pointer, to exactly that revision.
await setCurrent(target, { gen: revision.gen, rev: revision.rev });

Run it again later and it is a sync: only the new files are copied, and the pointer moves when they are all there. Do not copy the source’s idx/current.json yourself. It can point to a newer revision than the files you copied.

A pack is one file that holds one revision. You can scp it, attach it to a release, or put it in a bucket. You query it where it is. Nothing is unpacked.

import { packIndexToFile, storeAt } from "@aiuitech/thread-sdk/node";
await packIndexToFile(store, "docs.threadpack"); // the current revision
const index = await thread.open({ store: await storeAt("docs.threadpack") });
from thread_sdk import open_index, pack_index
pack_index(store, "docs.threadpack")
index = open_index("docs.threadpack")

Why use a pack:

  • One object to move. No folder of thousands of files to sync, and no half-copied index.
  • A consistent snapshot. It holds the revision that you asked for and nothing else.
  • Queried in place. A query reads the same small ranges from the pack that it would read from a bucket. This also works for a pack that is an object in a bucket, from a Worker.
  • The same from both SDKs. TypeScript and Python write byte-identical packs for the same revision.

Ideas for packs: ship a search index with a desktop or mobile app, publish the index of your documentation with a release, give a customer an export, keep a dated backup.

The rest of this page is the detail: which stores can copy to which, what a snapshot contains, the pack file format, and base packs.

Every store speaks the same BlobStore interface (get with ranges, head, optional put with create-only and compare-and-swap conditions, optional list), so any pair can be copied between:

store TypeScript Python
R2 binding R2BlobStore (Workers) -
S3-compatible endpoint (R2’s S3 API, AWS, MinIO) S3BlobStore S3BlobStore
a directory on disk FileBlobStore (thread-sdk/node) FileBlobStore
one pack file PackedBlobStore (over any store) PackedBlobStore (over any store)
memory MemoryBlobStore MemoryBlobStore

storeAt(path) (Node) and store_at(path) / open_index(path) (Python) open a directory or a pack from a path, sniffing the pack header.

The directory layout is the bucket layout, one file per object ({root}/idx/current.json, {root}/idx/{gen}/...). The two file stores write the same bytes and read each other’s directories; the interop test builds an index with one SDK and queries and extends it with the other. One caveat for concurrent writers from both SDKs into the same directory: the manifest create-only put (a hard link) serializes them correctly, but the compare-and-swap of current.json is guarded by a .thread-lock.d directory in Node and an advisory flock in Python, which do not exclude each other. Writers of one language, or writers taking turns, are fine.

A live prefix holds history: every manifest revision ever committed, slab revisions a compaction has superseded, delta segments a compaction has folded in. A query only needs what its revision lists. snapshotKeys(revState) / snapshot_keys(rev_state) returns exactly that, sorted:

  • the manifest idx/{gen}/manifest[.rev].json,
  • the base files it names (coarse.f32, fine.f32, pq.bin, slabs[.rev].json, and a centroid tree if one was published),
  • one slab per cell at the revision the slabs table names,
  • the delta segments, tombstone sets and lexical/docs segments the manifest lists.

idx/current.json is not in the list because a snapshot writes its own, pointing at the packed revision. An index opened from a snapshot is pinned to it: refresh() finds nothing newer.

// Node
import { packIndexToFile, storeAt } from "@aiuitech/thread-sdk/node";
await packIndexToFile(store, "/backups/idx-2026-10-04.threadpack"); // the current revision
await packIndexToFile(store, "g4-rev12.threadpack", { gen: "g4", rev: 12 }); // a specific one
const index = await thread.open({ store: await storeAt("g4-rev12.threadpack") });
// anywhere (Workers included): a pack as an object in a bucket, read by ranges
const packed = await PackedBlobStore.open(new R2BlobStore(env.BUCKET), "packs/g4-rev12.threadpack");
const index = await thread.open({ store: packed }); // the Cache API caches its objects
from thread_sdk import PackedBlobStore, S3BlobStore, open_index, pack_index
pack_index(store, "/backups/idx-2026-10-04.threadpack") # the current revision
pack_index(store, "g4-rev12.threadpack", gen="g4", rev=12)
index = open_index("g4-rev12.threadpack") # one file, zero setup
index = open_index(PackedBlobStore(S3BlobStore(...), "packs/g4-rev12.threadpack"))

What a pack gives you:

  • One object to move. scp it, attach it to a release, put it in a bucket, keep it in a Git LFS repo. No directory of thousands of slabs to sync, no half-copied index.
  • A consistent snapshot. It is the revision you asked for and nothing else; stale objects are left behind.
  • Queried in place. PackedBlobStore translates every read into a range read of the pack, so a query costs what it costs against a prefix: the scan prefixes of nprobe slabs, a few rerank records, two rounds for text.
  • Deterministic. The two SDKs write byte-identical packs for the same revision (the interop test checks).

What it does not give you: writes. A pack is immutable. To continue an index from a pack, copyObjects(pack, dst) into a directory or bucket and open that; the first upsert commits the next revision there.

Caches go in front of a pack, never behind it. open already does this: it wraps whatever store you pass in the Cache API (TypeScript) or a MemoryCache for remote stores (Python), so the cache sees the objects’ own keys (idx/g4/slab/00123) and a miss fetches one slab. A cache below the pack would see one 5 GB key and try to fill it on a miss; PackedBlobStore never forwards fill to its inner store for that reason.

All integers are little-endian. One file, four regions:

offset size field
0 8 magic "THRDPACK"
8 2 version u16 = 1
10 2 flags u16 = 0
12 4 reserved u32 = 0
16 ... objects: each begins at an offset that is a multiple of 8; zero padding between them
D L directory: UTF-8 JSON (below)
end - 32 8 directory offset D u64
end - 24 8 directory length L u64
end - 16 4 entry count u32
end - 12 2 version u16 = 1
end - 10 2 flags u16 = 0
end - 8 8 magic "THRDPACK"

The directory is {"entries":[[key, offset, length], ...], ...meta}, entries in the order written, offsets absolute. packIndex writes idx/current.json first, then the objects a cold open reads (baseKeys: the manifest, slabs table, tombstone sets, coarse.f32, pq.bin, the centroid tree and the smallest routing table), then the rest of snapshotKeys in key order, and records "gen", "rev" and "base": [offset, length], the range from idx/current.json to the end of the base objects: opening an index on a PackedBlobStore reads that range with ONE range read (PackedBlobStore.readBase / read_base) and serves the open from it, so a cold open of a pack in a bucket is head + tail + one range, then the first query; PackWriter takes any JSON-serializable meta. A reader does one head for the size, one range read of the last 64 KB (footer and, usually, the whole directory), and a second range read only if the directory is larger than that.

Why the footer is at the end: the writer streams objects as it reads them and never seeks back, so a pack can be written to a pipe or an upload stream as well as to a file. Why entries are 8-byte aligned: a pack mapped or held in memory can hand out f32 views without copying. Why JSON: the manifests already are, both SDKs parse it, and a 4096-slab directory is about 200 KB.

Flags and the reserved field are zero in version 1 and are the place to put a checksum or compression scheme later; a reader must reject a version it does not know.

Partial entries (warm packs). An entry may carry a fourth element, [key, offset, length, {"partial": true, "size": n}]: it holds only bytes [0, length) of the object key, which is n bytes in all. The format stays version 1: a reader that predates the marker ignores extra elements, which is why partial entries are only written to warm packs (meta.kind: "warm"), never to index snapshots. A warm pack holds the scan prefixes of an index’s most-probed slabs, keyed by the slabs’ own (immutable) keys; open({ warmPack }) / open_index(..., warm_pack=) fetches it with one GET in the background and serves those prefixes from memory (see Cold start). PackedBlobStore serves ranges inside a partial entry, reports the whole size from head, and refuses a read past the prefix rather than return a short object. Unknown fourth elements (anything without "partial": true) are ignored.

A pack and a directory are plain files, so the usual tools work: rsync -a laptop/idx/ bucket-mirror/, aws s3 sync, rclone. Copy idx/{gen}/ before idx/current.json if readers may open the destination while the copy is running, since current.json is the only pointer they follow.

A base pack is a pack of just what a cold open reads, written per revision by publish / publish-rev next to the index: idx/{gen}/base.{rev}.threadpack holds the manifest, slabs table, tombstone sets, coarse.f32, pq.bin, the centroid tree and one routing table (default the smallest, fine.i8); idx/{gen}/base.cold.{rev}.threadpack leaves the routing table out, for coldRouting: "tree". Directory metadata {gen, rev, kind: "base" | "base-cold", routing}. The SDKs know its key from idx/current.json, so a cold open is two round trips: current.json, then one whole GET of the pack (with the manifest read alongside in case there is none, unless current.json carries an inline copy of it, which every writer now adds when the manifest is at most 16 KB). The SDKs fetch one for routing: "i8" / "auto" and coldRouting: "tree" opens; the default routing: "f32" reads the separate files (all requested together once the manifest is known). Its objects are byte-identical copies of the separate files and keyed by them, so loading from a pack gives the same state as loading the files. buildBasePack / build_base_pack write the same bytes in both SDKs (the interop test checks). Details and numbers: Query path.