pub mod checksum;
pub mod data_output;
pub(super) mod file_backing;
pub mod fs;
pub(super) mod index_input;
pub mod index_output;
pub mod memory;
pub mod mmap;
pub use checksum::CRC32;
pub use data_output::{DataOutput, VecOutput};
pub use file_backing::FileBacking;
pub use fs::FSDirectory;
pub(crate) use index_input::IndexInput;
pub use index_output::IndexOutput;
pub use memory::MemoryDirectory;
pub use mmap::MmapDirectory;
pub use crate::codecs::lucene90::compound_reader::CompoundDirectory;
use std::io;
use std::sync::Arc;
#[derive(Clone, Debug)]
pub(crate) struct SegmentFile {
pub(crate) name: String,
pub(crate) data: Vec<u8>,
}
pub type SharedDirectory = Arc<dyn Directory>;
pub trait Directory: Send + Sync {
fn create_output(&self, name: &str) -> io::Result<Box<dyn IndexOutput>>;
fn list_all(&self) -> io::Result<Vec<String>>;
fn file_length(&self, name: &str) -> io::Result<u64>;
fn delete_file(&self, name: &str) -> io::Result<()>;
fn rename(&self, source: &str, dest: &str) -> io::Result<()>;
fn read_file(&self, name: &str) -> io::Result<Vec<u8>>;
fn open_file(&self, name: &str) -> io::Result<FileBacking> {
Ok(FileBacking::Owned(self.read_file(name)?))
}
fn write_file(&self, name: &str, data: &[u8]) -> io::Result<()> {
let mut out = self.create_output(name)?;
out.write_all(data)?;
Ok(())
}
fn sync(&self, names: &[&str]) -> io::Result<()> {
let _ = names;
Ok(())
}
fn sync_meta_data(&self) -> io::Result<()> {
Ok(())
}
}
impl Directory for Arc<dyn Directory> {
fn create_output(&self, name: &str) -> io::Result<Box<dyn IndexOutput>> {
(**self).create_output(name)
}
fn list_all(&self) -> io::Result<Vec<String>> {
(**self).list_all()
}
fn file_length(&self, name: &str) -> io::Result<u64> {
(**self).file_length(name)
}
fn delete_file(&self, name: &str) -> io::Result<()> {
(**self).delete_file(name)
}
fn rename(&self, source: &str, dest: &str) -> io::Result<()> {
(**self).rename(source, dest)
}
fn read_file(&self, name: &str) -> io::Result<Vec<u8>> {
(**self).read_file(name)
}
fn open_file(&self, name: &str) -> io::Result<FileBacking> {
(**self).open_file(name)
}
fn write_file(&self, name: &str, data: &[u8]) -> io::Result<()> {
(**self).write_file(name, data)
}
fn sync(&self, names: &[&str]) -> io::Result<()> {
(**self).sync(names)
}
fn sync_meta_data(&self) -> io::Result<()> {
(**self).sync_meta_data()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::store::index_output::align_offset;
#[test]
fn test_write_le_int() {
let mut buf = Vec::new();
VecOutput(&mut buf).write_le_int(0x04030201_i32).unwrap();
assert_eq!(buf, [0x01, 0x02, 0x03, 0x04]);
}
#[test]
fn test_write_le_long() {
let mut buf = Vec::new();
VecOutput(&mut buf)
.write_le_long(0x0807060504030201_i64)
.unwrap();
assert_eq!(buf, [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]);
}
#[test]
fn test_write_le_short() {
let mut buf = Vec::new();
VecOutput(&mut buf).write_le_short(0x0201_i16).unwrap();
assert_eq!(buf, [0x01, 0x02]);
}
#[test]
fn test_write_be_int() {
let mut buf = Vec::new();
VecOutput(&mut buf).write_be_int(0x04030201_i32).unwrap();
assert_eq!(buf, [0x04, 0x03, 0x02, 0x01]);
}
#[test]
fn test_write_be_long() {
let mut buf = Vec::new();
VecOutput(&mut buf)
.write_be_long(0x0807060504030201_i64)
.unwrap();
assert_eq!(buf, [0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01]);
}
#[test]
fn test_align_offset() {
assert_eq!(align_offset(0, 8), 0);
assert_eq!(align_offset(1, 8), 8);
assert_eq!(align_offset(7, 8), 8);
assert_eq!(align_offset(8, 8), 8);
assert_eq!(align_offset(9, 8), 16);
}
}