use std::collections::HashSet;
use std::fs::File;
use std::io::{BufWriter, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
use super::error::{ErrorKind, SegmentError};
use super::format::{
Alignment, BLOCK_LEN, HEADER_LEN, Header, MAX_SECTIONS, MAX_SEGMENT_LEN, ROW_LEN, Requirement,
Row, SectionId,
};
use crate::ids::{Digest128, FamilyId, SegmentId, WriterIncarnation};
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct SegmentMeta {
pub family: FamilyId,
pub segment: SegmentId,
pub incarnation: WriterIncarnation,
pub created_unix_ms: u64,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct SectionSpec {
pub id: SectionId,
pub version: u16,
pub alignment: Alignment,
pub requirement: Requirement,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct SegmentSummary {
pub path: PathBuf,
pub len: u64,
pub sections: usize,
}
#[derive(Debug)]
pub struct SegmentWriter {
path: PathBuf,
out: BufWriter<File>,
meta: SegmentMeta,
position: u64,
rows: Vec<PendingRow>,
seen: HashSet<SectionId>,
block_digests: Vec<Digest128>,
open: Option<OpenSection>,
}
#[derive(Debug)]
struct PendingRow {
spec: SectionSpec,
offset: u64,
len: u64,
item_count: u64,
first_block: usize,
digest: Digest128,
}
#[derive(Debug)]
struct OpenSection {
spec: SectionSpec,
offset: u64,
len: u64,
whole: blake3::Hasher,
block: blake3::Hasher,
block_fill: usize,
first_block: usize,
}
impl SegmentWriter {
pub fn create(path: &Path, meta: SegmentMeta) -> Result<Self, SegmentError> {
let file = crate::fs::create_new_private(path).map_err(|error| io(path, error))?;
let mut writer = Self {
path: path.to_path_buf(),
out: BufWriter::with_capacity(BLOCK_LEN, file),
meta,
position: 0,
rows: Vec::new(),
seen: HashSet::new(),
block_digests: Vec::new(),
open: None,
};
writer.put(&[0; HEADER_LEN])?;
Ok(writer)
}
pub fn begin_section(&mut self, spec: SectionSpec) -> Result<(), SegmentError> {
if let Some(open) = &self.open {
return Err(self.error(ErrorKind::UnfinishedSection(open.spec.id)));
}
if !self.seen.insert(spec.id) {
return Err(self.error(ErrorKind::DuplicateSection(spec.id)));
}
if self.rows.len() >= MAX_SECTIONS as usize {
return Err(self.error(ErrorKind::Malformed("too many sections")));
}
self.pad_to(spec.alignment.bytes())?;
self.open = Some(OpenSection {
spec,
offset: self.position,
len: 0,
whole: blake3::Hasher::new(),
block: blake3::Hasher::new(),
block_fill: 0,
first_block: self.block_digests.len(),
});
Ok(())
}
pub fn write(&mut self, mut bytes: &[u8]) -> Result<(), SegmentError> {
let Some(mut open) = self.open.take() else {
return Err(self.error(ErrorKind::Malformed("bytes written outside a section")));
};
open.whole.update(bytes);
open.len += bytes.len() as u64;
while !bytes.is_empty() {
let take = bytes.len().min(BLOCK_LEN - open.block_fill);
open.block.update(&bytes[..take]);
open.block_fill += take;
if open.block_fill == BLOCK_LEN {
self.block_digests
.push(Digest128::from_hash(&open.block.finalize()));
open.block.reset();
open.block_fill = 0;
}
let (now, rest) = bytes.split_at(take);
self.put(now)?;
bytes = rest;
}
self.open = Some(open);
Ok(())
}
pub fn end_section(&mut self, item_count: u64) -> Result<(), SegmentError> {
let Some(open) = self.open.take() else {
return Err(self.error(ErrorKind::Malformed("no section is open")));
};
if open.block_fill > 0 {
self.block_digests
.push(Digest128::from_hash(&open.block.finalize()));
}
self.rows.push(PendingRow {
spec: open.spec,
offset: open.offset,
len: open.len,
item_count,
first_block: open.first_block,
digest: Digest128::from_hash(&open.whole.finalize()),
});
Ok(())
}
pub fn write_section(
&mut self,
spec: SectionSpec,
bytes: &[u8],
item_count: u64,
) -> Result<(), SegmentError> {
self.begin_section(spec)?;
self.write(bytes)?;
self.end_section(item_count)
}
pub fn finish(mut self) -> Result<SegmentSummary, SegmentError> {
if let Some(open) = &self.open {
return Err(self.error(ErrorKind::UnfinishedSection(open.spec.id)));
}
self.pad_to(Alignment::Word.bytes())?;
let checksums_offset = self.position;
let mut checksums = Vec::with_capacity(self.block_digests.len() * Digest128::LEN);
for digest in &self.block_digests {
checksums.extend_from_slice(digest.as_bytes());
}
self.put(&checksums)?;
let table_offset = self.position;
let mut table = Vec::with_capacity(self.rows.len() * ROW_LEN);
for pending in &self.rows {
let row = Row {
id: pending.spec.id,
version: pending.spec.version,
requirement: pending.spec.requirement,
alignment: pending.spec.alignment,
offset: pending.offset,
len: pending.len,
item_count: pending.item_count,
checksums_offset: checksums_offset + (pending.first_block * Digest128::LEN) as u64,
digest: pending.digest,
};
table.extend_from_slice(&row.encode());
}
self.put(&table)?;
let header = Header {
family: self.meta.family,
segment: self.meta.segment,
incarnation: self.meta.incarnation,
created_unix_ms: self.meta.created_unix_ms,
section_count: u32::try_from(self.rows.len())
.map_err(|_| self.error(ErrorKind::Malformed("too many sections")))?,
checksums_offset,
table_offset,
table_digest: Digest128::of(&table),
checksums_digest: Digest128::of(&checksums),
};
let path = self.path.clone();
let len = self.position;
let sections = self.rows.len();
let mut file = self
.out
.into_inner()
.map_err(|error| io(&path, error.into_error()))?;
file.seek(SeekFrom::Start(0))
.and_then(|_| file.write_all(&header.encode()))
.and_then(|()| file.sync_all())
.map_err(|error| io(&path, error))?;
Ok(SegmentSummary {
path,
len,
sections,
})
}
fn pad_to(&mut self, alignment: u64) -> Result<(), SegmentError> {
let padding = self.position.next_multiple_of(alignment) - self.position;
const ZEROS: [u8; 64] = [0; 64];
self.put(&ZEROS[..usize::try_from(padding).expect("padding is below 64 bytes")])
}
fn put(&mut self, bytes: &[u8]) -> Result<(), SegmentError> {
let end = self.position + bytes.len() as u64;
if end > MAX_SEGMENT_LEN {
return Err(self.error(ErrorKind::TooLarge {
limit: MAX_SEGMENT_LEN,
}));
}
self.out
.write_all(bytes)
.map_err(|error| io(&self.path, error))?;
self.position = end;
Ok(())
}
fn error(&self, kind: ErrorKind) -> SegmentError {
SegmentError::new(&self.path, kind)
}
}
fn io(path: &Path, error: std::io::Error) -> SegmentError {
SegmentError::new(path, ErrorKind::Io(error))
}