use std::io::Read;
use dataprof_core::DataProfilerError;
pub fn csv_record_count<R: Read>(
reader: R,
delimiter: u8,
quote: u8,
) -> Result<u64, DataProfilerError> {
let mut reader = csv::ReaderBuilder::new()
.has_headers(false)
.flexible(true)
.delimiter(delimiter)
.quote(quote)
.from_reader(reader);
let mut record = csv::ByteRecord::new();
let mut count = 0;
while reader.read_byte_record(&mut record)? {
count += 1;
}
Ok(count)
}
pub fn observed_total_rows(total_bytes: u64, read_bytes: u64, rows: u64) -> Option<u64> {
if read_bytes == 0 || rows == 0 {
return None;
}
let estimate = (total_bytes as u128 * rows as u128).div_ceil(read_bytes as u128);
Some(estimate.max(rows as u128).min(u64::MAX as u128) as u64)
}