use crate::core::store::data_output::DataOutput;
use crate::core::store::{DataInput, OutputStreamIndexOutput};
use crate::core::util::close::Closeable;
use crate::core::util::error::lucene_error::{LuceneError, Result};
use std::collections::{HashMap, HashSet};
use std::fmt::{Display, Formatter};
use std::fs::File;
pub trait IndexOutput: DataOutput + Display + Closeable + Send + Sync {
fn get_file_pointer(&self) -> Result<usize>;
fn get_checksum(&mut self) -> Result<u64>;
fn get_name(&self) -> &str;
fn align_file_pointer(&mut self, alignment_bytes: usize) -> Result<usize> {
let offset = self.get_file_pointer()?;
let aligned_offset = align_offset(offset, alignment_bytes)?;
let count = aligned_offset - offset;
for _ in 0..count {
self.write_byte(0)?;
}
Ok(aligned_offset)
}
}
pub trait ErasedIndexOutput: IndexOutput {
fn copy_bytes_erased(&mut self, input: &mut dyn DataInput, num_bytes: usize) -> Result<()>;
}
impl<T> ErasedIndexOutput for T
where
T: IndexOutput,
{
fn copy_bytes_erased(&mut self, input: &mut dyn DataInput, num_bytes: usize) -> Result<()> {
self.copy_bytes(input, num_bytes)
}
}
pub type DynIndexOutput = dyn ErasedIndexOutput + Send + Sync;
pub type CustomIndexOutput = Box<DynIndexOutput>;
pub enum IndexOutputEnum {
Fs(OutputStreamIndexOutput<File>),
Custom(CustomIndexOutput),
}
impl IndexOutputEnum {
pub fn custom<O>(out: O) -> Self
where
O: IndexOutput + Send + Sync + 'static,
{
Self::Custom(Box::new(out))
}
}
impl DataOutput for IndexOutputEnum {
fn write_byte(&mut self, b: u8) -> Result<()> {
match self {
Self::Fs(inner) => inner.write_byte(b),
Self::Custom(inner) => inner.write_byte(b),
}
}
fn write_bytes_with_len(&mut self, b: &[u8], len: usize) -> Result<()> {
match self {
Self::Fs(inner) => inner.write_bytes_with_len(b, len),
Self::Custom(inner) => inner.write_bytes_with_len(b, len),
}
}
fn write_bytes_range(&mut self, b: &[u8], offset: usize, length: usize) -> Result<()> {
match self {
Self::Fs(inner) => inner.write_bytes_range(b, offset, length),
Self::Custom(inner) => inner.write_bytes_range(b, offset, length),
}
}
fn write_int(&mut self, i: i32) -> Result<()> {
match self {
Self::Fs(inner) => inner.write_int(i),
Self::Custom(inner) => inner.write_int(i),
}
}
fn write_ints(&mut self, values: &[i32]) -> Result<()> {
match self {
Self::Fs(inner) => inner.write_ints(values),
Self::Custom(inner) => inner.write_ints(values),
}
}
fn write_short(&mut self, i: i16) -> Result<()> {
match self {
Self::Fs(inner) => inner.write_short(i),
Self::Custom(inner) => inner.write_short(i),
}
}
fn write_vint(&mut self, i: i32) -> Result<()> {
match self {
Self::Fs(inner) => inner.write_vint(i),
Self::Custom(inner) => inner.write_vint(i),
}
}
fn write_zint(&mut self, i: i32) -> Result<()> {
match self {
Self::Fs(inner) => inner.write_zint(i),
Self::Custom(inner) => inner.write_zint(i),
}
}
fn write_long(&mut self, i: i64) -> Result<()> {
match self {
Self::Fs(inner) => inner.write_long(i),
Self::Custom(inner) => inner.write_long(i),
}
}
fn write_vlong(&mut self, i: i64) -> Result<()> {
match self {
Self::Fs(inner) => inner.write_vlong(i),
Self::Custom(inner) => inner.write_vlong(i),
}
}
fn write_signed_vlong(&mut self, i: i64) -> Result<()> {
match self {
Self::Fs(inner) => inner.write_signed_vlong(i),
Self::Custom(inner) => inner.write_signed_vlong(i),
}
}
fn write_zlong(&mut self, i: i64) -> Result<()> {
match self {
Self::Fs(inner) => inner.write_zlong(i),
Self::Custom(inner) => inner.write_zlong(i),
}
}
fn write_string(&mut self, s: &str) -> Result<()> {
match self {
Self::Fs(inner) => inner.write_string(s),
Self::Custom(inner) => inner.write_string(s),
}
}
fn copy_bytes<I>(&mut self, input: &mut I, num_bytes: usize) -> Result<()>
where
Self: Sized,
I: DataInput + ?Sized,
{
match self {
Self::Fs(inner) => inner.copy_bytes(input, num_bytes),
Self::Custom(inner) => {
let mut input_ref = input;
inner.copy_bytes_erased(&mut input_ref, num_bytes)
},
}
}
fn write_map_of_strings(&mut self, map: &HashMap<String, String>) -> Result<()> {
match self {
Self::Fs(inner) => inner.write_map_of_strings(map),
Self::Custom(inner) => inner.write_map_of_strings(map),
}
}
fn write_set_of_strings(&mut self, set: &HashSet<String>) -> Result<()> {
match self {
Self::Fs(inner) => inner.write_set_of_strings(set),
Self::Custom(inner) => inner.write_set_of_strings(set),
}
}
}
impl Display for IndexOutputEnum {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::Fs(inner) => inner.fmt(f),
Self::Custom(inner) => inner.fmt(f),
}
}
}
impl Closeable for IndexOutputEnum {
fn close(&mut self) -> Result<()> {
match self {
Self::Fs(inner) => inner.close(),
Self::Custom(inner) => inner.close(),
}
}
}
impl IndexOutput for IndexOutputEnum {
fn get_file_pointer(&self) -> Result<usize> {
match self {
Self::Fs(inner) => inner.get_file_pointer(),
Self::Custom(inner) => inner.get_file_pointer(),
}
}
fn get_checksum(&mut self) -> Result<u64> {
match self {
Self::Fs(inner) => inner.get_checksum(),
Self::Custom(inner) => inner.get_checksum(),
}
}
fn get_name(&self) -> &str {
match self {
Self::Fs(inner) => inner.get_name(),
Self::Custom(inner) => inner.get_name(),
}
}
fn align_file_pointer(&mut self, alignment_bytes: usize) -> Result<usize> {
match self {
Self::Fs(inner) => inner.align_file_pointer(alignment_bytes),
Self::Custom(inner) => inner.align_file_pointer(alignment_bytes),
}
}
}
pub fn align_offset(offset: usize, alignment_bytes: usize) -> Result<usize> {
if alignment_bytes == 0 || alignment_bytes.count_ones() != 1 {
return Err(LuceneError::illegal_argument(
"Alignment must be a power of 2",
));
}
let alignment_mask = alignment_bytes - 1;
Ok(
offset
.checked_add(alignment_mask)
.ok_or_else(|| LuceneError::number_overflow("Offset alignment overflow"))?
& !alignment_mask,
)
}
macro_rules! either_index_output {
($vis:vis $name:ident { $( $Variant:ident : $T:ident ),+ $(,)? }) => {
$vis enum $name<$( $T ),+> {
$( $Variant($T), )+
}
impl<$( $T ),+> DataOutput for $name<$( $T ),+>
where
$( $T: DataOutput ),+
{
fn write_byte(&mut self, b: u8) -> Result<()> {
match self {
$( Self::$Variant(inner) => inner.write_byte(b), )+
}
}
fn write_bytes_with_len(&mut self, b: &[u8], len: usize) -> Result<()> {
match self {
$( Self::$Variant(inner) => inner.write_bytes_with_len(b, len), )+
}
}
fn write_bytes_range(&mut self, b: &[u8], offset: usize, length: usize) -> Result<()> {
match self {
$( Self::$Variant(inner) => inner.write_bytes_range(b, offset, length), )+
}
}
fn write_int(&mut self, i: i32) -> Result<()> {
match self {
$( Self::$Variant(inner) => inner.write_int(i), )+
}
}
fn write_ints(&mut self, values: &[i32]) -> Result<()> {
match self {
$( Self::$Variant(inner) => inner.write_ints(values), )+
}
}
fn write_short(&mut self, i: i16) -> Result<()> {
match self {
$( Self::$Variant(inner) => inner.write_short(i), )+
}
}
fn write_vint(&mut self, i: i32) -> Result<()> {
match self {
$( Self::$Variant(inner) => inner.write_vint(i), )+
}
}
fn write_zint(&mut self, i: i32) -> Result<()> {
match self {
$( Self::$Variant(inner) => inner.write_zint(i), )+
}
}
fn write_long(&mut self, i: i64) -> Result<()> {
match self {
$( Self::$Variant(inner) => inner.write_long(i), )+
}
}
fn write_vlong(&mut self, i: i64) -> Result<()> {
match self {
$( Self::$Variant(inner) => inner.write_vlong(i), )+
}
}
fn write_signed_vlong(&mut self, i: i64) -> Result<()> {
match self {
$( Self::$Variant(inner) => inner.write_signed_vlong(i), )+
}
}
fn write_zlong(&mut self, i: i64) -> Result<()> {
match self {
$( Self::$Variant(inner) => inner.write_zlong(i), )+
}
}
fn write_string(&mut self, s: &str) -> Result<()> {
match self {
$( Self::$Variant(inner) => inner.write_string(s), )+
}
}
fn copy_bytes<I>(&mut self, input: &mut I, num_bytes: usize) -> Result<()>
where
I: DataInput + ?Sized,
{
match self {
$( Self::$Variant(inner) => inner.copy_bytes(input, num_bytes), )+
}
}
fn write_map_of_strings(&mut self, map: &HashMap<String, String>) -> Result<()> {
match self {
$( Self::$Variant(inner) => inner.write_map_of_strings(map), )+
}
}
fn write_set_of_strings(&mut self, set: &HashSet<String>) -> Result<()> {
match self {
$( Self::$Variant(inner) => inner.write_set_of_strings(set), )+
}
}
}
impl<$( $T ),+> Display for $name<$( $T ),+>
where
$( $T: Display ),+
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
$( Self::$Variant(inner) => inner.fmt(f), )+
}
}
}
impl<$( $T ),+> Closeable for $name<$( $T ),+>
where
$( $T: Closeable ),+
{
fn close(&mut self) -> Result<()> {
match self {
$( Self::$Variant(inner) => inner.close(), )+
}
}
}
impl<$( $T ),+> IndexOutput for $name<$( $T ),+>
where
$( $T: IndexOutput ),+
{
fn get_file_pointer(&self) -> Result<usize>{
match self {
$( Self::$Variant(inner) => inner.get_file_pointer(), )+
}
}
fn get_checksum(&mut self) -> Result<u64> {
match self {
$( Self::$Variant(inner) => inner.get_checksum(), )+
}
}
fn get_name(&self) -> &str {
match self {
$( Self::$Variant(inner) => inner.get_name(), )+
}
}
fn align_file_pointer(&mut self, alignment_bytes: usize) -> Result<usize> {
match self {
$( Self::$Variant(inner) => inner.align_file_pointer(alignment_bytes), )+
}
}
}
};
}
either_index_output!(pub IndexOutputEnum2 { A: A, B: B });
either_index_output!(pub IndexOutputEnum3 { A: A, B: B, C: C });