use std::collections::BTreeMap;
use crate::ids::{ContentKey, Digest128};
use crate::postings::{PostingCursor, PostingLayout, PostingsBuilder, Width};
use crate::segment::{
Alignment, ErrorKind, KeySpace, KnownSection, Requirement, SectionHandle, SectionId,
SectionKind, SectionSpec, Segment, SegmentError, SegmentWriter,
};
use crate::table::{
LookupTable, LookupTableBuilder, UnitLocation, UnitTable, UnitTableBuilder, UnitTableParts,
};
use super::trigrams::ContentTrigrams;
const UNITS: SectionKind = SectionKind::new(0x0100);
const UNIT_DATA: SectionKind = SectionKind::new(0x0101);
const TRIGRAM_LOOKUP: SectionKind = SectionKind::new(0x0102);
const TRIGRAM_POSTINGS: SectionKind = SectionKind::new(0x0103);
const VERSION: u16 = 1;
const KEY_SPACE: KeySpace = KeySpace::new(0);
const UNIT_DATA_LEN: usize = 13;
const UNIT_DATA_LEN_U32: u32 = 13;
pub const KNOWN: [KnownSection; 4] = [
KnownSection {
kind: UNITS,
version: VERSION,
},
KnownSection {
kind: UNIT_DATA,
version: VERSION,
},
KnownSection {
kind: TRIGRAM_LOOKUP,
version: VERSION,
},
KnownSection {
kind: TRIGRAM_POSTINGS,
version: VERSION,
},
];
fn posting_layout() -> PostingLayout {
PostingLayout::new(&[Width::U8])
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum TextStatus {
Indexed,
Binary,
Oversize,
}
impl TextStatus {
const fn code(self) -> u8 {
match self {
Self::Indexed => 0,
Self::Binary => 1,
Self::Oversize => 2,
}
}
const fn from_code(code: u8) -> Option<Self> {
match code {
0 => Some(Self::Indexed),
1 => Some(Self::Binary),
2 => Some(Self::Oversize),
_ => None,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct TextUnit {
pub len: u64,
pub distinct: u32,
pub status: TextStatus,
}
#[derive(Debug, Default)]
pub struct TextSectionsBuilder {
units: BTreeMap<ContentKey, (TextUnit, Vec<(u32, u8)>)>,
}
impl crate::heap::HeapBytes for TextSectionsBuilder {
fn heap_bytes(&self, _seen: &mut crate::heap::Seen) -> usize {
let entry =
std::mem::size_of::<ContentKey>() + std::mem::size_of::<(TextUnit, Vec<(u32, u8)>)>();
crate::heap::btree_bytes(self.units.len(), entry)
+ self
.units
.values()
.map(|(_, trigrams)| trigrams.capacity() * std::mem::size_of::<(u32, u8)>())
.sum::<usize>()
}
}
impl TextSectionsBuilder {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn add_indexed(&mut self, key: ContentKey, len: u64, trigrams: &ContentTrigrams) {
let unit = TextUnit {
len,
distinct: u32::try_from(trigrams.distinct()).unwrap_or(u32::MAX),
status: TextStatus::Indexed,
};
self.units
.entry(key)
.or_insert_with(|| (unit, trigrams.entries().to_vec()));
}
pub fn add_unindexed(&mut self, key: ContentKey, len: u64, status: TextStatus) {
debug_assert_ne!(status, TextStatus::Indexed);
let unit = TextUnit {
len,
distinct: 0,
status,
};
self.units.entry(key).or_insert((unit, Vec::new()));
}
#[must_use]
pub fn len(&self) -> usize {
self.units.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.units.is_empty()
}
pub fn retain(&mut self, mut keep: impl FnMut(ContentKey) -> bool) {
self.units.retain(|key, _| keep(*key));
}
pub fn write(self, writer: &mut SegmentWriter) -> Result<(), SegmentError> {
let mut table = UnitTableBuilder::new(Digest128::LEN);
let mut data = Vec::with_capacity(self.units.len() * UNIT_DATA_LEN);
let mut lists = BTreeMap::<u32, (Vec<u32>, Vec<u32>)>::new();
for (key, (unit, trigrams)) in self.units {
let ordinal = table.push(
key.digest().as_bytes(),
UnitLocation {
offset: data.len() as u64,
len: UNIT_DATA_LEN_U32,
},
);
push_unit_data(&mut data, unit);
for (trigram, mask) in trigrams {
let (ordinals, masks) = lists.entry(trigram).or_default();
ordinals.push(ordinal);
masks.push(u32::from(mask));
}
}
let mut postings = PostingsBuilder::new(posting_layout());
let mut lookup = LookupTableBuilder::new();
for (trigram, (ordinals, masks)) in lists {
let list = postings.add_list(&ordinals, &[&masks]);
lookup.push(u64::from(trigram), list);
}
let (table, units) = table.finish();
writer.write_section(spec(UNITS), &table, units)?;
writer.write_section(spec(UNIT_DATA), &data, units)?;
let (lookup, keys) = lookup.finish();
writer.write_section(spec(TRIGRAM_LOOKUP), &lookup, keys)?;
let (postings, entries) = postings.finish();
writer.write_section(spec(TRIGRAM_POSTINGS), &postings, entries)
}
}
fn push_unit_data(data: &mut Vec<u8>, unit: TextUnit) {
data.extend_from_slice(&unit.len.to_le_bytes());
data.extend_from_slice(&unit.distinct.to_le_bytes());
data.push(unit.status.code());
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum TextCopy {
Stored { source: usize, ordinal: u32 },
Unindexed { len: u64, status: TextStatus },
}
pub fn write_folded(
writer: &mut SegmentWriter,
sources: &[TextSegment<'_>],
units: &[(ContentKey, TextCopy)],
) -> Result<(), SegmentError> {
let mut renumbered = sources
.iter()
.map(|source| vec![crate::fold::NOT_COPIED; source.len() as usize])
.collect::<Vec<_>>();
let count = u32::try_from(units.len()).expect("a text table holds fewer than 2^32 units");
writer.begin_section(spec(UNITS))?;
writer.write(&(Digest128::LEN as u16).to_le_bytes())?;
writer.write(&[0, 0])?;
writer.write(&count.to_le_bytes())?;
let mut previous = None;
for (ordinal, (key, copy)) in units.iter().enumerate() {
assert!(
previous.is_none_or(|old| old < *key),
"unit keys must be strictly ascending"
);
previous = Some(*key);
writer.write(key.digest().as_bytes())?;
writer.write(&((ordinal * UNIT_DATA_LEN) as u64).to_le_bytes())?;
writer.write(&UNIT_DATA_LEN_U32.to_le_bytes())?;
if let TextCopy::Stored {
source,
ordinal: old,
} = *copy
{
renumbered[source][old as usize] = ordinal as u32;
}
}
writer.end_section(u64::from(count))?;
writer.begin_section(spec(UNIT_DATA))?;
for (_, copy) in units {
let unit = match *copy {
TextCopy::Stored { source, ordinal } => sources[source].unit(ordinal)?,
TextCopy::Unindexed { len, status } => {
debug_assert_ne!(status, TextStatus::Indexed);
TextUnit {
len,
distinct: 0,
status,
}
}
};
let mut data = Vec::with_capacity(UNIT_DATA_LEN);
push_unit_data(&mut data, unit);
writer.write(&data)?;
}
writer.end_section(u64::from(count))?;
let postings = sources
.iter()
.zip(&renumbered)
.map(|(source, renumbered)| crate::fold::PostingSource {
segment: source.segment,
lookup: LookupTable::reopen(source.segment, source.lookup.section()),
postings: source.postings,
renumbered,
})
.collect::<Vec<_>>();
crate::fold::write_postings(
writer,
spec(TRIGRAM_POSTINGS),
spec(TRIGRAM_LOOKUP),
&posting_layout(),
&postings,
)
}
fn spec(kind: SectionKind) -> SectionSpec {
SectionSpec {
id: SectionId::new(kind, KEY_SPACE),
version: VERSION,
alignment: Alignment::Word,
requirement: Requirement::Required,
}
}
#[derive(Debug)]
pub struct TextSegment<'s> {
segment: &'s Segment,
units: UnitTable<'s>,
data: SectionHandle,
lookup: LookupTable<'s>,
postings: SectionHandle,
}
#[derive(Clone, Copy, Debug)]
pub struct TextSections {
units: UnitTableParts,
data: SectionHandle,
lookup: SectionHandle,
postings: SectionHandle,
}
impl<'s> TextSegment<'s> {
pub fn open(segment: &'s Segment) -> Result<Option<Self>, SegmentError> {
let handles = [UNITS, UNIT_DATA, TRIGRAM_LOOKUP, TRIGRAM_POSTINGS]
.map(|kind| segment.section(SectionId::new(kind, KEY_SPACE)));
let [Some(units), Some(data), Some(lookup), Some(postings)] = handles else {
if handles.iter().any(Option::is_some) {
return Err(SegmentError::new(
segment.path(),
ErrorKind::Malformed("a segment has only some text sections"),
));
}
return Ok(None);
};
let units = UnitTable::open(segment, units)?;
if segment.info(data).len != u64::from(units.len()) * UNIT_DATA_LEN as u64 {
return Err(SegmentError::new(
segment.path(),
ErrorKind::Malformed("text unit data does not match the unit table"),
));
}
Ok(Some(Self {
segment,
units,
data,
lookup: LookupTable::open(segment, lookup)?,
postings,
}))
}
#[must_use]
pub fn sections(&self) -> TextSections {
TextSections {
units: self.units.parts(),
data: self.data,
lookup: self.lookup.section(),
postings: self.postings,
}
}
#[must_use]
pub fn with_sections(segment: &'s Segment, sections: TextSections) -> Self {
Self {
segment,
units: UnitTable::attach(segment, sections.units),
data: sections.data,
lookup: LookupTable::reopen(segment, sections.lookup),
postings: sections.postings,
}
}
#[must_use]
pub fn len(&self) -> u32 {
self.units.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.units.is_empty()
}
pub fn keys(&self) -> Result<Vec<ContentKey>, SegmentError> {
(0..self.units.len())
.map(|ordinal| {
let (key, _) = self.units.entry(ordinal)?;
let bytes = <[u8; Digest128::LEN]>::try_from(key.as_slice())
.map_err(|_| self.malformed("a text unit key is not a content key"))?;
Ok(ContentKey::from_digest(Digest128::from_bytes(bytes)))
})
.collect()
}
pub fn unit(&self, ordinal: u32) -> Result<TextUnit, SegmentError> {
let mut bytes = [0; UNIT_DATA_LEN];
self.segment.read(
self.data,
u64::from(ordinal) * UNIT_DATA_LEN as u64,
&mut bytes,
)?;
let status = TextStatus::from_code(bytes[12])
.ok_or_else(|| self.malformed("unknown text status"))?;
Ok(TextUnit {
len: crate::le::u64_at(&bytes, 0),
distinct: crate::le::u32_at(&bytes, 8),
status,
})
}
pub fn count(&self, trigram: u32) -> Result<u32, SegmentError> {
Ok(self
.lookup
.get(u64::from(trigram))?
.map_or(0, |list| list.count))
}
pub fn ordinals(
&self,
trigram: u32,
next: u8,
within: &[u32],
) -> Result<Vec<u32>, SegmentError> {
let Some(list) = self.lookup.get(u64::from(trigram))? else {
return Ok(Vec::new());
};
let mut cursor = PostingCursor::new(self.segment, self.postings, posting_layout(), list);
let mut found = Vec::new();
let mut wanted = within.iter().copied().peekable();
while let Some(&target) = wanted.peek() {
cursor.skip_below(target)?;
let Some(block) = cursor.next_block()? else {
break;
};
let masks = &block.columns[0];
for (ordinal, mask) in block.ordinals.iter().zip(masks) {
while wanted.peek().is_some_and(|target| target < ordinal) {
wanted.next();
}
if wanted.peek() == Some(ordinal) {
if next == 0 || u32::from(next) & mask != 0 {
found.push(*ordinal);
}
wanted.next();
}
}
}
Ok(found)
}
pub fn all_ordinals(&self, trigram: u32) -> Result<Vec<u32>, SegmentError> {
let Some(list) = self.lookup.get(u64::from(trigram))? else {
return Ok(Vec::new());
};
let mut found = Vec::with_capacity(list.count as usize);
let mut cursor = PostingCursor::new(self.segment, self.postings, posting_layout(), list);
while let Some(block) = cursor.next_block()? {
found.extend_from_slice(&block.ordinals);
}
Ok(found)
}
fn malformed(&self, reason: &'static str) -> SegmentError {
SegmentError::new(self.segment.path(), ErrorKind::Malformed(reason))
}
}
crate::heap_bytes_none!(TextSections);