#[cfg(not(target_arch = "wasm32"))]
use std::sync::Arc;
use crate::error::VectorError;
use super::state::HnswIndex;
impl HnswIndex {
#[cfg(not(target_arch = "wasm32"))]
pub fn with_backing(
&mut self,
b: Arc<dyn crate::segment_backing::VectorSegmentBacking>,
) -> Result<&mut Self, VectorError> {
if b.dim() != self.dim {
return Err(VectorError::StoredDimensionMismatch {
expected: self.dim,
got: b.dim(),
});
}
if b.len() < self.nodes.len() {
return Err(VectorError::VectorUnavailable { id: b.len() as u32 });
}
for (id, node) in self.nodes.iter().enumerate() {
if !node.storage.as_bytes().is_empty() {
continue;
}
let id = id as u32;
match b.get_vector(id) {
Some(v) if v.len() == self.dim => {}
_ => return Err(VectorError::VectorUnavailable { id }),
}
}
self.backing = Some(b);
Ok(self)
}
#[cfg(not(target_arch = "wasm32"))]
pub(super) fn backing_vector(&self, id: u32) -> Result<Vec<f32>, VectorError> {
self.backing
.as_ref()
.and_then(|b| b.get_vector(id))
.map(<[f32]>::to_vec)
.ok_or(VectorError::VectorUnavailable { id })
}
#[cfg(target_arch = "wasm32")]
pub(super) fn backing_vector(&self, id: u32) -> Result<Vec<f32>, VectorError> {
Err(VectorError::VectorUnavailable { id })
}
}