Skip to main content

Crate itzma_core

Crate itzma_core 

Source
Expand description

Itzma core: object-storage-backed vector retrieval.

Vectors live as plain objects in a bucket. There is no server in the middle: any process with access to the bucket can write, and any process can query. Correctness comes from two primitives the object store already provides, put-if-absent and conditional update.

§Where to start

Namespace is the public API. Create one, upsert documents, query for nearest neighbors:

use std::sync::Arc;

use itzma_core::{Document, Metric, Namespace, Schema};
use object_store::memory::InMemory;

let store = Arc::new(InMemory::new());
let schema = Schema::new(2, Metric::L2);
let ns = Namespace::create(store, "docs", schema).await?;

ns.upsert(vec![Document { id: "a".into(), vector: vec![1.0, 0.0], attrs: None, sparse: None }]).await?;
let hits = ns.query(&[0.0, 0.0], 5).await?;
assert_eq!(hits[0].id, "a");

Any Arc<dyn object_store::ObjectStore> works as the backend: in-memory for tests, local disk, or S3, GCS and Azure. Note that LocalFileSystem does not implement conditional update, which manifest::update, manifest::update_if and so Namespace::compact and Namespace::upgrade_wal_format and Namespace::upgrade_segment_format need.

§Modules

ModuleJob
namespaceThe Namespace handle: create, open, upsert, delete (each also keyed), query, query_sparse, filter, index, index_sparse, compact, gc, maintain, recover
documentDocument, WalOp, Schema, Metric
attrsAttrSchema, AttrType: the typed attribute fields a namespace declares
predicatePredicate: a typed condition over attributes
filterFilterOptions, FilterPage, Row and the run behind Namespace::filter
walSequenced write-ahead log: wal::append, wal::append_keyed, wal::read_log
manifestThe namespace manifest and its compare-and-swap update
schema_objectschema.json, the schema written once at create
segmentSegment binary codecs (versions 2 and 3), ids, and the manifest invariant check
layerlayer::Layer: the net effect of applying operations in order
compactionCompaction, CompactionOptions and the run behind Namespace::compact
gcGcOptions, GcReport and the run behind Namespace::gc
indexerIndexOptions, IndexReport and the run behind Namespace::index
sparseSparseVector storage beside a segment: sparse.bin
attr_indexerIndexAttrsOptions, IndexAttrsReport and the run behind Namespace::index_attrs
sparse_indexerIndexSparseOptions, IndexSparseReport and the run behind Namespace::index_sparse
maintainMaintenanceOptions and the run behind Namespace::maintain
columnsDocColumns: a batch of upserts held as columns, for Arrow input
leaseMaintenanceLease: which process maintains a namespace
inspectinspect::report: a read-only look at what a namespace holds and what looks wrong
searchDistance metrics and the flat top-k scan
errorItzmaError and Result

A query or a compaction run reads at most a byte budget, DEFAULT_READ_BUDGET unless set with Namespace::with_read_budget, and stops with ItzmaError::ReadBudgetExceeded past it.

wal, manifest, layer, segment and compaction are public so integration tests can drive them directly. Application code should stay on Namespace.

§On-disk layout

<namespace>/
  schema.json                    dimension, metric and attribute types, written once, never changed
  maintenance/lease.json         which process maintains the namespace, written by a loop's lease
  gc/fenced-through              how far the WAL fences reach, a hint for `gc` (only with fence_wal)
  manifest/current.json          schema, live segments, WAL fold point
  wal/00000000000000000001.json  one object per write batch (binary, see below)
  wal/00000000000000000002.json
  segments/<lo>-<hi>/data.bin    compacted data, named by the WAL range it covers
  segments/<lo>-<hi>/index-<n>.bin an index of that segment, generation n, once `index` built one
  segments/<lo>-<hi>/sparse.bin  the segment's sparse vectors, only if a document has one
  segments/<lo>-<hi>/sparse-idx-<n>.bin its sparse inverted index, once `index_sparse` built one

The manifest is JSON. WAL objects and segments are binary, and a WAL object is ZSTD-compressed when that makes it smaller. The WAL key keeps its .json suffix on purpose: every writer has to try the same key for the same sequence number, or put-if-absent stops deciding the race. A write that carries an idempotency key (WriteKey) is version 3, the same object with the key in the header. A batch with a sparse vector is version 4, or 5 with a key.

