use std::path::Path;
use crate::blobstore::BlobstoreReader;
use crate::common::universal_io::{CachedReadFs, Populate, UniversalRead, UniversalReadFs};
use super::ReadOnlySparseVectorStorage;
use crate::segment::common::flags::in_memory_bitvec_flags::InMemoryBitvecFlags;
use crate::segment::common::operation_error::{OperationError, OperationResult};
use crate::segment::vector_storage::sparse::mmap_sparse_vector_storage::{DELETED_DIRNAME, STORAGE_DIRNAME};
use crate::segment::vector_storage::sparse::stored_sparse_vectors::StoredSparseVector;
impl<S: UniversalRead> ReadOnlySparseVectorStorage<S> {
pub fn preopen(
fs: &impl CachedReadFs<File = S>,
path: &Path,
populate: Populate,
) -> OperationResult<()> {
BlobstoreReader::<StoredSparseVector, S>::preopen(fs, path.join(STORAGE_DIRNAME), populate)
.map_err(|err| {
OperationError::service_error(format!(
"Failed to preopen read-only sparse vector storage: {err}"
))
})?;
InMemoryBitvecFlags::preopen(fs, &path.join(DELETED_DIRNAME))?;
Ok(())
}
pub fn open(
fs: &impl UniversalReadFs<File = S>,
path: &Path,
populate: Populate,
) -> OperationResult<Self> {
let storage = BlobstoreReader::<StoredSparseVector, S>::open(
fs,
path.join(STORAGE_DIRNAME),
populate,
)
.map_err(|err| {
OperationError::service_error(format!(
"Failed to open read-only sparse vector storage: {err}"
))
})?;
let deleted = InMemoryBitvecFlags::open::<S>(fs, &path.join(DELETED_DIRNAME))?;
let next_point_offset = deleted
.as_bitslice()
.last_one()
.map(|i| i + 1)
.max(Some(storage.max_point_offset()? as usize))
.unwrap_or_default();
Ok(Self {
storage,
deleted,
next_point_offset,
})
}
}