use crate::core::index::BytesRef;
use crate::core::util::access::ByteSource;
use crate::core::util::accountable::Accountable;
use crate::core::util::bit_util::BitUtil;
use crate::core::util::bytes_ref_hash::do_hash;
use crate::core::util::error::lucene_error::{LuceneError, Result};
use crate::core::util::{
BYTE_BLOCK_MASK, BYTE_BLOCK_SHIFT, BYTE_BLOCK_SIZE, ByteBlockPool, SliceCopyOps,
};
pub struct BytesRefBlockPoolPosition {
pub block_index: usize,
pub offset: usize,
pub length: usize,
}
pub struct BytesRefBlockPool;
impl Default for BytesRefBlockPool {
fn default() -> Self {
Self::new()
}
}
impl BytesRefBlockPool {
pub fn new() -> Self {
Self {}
}
pub fn reset(&mut self, byte_block_pool: &mut ByteBlockPool) {
byte_block_pool.reset(false, false)
}
pub fn fill_bytes_ref(
&self,
start: i32,
byte_block_pool: &ByteBlockPool,
) -> BytesRefBlockPoolPosition {
let block_index = (start >> BYTE_BLOCK_SHIFT) as usize;
let block = byte_block_pool.get_buffer(block_index);
let pos = (start & BYTE_BLOCK_MASK) as usize;
let (length, offset) = Self::length_and_offset(block, pos);
BytesRefBlockPoolPosition {
block_index,
offset,
length,
}
}
#[inline]
fn length_and_offset(block: &[u8], pos: usize) -> (usize, usize) {
if (block[pos] & 0x80) == 0 {
(block[pos] as usize, pos + 1)
} else {
((BitUtil::get_i16_be(block, pos) & 0x7FFF) as usize, pos + 2)
}
}
#[inline]
pub(crate) fn borrow_bytes_ref<'a>(
&self,
start: i32,
byte_block_pool: &'a ByteBlockPool,
) -> BytesRef<&'a [u8]> {
let block_index = (start >> BYTE_BLOCK_SHIFT) as usize;
let block = byte_block_pool.get_buffer(block_index);
let pos = (start & BYTE_BLOCK_MASK) as usize;
let (length, offset) = Self::length_and_offset(block, pos);
BytesRef {
bytes: &block[offset..offset + length],
offset: 0,
length,
}
}
pub fn add_bytes_ref<BS>(&mut self, bytes: &BytesRef<BS>, pool: &mut ByteBlockPool) -> Result<i32>
where
BS: ByteSource,
{
let length = bytes.length;
let len2 = 2 + length as i32;
if len2 + pool.byte_upto > BYTE_BLOCK_SIZE {
if len2 > BYTE_BLOCK_SIZE {
return Err(LuceneError::max_bytes_length_exceeded(format!(
"bytes can be at most {} in length; got {}",
BYTE_BLOCK_SIZE - 2,
bytes.length
)));
}
pool.next_buffer()?;
}
let text_start = pool.byte_upto + pool.byte_offset;
let buffer_upto = pool.byte_upto as usize;
let buffer_index = pool.buffer_upto()?;
let buffer = pool.get_buffer_mut(buffer_index);
let bytes_slice = bytes.as_byte_slice();
let new_length = if length < 128 {
buffer[buffer_upto] = length as u8;
buffer.copy_from(bytes_slice, buffer_upto + 1);
length + 1
} else {
BitUtil::set_i16_be(buffer, buffer_upto, (length | 0x8000) as i16);
buffer.copy_from(bytes_slice, buffer_upto + 2);
length + 2
};
pool.byte_upto += new_length as i32;
Ok(text_start)
}
pub fn hash(&self, start: i32, pool: &ByteBlockPool) -> i32 {
let offset = (start & BYTE_BLOCK_MASK) as usize;
let bytes = pool.get_buffer((start >> BYTE_BLOCK_SHIFT) as usize);
let (len, pos) = if (bytes[offset] & 0x80) == 0 {
(bytes[offset] as usize, offset + 1)
} else {
let len = BitUtil::get_i16_be(bytes, offset) & 0x7FFF;
(len as usize, offset + 2)
};
do_hash(bytes, pos, len)
}
pub fn equals<BS>(&self, start: i32, b: &BytesRef<BS>, pool: &ByteBlockPool) -> bool
where
BS: ByteSource,
{
let pos = (start & BYTE_BLOCK_MASK) as usize;
let bytes = pool.get_buffer((start >> BYTE_BLOCK_SHIFT) as usize);
let (length, offset) = if (bytes[pos] & 0x80) == 0 {
(bytes[pos] as usize, pos + 1)
} else {
let length = BitUtil::get_i16_be(bytes, pos) & 0x7FFF;
(length as usize, pos + 2)
};
bytes[offset..offset + length] == *b.as_byte_slice()
}
}
impl Accountable for BytesRefBlockPool {
fn ram_bytes_used(&self) -> Result<i64> {
Ok(0)
}
}