use std::collections::HashMap;
use rusqlite::{OptionalExtension, params};
use super::block;
use super::engine::Records;
use super::event::Event;
use super::events::AllEvents;
use super::head::Codec;
use super::pack::PATIENCE;
use super::read::EVENT_HELD;
use super::repair::Part;
use super::segment::Schema;
use crate::sqlite::sql_error;
use crate::{Error, Result};
const POLL_EVENTS: usize = 1000;
const MOST_POLL: usize = 10_000;
#[derive(Clone, Debug, PartialEq)]
pub struct PollBatch {
pub events: Vec<Event>,
pub cursor: String,
pub expired: u64,
}
impl AllEvents {
pub fn poll(&self, cursor: Option<&str>) -> Result<PollBatch> {
self.poll_limit(cursor, POLL_EVENTS)
}
pub fn poll_limit(&self, cursor: Option<&str>, limit: usize) -> Result<PollBatch> {
if !(1..=MOST_POLL).contains(&limit) {
return Err(Error::invalid(format!("a poll of {limit} events: it takes 1 to {MOST_POLL}")).within("events"));
}
let from = cursor.map(parse).transpose().map_err(|error| error.within("events"))?;
self.records.poll(from, limit).map_err(|error| error.within("events"))
}
}
fn parse(cursor: &str) -> Result<(i64, i64)> {
let place =
cursor.split_once(':').and_then(|(segment, offset)| Some((segment.parse().ok()?, offset.parse().ok()?)));
place
.filter(|(segment, offset)| *segment >= 0 && *offset >= 0)
.ok_or_else(|| Error::invalid(format!("cursor {cursor:?}, which is no poll's")))
}
struct Fetched {
segment: i64,
holder: i64,
stream: i64,
from: i64,
to: i64,
skip: i64,
count: i64,
row: Vec<u8>,
blocks: Vec<(i64, i64, Vec<u8>)>,
expired: u64,
}
const NEXT_SEGMENT: &str = "select id, stream, holder, start, count from segments where id >= ?1 order by id limit 1";
const SEGMENT_ROW: &str = "select body from segments where id = ?1";
const BLOCKS: &str = "select id, count from blocks where segment = ?1 order by id";
const BLOCK_BODY: &str = "select body from blocks where id = ?1";
const EXPIRED: &str = "select segment, events from expired where segment >= ?1 and segment < ?2";
const NEXT_ID: &str = "select coalesce((select seq from sqlite_sequence where name = 'segments'), 0) + 1";
enum Found {
Batch(Box<Fetched>),
Nothing { next: i64 },
}
impl Records {
fn poll(&self, from: Option<(i64, i64)>, limit: usize) -> Result<PollBatch> {
let (segment, offset) = from.unwrap_or((0, 0));
let fetched = match self.fetch_batch((segment, offset), from.is_some(), limit)? {
Found::Batch(fetched) => fetched,
Found::Nothing { next } => {
let cursor = if from.is_some() { format!("{segment}:{offset}") } else { format!("{next}:0") };
return Ok(PollBatch { events: Vec::new(), cursor, expired: 0 });
}
};
let fetched_bytes = fetched.row.len() + fetched.blocks.iter().map(|(.., body)| body.len()).sum::<usize>();
let events = (fetched.to - fetched.from).max(0) as u64;
let _held =
self.memory.reserve_for_read(fetched_bytes as u64 + events * EVENT_HELD, "records: a poll", PATIENCE)?;
let name = self.name_of(fetched.stream);
let mut codec = Codec::default();
let within = |error: Error| self.met((Part::Segment, fetched.holder), &name, error);
let schema = Schema::parse(&fetched.row, &mut codec).map_err(within)?;
let mut events = Vec::with_capacity((fetched.to - fetched.from) as usize);
for (begins, block, body) in &fetched.blocks {
let parsed = block::parse(&schema, (*block, body), &mut codec).map_err(within)?;
let (low, high) =
((fetched.from - begins).max(0) as usize, (fetched.to - begins).min(parsed.len() as i64) as usize);
events.extend(parsed.into_iter().take(high).skip(low).map(|(_, event)| event.into_event(&name)));
}
let read = fetched.skip + events.len() as i64;
let cursor = if read < fetched.count {
format!("{}:{read}", fetched.segment)
} else {
format!("{}:0", fetched.segment + 1)
};
Ok(PollBatch { events, cursor, expired: fetched.expired })
}
fn fetch_batch(&self, (segment, offset): (i64, i64), counted: bool, limit: usize) -> Result<Found> {
let failed = |error| sql_error("read a poll's batch", error);
self.file.read(|connection| {
let next = connection.prepare_cached(NEXT_SEGMENT).and_then(|mut select| {
select
.query_row([segment], |row| {
Ok((row.get(0)?, row.get(1)?, row.get::<_, Option<i64>>(2)?, row.get(3)?, row.get(4)?))
})
.optional()
});
let Some((id, stream, holder, start, count)): Option<(i64, i64, Option<i64>, i64, i64)> =
next.map_err(failed)?
else {
let next =
connection.prepare_cached(NEXT_ID).and_then(|mut select| select.query_row([], |row| row.get(0)));
return Ok(Found::Nothing { next: next.map_err(failed)? });
};
let expired = if counted { expired_between(connection, (segment, offset), id)? } else { 0 };
let skip = if id == segment { offset.min(count) } else { 0 };
let holder = holder.unwrap_or(id);
let (from, to) = (start + skip, start + count.min(skip + limit as i64));
let mut blocks = Vec::new();
let mut select = connection.prepare_cached(BLOCKS).map_err(failed)?;
let counts = select.query_map([holder], |row| Ok((row.get::<_, i64>(0)?, row.get::<_, i64>(1)?)));
let mut begins = 0;
for block in counts.map_err(failed)? {
let (block, events) = block.map_err(failed)?;
if begins < to && begins + events > from {
let body = connection
.prepare_cached(BLOCK_BODY)
.and_then(|mut select| select.query_row([block], |row| row.get(0)));
blocks.push((begins, block, body.map_err(failed)?));
}
begins += events;
}
let row = connection
.prepare_cached(SEGMENT_ROW)
.and_then(|mut select| select.query_row([holder], |row| row.get(0)));
let row = row.map_err(failed)?;
Ok(Found::Batch(Box::new(Fetched {
segment: id,
holder,
stream,
from,
to,
skip,
count,
row,
blocks,
expired,
})))
})
}
}
fn expired_between(connection: &rusqlite::Connection, (segment, offset): (i64, i64), found: i64) -> Result<u64> {
if found <= segment {
return Ok(0);
}
let failed = |error| sql_error("count what a poll never read", error);
let mut select = connection.prepare_cached(EXPIRED).map_err(failed)?;
let rows = select.query_map(params![segment, found], |row| Ok((row.get::<_, i64>(0)?, row.get::<_, i64>(1)?)));
let held: HashMap<i64, i64> = rows.and_then(Iterator::collect).map_err(failed)?;
let own = match segment {
0 => 0,
_ => held.get(&segment).map_or(1, |events| (events - offset).max(1)),
};
let others: i64 = held.iter().filter(|(id, _)| **id != segment).map(|(_, events)| *events).sum();
let unknown = (found - segment - 1) - held.keys().filter(|id| **id != segment).count() as i64;
Ok((own + others + unknown).max(0) as u64)
}
#[cfg(test)]
#[path = "poll_tests.rs"]
mod tests;