use std::{cmp::Ordering, num::NonZeroU32, path::Path};
use idx_file::{Found, IdxFile};
use various_data_file::{DataAddress, VariousDataFile};
pub struct BinarySet {
index: IdxFile<DataAddress>,
data_file: VariousDataFile,
}
impl BinarySet {
pub fn new<P: AsRef<Path>>(path: P, allocation_lot: u32) -> Self {
let path = path.as_ref();
let file_name = path.file_name().map_or("".into(), |f| f.to_string_lossy());
Self {
index: IdxFile::new(
{
let mut path = path.to_path_buf();
path.set_file_name(&(file_name.to_string() + ".i"));
path
},
allocation_lot,
),
data_file: VariousDataFile::new({
let mut path = path.to_path_buf();
path.set_file_name(&(file_name.into_owned() + ".d"));
path
}),
}
}
pub fn bytes(&self, row: NonZeroU32) -> Option<&'static [u8]> {
self.index.get(row).map(|v| self.data_file.bytes(v))
}
pub fn row(&self, content: &[u8]) -> Option<NonZeroU32> {
let found = self.search(content);
if found.ord() == Ordering::Equal {
Some(found.row().unwrap())
} else {
None
}
}
pub fn row_or_insert(&mut self, content: &[u8]) -> NonZeroU32 {
let found = self.search(content);
let found_row = found.row();
if found.ord() == Ordering::Equal && found_row.is_some() {
found_row.unwrap()
} else {
let row = unsafe { NonZeroU32::new_unchecked(self.index.rows_count() + 1) };
unsafe {
self.index.insert_unique_unchecked(
row,
self.data_file.insert(content).address().clone(),
found,
);
}
row
}
}
fn search(&self, target: &[u8]) -> Found {
self.index.search(|v| self.data_file.bytes(v).cmp(target))
}
}