use kevy_index::IndexSpec;
use kevy_store::Store;
use super::{BuildState, Segment, ShardIndexes};
use crate::state::CatalogState;
pub(super) fn freeze_text_batches(st: &mut ShardIndexes, table: &[u8], keys: &[Vec<u8>], dir: &std::path::Path) {
for si in &mut st.idx {
let (Some(cold), Some(ts), BuildState::Ready) =
(&mut si.cold_text, &mut si.text, &si.build)
else {
continue;
};
if table_of(&si.spec.name) != table {
continue;
}
match cold.freeze_batch(ts, &si.spec.name, keys, dir) {
Ok(true) => st.stats_dirty = true,
Ok(false) => {}
Err(e) => eprintln!(
"kevy: text freeze '{}': {e}",
String::from_utf8_lossy(&si.spec.name)
),
}
}
}
pub(super) fn evict_and_slide(
win: &mut kevy_window::WindowRt,
name: &[u8],
seg: &mut Segment,
store: &mut Store,
dir: &std::path::Path,
drives_rows: bool,
) -> bool {
if drives_rows && let Some(rows) = win.pending_rows(seg) {
let sealed = store
.enable_seg_rows(dir)
.and_then(|()| store.seal_rows_to_seg(table_of(name), &rows));
match sealed {
Ok(None) => {}
Ok(Some(batch)) => {
crate::kevy_rt_push_tick_frame(&batch.file);
store.commit_row_eviction(&batch);
}
Err(e) => {
eprintln!("kevy: row eviction '{}': {e}", String::from_utf8_lossy(name));
return false;
}
}
}
match win.slide(name, seg, dir) {
Ok(true) => {
kevy_sys::malloc_trim_now();
true
}
Ok(false) => false,
Err(e) => {
eprintln!("kevy: window slide '{}': {e}", String::from_utf8_lossy(name));
false
}
}
}
pub(super) fn table_of(index_name: &[u8]) -> &[u8] {
let dot = index_name.iter().position(|&b| b == b'.').unwrap_or(index_name.len());
&index_name[..dot]
}
pub(super) fn window_for(
catalogs: &CatalogState,
spec: &IndexSpec,
) -> Option<(kevy_index::WindowSpec, kevy_index::WindowShape)> {
kevy_index::window_for(catalogs.table()?.as_ref(), &spec.name)
}
pub(super) fn window_driver(catalogs: &CatalogState, index_name: &[u8]) -> bool {
catalogs.table().is_some_and(|t| kevy_index::window_driver(t.as_ref(), index_name))
}
pub(super) fn text_window_for(catalogs: &CatalogState, spec: &IndexSpec) -> bool {
catalogs.table().is_some_and(|t| kevy_index::window_text_for(t.as_ref(), spec))
}
pub(super) fn shard_segs_dir(
state: &crate::state::RuntimeState,
shard_id: usize,
) -> Option<std::path::PathBuf> {
state.sidecar_dir().map(|d| kevy_persist::layout::segs_dir(d, shard_id))
}