Skip to main content

Mergeable

Trait Mergeable 

Source
pub trait Mergeable: Index {
    // Required method
    fn merge(&mut self, other: Self) -> Result<()>;
}
Expand description

A backend that can absorb another instance of itself.

Implement this for an Index so merge and IndexBuilder::build_merged can combine sharded builds into one index. The trait deliberately takes other by value: merging consumes the source, letting an implementation move its storage rather than copy it.

§Contract

  • Same shape. other must share self’s dim and metric. An implementation should return InvalidConfig on a mismatch rather than silently producing a corrupt index.
  • Set union. After self.merge(other)?, every id searchable in either input is searchable in self. This is where cross-shard id collisions surface: if both inputs hold the same id, an implementation returns Duplicate.
  • Failure is observable, not silent. On Err, self may be left partially merged; the operation is not transactional. Callers that need the original back should merge into a clone.

§Examples

A flat index merges by appending the other’s rows:

use std::sync::Arc;
use iqdb_build::{Mergeable, merge};
use iqdb_types::{DistanceMetric, VectorId};
impl Mergeable for Flat {
    fn merge(&mut self, other: Self) -> Result<()> {
        if other.dim() != self.dim() || other.metric() != self.metric() {
            return Err(IqdbError::InvalidConfig { reason: "merge shape mismatch" });
        }
        for (id, vector) in other.rows {
            self.insert(id, vector, None)?; // re-checks duplicates
        }
        Ok(())
    }
}
let mut a = Flat::new(1, DistanceMetric::Euclidean, FlatConfig::default())?;
a.insert(VectorId::from(1u64), Arc::from([0.0_f32].as_slice()), None)?;
let mut b = Flat::new(1, DistanceMetric::Euclidean, FlatConfig::default())?;
b.insert(VectorId::from(2u64), Arc::from([1.0_f32].as_slice()), None)?;

a.merge(b)?;
assert_eq!(a.len(), 2);

Required Methods§

Source

fn merge(&mut self, other: Self) -> Result<()>

Absorb other into self, leaving self searchable over the union of both. See the trait contract for the guarantees and failure modes.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§