use std::fs::File;
use std::io::{Read, Seek, SeekFrom};
use std::path::Path;
use super::FileProcessor;
use crate::errors::TaleError;
use crate::readers::strategies::StaticStrategy;
#[derive(Debug)]
pub struct FileChunk {
pub data: Vec<u8>,
pub start_offset: u64,
pub end_offset: u64,
pub starts_at_line_boundary: bool,
pub ends_at_line_boundary: bool,
}
impl FileChunk {
pub fn new(data: Vec<u8>, start_offset: u64, end_offset: u64) -> Self {
let starts_at_line_boundary = start_offset == 0 || data.first() != Some(&b'\n');
let ends_at_line_boundary = data.last() == Some(&b'\n');
Self {
data,
start_offset,
end_offset,
starts_at_line_boundary,
ends_at_line_boundary,
}
}
pub fn size(&self) -> usize {
self.data.len()
}
pub fn is_empty(&self) -> bool {
self.data.is_empty()
}
pub fn lines(&self) -> impl Iterator<Item = &str> {
let data_str = std::str::from_utf8(&self.data).unwrap_or("");
data_str.lines()
}
pub fn find_last_line_boundary(&self) -> Option<usize> {
self.data.iter().rposition(|&b| b == b'\n')
}
pub fn split_at_last_line(&mut self) -> Option<Vec<u8>> {
if let Some(boundary) = self.find_last_line_boundary() {
let remainder = self.data.split_off(boundary + 1);
self.end_offset = self.start_offset + self.data.len() as u64;
self.ends_at_line_boundary = true;
Some(remainder)
} else {
None
}
}
}
#[derive(Debug)]
pub struct ChunkedFileReader {
file: File,
file_size: u64,
current_position: u64,
pending_data: Vec<u8>,
strategy: StaticStrategy,
}
impl ChunkedFileReader {
pub fn new<P: AsRef<Path>>(path: P) -> Result<Self, TaleError> {
let file_size = std::fs::metadata(&path)?.len();
let strategy = StaticStrategy::optimal_for_file(file_size);
let file = File::open(&path)?;
Ok(Self {
file,
file_size,
current_position: 0,
pending_data: Vec::new(),
strategy,
})
}
pub fn with_strategy<P: AsRef<Path>>(path: P, strategy: StaticStrategy) -> Result<Self, TaleError> {
let mut reader = Self::new(path)?;
reader.strategy = strategy;
Ok(reader)
}
pub fn static_optimal<P: AsRef<Path>>(path: P) -> Result<Self, TaleError> {
Self::new(path)
}
pub fn with_optimal_config<P: AsRef<Path>>(path: P) -> Result<Self, TaleError> {
Self::new(path)
}
pub fn file_size(&self) -> u64 {
self.file_size
}
pub fn position(&self) -> u64 {
self.current_position
}
pub fn is_at_end(&self) -> bool {
self.current_position >= self.file_size
}
pub fn read_chunk(&mut self) -> Result<Option<FileChunk>, TaleError> {
if self.is_at_end() && self.pending_data.is_empty() {
return Ok(None);
}
let chunk_size = self.strategy.initial_chunk_size();
let pending_len = self.pending_data.len();
let mut buffer = vec![0u8; chunk_size];
let bytes_read = if self.is_at_end() {
0
} else {
self.file.read(&mut buffer).map_err(TaleError::from)?
};
if bytes_read == 0 && self.pending_data.is_empty() {
return Ok(None);
}
buffer.truncate(bytes_read);
if !self.pending_data.is_empty() {
let mut combined = std::mem::take(&mut self.pending_data);
combined.extend_from_slice(&buffer);
buffer = combined;
}
let start_offset = self.current_position - pending_len as u64;
self.current_position += bytes_read as u64;
let mut chunk = FileChunk::new(buffer, start_offset, self.current_position);
if !chunk.ends_at_line_boundary
&& !self.is_at_end()
&& let Some(remainder) = chunk.split_at_last_line()
{
self.pending_data = remainder;
}
Ok(Some(chunk))
}
pub fn seek(&mut self, pos: SeekFrom) -> Result<u64, TaleError> {
let new_pos = self.file.seek(pos).map_err(TaleError::from)?;
self.current_position = new_pos;
self.pending_data.clear();
Ok(new_pos)
}
}
impl FileProcessor for ChunkedFileReader {
fn process_lines<F>(&mut self, mut line_processor: F) -> Result<(), TaleError>
where
F: FnMut(&str) -> Result<(), TaleError>,
{
while let Some(chunk) = self.read_chunk()? {
for line in chunk.lines() {
line_processor(line)?;
}
}
Ok(())
}
fn skip_lines(&mut self, count: u64) -> Result<(), TaleError> {
let mut lines_skipped = 0u64;
while lines_skipped < count {
if let Some(chunk) = self.read_chunk()? {
let mut lines_in_chunk = 0u64;
let mut last_newline_pos = None;
for (i, &byte) in chunk.data.iter().enumerate() {
if byte == b'\n' {
lines_in_chunk += 1;
last_newline_pos = Some(i);
if lines_skipped + lines_in_chunk == count {
let position_after_newline = i + 1;
if position_after_newline < chunk.data.len() {
self.pending_data = chunk.data[position_after_newline..].to_vec();
}
return Ok(());
}
}
}
lines_skipped += lines_in_chunk;
if !chunk.ends_at_line_boundary && lines_skipped < count {
if let Some(last_nl) = last_newline_pos {
let after_last_newline = last_nl + 1;
if after_last_newline < chunk.data.len() {
self.pending_data = chunk.data[after_last_newline..].to_vec();
}
} else {
self.pending_data = chunk.data;
}
}
} else {
break;
}
}
Ok(())
}
fn file_size(&self) -> u64 {
self.file_size
}
fn seek(&mut self, pos: SeekFrom) -> Result<u64, TaleError> {
self.seek(pos)
}
fn position(&self) -> u64 {
self.current_position
}
}