use std::path::{Path, PathBuf};
use crate::blobstore::Blob;
use crate::common::bitvec::BitSlice;
use crate::common::universal_io::{CachedReadFs, Populate, UniversalRead, UniversalReadFs};
use super::super::MapIndexKey;
use super::super::mutable_map_index::read_only::ReadOnlyAppendableMapIndex;
use super::super::on_disk_map_index::OnDiskMapIndex;
use super::ReadOnlyMapIndex;
use crate::segment::common::operation_error::OperationResult;
use crate::segment::index::field_index::map_index::immutable_map_index::ImmutableMapIndex;
use crate::segment::index::payload_config::IndexMutability;
impl<N: MapIndexKey + ?Sized, S: UniversalRead> ReadOnlyMapIndex<N, S>
where
Vec<<N as MapIndexKey>::Owned>: Blob + Send + Sync,
{
pub fn preopen_appendable(
fs: &impl CachedReadFs<File = S>,
dir: PathBuf,
) -> OperationResult<bool> {
ReadOnlyAppendableMapIndex::<N, S>::preopen(fs, dir)
}
pub fn open_appendable(
fs: &impl UniversalReadFs<File = S>,
dir: PathBuf,
) -> OperationResult<Option<Self>> {
Ok(ReadOnlyAppendableMapIndex::open(fs, dir)?.map(Self::Appendable))
}
pub fn preopen_immutable(
fs: &impl CachedReadFs<File = S>,
dir: &Path,
is_on_disk: bool,
) -> OperationResult<bool> {
let effective_is_on_disk =
is_on_disk || crate::common::low_memory::low_memory_mode().prefer_disk();
let populate = match effective_is_on_disk {
true => Populate::No,
false => Populate::PreferBackground,
};
OnDiskMapIndex::<N, S>::preopen(fs, dir, populate)
}
pub fn open_immutable(
fs: &impl UniversalReadFs<File = S>,
path: &Path,
is_on_disk: bool,
deleted_points: &BitSlice,
) -> OperationResult<Option<Self>> {
let effective_is_on_disk =
is_on_disk || crate::common::low_memory::low_memory_mode().prefer_disk();
let populate = match effective_is_on_disk {
true => Populate::No,
false => Populate::PreferBackground,
};
let Some(on_disk_index) = OnDiskMapIndex::open(fs, path, populate, deleted_points)? else {
return Ok(None);
};
if effective_is_on_disk {
Ok(Some(Self::OnDisk(on_disk_index)))
} else {
Ok(Some(Self::Immutable(ImmutableMapIndex::load_from_on_disk(
on_disk_index,
)?)))
}
}
pub fn get_mutability_type(&self) -> IndexMutability {
match self {
Self::Appendable(_) => IndexMutability::Mutable,
Self::Immutable(_) => IndexMutability::Immutable,
Self::OnDisk(_) => IndexMutability::Immutable,
}
}
}