use serde_json::{Value, json};
use sha2::{Digest, Sha256};
use super::split::width;
use super::{Header, Kind, TurnRecord};
use crate::pyfmt::dumps;
use crate::store::cursor;
pub const PAGE_BYTES: u64 = 983_040;
pub const HEAD_BYTES: usize = 32_768;
const METHOD: &str = "records-v1/read";
pub const FOREIGN: &str = "page continuation belongs to a different request";
#[derive(Clone, Debug)]
pub struct RecordRead {
pub turn: Option<u64>,
pub after: u64,
pub through: Option<u64>,
pub seq: Option<u64>,
pub piece: bool,
pub kinds: Option<Vec<Kind>>,
pub heads: Vec<(Kind, usize)>,
pub page_bytes: u64,
pub continuation: Option<String>,
}
impl Default for RecordRead {
fn default() -> Self {
Self {
turn: None,
after: 0,
through: None,
seq: None,
piece: false,
kinds: None,
heads: Vec::new(),
page_bytes: PAGE_BYTES,
continuation: None,
}
}
}
impl RecordRead {
pub fn check(&self) -> Result<(), String> {
if self.turn.is_some() && (self.after > 0 || self.through.is_some()) {
return Err("turn excludes after and through".into());
}
if self.turn == Some(0) || self.through == Some(0) {
return Err("turn and through are at least 1".into());
}
if self.seq.is_some() && self.turn.is_none() {
return Err("seq requires turn".into());
}
if self.piece && self.seq.is_none() {
return Err("piece requires turn and seq".into());
}
if let Some((kind, _)) = self
.heads
.iter()
.find(|(_, n)| !(1..=HEAD_BYTES).contains(n))
{
return Err(format!(
"heads {:?} must be 1 to {HEAD_BYTES}",
kind.as_str()
));
}
if self.page_bytes == 0 {
return Err("page_bytes must be >= 1".into());
}
if self.after == u64::MAX {
return Err(format!("after must be below {}", u64::MAX));
}
Ok(())
}
pub fn selects(&self, kind: Kind) -> bool {
self.kinds
.as_ref()
.is_none_or(|kinds| kinds.contains(&kind))
}
pub fn head(&self, kind: Kind) -> Option<usize> {
self.heads
.iter()
.find(|(named, _)| *named == kind)
.map(|(_, n)| *n)
}
pub fn start(&self) -> (u64, u64) {
match self.turn {
Some(turn) => (turn, self.seq.unwrap_or(0)),
None => (self.after.saturating_add(1), 0),
}
}
pub fn begin(&self, conversation_id: &str) -> Result<(ReadPosition, bool), String> {
let (turn, seq) = self.start();
let Some(token) = self.continuation.as_deref() else {
let start = ReadPosition {
turn,
seq,
..ReadPosition::default()
};
return Ok((start, false));
};
let at = ReadPosition::decode(token, &self.fingerprint(conversation_id))?;
let inside = (turn..=self.last()).contains(&at.turn)
&& (self.piece || at.turn > turn || at.seq >= seq);
if inside {
Ok((at, true))
} else {
Err(FOREIGN.into())
}
}
pub fn last(&self) -> u64 {
self.turn.or(self.through).unwrap_or(u64::MAX)
}
pub fn fingerprint(&self, conversation_id: &str) -> String {
let mut kinds: Option<Vec<&str>> = self
.kinds
.as_ref()
.map(|k| k.iter().map(|k| k.as_str()).collect());
if let Some(kinds) = kinds.as_mut() {
kinds.sort_unstable();
kinds.dedup();
}
let mut heads: Vec<(&str, usize)> =
self.heads.iter().map(|(k, n)| (k.as_str(), *n)).collect();
heads.sort_unstable();
let selection = json!([
METHOD,
conversation_id,
self.turn,
self.after,
self.through,
self.seq,
self.piece,
kinds,
heads
]);
hex(&Sha256::digest(dumps(&selection, true).as_bytes()))
}
}
fn hex(bytes: &[u8]) -> String {
bytes.iter().map(|byte| format!("{byte:02x}")).collect()
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct ReadPosition {
pub turn: u64,
pub seq: u64,
pub log_offset: u64,
pub log_line: u64,
pub record_offset: u64,
}
impl ReadPosition {
pub fn encode(&self, fingerprint: &str) -> String {
let parts = [
self.turn,
self.seq,
self.log_offset,
self.log_line,
self.record_offset,
]
.map(|n| json!(n));
let mut all = vec![json!(fingerprint)];
all.extend(parts);
cursor::encode(METHOD, &all)
}
pub fn decode(token: &str, fingerprint: &str) -> Result<Self, String> {
let kinds = [
cursor::Kind::Str,
cursor::Kind::Int,
cursor::Kind::Int,
cursor::Kind::Int,
cursor::Kind::Int,
cursor::Kind::Int,
];
let parts = cursor::decode(METHOD, token, &kinds)?;
if parts[0] != fingerprint {
return Err(FOREIGN.into());
}
let number = |at: usize| {
parts[at]
.as_u64()
.ok_or_else(|| format!("malformed {METHOD} cursor"))
};
Ok(Self {
turn: number(1)?,
seq: number(2)?,
log_offset: number(3)?,
log_line: number(4)?,
record_offset: number(5)?,
})
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ReadItem {
Header(Header),
Record {
turn: u64,
record: TurnRecord,
tail: Option<String>,
truncated: bool,
},
}
impl ReadItem {
pub fn to_json(&self) -> Value {
match self {
Self::Header(header) => header.to_json(),
Self::Record {
turn,
record,
tail,
truncated,
} => {
let mut value = record.to_json(Some(*turn));
let map = value.as_object_mut().expect("a record is an object");
map.insert("tail".into(), json!(tail));
map.insert("truncated".into(), json!(truncated));
value
}
}
}
pub fn bytes(&self) -> u64 {
dumps(&self.to_json(), false).len() as u64 + 1
}
}
pub fn head(text: &str, bytes: usize) -> &str {
let mut used = 2;
for (at, ch) in text.char_indices() {
used += width(ch);
if used > bytes {
return &text[..at];
}
}
if used > bytes { "" } else { text }
}
pub fn tail(text: &str, bytes: usize) -> &str {
let mut used = 2;
for (at, ch) in text.char_indices().rev() {
used += width(ch);
if used > bytes {
return &text[at + ch.len_utf8()..];
}
}
if used > bytes { "" } else { text }
}