#![allow(
clippy::cast_precision_loss,
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
clippy::cast_possible_wrap,
clippy::float_cmp,
clippy::approx_constant
)]
use parking_lot::RwLock;
use std::io;
use std::sync::Arc;
use super::traits::VectorStorage;
use super::MmapStorage;
pub async fn reserve_capacity_async(
storage: Arc<RwLock<MmapStorage>>,
vector_count: usize,
) -> io::Result<()> {
tokio::task::spawn_blocking(move || {
let mut guard = storage.write();
guard.reserve_capacity(vector_count)
})
.await
.map_err(|e| io::Error::other(format!("Task join error: {e}")))?
}
pub async fn compact_async(storage: Arc<RwLock<MmapStorage>>) -> io::Result<usize> {
tokio::task::spawn_blocking(move || {
let mut guard = storage.write();
guard.compact()
})
.await
.map_err(|e| io::Error::other(format!("Task join error: {e}")))?
}
pub async fn flush_async(storage: Arc<RwLock<MmapStorage>>) -> io::Result<()> {
tokio::task::spawn_blocking(move || {
let mut guard = storage.write();
guard.flush()
})
.await
.map_err(|e| io::Error::other(format!("Task join error: {e}")))?
}
pub async fn store_batch_async(
storage: Arc<RwLock<MmapStorage>>,
vectors: Vec<(u64, Vec<f32>)>,
) -> io::Result<usize> {
tokio::task::spawn_blocking(move || {
let mut guard = storage.write();
let mut count = 0;
for (id, vector) in vectors {
guard.store(id, &vector)?;
count += 1;
}
Ok(count)
})
.await
.map_err(|e| io::Error::other(format!("Task join error: {e}")))?
}
#[cfg(test)]
#[path = "async_ops_tests.rs"]
mod tests;