Nothing in the layout is ever overwritten but the manifest, and the WAL objects that Namespace::gc is asked to fence. GC deletes segments that a merge or a later fold superseded, and with GcOptions::fence_wal replaces folded WAL objects with 8-byte fences that keep put-if-absent working at their keys. Nothing else is removed. The schema is kept in the manifest and again in schema.json, which is written once, so losing the manifest does not lose it. Namespace::recover then writes a new manifest from the schema object, the segment names and the WAL listing. Namespace::maintain is the call to schedule: it compacts when the WAL tail is long enough and then collects.

Every object carries a format_version so the encoding can change later. A reader that meets a version it does not know fails with ItzmaError::UnsupportedFormat instead of guessing. The manifest also records the newest WAL format the namespace may contain, and a build that reads an older one refuses the namespace with ItzmaError::WalFormatTooNew before it touches the WAL. Namespace::upgrade_wal_format raises that level, one way. A second level, segment_format, does the same for segments: a build that reads an older segment format is refused with ItzmaError::SegmentFormatTooNew, and Namespace::upgrade_segment_format raises it. Compaction writes the segment version that level allows.

Re-exports§

pub use attr_indexer::IndexAttrsOptions;
pub use attr_indexer::IndexAttrsReport;
pub use attrs::AttrSchema;
pub use attrs::AttrType;
pub use columns::AttrColumn;
pub use columns::DocColumns;
pub use columns::SparseColumn;
pub use compaction::Compaction;
pub use compaction::CompactionOptions;
pub use document::Document;
pub use document::Metric;
pub use document::Schema;
pub use document::SparseVector;
pub use document::WalOp;
pub use error::ItzmaError;
pub use error::Result;
pub use filter::FilterOptions;
pub use filter::FilterPage;
pub use filter::Row;
pub use gc::GcOptions;
pub use gc::GcReport;
pub use indexer::IndexKind;
pub use indexer::IndexOptions;
pub use indexer::IndexReport;
pub use lease::DEFAULT_LEASE_RENEW;
pub use lease::DEFAULT_LEASE_TTL;
pub use lease::LeaseOptions;
pub use lease::MaintenanceLease;
pub use lease::Role;
pub use maintain::CompactTrigger;
pub use maintain::MaintenanceOptions;
pub use maintain::MaintenanceReport;
pub use manifest::AttrIndexEntry;
pub use manifest::IndexEntry;
pub use manifest::Manifest;
pub use manifest::SparseIndexEntry;
pub use namespace::Namespace;
pub use predicate::AttrValue;
pub use predicate::Compare;
pub use predicate::Predicate;
pub use predicate::TextCompare;
pub use recover::RecoverReport;
pub use search::Hit;
pub use segment::Matrix;
pub use sparse_indexer::IndexSparseOptions;
pub use sparse_indexer::IndexSparseReport;
pub use wal::WriteKey;

Modules§

attr_indexer
Building the attribute index of one segment and publishing it: Namespace::index_attrs indexes each live segment in turn. Mirrors sparse_indexer.rs’s shape — put-if-absent, then one manifest compare-and-swap — but reads the whole segment the way indexer.rs’s dense index does (there is no min_rows floor: building this index is one pass over attributes already being decoded, not a quantization or graph build).
attrs
Typed attributes: the declared type of each field, and the check a document’s attrs has to pass to be written.
columns
A batch of upserts held as columns.
compaction
Compaction: fold the WAL tail into a segment, or merge the segments.
document
The data model: documents, WAL operations, and the per-namespace schema.
error
The error type shared by every module in the crate.
filter
The filter-only query behind Namespace::filter : one page of the id-order merge, read a block at a time, with the predicate tested on each winner.
gc
Garbage collection: dead segments, and optionally old WAL objects.
indexer
Building the index of one segment and publishing it: Namespace::index indexes each live segment in turn.
inspect
A read-only look at one namespace: what is stored and what looks wrong.
layer
Layers: the net effect of applying operations in order.
lease
The maintenance lease: which of the processes sharing a namespace runs its maintenance.
maintain
Maintenance: decide whether a compaction is due, run it, then collect.
manifest
The namespace manifest and its compare-and-swap update.
namespace
The Namespace handle: the public entry point for reading and writing.
predicate
Filter predicates.
quant
Scalar quantization to one byte per dimension (the SQ8 codec), public so code outside this crate can reuse the exact quantization the SQ8 index uses instead of a second implementation that could drift from it.
recover
Rebuilding a lost manifest.
scan
Reading a namespace in id order: Namespace::scan and the k-way merge it shares with Namespace::filter.
schema_object
The schema object: <namespace>/schema.json.
search
Flat (brute-force) nearest-neighbor search.
segment
Segments: immutable binary snapshots of a layer.
sparse
The sparse.bin sidecar: one segment’s raw sparse vectors, row-addressed the same way data.bin’s dense matrix is. Written by compaction only when at least one document in the segment has one (crate::compaction); read whole by Namespace::index_sparse to build the inverted index, and by the query engine’s unindexed-segment fallback.
sparse_indexer
Building the sparse inverted index of one segment and publishing it : Namespace::index_sparse. Mirrors indexer.rs’s shape for the dense index — put-if-absent, then one manifest compare-and-swap — with one algorithm, so there is no kind.
wal
The write-ahead log, sequenced by put-if-absent.

