use std::collections::BTreeMap;
use std::fmt;
use std::sync::{MutexGuard, PoisonError};
use std::time::SystemTime;
use rusqlite::{Connection, OptionalExtension, params};
use super::block;
use super::engine::{Records, execute};
use super::events::AllEvents;
use super::head::{self, Codec};
use super::maintain::forget_ids;
use super::pack::{HELD, PATIENCE, settle};
use super::segment::Schema;
use crate::sqlite::sql_error;
use crate::{Error, ErrorKind, Result};
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Part {
HeadRow,
Segment,
}
impl fmt::Display for Part {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Part::HeadRow => "head row",
Part::Segment => "segment",
})
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Damage {
pub part: Part,
pub id: i64,
pub stream: String,
pub first: SystemTime,
pub last: SystemTime,
pub error: String,
}
impl fmt::Display for Damage {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} {} of {}", self.part, self.id, self.stream)
}
}
pub(super) type Damaged = BTreeMap<(Part, i64), (String, String)>;
type SegmentBytes = ((Vec<u8>, i64), Vec<(i64, Vec<u8>)>);
const HEAD_TIMES: &str = "select first_at, last_at from heads where id = ?1";
const HEAD_SPAN: &str = "select stream, first_at, last_at from heads where id = ?1";
const SEGMENT_SPAN: &str = "select stream, first_at, last_at from segments where id = ?1 and holder is null";
const SEGMENT_TIMES: &str = "select first_at, last_at from segments where id = ?1 and holder is null";
const HEAD_BODY: &str = "select body from heads where id = ?1";
const HEAD_COUNTS: &str = "select stream, late, count, weight from heads where id = ?1";
const DROP_HEAD_ROW: &str = "delete from heads where id = ?1";
const SEGMENT_BODY: &str = "select body, weight from segments where id = ?1 and holder is null";
const SEGMENT_BLOCKS: &str = "select id, body from blocks where segment = ?1 order by id";
const NOTE_DROPPED: &str = "insert or replace into expired (segment, events, at)
select id, count, ?2 from segments where id = ?1 or holder = ?1";
const DROP_SEGMENT: [(&str, &str); 6] = [
("delete from block_traces where block in (select id from blocks where segment = ?1)", "blocks' traces"),
("delete from block_filters where block in (select id from blocks where segment = ?1)", "blocks' filters"),
("delete from segment_keys where segment = ?1", "keys"),
("delete from blocks where segment = ?1", "blocks"),
("delete from segments where holder = ?1", "places"),
("delete from segments where id = ?1", "row"),
];
impl AllEvents {
pub fn damaged(&self) -> Result<Vec<Damage>> {
self.records.damaged().map_err(|error| error.within("events"))
}
pub fn drop(&self, damage: &Damage) -> Result<()> {
self.records.drop_damage(damage).map_err(|error| error.within(format!("events: {damage}")))
}
}
impl Records {
pub(super) fn met(&self, (part, id): (Part, i64), stream: &str, error: Error) -> Error {
if error.kind() == ErrorKind::Corrupt {
let met = (stream.to_owned(), error.to_string());
if self.lock_damaged().insert((part, id), met).is_none() {
tracing::warn!(
target: "tinystore",
"records: {part} {id} of {stream} no longer reads, and reads over it fail till it is dropped: {error}"
);
}
}
error.within(format!("{part} {id} of {stream}"))
}
pub(super) fn is_damaged(&self, part: Part, id: i64) -> bool {
self.lock_damaged().contains_key(&(part, id))
}
pub(super) fn name_of(&self, stream: i64) -> String {
let known = self.every_stream().into_iter().find(|known| known.id == stream);
known.map_or_else(|| stream.to_string(), |known| known.name.to_string())
}
fn damaged(&self) -> Result<Vec<Damage>> {
let met: Vec<((Part, i64), (String, String))> =
self.lock_damaged().iter().map(|(key, met)| (*key, met.clone())).collect();
let failed = |error| sql_error("read the times of what no longer reads", error);
let times: Vec<Option<(i64, i64)>> = self.file.read(|connection| {
let mut timed = Vec::with_capacity(met.len());
for ((part, id), _) in &met {
let sql = if *part == Part::HeadRow { HEAD_TIMES } else { SEGMENT_TIMES };
let mut select = connection.prepare_cached(sql).map_err(failed)?;
let read = select.query_row([id], |row| Ok((row.get(0)?, row.get(1)?))).optional().map_err(failed)?;
timed.push(
read.map(|(first, last): (Option<i64>, Option<i64>)| (first.unwrap_or(0), last.unwrap_or(0))),
);
}
Ok(timed)
})?;
let mut damages = Vec::new();
for (((part, id), (stream, error)), times) in met.into_iter().zip(times) {
match times {
Some((first, last)) => {
damages.push(Damage { part, id, stream, first: time_of(first), last: time_of(last), error });
}
None => drop(self.lock_damaged().remove(&(part, id))),
}
}
Ok(damages)
}
fn drop_damage(&self, damage: &Damage) -> Result<()> {
let _one = self.maintaining.lock().unwrap_or_else(PoisonError::into_inner);
if self.still_reads(damage)? {
return Err(Error::invalid("it still reads, and only what no longer reads is dropped"));
}
match damage.part {
Part::HeadRow => self.drop_head_row(damage.id)?,
Part::Segment => self.drop_segment(damage.id)?,
}
self.lock_damaged().remove(&(damage.part, damage.id));
Ok(())
}
fn still_reads(&self, damage: &Damage) -> Result<bool> {
let failed = |error| sql_error("read what a drop removes", error);
let mut codec = Codec::default();
if damage.part == Part::HeadRow {
let body: Option<Vec<u8>> = self.file.read(|connection| {
let mut select = connection.prepare_cached(HEAD_BODY).map_err(failed)?;
select.query_row([damage.id], |row| row.get(0)).optional().map_err(failed)
})?;
return Ok(body.is_some_and(|body| head::parse(&body, &mut codec).is_ok()));
}
let read: Option<SegmentBytes> = self.file.read(|connection| {
let mut select = connection.prepare_cached(SEGMENT_BODY).map_err(failed)?;
let row = select.query_row([damage.id], |row| Ok((row.get(0)?, row.get(1)?))).optional().map_err(failed)?;
let Some(row) = row else { return Ok(None) };
let mut blocks = connection.prepare_cached(SEGMENT_BLOCKS).map_err(failed)?;
let bodies =
blocks.query_map([damage.id], |row| Ok((row.get(0)?, row.get(1)?))).and_then(Iterator::collect);
Ok(Some((row, bodies.map_err(failed)?)))
})?;
let Some(((body, weight), blocks)) = read else { return Ok(false) };
let _held = self.memory.reserve(weight.max(0) as u64 * HELD, "records: reading a segment to drop", PATIENCE)?;
let schema = Schema::parse(&body, &mut codec);
let reads = schema.and_then(|schema| {
blocks.iter().try_for_each(|(id, body)| block::parse(&schema, (*id, body), &mut codec).map(drop))
});
Ok(reads.is_ok())
}
fn drop_head_row(&self, id: i64) -> Result<()> {
let emptied = self.file.write(0, move |tx| {
let mut select = tx.prepare_cached(HEAD_COUNTS).map_err(|error| sql_error("read a head row", error))?;
let counts = select.query_row([id], |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?, row.get(3)?)));
let counts: Option<(i64, bool, i64, i64)> =
counts.optional().map_err(|error| sql_error("read a head row", error))?;
let Some((stream, late, events, weight)) = counts else { return Ok(None) };
let held = held_span(tx, HEAD_SPAN, id)?;
execute(tx, DROP_HEAD_ROW, [id], "remove a head row that no longer reads")?;
held.map_or(Ok(()), |(stream, span)| forget_ids(tx, stream, span))?;
settle(tx, (stream, late), events, weight).map(|emptied| (emptied && !late).then_some(stream))
})?;
if let Some(stream) = emptied {
self.waiting.lock().unwrap_or_else(PoisonError::into_inner).remove(&stream);
}
Ok(())
}
fn drop_segment(&self, id: i64) -> Result<()> {
let now = self.now();
self.file.write(0, move |tx| {
let held = held_span(tx, SEGMENT_SPAN, id)?;
execute(tx, NOTE_DROPPED, params![id, now], "note what a dropped segment held")?;
DROP_SEGMENT.iter().try_for_each(|(sql, what)| {
execute(tx, sql, [id], &format!("remove a dropped segment's {what}")).map(drop)
})?;
held.map_or(Ok(()), |(stream, span)| forget_ids(tx, stream, span))
})
}
fn lock_damaged(&self) -> MutexGuard<'_, Damaged> {
self.damaged.lock().unwrap_or_else(PoisonError::into_inner)
}
}
fn held_span(tx: &Connection, sql: &'static str, id: i64) -> Result<Option<(i64, (i64, i64))>> {
let read = tx.prepare_cached(sql).and_then(|mut select| {
let row =
|row: &rusqlite::Row<'_>| Ok((row.get(0)?, row.get::<_, Option<i64>>(1)?, row.get::<_, Option<i64>>(2)?));
select.query_row([id], row).optional()
});
let read = read.map_err(|error| sql_error("read the times a part holds", error))?;
Ok(read.and_then(|(stream, first, last)| Some((stream, (first?, last?)))))
}
use crate::clock::from_unix_nanos as time_of;
#[cfg(test)]
#[path = "repair_tests.rs"]
mod tests;