use std::path::{Path, PathBuf};
use crate::common::bitvec::BitSlice;
use crate::common::universal_io::{CachedReadFs, Populate, UniversalRead, UniversalReadFs};
use super::super::mutable_geo_index::read_only::ReadOnlyAppendableGeoIndex;
use super::super::on_disk_geo_index::OnDiskGeoIndex;
use super::ReadOnlyGeoIndex;
use crate::segment::common::operation_error::OperationResult;
use crate::segment::index::field_index::geo_index::immutable_geo_index::ImmutableGeoIndex;
impl<S: UniversalRead> ReadOnlyGeoIndex<S> {
pub fn preopen_appendable(
fs: &impl CachedReadFs<File = S>,
dir: PathBuf,
) -> OperationResult<bool> {
ReadOnlyAppendableGeoIndex::preopen(fs, dir)
}
pub fn preopen_immutable(
fs: &impl CachedReadFs<File = S>,
path: &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,
};
OnDiskGeoIndex::preopen(fs, path, populate)
}
pub fn open_appendable(
fs: &impl UniversalReadFs<File = S>,
dir: PathBuf,
) -> OperationResult<Option<Self>> {
Ok(ReadOnlyAppendableGeoIndex::open(fs, dir)?.map(Self::Appendable))
}
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 = Populate::from(!effective_is_on_disk);
let Some(on_disk_index) = OnDiskGeoIndex::open(fs, path, populate, deleted_points)? else {
return Ok(None);
};
let index = if is_on_disk {
Self::OnDisk(on_disk_index)
} else {
Self::Immutable(ImmutableGeoIndex::load_from_on_disk(on_disk_index)?)
};
Ok(Some(index))
}
}