use std::path::PathBuf;
use crate::writer::index::selection::{IndexingState, SelectionRefusal};
#[derive(Clone, PartialEq, Eq, Debug)]
#[non_exhaustive]
pub enum ReindexAction {
Rebuild {
path: String,
state: IndexingState,
entries: u64,
entry_bytes: u64,
},
RebuildLucene {
path: String,
state: IndexingState,
rules: usize,
documents: u64,
stored_bytes: u64,
indexed_bytes: u64,
binary_text_policy: String,
},
Reset {
path: String,
lane: String,
hidden_children: Vec<String>,
},
NothingToDo {
path: String,
reason: NoWorkReason,
},
}
impl ReindexAction {
#[must_use]
pub fn path(&self) -> &str {
match self {
Self::Rebuild { path, .. }
| Self::RebuildLucene { path, .. }
| Self::Reset { path, .. }
| Self::NothingToDo { path, .. } => path,
}
}
#[must_use]
pub fn is_no_op(&self) -> bool {
matches!(self, Self::NothingToDo { .. })
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[non_exhaustive]
pub enum NoWorkReason {
NoRemovableHiddenChild,
}
impl std::fmt::Display for NoWorkReason {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::NoRemovableHiddenChild => {
write!(formatter, "it has no hidden child to remove")
}
}
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
#[non_exhaustive]
pub enum ReindexWarning {
Skipped {
refusal: SelectionRefusal,
},
WorkDirectoryMayBeTooSmall {
directory: PathBuf,
estimated_bytes: u64,
available_bytes: u64,
},
ResidueUnderDefaultWorkDirectory {
directory: PathBuf,
},
}
impl std::fmt::Display for ReindexWarning {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Skipped { refusal } => write!(formatter, "{refusal}"),
Self::WorkDirectoryMayBeTooSmall {
directory,
estimated_bytes,
available_bytes,
} => write!(
formatter,
"the work directory {} reports {} free, and one definition's spill may \
reach {} — the run refuses on ENOSPC before any index record is \
written, but it refuses late",
directory.display(),
crate::format_byte_size(*available_bytes),
crate::format_byte_size(*estimated_bytes)
),
Self::ResidueUnderDefaultWorkDirectory { directory } => write!(
formatter,
"{} is left over from an earlier run; it is not in the way, but nothing \
will remove it either",
directory.display()
),
}
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct ReindexPlan {
pub directory: PathBuf,
pub actions: Vec<ReindexAction>,
pub warnings: Vec<ReindexWarning>,
pub work_directory: PathBuf,
pub work_directory_estimate_bytes: u64,
}
impl ReindexPlan {
#[must_use]
pub fn is_empty(&self) -> bool {
self.actions.iter().all(ReindexAction::is_no_op)
}
#[must_use]
pub fn rebuild_count(&self) -> usize {
self.actions
.iter()
.filter(|action| matches!(action, ReindexAction::Rebuild { .. }))
.count()
}
#[must_use]
pub fn lucene_rebuild_count(&self) -> usize {
self.actions
.iter()
.filter(|action| matches!(action, ReindexAction::RebuildLucene { .. }))
.count()
}
#[must_use]
pub fn reset_count(&self) -> usize {
self.actions
.iter()
.filter(|action| matches!(action, ReindexAction::Reset { .. }))
.count()
}
}
#[must_use]
pub fn lucene_work_directory_estimate(
stored_bytes: u64,
indexed_bytes: u64,
sort_budget_bytes: u64,
) -> u64 {
stored_bytes
.saturating_add(indexed_bytes)
.saturating_mul(3)
.saturating_add(
(crate::writer::index::MAXIMUM_FAN_IN as u64).saturating_mul(sort_budget_bytes),
)
}
#[must_use]
pub fn work_directory_estimate(entry_bytes: u64, sort_budget_bytes: u64) -> u64 {
entry_bytes.saturating_add(
(crate::writer::index::MAXIMUM_FAN_IN as u64).saturating_mul(sort_budget_bytes),
)
}