Structs§

CacheOptions
How to size CacheStore’s tiers. nvme is optional — omit it for a RAM-only cache (tests, or a deployment with no local disk to spare).
CacheStore
A read-through cache in front of a real ObjectStore: RAM first, then a local NVMe cache (if configured), then inner. Only reads are cached — put, delete, list and copy pass straight through, since every object here is immutable once written and there is nothing to invalidate, only capacity to manage.
CpuThreads
Thread counts for this process’s two compute pools.
DecodedCache
Parsed index heads, partitions, segment directories, blocks and decoded segments, shared by every handle given it with Namespace::with_decoded_cache.
DecodedCacheOptions
How a DecodedCache is sized.
FetchPolicy
How a ranged read turns “these pieces of this object” into requests. Chosen per store kind (see FetchPolicy::local, FetchPolicy::object_store) and set on a Namespace with with_fetch_policy.
GateOptions
The limits of one GatedStore.
GatedStore
An ObjectStore wrapper that admits reads only while the bytes and the requests in flight stay under its GateOptions.
NvmeOptions
The local NVMe tier’s settings.
QueryOptions
Settings for Namespace::query_with.
SparseQueryOptions
Settings for Namespace::query_sparse.
StreamError
A stream that stopped: the error, and what landed before it. Every object in landed is whole and in the WAL, one after the other.
StreamOptions
How Namespace::upsert_stream cuts and queues a stream.
StreamReport
What a stream wrote: the first and last seq it landed at, and how many rows and objects. On an error, StreamError::landed is this for what landed before it.
UploadOptions
How a handle writes segments and their sidecars, set with Namespace::with_upload_options.

Constants§

DEFAULT_DECODED_MAX_BYTES
The limit a DecodedCache has when the caller does not say: 256 MiB.
DEFAULT_FALLBACK_MAX
The default UploadOptions::fallback_max: 256 MiB.
DEFAULT_GATE_MAX_BYTES
Default GateOptions::max_bytes of a query gate: 64 MiB in flight.
DEFAULT_GATE_MAX_REQUESTS
Default GateOptions::max_requests: 256 reads in flight.
DEFAULT_PART_SIZE
The default UploadOptions::part_size: 8 MiB. S3 needs at least 5 MiB in every part but the last.
DEFAULT_RAM_MAX_BYTES
A RAM-only default: 256 MiB. A guess with no workload behind it yet, the same caveat DEFAULT_READ_BUDGET and CompactTrigger’s defaults already carry — this is a starting point to tune from real usage, not a measured number.
DEFAULT_READ_BUDGET
The default read budget of a Namespace: 1 GiB.
DEFAULT_RERANK
The default for QueryOptions::rerank, a guess until the recall harness has measured one.
DEFAULT_STREAM_ABOVE
The default UploadOptions::stream_above: 64 MiB.
DEFAULT_STREAM_DEPTH
The default StreamOptions::depth.
DEFAULT_STREAM_TARGET_BYTES
The default StreamOptions::target_bytes: half the WAL’s 64 MiB body limit, so a chunk’s own estimate never pushes it over.
DEFAULT_UPLOAD_CONCURRENCY
The default UploadOptions::concurrency.
MIN_PART_SIZE
The smallest UploadOptions::part_size: 5 MiB, S3’s smallest part.

Functions§

configure_cpu
Set the thread counts of this process’s compute pools. The pools belong to the process, not to a Namespace, so this is called once, before the first query or build. A forked child has no pools yet and may call it again.