Module adapters

Module adapters 

Source
Expand description

Vector database adapters for external integration.

This module provides a unified interface for working with different vector database backends, including IPFRS-native indices and external systems like Qdrant, Milvus, Pinecone, and Weaviate.

§Architecture

The adapter layer provides:

  • A common VectorBackend trait for all implementations
  • Type-safe operations for indexing and search
  • Migration utilities between different backends
  • Batch operation support for efficiency

§Basic Usage

use ipfrs_semantic::adapters::{VectorBackend, IpfrsBackend, BackendConfig};
use ipfrs_core::Cid;

// Create an IPFRS-native backend with custom dimension
let config = BackendConfig {
    dimension: 4,
    ..Default::default()
};
let mut backend = IpfrsBackend::new(config)?;

// Insert vectors
let cid: Cid = "bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi".parse()?;
let embedding = vec![0.1, 0.2, 0.3, 0.4];
backend.insert(cid, &embedding, None)?;

// Search for similar vectors
let query = vec![0.15, 0.25, 0.35, 0.45];
let results = backend.search(&query, 10, None)?;

println!("Found {} results", results.len());

§Implementing Custom Backends

To integrate with external vector databases, implement the VectorBackend trait:

use ipfrs_semantic::adapters::*;
use ipfrs_core::{Cid, Result};

struct MyCustomBackend {
    // Your backend state (e.g., connection pool, client)
    dimension: usize,
}

impl VectorBackend for MyCustomBackend {
    fn insert(&mut self, cid: Cid, vector: &[f32], metadata: Option<Metadata>) -> Result<()> {
        // Validate dimension
        if vector.len() != self.dimension {
            return Err(ipfrs_core::Error::InvalidInput(
                format!("Expected {} dimensions, got {}", self.dimension, vector.len())
            ));
        }

        // Insert into your backend
        // Example: self.client.insert(cid.to_string(), vector, metadata)?;
        Ok(())
    }

    fn search(
        &mut self,
        query: &[f32],
        k: usize,
        filter: Option<&MetadataFilter>,
    ) -> Result<Vec<BackendSearchResult>> {
        // Perform search in your backend
        // Example: let results = self.client.search(query, k, filter)?;
        Ok(vec![])
    }

    fn delete(&mut self, cid: &Cid) -> Result<()> {
        // Delete from your backend
        // Example: self.client.delete(cid.to_string())?;
        Ok(())
    }

    fn get(&self, cid: &Cid) -> Result<Option<(Vec<f32>, Option<Metadata>)>> {
        // Retrieve from your backend
        // Example: self.client.get(cid.to_string())
        Ok(None)
    }

    fn count(&self) -> Result<usize> {
        // Return total count from your backend
        // Example: self.client.count()
        Ok(0)
    }

    fn clear(&mut self) -> Result<()> {
        // Clear all data from your backend
        // Example: self.client.clear()
        Ok(())
    }

    fn stats(&self) -> BackendStats {
        BackendStats::default()
    }
}

§Migration Between Backends

The module provides utilities to migrate data between different backends:

use ipfrs_semantic::adapters::*;

// Migrate specific CIDs from one backend to another
let cids = vec![/* ... */];
let stats = migrate_vectors(&mut source_backend, &mut dest_backend, &cids)?;
println!("Migrated {} vectors, {} not found", stats.migrated, stats.not_found);

Structs§

BackendConfig
Configuration for vector database backends
BackendMigration
Migration utilities for moving data between backends
BackendRegistry
Backend registry for managing multiple backends
BackendSearchResult
Search result from a vector backend
BackendStats
Statistics for a vector backend
IpfrsBackend
IPFRS-native backend using HNSW index
MigrationStats
Statistics from a migration operation

Traits§

VectorBackend
Common interface for vector database backends