pub struct Namespace { /* private fields */ }Expand description
A handle to one namespace on an object store.
Get one with Namespace::create (new namespace) or Namespace::open
(existing). Handles are cheap: they hold the store, the id, the schema and
a sequence hint. Methods take &self, so a handle can be shared across
tasks, and separate handles on the same namespace are safe to use
concurrently. Writers are sequenced by the object store, not by the
handle.
§Consistency
- A write is durable once
upsertordeletereturns. - A
querythat starts after a write returned sees that write, even from a different handle or process (read-your-writes). - Readers see a consistent prefix of the log: all of a batch or none of it, and never a later batch without an earlier one.
- Two writers racing get distinct seqs and are ordered by seq. If both touch the same id, the higher seq wins.
§Examples
use std::sync::Arc;
use itzma_core::{Document, Metric, Namespace, Schema};
use object_store::memory::InMemory;
let store = Arc::new(InMemory::new());
let ns = Namespace::create(store, "docs", Schema::new(2, Metric::L2)).await?;
ns.upsert(vec![
Document { id: "near".into(), vector: vec![1.0, 0.0], attrs: None, sparse: None },
Document { id: "far".into(), vector: vec![9.0, 9.0], attrs: None, sparse: None },
])
.await?;
let hits = ns.query(&[0.0, 0.0], 1).await?;
assert_eq!(hits[0].id, "near");Implementations§
Source§impl Namespace
impl Namespace
Sourcepub async fn create(
store: Arc<dyn ObjectStore>,
id: &str,
schema: Schema,
) -> Result<Self>
pub async fn create( store: Arc<dyn ObjectStore>, id: &str, schema: Schema, ) -> Result<Self>
Create a new, empty namespace and return a handle to it.
Writes the manifest with put-if-absent. If several callers race to
create the same id, exactly one succeeds. The schema is fixed from this
point on, and is written a second time to schema.json, an object that
is never changed, so losing the manifest does not lose it.
The manifest is written first and decides whether the namespace exists.
If the call fails after that, before the schema object is written, the
namespace exists: open it, which writes the object. A create that
finds a schema object for the id holding a different schema is refused
with ItzmaError::NamespaceExists and writes nothing.
Creation is explicit, unlike the implicit create-on-first-write, because the dimension and metric have to be declared up front.
§Errors
ItzmaError::InvalidNamespaceIdunlessidmatches[a-z0-9_-]{1,64}.ItzmaError::InvalidSchemaifschema.dim == 0, or the attribute schema has more than 256 fields, a field name outside[A-Za-z0-9_]{1,64}, or is free-form.ItzmaError::NamespaceExistsif the id is taken.ItzmaError::UnsupportedFormatorItzmaError::Serdeif the id has a schema object this build cannot read.ItzmaError::Storagefor any backend failure.
Sourcepub async fn open(store: Arc<dyn ObjectStore>, id: &str) -> Result<Self>
pub async fn open(store: Arc<dyn ObjectStore>, id: &str) -> Result<Self>
Open an existing namespace and return a handle to it.
Reads the manifest to learn the schema and where the WAL resumes. The
handle keeps no other state from this read: each later query loads
the manifest again.
Then it reads schema.json and checks it holds the manifest’s schema.
If the object is missing, open writes it from the manifest, which
finishes a create that stopped between its two writes and covers a
namespace made before the object existed. That makes open
a write, once, for such a namespace, so it needs write access then.
§Errors
ItzmaError::InvalidNamespaceIdfor a malformed id.ItzmaError::NamespaceNotFoundif no such namespace exists.ItzmaError::CorruptManifestifschema.jsonholds a different schema from the manifest. Nothing is changed.ItzmaError::UnsupportedFormatif the manifest was written by an incompatible version.ItzmaError::WalFormatTooNewif a newer build raised the namespace’s WAL format past what this build reads. Every method that loads the manifest returns it too.ItzmaError::Serde/ItzmaError::Storageon a corrupt object or backend failure.
Sourcepub async fn recover(
store: Arc<dyn ObjectStore>,
id: &str,
) -> Result<(Self, RecoverReport)>
pub async fn recover( store: Arc<dyn ObjectStore>, id: &str, ) -> Result<(Self, RecoverReport)>
Write a new manifest for a namespace that has lost its own, and return a handle to it.
The schema comes from schema.json. The live segments are a chain from
seq 1 built from the segment names, taking at each step the segment that
reaches furthest and decodes, and the WAL past its end is left as the
tail. A segment that is missing or does not decode only shortens the
chain: the WAL from there on is read instead, and the next
compact rebuilds the segment. The new manifest carries
the newest WAL format level this build writes, and as segment level the
newest version any segment object of the namespace was written in, or
segment::DEFAULT_FORMAT if that is higher (a raise with no segment
written since leaves no trace, so it comes back at the default). A build
that reads less refuses the namespace. Recovering asserts that every
build that touches it reads what this one does. Writers are not held up,
since they never read the manifest.
Only the manifest is written, and only with put-if-absent. WAL objects,
segments and schema.json are read and left as they are, and a segment
the chain does not use is not deleted (that is gc’s job).
Segments are checked by decoding them one at a time, under the default
read budget, so a run reads every live segment once.
§Errors
ItzmaError::InvalidNamespaceIdfor a malformed id.ItzmaError::ManifestExistsif the namespace has a manifest, or another recovery wrote one first. Nothing is changed.ItzmaError::Unrecoverableifschema.jsonis missing, or a range of seqs has no readable copy (no segment that decodes covers it, and its WAL objects are missing or fenced). The error names what to restore from a replica, a versioned bucket or a backup, and a rerun can succeed once it is back. Nothing is written.ItzmaError::UnsupportedFormatif a segment is of a format this build does not read. That is a newer build’s data, not damage, so the run stops and does not skip it.ItzmaError::ReadBudgetExceededif a single segment is over the default read budget.ItzmaError::Storagefor any backend failure.
Sourcepub fn with_read_budget(self, bytes: u64) -> Self
pub fn with_read_budget(self, bytes: u64) -> Self
Set the most bytes one query or one
compact run may read, and return the handle. The
default is DEFAULT_READ_BUDGET, 1 GiB. The budget belongs to this
handle: it is not stored, and another handle on the same namespace
keeps its own.
Every segment and WAL object a run reads is counted before its body
is, and a run that would go over stops with
ItzmaError::ReadBudgetExceeded and writes nothing. The count is
stored bytes (a compressed WAL object counts as its uncompressed
size), a proxy for memory that does not see the extra weight of
attributes once decoded. Since a query or a merge holds every live
document in memory, this is also the largest namespace either can
handle.
Sourcepub fn with_cache(self, cached: Arc<dyn ObjectStore>) -> Self
pub fn with_cache(self, cached: Arc<dyn ObjectStore>) -> Self
Let the query path (query, query_with) read segment, index, and WAL-
tail bytes through cached instead of the store this handle was opened
with, and return the handle. Pass a CacheStore
wrapping the same backing store to let repeated queries reuse warm
RAM/NVMe bytes instead of re-fetching from the object store every time.
The manifest is never affected: every query still loads it fresh from
the store this handle was opened with, on every call, so a write is
visible to the very next query exactly as it is without this method —
cached only ever serves segment, index, and WAL-tail reads. Compaction,
GC, index builds, and inspect are also unaffected; they always use the
store this handle was opened with, never cached. The manifest is
excluded so a write is always visible to the next query.
§Examples
use std::sync::Arc;
use itzma_core::{CacheOptions, CacheStore, Namespace, Schema, Metric};
use object_store::memory::InMemory;
let store: Arc<dyn object_store::ObjectStore> = Arc::new(InMemory::new());
let ns = Namespace::create(store.clone(), "docs", Schema::new(4, Metric::L2))
.await
.unwrap();
let cache = CacheStore::new(store, CacheOptions::default()).unwrap();
let ns = ns.with_cache(Arc::new(cache));Sourcepub fn with_decoded_cache(self, cache: Arc<DecodedCache>) -> Self
pub fn with_decoded_cache(self, cache: Arc<DecodedCache>) -> Self
Keep what the query path parses (index heads and partitions, segment
directories, Bloom filters, id, attribute and tombstone blocks, sparse
term directories, and whole segments the flat path decodes) in cache
between queries, and return the handle.
A structure found in the cache is neither fetched nor charged to the
read budget, so a warm query can read nothing from the store but the
manifest and the WAL listing (the handle keeps its folded tail itself).
One cache can be
shared by many handles, on one store or several. Compaction, GC, index
builds, inspect and recover never use it.
§Examples
use std::sync::Arc;
use itzma_core::{DecodedCache, DecodedCacheOptions, Metric, Namespace, Schema};
use object_store::memory::InMemory;
let cache = DecodedCache::new(DecodedCacheOptions::default());
let ns = Namespace::create(Arc::new(InMemory::new()), "docs", Schema::new(4, Metric::L2))
.await?
.with_decoded_cache(cache);
let hits = ns.query(&[0.0; 4], 10).await?;
assert!(hits.is_empty());Sourcepub fn with_fetch_policy(self, policy: FetchPolicy) -> Self
pub fn with_fetch_policy(self, policy: FetchPolicy) -> Self
Set how ranged reads turn wanted byte ranges into requests,
and return the handle. The default, FetchPolicy::object_store, is
tuned for S3 and similar stores; pass FetchPolicy::local for an
InMemory or LocalFileSystem store, where a request costs about as
much as the bytes it reads.
Sourcepub fn schema(&self) -> &Schema
pub fn schema(&self) -> &Schema
The schema this namespace was created with. Read from the manifest at
create or open time; it never changes afterwards.
Sourcepub async fn upsert(&self, docs: Vec<Document>) -> Result<u64>
pub async fn upsert(&self, docs: Vec<Document>) -> Result<u64>
Insert documents, replacing any that already have the same id.
The whole slice is committed as one WAL entry, so it becomes visible atomically: readers see all of it or none of it. Every vector is checked against the schema first, and one bad document rejects the entire call with nothing written.
If the same id appears twice in docs, the later one wins. Returns the
WAL sequence number the batch was committed at.
§Errors
ItzmaError::EmptyBatchifdocsis empty.ItzmaError::InvalidIdif a document’sidis empty or longer thandocument::MAX_ID_BYTES.ItzmaError::DimensionMismatch/ItzmaError::InvalidVectorfor a bad vector (seeSchema::validate).ItzmaError::UnknownAttr,ItzmaError::AttrTypeMismatchandItzmaError::InvalidAttrsif a document’sattrsbreak the namespace’s attribute schema. One bad document rejects the whole batch. A namespace made before typed attributes checks nothing.ItzmaError::WriteContentionif 32 sequence numbers in a row were taken by other writers. Nothing was written; retrying is safe.ItzmaError::Storagefor any backend failure.
§Examples
use std::sync::Arc;
use itzma_core::{Document, Metric, Namespace, Schema};
use object_store::memory::InMemory;
let store = Arc::new(InMemory::new());
let ns = Namespace::create(store, "docs", Schema::new(1, Metric::L2)).await?;
let doc = |v: f32| Document { id: "a".into(), vector: vec![v], attrs: None, sparse: None };
assert_eq!(ns.upsert(vec![doc(1.0)]).await?, 1);
assert_eq!(ns.upsert(vec![doc(5.0)]).await?, 2); // replaces "a"
let hits = ns.query(&[5.0], 10).await?;
assert_eq!(hits.len(), 1);
assert_eq!(hits[0].distance, 0.0);Sourcepub async fn delete(&self, ids: Vec<String>) -> Result<u64>
pub async fn delete(&self, ids: Vec<String>) -> Result<u64>
Remove documents by id.
Like upsert, the whole call is one atomic WAL entry. Ids that do not
exist are ignored, so deleting is idempotent. Returns the WAL sequence
number the batch was committed at.
The documents are hidden from queries immediately, but their bytes are
not erased. They stay in the WAL until gc fences its
objects, which is off unless asked for, and in any segment that
holds them until that segment is superseded and collected.
§Errors
ItzmaError::EmptyBatchifidsis empty.ItzmaError::InvalidIdif an id is empty or longer thandocument::MAX_ID_BYTES.ItzmaError::WriteContention/ItzmaError::Storageas forupsert.
§Examples
use std::sync::Arc;
use itzma_core::{Document, Metric, Namespace, Schema};
use object_store::memory::InMemory;
let store = Arc::new(InMemory::new());
let ns = Namespace::create(store, "docs", Schema::new(1, Metric::L2)).await?;
ns.upsert(vec![Document { id: "a".into(), vector: vec![1.0], attrs: None, sparse: None }]).await?;
ns.delete(vec!["a".into()]).await?;
assert!(ns.query(&[1.0], 10).await?.is_empty());Sourcepub async fn upsert_keyed(
&self,
write: &WriteKey,
docs: Vec<Document>,
) -> Result<u64>
pub async fn upsert_keyed( &self, write: &WriteKey, docs: Vec<Document>, ) -> Result<u64>
Insert documents under an idempotency key, so that retrying the call does not write the batch twice.
Behaves like upsert, and the batch is committed as one
WAL entry. What is different is the key, in write: a first attempt is
WriteKey::new, and a call that may repeat an earlier one is
WriteKey::retry, which looks for the key in the WAL objects from a
seq the caller names before it writes anything. A found key returns the
seq of the original and writes nothing. The rules, and what they do not
cover, are on WriteKey.
The namespace must be at WAL level 3, which every namespace created by
this build is. One created by an older build has to be raised first with
upgrade_wal_format. A handle reads the
manifest for the level once, and only if it does not already know the
level is high enough. Unkeyed writes never read it.
§Errors
As for upsert, plus:
ItzmaError::InvalidKeyunless the key is 1 to 128 bytes.ItzmaError::WalFormatTooOldif the namespace is below level 3.ItzmaError::KeyConflictif the key was used for a batch of a different size.ItzmaError::WalFormatTooNewif the manifest read for the level shows a format this build cannot read.
§Examples
use std::sync::Arc;
use itzma_core::{Document, Metric, Namespace, Schema, WriteKey};
use object_store::memory::InMemory;
let store = Arc::new(InMemory::new());
let ns = Namespace::create(store, "docs", Schema::new(1, Metric::L2)).await?;
let batch = || vec![Document { id: "a".into(), vector: vec![1.0], attrs: None, sparse: None }];
let seq = ns.upsert_keyed(&WriteKey::new("load-7/batch-1"), batch()).await?;
// The acknowledgement never arrived, so the caller tries again from
// the last seq it saw committed (here nothing, so 1).
let retry = WriteKey::retry("load-7/batch-1", 1);
assert_eq!(ns.upsert_keyed(&retry, batch()).await?, seq);Sourcepub async fn upsert_columns(&self, cols: &DocColumns) -> Result<u64>
pub async fn upsert_columns(&self, cols: &DocColumns) -> Result<u64>
Insert a columnar batch, one row a document: the same WAL
object, byte for byte, that upsert writes for the
same documents, without building them. The batch is checked with
DocColumns::validate first, and one bad row rejects the whole call
with nothing written.
§Errors
The errors of upsert. An input with one defect gets
exactly upsert’s error; with several, the first one reported can
differ (see DocColumns::validate). A batch with a sparse column
needs WAL level 5 (ItzmaError::WalFormatTooOld).
§Examples
use std::sync::Arc;
use itzma_core::{DocColumns, Metric, Namespace, Schema};
use object_store::memory::InMemory;
let ns = Namespace::create(Arc::new(InMemory::new()), "docs", Schema::new(2, Metric::L2)).await?;
// Ids "a" and "b", vectors [1, 0] and [0, 1].
let batch = DocColumns::new(b"ab".to_vec(), vec![0, 1, 2], vec![1.0, 0.0, 0.0, 1.0], 2)?;
assert_eq!(ns.upsert_columns(&batch).await?, 1);
assert_eq!(ns.query(&[1.0, 0.0], 1).await?[0].id, "a");Sourcepub async fn upsert_stream<S>(
&self,
input: S,
options: &StreamOptions,
) -> Result<StreamReport, StreamError>
pub async fn upsert_stream<S>( &self, input: S, options: &StreamOptions, ) -> Result<StreamReport, StreamError>
Load a stream of columnar batches in bounded memory.
The input is cut into chunks of about
StreamOptions::target_bytes of encoded body, wherever its own
batches happen to end, so the same rows always make the same chunks.
Each chunk is checked as upsert_columns
checks a batch and encoded on the compute pool while the previous one
is being written; exactly one PUT is in flight, so the chunks land in
order and a crash can never leave a hole between them. At most
StreamOptions::depth encoded chunks wait for the writer, so memory
is about (depth + 2) * target_bytes however long the input is.
Each chunk is its own WAL object, made visible on its own: a reader can see the first chunks of a load before the rest.
The batches of one stream must have the same columns: the same dim,
the same attribute names, each attribute column of the same kind (an
all-null AttrColumn::Null fits any kind; a number column cannot
switch between integers and floats from one batch to the next, which
upsert of documents would allow), and all a sparse column or none.
Otherwise the stream stops with ItzmaError::InvalidColumns.
§Errors
A StreamError: the error, and in
landed what was written before it, whole and
in order. Chunks the encoder had already queued when a later chunk
failed its check are written first. The errors are those of
upsert_columns, plus an error from the input itself,
ItzmaError::EmptyBatch for an input with no rows, and
ItzmaError::InvalidConfig for bad options (before the input is
polled). To resume an unkeyed load, feed the input again from row
landed.rows.
§Examples
use std::sync::Arc;
use itzma_core::{DocColumns, Metric, Namespace, Schema, StreamOptions};
use object_store::memory::InMemory;
let ns = Namespace::create(Arc::new(InMemory::new()), "docs", Schema::new(1, Metric::L2)).await?;
let batches = vec![
DocColumns::new(b"a".to_vec(), vec![0, 1], vec![1.0], 1),
DocColumns::new(b"b".to_vec(), vec![0, 1], vec![2.0], 1),
];
let report = ns
.upsert_stream(futures::stream::iter(batches), &StreamOptions::default())
.await
.map_err(|e| e.error)?;
assert_eq!((report.rows, report.objects), (2, 1));Sourcepub async fn upsert_stream_keyed<S>(
&self,
write: &WriteKey,
input: S,
options: &StreamOptions,
) -> Result<StreamReport, StreamError>
pub async fn upsert_stream_keyed<S>( &self, write: &WriteKey, input: S, options: &StreamOptions, ) -> Result<StreamReport, StreamError>
upsert_stream under an idempotency key: chunk
n is written under <key>#<n>, so the key can be at most 107 bytes
(room for # and any chunk number). Those chunk keys share the key
space of upsert_keyed: do not give a plain
keyed write a key of the form <stream key>#<number>, or the two can
be taken for each other.
A retry with WriteKey::retry and the same input and
target_bytes cuts the same chunks, finds the ones that landed by
their keys and writes only the rest, however the input is
batched the second time. Its search_from must be at or before the
first chunk the first attempt may have written: the failed attempt’s
landed.first_seq, or, when that is None
(nothing was acknowledged, though the first put may have landed), the
namespace’s next seq before the first attempt. The search reads the
headers from there once; each later chunk is looked for only past the
one before it.
§Errors
Those of upsert_stream and upsert_keyed;
ItzmaError::InvalidKey for a key over 107 bytes, before the input
is polled.
Sourcepub async fn upsert_columns_keyed(
&self,
write: &WriteKey,
cols: &DocColumns,
) -> Result<u64>
pub async fn upsert_columns_keyed( &self, write: &WriteKey, cols: &DocColumns, ) -> Result<u64>
upsert_columns under an idempotency key, with
the rules of upsert_keyed.
§Errors
Those of upsert_columns and upsert_keyed.
Sourcepub async fn delete_keyed(
&self,
write: &WriteKey,
ids: Vec<String>,
) -> Result<u64>
pub async fn delete_keyed( &self, write: &WriteKey, ids: Vec<String>, ) -> Result<u64>
Remove documents by id under an idempotency key. The keyed form of
delete, with the rules and errors of
upsert_keyed.
A delete is where a replay does the most harm, since it can undo a newer upsert of the same id. With a key, the retry finds the delete already in the log and leaves it alone.
Sourcepub async fn upgrade_wal_format(&self, level: u32) -> Result<()>
pub async fn upgrade_wal_format(&self, level: u32) -> Result<()>
Raise the namespace’s WAL format to at least level, so that writes
which need it are allowed.
One compare-and-swap on the manifest. It only goes up: a level at or
below the current one changes nothing and writes nothing, and two
callers racing both succeed. Level 3 is what upsert_keyed
needs, level 4 is what gc needs to fence WAL objects, and
level 5 is what a sparse-carrying upsert or
upsert_keyed needs (WAL object
versions 4 and 5). A namespace created by this build starts at 5.
Raise it only once every build that reads or compacts this namespace
reads the new format (version 3 for level 3, fences for level 4, and
versions 4/5’s sparse column for level 5). From the next manifest read, a build that does not is
refused with ItzmaError::WalFormatTooNew (the gate), which is what
keeps it away, but it cannot be undone: there is no way to lower the
level. Writers that already hold a handle keep writing what they wrote,
which stays readable.
Needs conditional update on the store, which LocalFileSystem does not
implement.
§Errors
ItzmaError::WalFormatTooNewiflevelis above what this build reads (wal::FORMAT_VERSION).ItzmaError::ManifestContentionafter 32 lost rounds.- Anything
manifest::loadcan return.
Sourcepub async fn upgrade_segment_format(&self, level: u32) -> Result<()>
pub async fn upgrade_segment_format(&self, level: u32) -> Result<()>
Raise the namespace’s segment format to at least level, so that
compaction writes segments in that version.
One compare-and-swap on the manifest. It only goes up: a level at or
below the current one changes nothing and writes nothing, and two
callers racing both succeed. Level 2 is the block-addressable layout.
Level 3 stores a typed namespace’s attributes as columns with
a zone map per block, and a free-form namespace’s as level 2 does. A
namespace created by this build starts at
segment::DEFAULT_FORMAT, and one made before the levels existed is
at 1 and stays there until this is called.
Nothing is rewritten. The segments already there keep their version, and
each later fold or merge writes the new one, so a namespace can hold
both. Raise it only once every build that reads or compacts this
namespace reads the new version. From the next manifest read, a build
that does not is refused with ItzmaError::SegmentFormatTooNew (the
gate), but the raise cannot be undone: there is no way to lower the
level.
A compaction that stopped after writing a segment at the old level and
is rerun after the raise builds different bytes for the same key, and
compact reports ItzmaError::CorruptSegment rather
than publish over it. Nothing is published. Delete that
orphan segment object (it is listed as an orphan by
inspect::report) and rerun.
Needs conditional update on the store, which LocalFileSystem does not
implement.
§Errors
ItzmaError::SegmentFormatTooNewiflevelis above what this build reads (segment::FORMAT_VERSION).ItzmaError::ManifestContentionafter 32 lost rounds.- Anything
manifest::loadcan return.
Sourcepub async fn compact(&self, options: &CompactionOptions) -> Result<Compaction>
pub async fn compact(&self, options: &CompactionOptions) -> Result<Compaction>
Fold the un-compacted WAL into a segment, or merge segments when there are too many, and publish the result.
What happens depends on the state:
- WAL tail empty and at most
max_segmentssegments: nothing to do. - Adding one more segment would exceed
max_segments: every segment and the tail are merged into one base segment. - Otherwise: the tail becomes a new segment beside the existing ones.
The segment is written with put-if-absent, then listed in the manifest
by one compare-and-swap. Writers are never blocked and never see the
manifest change. Queries running during a compaction are unaffected.
Any process may call this, as often as it likes, concurrently: racing
compactors build the same object, one wins the swap, and the rest get
Compaction::Superseded. A run that crashes before publishing leaves
an orphan segment that the next run verifies against the bytes it
builds and reuses. An orphan that differs makes compact fail with
ItzmaError::CorruptSegment and publish nothing.
The swap also drops the manifest’s index entries (see
index) of any segment the run retires, so a merge clears
them, a fold keeps them, and the segment written starts without one.
Compaction never deletes anything: not the WAL objects that were
folded, not superseded segments, not orphans. Superseded segments and
dead orphans are removed by a separate call to gc, and
folded WAL objects are kept until that call fences them. If a
segment the run needs disappears, or a WAL object it reads has become a
fence, because a compaction and a garbage collection ran after it
loaded the manifest, the run reloads the manifest and returns
Compaction::Superseded.
Publishing needs conditional update on the store, which
LocalFileSystem does not implement. There a run fails at the publish
step with a storage error after writing its segment.
§Errors
ItzmaError::ManifestContentionafter 32 lost publish rounds.ItzmaError::CorruptManifest/ItzmaError::CorruptSegmentif the manifest or a segment it lists is damaged.ItzmaError::CorruptSegmentalso when an object already sits at the new segment’s key with different bytes.ItzmaError::WalGapif the log has a hole.ItzmaError::InvalidConfigwhen a merge is due andoptions.max_segment_bytesis above the handle’s read budget, before any segment is read or anything written. A fold does not need the check, so a handle with a small query budget still folds.ItzmaError::ReadBudgetExceededif the tail, or one merge group, is more than the read budget: the tail and each group get a budget of their own, so a namespace bigger than the budget still merges. Nothing is published; groups written before the refused one stay as orphans that a rerun reuses orgccollects.ItzmaError::DimensionMismatchif a WAL document’s vector does not match the schema (only possible for a hand-edited WAL).ItzmaError::UnsupportedFormat,ItzmaError::SerdeorItzmaError::Storagefor unreadable objects or backend failures.
§Examples
use std::sync::Arc;
use itzma_core::{Compaction, CompactionOptions, Document, Metric, Namespace, Schema};
use object_store::memory::InMemory;
let store = Arc::new(InMemory::new());
let ns = Namespace::create(store, "docs", Schema::new(1, Metric::L2)).await?;
ns.upsert(vec![Document { id: "a".into(), vector: vec![1.0], attrs: None, sparse: None }]).await?;
let options = CompactionOptions::default();
let outcome = ns.compact(&options).await?;
assert!(matches!(outcome, Compaction::Published { folded_through: 1, .. }));
assert_eq!(ns.query(&[1.0], 1).await?[0].id, "a");
// Nothing new has been written, so there is nothing to fold.
assert_eq!(ns.compact(&options).await?, Compaction::Nothing);Sourcepub async fn index(&self, options: &IndexOptions) -> Result<IndexReport>
pub async fn index(&self, options: &IndexOptions) -> Result<IndexReport>
Build and publish an index for each live segment that has none.
An index lets a query search a segment without reading it whole
(see query); a segment with no index is read whole, as it
always was, so a namespace never has to be indexed.
Segments are taken one at a time. For each one that is version 2 and has
at least IndexOptions::min_rows documents, the data object is read
(under a read budget of its own, so a namespace is not limited by the sum
of its segments, but one segment must fit in it), the index object
segments/<id>/index-<generation>.bin is written
with put-if-absent, and one
manifest compare-and-swap lists it. Until that swap the object is an
orphan that changes nothing, and a run that stops there leaves one that the
next run checks and reuses. Any valid index of the segment is accepted, so
two indexers racing on the same segment both succeed and one is counted as
already_indexed.
A fold or merge (compact) never indexes: it leaves the segment it writes
without one, and a merge also drops the entries of the segments it retires.
Call index again afterwards. IndexOptions::rebuild writes generation
n + 1 for a segment that already has generation n, and the old object
is left for gc, which removes index objects that no manifest
names once they are min_age old.
Needs conditional update on the store, like compact.
§Errors
ItzmaError::SegmentFormatTooOldif the namespace is below segment format 2. Nothing is touched.ItzmaError::ReadBudgetExceededif one segment is bigger than the read budget.ItzmaError::CorruptSegmentif a segment is damaged. A segment a compaction and a garbage collection removed after this call loaded the manifest is counted assupersededinstead.ItzmaError::CorruptIndexif an object already sits at the new index’s key and does not fit the segment. Delete it and rerun.ItzmaError::ManifestContentionafter 32 lost publish rounds.- Anything
manifest::loadcan return.
A failure part way leaves the segments before it indexed. Call again to finish.
§Examples
use std::sync::Arc;
use itzma_core::{CompactionOptions, Document, IndexOptions, Metric, Namespace, Schema};
use object_store::memory::InMemory;
let store = Arc::new(InMemory::new());
let ns = Namespace::create(store, "docs", Schema::new(2, Metric::L2)).await?;
ns.upsert(vec![Document { id: "a".into(), vector: vec![1.0, 0.0], attrs: None, sparse: None }]).await?;
ns.compact(&CompactionOptions::default()).await?;
// One document is under the default minimum, so nothing is indexed.
let report = ns.index(&IndexOptions::default()).await?;
assert_eq!((report.indexed, report.skipped_small), (0, 1));
// A minimum of zero indexes it.
let report = ns.index(&IndexOptions::default().min_rows(0)).await?;
assert_eq!(report.indexed, 1);Sourcepub async fn index_sparse(
&self,
options: &IndexSparseOptions,
) -> Result<IndexSparseReport>
pub async fn index_sparse( &self, options: &IndexSparseOptions, ) -> Result<IndexSparseReport>
Build and publish a sparse inverted index for each live segment that
has a sparse.bin and no sparse index yet. A segment with
no sparse documents has no sparse.bin and is skipped — there is
nothing to index. A segment with a sparse sidecar and no index is
still searchable by query_sparse, read whole;
the index only lets a query fetch the postings of its own terms
instead.
Mirrors index’s shape: one segment at a time, an
object written with put-if-absent, one manifest compare-and-swap.
IndexSparseOptions::rebuild writes generation n + 1 for a
segment that already has generation n; the old object is left for
gc, which removes sparse index objects no manifest names
once they are min_age old. A fold or merge (compact) never builds
one, and a merge drops the entries of the segments it retires.
Needs conditional update on the store, like compact.
§Errors
ItzmaError::CorruptSegmentif asparse.binis damaged. A segment a compaction and a garbage collection removed after this call loaded the manifest is counted assupersededinstead.ItzmaError::CorruptSparseIndexif an object already sits at the new index’s key and does not fit the segment. Delete it and rerun.ItzmaError::ReadBudgetExceededif onesparse.binis bigger than the read budget.ItzmaError::ManifestContentionafter 32 lost publish rounds.- Anything
manifest::loadcan return.
A failure part way leaves the segments before it indexed. Call again to finish.
§Examples
use std::sync::Arc;
use itzma_core::{CompactionOptions, Document, IndexSparseOptions, Metric, Namespace, Schema};
use object_store::memory::InMemory;
let store = Arc::new(InMemory::new());
let ns = Namespace::create(store, "docs", Schema::new(1, Metric::L2)).await?;
ns.upsert(vec![Document {
id: "a".into(),
vector: vec![1.0],
attrs: None,
sparse: Some(vec![(7, 0.5)]),
}])
.await?;
ns.compact(&CompactionOptions::default()).await?;
let report = ns.index_sparse(&IndexSparseOptions::default()).await?;
assert_eq!(report.indexed, 1);Sourcepub async fn gc(&self, options: &GcOptions) -> Result<GcReport>
pub async fn gc(&self, options: &GcOptions) -> Result<GcReport>
Delete the segments that a merge or a later fold has superseded, and
the orphans that can no longer be reused (phase A). With
GcOptions::fence_wal it also replaces folded WAL objects with fences
(phase B).
A segment is dead when it is not in the manifest, its last seq is at or
below wal_folded_through, and it is not 1-<wal_folded_through> (the
id a merge over an empty tail would produce and reuse). Only dead
segments are deleted, and only when both they and the live segment that
replaced them are at least GcOptions::min_age old. Live segments,
orphans past the fold point and the manifest are never touched. With
GcOptions::dry_run nothing is deleted or fenced.
Fencing is off unless asked for. A fence is an 8-byte object that
takes the place of a WAL object at the same key, so put-if-absent still
refuses a writer whose hint is stale, and the bytes of the old batch are
gone. An object is fenced when its seq is at or below
wal_folded_through, and both it and the live segment that holds its seq
are at least min_age old. After that the segments are the only copy of
what those objects held. A damaged segment can no longer be rebuilt from
the WAL, and neither can a manifest reset to fold nothing. The namespace
must be at WAL format level 4 (see
upgrade_wal_format), and every build that
reads it must know fences, which the gate enforces. A fence has no
idempotency key, so a keyed retry can find its original only until the
object is fenced, at least min_age after the write.
GC is a separate call because deleting has its own cost and failures,
and nothing hosts a background job yet. It reads the manifest and never
writes it, so it needs no conditional update and also works on
LocalFileSystem. It may run at any time, from any number of processes
at once, beside writers, compactors and queries. A query that loaded
the manifest before a merge and reads after GC reloads and finishes on
the new one, and so does a compaction (see query and
compact). That covers a fence in the tail as well as a
missing segment. Roll out readers with that behavior before running GC.
A fencing run lists the WAL only past a small hint object,
gc/fenced-through, that it keeps up to date, so the listing
does not grow with the fenced part of the log.
Inside a live segment’s directory it also deletes index objects
that the manifest does not name: an older generation that a
rebuild replaced, or an orphan of a build that never published. Such an
object goes once it and the index that replaced it are min_age old (an
orphan with no replacement needs only its own age). The index the
manifest names is never deleted.
GC deletes segments, so a document’s old bytes stay in the store until the segments that held them are collected, and in the WAL until its objects are fenced.
§Errors
ItzmaError::CorruptManifestif the manifest’s segment list breaks its invariant.ItzmaError::WalFormatTooOldiffence_walis set and the namespace is below level 4. Nothing is changed.ItzmaError::UnsupportedFormat,ItzmaError::SerdeorItzmaError::Storagefor an unreadable manifest or a backend failure. A failure part way leaves some dead segments deleted and others not, and some objects fenced and others whole; rerunning finishes the job.
§Examples
use std::sync::Arc;
use std::time::Duration;
use itzma_core::{CompactionOptions, Document, GcOptions, Metric, Namespace, Schema};
use object_store::memory::InMemory;
let store = Arc::new(InMemory::new());
let ns = Namespace::create(store, "docs", Schema::new(1, Metric::L2)).await?;
let doc = |id: &str| Document { id: id.into(), vector: vec![1.0], attrs: None, sparse: None };
// Two folds, then a merge that replaces both segments.
let tight = CompactionOptions { max_segments: 1, max_segment_bytes: itzma_core::compaction::DEFAULT_MAX_SEGMENT_BYTES };
ns.upsert(vec![doc("a")]).await?;
ns.compact(&tight).await?;
ns.upsert(vec![doc("b")]).await?;
ns.compact(&tight).await?;
// The default keeps anything younger than a day.
assert_eq!(ns.gc(&GcOptions::default()).await?.segments_deleted, 0);
let now = GcOptions { min_age: Duration::ZERO, ..GcOptions::default() };
assert_eq!(ns.gc(&now).await?.segments_deleted, 1);
assert_eq!(ns.query(&[1.0], 10).await?.len(), 2);
// The WAL is only touched when asked. Both objects are folded now.
assert_eq!(ns.gc(&now).await?.wal_objects_fenced, 0);
let fencing = GcOptions { fence_wal: true, ..now };
assert_eq!(ns.gc(&fencing).await?.wal_objects_fenced, 2);
assert_eq!(ns.query(&[1.0], 10).await?.len(), 2);Sourcepub async fn maintain(
&self,
options: &MaintenanceOptions,
) -> Result<MaintenanceReport>
pub async fn maintain( &self, options: &MaintenanceOptions, ) -> Result<MaintenanceReport>
Compact if the WAL tail is long enough, then collect, and report what happened. The caller decides when to call it, from a cron job or a loop. The library starts no task of its own.
It lists the WAL tail past wal_folded_through (sizes and write times,
no GETs) and runs compact with
MaintenanceOptions::compaction if the tail is not empty and any
CompactTrigger threshold is crossed, or if
there are more live segments than max_segments. Then, if
MaintenanceOptions::gc is set, it runs gc, which
happens whether or not a compaction was due. The compaction goes first,
so the segments it replaced are candidates at once.
It is safe from any number of processes at once, because compact and
gc are. A compaction that another process won reports
Compaction::Superseded. A call that stopped part way is finished by
calling it again. It reads the manifest twice, since compact loads its
own.
§Errors
Those of compact and gc, including
ItzmaError::WalFormatTooNew and ItzmaError::WalFormatTooOld
(fence_wal below level 4). A failure in the compaction stops the call
before the collection.
§Examples
use std::sync::Arc;
use itzma_core::{CompactTrigger, Compaction, Document, MaintenanceOptions, Metric, Namespace, Schema};
use object_store::memory::InMemory;
let store = Arc::new(InMemory::new());
let ns = Namespace::create(store, "docs", Schema::new(1, Metric::L2)).await?;
let doc = |id: &str| Document { id: id.into(), vector: vec![1.0], attrs: None, sparse: None };
// Fold once the tail has two objects.
let options = MaintenanceOptions {
compact_when: CompactTrigger { tail_objects: 2, ..CompactTrigger::default() },
..MaintenanceOptions::default()
};
ns.upsert(vec![doc("a")]).await?;
assert_eq!(ns.maintain(&options).await?.compaction, None);
ns.upsert(vec![doc("b")]).await?;
let report = ns.maintain(&options).await?;
assert_eq!(report.tail_objects, 2);
assert!(matches!(report.compaction, Some(Compaction::Published { folded_through: 2, .. })));Sourcepub async fn query(&self, vector: &[f32], top_k: usize) -> Result<Vec<Hit>>
pub async fn query(&self, vector: &[f32], top_k: usize) -> Result<Vec<Hit>>
Find the top_k documents closest to vector, closest first.
Steps:
- Validate
vectoragainst the schema. - Load the manifest (one GET, no cache) and check that its segment
list is contiguous and ends at
wal_folded_through. - In parallel, fetch and decode the live segments (16 at a time) and
read the WAL entries after
wal_folded_through, checking they are contiguous. Each entry is folded into a layer of its own as it arrives. Every object is charged to the read budget before its body is read. - Scan each source for its closest documents and drop any that a newer source holds as a document or a tombstone, so the last op on an id wins, including deletes.
- Keep the best
top_kacross sources (ascrate::search::flat_top_kwould over the replayed documents).
The metric comes from the schema. Distances follow the “lower is closer”
convention documented on crate::search::distance, and ties break by id.
If fewer than top_k documents exist, all of them are returned; if
top_k is 0 the result is empty.
A query started after a compaction published sees the same documents as one started before it. A query racing a compaction works from the manifest it loaded. If garbage collection has since deleted a segment that manifest lists, the query loads the manifest again and reads against the new one, three attempts in all. A segment that is missing from an unchanged manifest is damage, and the query fails.
§Cost
Every call fetches all live segments and the un-compacted WAL tail and
scans every document, so cost is O(live documents) plus one request
per segment and per tail entry. Run compact to keep
the tail short. Caching and indexes are what remove the rest.
A query holds every live document in memory, so it reads at most the
handle’s read budget (see with_read_budget).
§Errors
ItzmaError::DimensionMismatch/ItzmaError::InvalidVectorfor a bad query vector.ItzmaError::CorruptManifestif the segment list breaks its invariant.ItzmaError::CorruptSegmentif a listed segment is missing from the current manifest, damaged, or has the wrong dimension.ItzmaError::WalGapif the log has a hole.ItzmaError::ReadBudgetExceededif the live segments and the tail add up to more than the read budget.ItzmaError::UnsupportedFormat,ItzmaError::SerdeorItzmaError::Storagefor unreadable objects or backend failures.
§Examples
use std::sync::Arc;
use itzma_core::{AttrSchema, AttrType, Document, Metric, Namespace, Schema};
use object_store::memory::InMemory;
let store = Arc::new(InMemory::new());
let schema = Schema::new(2, Metric::Cosine)
.with_attrs(AttrSchema::new().field("label", AttrType::Text));
let ns = Namespace::create(store, "docs", schema).await?;
let doc = |id: &str, v: [f32; 2]| Document {
id: id.into(),
vector: v.to_vec(),
attrs: Some(serde_json::json!({ "label": id })),
sparse: None,
};
ns.upsert(vec![doc("east", [1.0, 0.0]), doc("north", [0.0, 1.0])]).await?;
let hits = ns.query(&[2.0, 0.1], 2).await?;
assert_eq!(hits[0].id, "east");
assert_eq!(hits[0].attrs, Some(serde_json::json!({ "label": "east" })));
assert!(hits[0].distance < hits[1].distance);Sourcepub async fn query_with(
&self,
vector: &[f32],
top_k: usize,
options: &QueryOptions,
) -> Result<Vec<Hit>>
pub async fn query_with( &self, vector: &[f32], top_k: usize, options: &QueryOptions, ) -> Result<Vec<Hit>>
query with QueryOptions: how many candidates an
index proposes per hit wanted (QueryOptions::rerank), how many of its
partitions to search (QueryOptions::nprobe), and whether to ignore
indexes and scan flat (QueryOptions::exact).
QueryOptions::filter keeps only documents whose attributes match a
Predicate. It is checked against the attribute schema
before any read: an undeclared field is ItzmaError::UnknownAttr, an
operator that does not fit its field is ItzmaError::AttrTypeMismatch,
and a namespace with no attribute schema refuses it with
ItzmaError::InvalidQuery. The answer is the closest top_k live
documents that match, and has fewer only when fewer match. The newest
version of an id is the one tested. A selective filter widens the search
until it has enough matches or has read every row, bounded by the read
budget.
With no live segment holding an index this build can search (an empty
namespace, one whose segments are not indexed, or exact: true), this is
exactly the flat scan query has always done, and the options change
nothing else.
Otherwise a live segment with an index is read through it: its algorithm
proposes rerank * top_k candidate rows by approximate distance (every
row if rerank is None, which makes the answer equal the flat scan’s),
each is checked against every newer source (a segment or the WAL tail)
and dropped if one holds it as a document or a tombstone, and what
survives is rescored against its exact vector. A segment left short of
top_k live candidates by shadowing is asked again for more before the
next source is read. Segments without an index, and the WAL tail, are
read whole and scanned flat, like today. The merge across sources keeps
the closest top_k by exact distance, and only then are their attributes
fetched.
§Errors
ItzmaError::InvalidQueryfor arerankornprobeof zero.- Everything
querycan return. ItzmaError::CorruptIndexfor a damaged index, or one that proposed a row past its segment. Never falls back to a flat scan; retry withexact: true, orindexwithrebuildto replace it.
§Examples
use std::sync::Arc;
use itzma_core::{Document, Metric, Namespace, QueryOptions, Schema};
use object_store::memory::InMemory;
let store = Arc::new(InMemory::new());
let ns = Namespace::create(store, "docs", Schema::new(1, Metric::L2)).await?;
ns.upsert(vec![Document { id: "a".into(), vector: vec![1.0], attrs: None, sparse: None }]).await?;
let exact = QueryOptions::default().exact(true);
assert_eq!(ns.query(&[0.0], 1).await?, ns.query_with(&[0.0], 1, &exact).await?);Sourcepub async fn query_sparse(
&self,
query: &SparseVector,
top_k: usize,
options: &SparseQueryOptions,
) -> Result<Vec<Hit>>
pub async fn query_sparse( &self, query: &SparseVector, top_k: usize, options: &SparseQueryOptions, ) -> Result<Vec<Hit>>
Find the top_k documents whose sparse vectors score highest against
query, best first.
query is a set of (term id, weight) pairs in the same vocabulary the
documents’ sparse vectors use. A document’s score
is the dot product over the terms it shares with the query, an exact
sum with no approximation, so there is no rerank step and nothing to
tune. A document that shares no term with the query, or has no sparse
vector, is never a hit. A term repeated in query counts as the sum of
its weights.
Hits are Hits like query’s, and follow the same
convention: distance is lower for a closer match, so it is the score
negated. Ties break by id.
SparseQueryOptions::filter keeps only documents whose attributes
match a Predicate, with the same checks and meaning as
QueryOptions::filter.
Shadowing works as it does for query_with: the
newest version of an id decides, so an overwrite or a delete hides the
older document’s sparse vector. A segment with a sparse index
(index_sparse) is read in pieces, fetching only
the postings of the query’s own terms. Every other segment, and the WAL
tail, is read whole and scored directly, so an unindexed namespace
gives the same answer, only at a higher cost. Reads go through the
handle’s cache if with_cache set one, and are
bounded by the read budget.
§Errors
ItzmaError::InvalidQueryfor atop_kof zero, an emptyqueryor a non-finite weight in it, before any storage call.ItzmaError::CorruptSegmentfor a damagedsparse.bin.ItzmaError::CorruptSparseIndexfor a damaged sparse index, or one that named a row past its segment. A query never falls back to a flat scan over one;index_sparsewithrebuildreplaces it.ItzmaError::ReadBudgetExceededand the other errors ofquery.
§Examples
use std::sync::Arc;
use itzma_core::{Document, Metric, Namespace, Schema, SparseQueryOptions};
use object_store::memory::InMemory;
let store = Arc::new(InMemory::new());
let ns = Namespace::create(store, "docs", Schema::new(1, Metric::L2)).await?;
let doc = |id: &str, terms: Vec<(u32, f32)>| Document {
id: id.into(),
vector: vec![0.0],
attrs: None,
sparse: Some(terms),
};
ns.upsert(vec![doc("cats", vec![(1, 2.0), (2, 1.0)]), doc("dogs", vec![(3, 2.0)])]).await?;
let hits = ns.query_sparse(&vec![(1, 1.0), (2, 1.0)], 5, &SparseQueryOptions::default()).await?;
assert_eq!(hits.len(), 1);
assert_eq!(hits[0].id, "cats");
assert_eq!(hits[0].distance, -3.0);Sourcepub async fn filter(
&self,
predicate: &Predicate,
options: &FilterOptions,
) -> Result<FilterPage>
pub async fn filter( &self, predicate: &Predicate, options: &FilterOptions, ) -> Result<FilterPage>
Return the documents whose attributes match predicate, in ascending id
order, one page at a time.
No vector is involved and none is read: a Row is an id
and its attributes. The predicate is checked against the namespace’s
attribute schema first, so a field that is not declared or an operator
that does not fit its type is an error and never an empty result. A
comparison against a null field is false and Predicate::Not flips it
(see Predicate for the difference from SQL).
The newest version of an id decides it, as for every query: an upsert
that no longer matches hides the older copy that did, and a delete hides
it too. Ids are compared in byte order. A page holds at most
FilterOptions::limit rows, and FilterPage::next is the id to pass
as FilterOptions::after for the following one. Paging is not a
snapshot: a write or a compaction between two pages can change what the
second sees, and each page is consistent on its own.
The cost is linear in the ids and attributes read, and a page stops once
it has limit matches, so a loose filter is cheap and a selective one
reads most of the namespace. Reads are bounded by the handle’s read
budget and go through its cache if with_cache set
one. A scan that would go over the budget stops with
ItzmaError::ReadBudgetExceeded and returns no partial page and no
cursor, so a selective filter on a namespace bigger than the budget
cannot be paged through until the budget is raised.
§Errors
ItzmaError::InvalidQueryfor alimitof zero or above 10,000, a malformed predicate (seePredicate), or a namespace made before typed attributes, which has nothing to filter on.ItzmaError::UnknownAttrandItzmaError::AttrTypeMismatchfor a predicate that does not fit the schema.ItzmaError::ReadBudgetExceededand the other errors ofquery.
§Examples
use std::sync::Arc;
use itzma_core::{
AttrSchema, AttrType, Compare, Document, FilterOptions, Metric, Namespace,
Predicate, Schema,
};
use object_store::memory::InMemory;
let schema = Schema::new(1, Metric::L2)
.with_attrs(AttrSchema::new().field("price", AttrType::Number));
let ns = Namespace::create(Arc::new(InMemory::new()), "shop", schema).await?;
let item = |id: &str, price: f64| Document {
id: id.into(),
vector: vec![0.0],
attrs: Some(serde_json::json!({ "price": price })),
sparse: None,
};
ns.upsert(vec![item("a", 5.0), item("b", 50.0), item("c", 12.0)]).await?;
let cheap = Predicate::number("price", Compare::Lt, 20.0);
let page = ns.filter(&cheap, &FilterOptions::default()).await?;
let ids: Vec<&str> = page.rows.iter().map(|r| r.id.as_str()).collect();
assert_eq!(ids, ["a", "c"]);Auto Trait Implementations§
impl !Freeze for Namespace
impl !RefUnwindSafe for Namespace
impl !UnwindSafe for Namespace
impl Send for Namespace
impl Sync for Namespace
impl Unpin for Namespace
impl UnsafeUnpin for Namespace
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more