use std::fmt::{Debug, Display, Formatter};
use std::path::{Path, PathBuf};
use crate::core::store::fs_directory::FSDirectory;
use crate::core::store::fs_directory_base::FSDirectoryBase;
use crate::core::store::lock_factory::LockFactory;
use crate::core::store::memory_segment_index_input::MemorySegmentIndexInput;
use crate::core::store::native_fs_lock_factory::NativeFSLockFactory;
use crate::core::store::{IOContext, ReadAdvice, fs_lock_factory};
use crate::core::util::error::lucene_error::{LuceneError, Result};
#[cfg(target_pointer_width = "64")]
pub const DEFAULT_MAX_CHUNK_SIZE: u64 = 1u64 << 34;
#[cfg(not(target_pointer_width = "64"))]
pub const DEFAULT_MAX_CHUNK_SIZE: u64 = 1u64 << 28;
pub type MMapPreloadPredicate = dyn Fn(&str, &IOContext) -> bool + Send + Sync;
pub enum MMapPreload {
AllFiles,
NoFiles,
BasedOnLoadIOContext,
Custom(Box<MMapPreloadPredicate>),
}
impl Debug for MMapPreload {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::AllFiles => f.write_str("AllFiles"),
Self::NoFiles => f.write_str("NoFiles"),
Self::BasedOnLoadIOContext => f.write_str("BasedOnLoadIOContext"),
Self::Custom(_) => f.write_str("Custom"),
}
}
}
impl MMapPreload {
pub fn custom<F>(preload: F) -> Self
where
F: Fn(&str, &IOContext) -> bool + Send + Sync + 'static,
{
Self::Custom(Box::new(preload))
}
fn test(&self, filename: &str, context: &IOContext) -> bool {
match self {
Self::AllFiles => true,
Self::NoFiles => false,
Self::BasedOnLoadIOContext => *context.get_read_advice() == ReadAdvice::RandomPreload,
Self::Custom(preload) => preload(filename, context),
}
}
}
pub struct MMapDirectory {
preload: MMapPreload,
chunk_size_power: u32,
}
impl Default for MMapDirectory {
fn default() -> Self {
Self {
preload: MMapPreload::NoFiles,
chunk_size_power: DEFAULT_MAX_CHUNK_SIZE.trailing_zeros(),
}
}
}
impl MMapDirectory {
pub const ALL_FILES: MMapPreload = MMapPreload::AllFiles;
pub const NO_FILES: MMapPreload = MMapPreload::NoFiles;
pub const BASED_ON_LOAD_IO_CONTEXT: MMapPreload = MMapPreload::BasedOnLoadIOContext;
pub fn new<P>(directory: P) -> Result<FSDirectory<NativeFSLockFactory, Self>>
where
P: Into<PathBuf>,
{
Self::with_lock_factory(directory, fs_lock_factory::get_default())
}
pub fn with_lock_factory<D, P>(directory: P, lock_factory: D) -> Result<FSDirectory<D, Self>>
where
D: LockFactory,
P: Into<PathBuf>,
{
Self::with_lock_factory_and_max_chunk_size(directory, lock_factory, DEFAULT_MAX_CHUNK_SIZE)
}
pub fn with_max_chunk_size<P>(
directory: P,
max_chunk_size: u64,
) -> Result<FSDirectory<NativeFSLockFactory, Self>>
where
P: Into<PathBuf>,
{
Self::with_lock_factory_and_max_chunk_size(
directory,
fs_lock_factory::get_default(),
max_chunk_size,
)
}
pub fn with_lock_factory_and_max_chunk_size<D, P>(
directory: P,
lock_factory: D,
max_chunk_size: u64,
) -> Result<FSDirectory<D, Self>>
where
D: LockFactory,
P: Into<PathBuf>,
{
let mut directory = FSDirectory::with_lock_factory(directory, lock_factory, Self::default())?;
directory.sub_fs_directory.chunk_size_power = Self::chunk_size_power(max_chunk_size)?;
Ok(directory)
}
pub fn set_preload(&mut self, preload: MMapPreload) {
self.preload = preload;
}
pub fn get_max_chunk_size(&self) -> u64 {
1u64 << self.chunk_size_power
}
pub fn supports_madvise() -> bool {
cfg!(any(target_os = "linux", target_os = "macos"))
}
fn chunk_size_power(max_chunk_size: u64) -> Result<u32> {
if max_chunk_size == 0 {
return Err(LuceneError::illegal_argument(
"Maximum chunk size for mmap must be >0",
));
}
let chunk_size_power = u64::BITS - 1 - max_chunk_size.leading_zeros();
debug_assert!((1u64 << chunk_size_power) <= max_chunk_size);
debug_assert!((1u64 << chunk_size_power) > (max_chunk_size / 2));
Ok(chunk_size_power)
}
}
impl Display for MMapDirectory {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "MMapDirectory")
}
}
impl FSDirectoryBase for MMapDirectory {
type Output = MemorySegmentIndexInput;
fn open_input(&self, name: &str, context: &IOContext, path: &Path) -> Result<Self::Output> {
let file_path = path.join(name);
let preload = self.preload.test(name, context);
MemorySegmentIndexInput::new(
format!("MemorySegmentIndexInput(path=\"{}\")", file_path.display()),
&file_path,
*context.get_read_advice(),
self.chunk_size_power,
preload,
)
}
}
impl<D> FSDirectory<D, MMapDirectory>
where
D: LockFactory,
{
pub fn set_preload(&mut self, preload: MMapPreload) {
self.sub_fs_directory.set_preload(preload);
}
pub fn get_max_chunk_size(&self) -> u64 {
self.sub_fs_directory.get_max_chunk_size()
}
}