use std::collections::HashMap;
use std::fmt;
use std::sync::PoisonError;
use rusqlite::{Connection, OptionalExtension, params};
use super::block;
use super::engine::{Records, execute, nanos};
use super::event::{Fields, Kept};
use super::events::AllEvents;
use super::head::{self, Codec, ROW_EVENTS};
use super::pack::{CONTEXT_KEY, FIELD_KEY, HELD, NAME_KEY, PATIENCE, insert_keys};
use super::read::EVENT_HELD;
use super::repair::Part;
use super::segment::Schema;
use crate::Result;
use crate::limits::COUNT_BYTES;
use crate::sqlite::sql_error;
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct StreamFields {
pub event_names: Vec<NameCount>,
pub fields: Vec<FieldInfo>,
pub context: Vec<FieldInfo>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct NameCount {
pub name: String,
pub count: u64,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FieldInfo {
pub name: String,
pub kinds: Vec<Kind>,
pub count: u64,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Kind {
Float,
Integer,
Text,
Bool,
Object,
List,
Null,
}
impl Kind {
const ALL: [Kind; 7] = [Kind::Float, Kind::Integer, Kind::Text, Kind::Bool, Kind::Object, Kind::List, Kind::Null];
pub(super) fn of(json: &str) -> Kind {
let json = json.trim_start();
match json.as_bytes().first() {
Some(b'{') => Kind::Object,
Some(b'[') => Kind::List,
Some(b'"') => Kind::Text,
Some(b't' | b'f') => Kind::Bool,
Some(b'n') => Kind::Null,
_ if json.contains(['.', 'e', 'E']) => Kind::Float,
_ => Kind::Integer,
}
}
fn bit(self) -> u8 {
match self {
Kind::Float => 1,
Kind::Integer => 1 << 1,
Kind::Text => 1 << 2,
Kind::Bool => 1 << 3,
Kind::Object => 1 << 4,
Kind::List => 1 << 5,
Kind::Null => 1 << 6,
}
}
fn all_in(bits: u8) -> Vec<Kind> {
Kind::ALL.into_iter().filter(|kind| bits & kind.bit() != 0).collect()
}
pub fn name(self) -> &'static str {
match self {
Kind::Float => "float",
Kind::Integer => "integer",
Kind::Text => "text",
Kind::Bool => "bool",
Kind::Object => "object",
Kind::List => "list",
Kind::Null => "null",
}
}
}
impl fmt::Display for Kind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.name())
}
}
impl AllEvents {
pub fn fields(&self, stream: &str) -> Result<StreamFields> {
self.records.carried(stream).map_err(|error| error.within(format!("events {stream}")))
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(super) struct Count {
pub(super) events: u64,
pub(super) kinds: u8,
last: u64,
}
pub(super) type KeyRow = (u8, String, Count);
#[derive(Debug, Default)]
pub(super) struct Tally {
names: HashMap<String, Count>,
fields: HashMap<String, Count>,
context: HashMap<String, Count>,
added: u64,
bytes: usize,
}
const KEY_HELD: usize = 64;
impl Tally {
pub(super) fn add(&mut self, event: &Kept) {
self.added += 1;
let serial = self.added;
count_of(&mut self.names, &mut self.bytes, &event.name).events += 1;
add_pairs(&mut self.fields, &mut self.bytes, &event.fields, serial);
add_pairs(&mut self.context, &mut self.bytes, &event.context, serial);
}
fn add_row(&mut self, kind: u8, key: &str, events: u64, kinds: u8) {
let map = match kind {
NAME_KEY => &mut self.names,
FIELD_KEY => &mut self.fields,
_ => &mut self.context,
};
let count = count_of(map, &mut self.bytes, key);
(count.events, count.kinds) = (count.events + events, count.kinds | kinds);
}
pub(super) fn rows(self) -> Vec<KeyRow> {
let kinds = [(NAME_KEY, self.names), (FIELD_KEY, self.fields), (CONTEXT_KEY, self.context)];
let rows =
kinds.into_iter().flat_map(|(kind, map)| map.into_iter().map(move |(key, count)| (kind, key, count)));
let mut rows: Vec<KeyRow> = rows.collect();
rows.sort_by(|(a, a_key, _), (b, b_key, _)| (a, a_key).cmp(&(b, b_key)));
rows
}
fn carried(self) -> StreamFields {
let fields = |map: HashMap<String, Count>| {
let infos = map.into_iter().map(|(name, count)| FieldInfo {
name,
kinds: Kind::all_in(count.kinds),
count: count.events,
});
let mut infos: Vec<FieldInfo> = infos.collect();
infos.sort_by(|a, b| a.name.cmp(&b.name));
infos
};
let names = self.names.into_iter().map(|(name, count)| NameCount { name, count: count.events });
let mut names: Vec<NameCount> = names.collect();
names.sort_by(|a, b| a.name.cmp(&b.name));
StreamFields { event_names: names, fields: fields(self.fields), context: fields(self.context) }
}
fn bounded(&self) -> Result<()> {
let most = COUNT_BYTES.bound as usize;
if self.bytes <= most {
return Ok(());
}
let refused = format!("past the {most} bytes of different keys a stream's fields hold");
Err(COUNT_BYTES.refuse(self.bytes as u64, refused))
}
}
fn count_of<'m>(map: &'m mut HashMap<String, Count>, bytes: &mut usize, key: &str) -> &'m mut Count {
if !map.contains_key(key) {
*bytes += key.len() + KEY_HELD;
map.insert(key.to_owned(), Count::default());
}
map.get_mut(key).unwrap_or_else(|| unreachable!("a key just made"))
}
fn add_pairs(map: &mut HashMap<String, Count>, bytes: &mut usize, pairs: &Fields, serial: u64) {
for (key, json) in pairs.iter() {
let count = count_of(map, bytes, key);
if count.last != serial {
(count.events, count.last) = (count.events + 1, serial);
}
count.kinds |= Kind::of(json).bit();
}
}
const SEGMENTS_CARRY: &str = "select k.kind, k.key, k.kinds, sum(k.events)
from segments s join segment_keys k on k.segment = s.id
where s.stream = ?1 and s.holder is null and s.last_at >= ?2 and k.events is not null
group by k.kind, k.key, k.kinds";
const HEADS_OF: &str = "select id, count, body from heads where stream = ?1 and last_at >= ?2";
type HeadRow = (i64, u64, Vec<u8>);
impl Records {
fn carried(&self, name: &str) -> Result<StreamFields> {
crate::text::check_name("a stream's", name)?;
let Some(stream) = self.stream(name) else { return Ok(StreamFields::default()) };
let cutoff = self.now().saturating_sub(nanos(stream.keeps()));
let failed = |error| sql_error("read what a stream's events carry", error);
let (mut tally, heads) = self.file.read(|connection| {
let tally = segments_carry(connection, stream.id, cutoff)?;
let mut select = connection.prepare_cached(HEADS_OF).map_err(failed)?;
let rows = select.query_map(params![stream.id, cutoff], |row| {
Ok((row.get(0)?, row.get::<_, i64>(1)?.unsigned_abs(), row.get(2)?))
});
Ok((tally, rows.and_then(Iterator::collect::<rusqlite::Result<Vec<HeadRow>>>).map_err(failed)?))
})?;
let bytes: usize = heads.iter().map(|(_, _, body)| body.len()).sum();
let row_events = heads.iter().map(|(_, events, _)| *events).max().unwrap_or(0).min(ROW_EVENTS as u64);
let _held =
self.memory.reserve_for_read(bytes as u64 + row_events * EVENT_HELD, "records: fields", PATIENCE)?;
let mut codec = Codec::default();
for (id, _, body) in heads {
let events = head::parse(&body, &mut codec).map_err(|error| self.met((Part::HeadRow, id), name, error))?;
events.iter().for_each(|event| tally.add(event));
tally.bounded()?;
}
Ok(tally.carried())
}
}
fn segments_carry(connection: &Connection, stream: i64, cutoff: i64) -> Result<Tally> {
let failed = |error| sql_error("read what a stream's events carry", error);
let mut select = connection.prepare_cached(SEGMENTS_CARRY).map_err(failed)?;
let mut rows = select.query(params![stream, cutoff]).map_err(failed)?;
let mut tally = Tally::default();
while let Some(row) = rows.next().map_err(failed)? {
let (kind, key, kinds): (u8, String, u8) =
(row.get(0).map_err(failed)?, row.get(1).map_err(failed)?, row.get(2).map_err(failed)?);
let events: i64 = row.get(3).map_err(failed)?;
tally.add_row(kind, &key, events.unsigned_abs(), kinds);
tally.bounded()?;
}
Ok(tally)
}
const UNCOUNTED_A_PASS: usize = 16;
const UNCOUNTED: &str = "select k.segment, s.weight from
(select distinct segment from segment_keys where events is null order by segment limit cast(?1 as integer)) k
left join segments s on s.id = k.segment and s.holder is null";
const HOLDER: &str = "select stream, body from segments where id = ?1 and holder is null";
const BLOCKS: &str = "select id, body from blocks where segment = ?1 order by id";
const IS_HOLDER: &str = "select exists (select 1 from segments where id = ?1 and holder is null)";
const DROP_KEYS: &str = "delete from segment_keys where segment = ?1";
type Uncounted = (i64, Vec<u8>, Vec<(i64, Vec<u8>)>);
impl Records {
pub(super) fn count_uncounted(&self) -> Result<usize> {
let mut counted = 0;
for (segment, weight) in self.uncounted()? {
let Some(weight) = weight else {
self.write_counts(segment, Vec::new())?;
continue;
};
let _held = self.memory.reserve(weight.unsigned_abs() * HELD, "records: counting a segment", PATIENCE)?;
let Some((stream, row, blocks)) = self.segment_bytes(segment)? else { continue };
match counts_of(&row, &blocks, &mut Codec::default()) {
Ok(rows) => {
self.write_counts(segment, rows)?;
counted += 1;
}
Err(error) => drop(self.met((Part::Segment, segment), &self.name_of(stream), error)),
}
}
Ok(counted)
}
fn uncounted(&self) -> Result<Vec<(i64, Option<i64>)>> {
let failed = |error| sql_error("find the segments to count", error);
let passed = self.damaged.lock().unwrap_or_else(PoisonError::into_inner).len();
let found: Vec<(i64, Option<i64>)> = self.file.read(|connection| {
let mut select = connection.prepare_cached(UNCOUNTED).map_err(failed)?;
let found = select.query_map([(UNCOUNTED_A_PASS + passed) as i64], |row| Ok((row.get(0)?, row.get(1)?)));
found.and_then(Iterator::collect).map_err(failed)
})?;
let fine = found.into_iter().filter(|(segment, _)| !self.is_damaged(Part::Segment, *segment));
Ok(fine.take(UNCOUNTED_A_PASS).collect())
}
fn segment_bytes(&self, segment: i64) -> Result<Option<Uncounted>> {
let failed = |error| sql_error("read a segment to count", error);
self.file.read(|connection| {
let holder = connection
.prepare_cached(HOLDER)
.and_then(|mut select| select.query_row([segment], |row| Ok((row.get(0)?, row.get(1)?))).optional());
let Some((stream, row)) = holder.map_err(failed)? else { return Ok(None) };
let mut select = connection.prepare_cached(BLOCKS).map_err(failed)?;
let blocks = select.query_map([segment], |row| Ok((row.get(0)?, row.get(1)?)));
Ok(Some((stream, row, blocks.and_then(Iterator::collect).map_err(failed)?)))
})
}
fn write_counts(&self, segment: i64, rows: Vec<KeyRow>) -> Result<()> {
self.file.write(0, move |tx| {
let is_holder =
tx.prepare_cached(IS_HOLDER).and_then(|mut select| select.query_row([segment], |row| row.get(0)));
let is_holder: bool = is_holder.map_err(|error| sql_error("find a segment to count", error))?;
execute(tx, DROP_KEYS, [segment], "remove a segment's keys without counts")?;
if is_holder { insert_keys(tx, segment, &rows) } else { Ok(()) }
})
}
}
fn counts_of(row: &[u8], blocks: &[(i64, Vec<u8>)], codec: &mut Codec) -> Result<Vec<KeyRow>> {
let schema = Schema::parse(row, codec)?;
let mut tally = Tally::default();
for (block, body) in blocks {
block::parse(&schema, (*block, body), codec)?.iter().for_each(|(_, event)| tally.add(event));
}
Ok(tally.rows())
}
#[cfg(test)]
#[path = "carried_tests.rs"]
mod tests;