mod fsync;
pub use fsync::*;
mod ttl;
pub use ttl::*;
#[cfg(test)]
mod tests;
use std::collections::{HashMap, HashSet};
use std::ops::Bound;
use std::path::PathBuf;
use std::time::Duration;
use tokio::sync::broadcast;
use tokio::sync::mpsc::{self, UnboundedReceiver, UnboundedSender};
use std::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use nu_protocol::engine::EngineState;
use scru128::Scru128Id;
use serde::{Deserialize, Deserializer, Serialize};
use fjall::{
config::{BlockSizePolicy, HashRatioPolicy, RestartIntervalPolicy},
Database, Error as FjallError, Keyspace, KeyspaceCreateOptions, OwnedWriteBatch, PersistMode,
};
#[derive(Debug)]
pub enum StoreError {
Locked,
Other(FjallError),
}
impl std::fmt::Display for StoreError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
StoreError::Locked => write!(f, "Store is locked by another process"),
StoreError::Other(e) => write!(f, "{e}"),
}
}
}
impl std::error::Error for StoreError {}
#[derive(PartialEq, Eq, Serialize, Deserialize, Clone, Default, bon::Builder)]
pub struct Frame {
#[builder(start_fn, into)]
pub topic: String,
#[builder(default)]
pub id: Scru128Id,
pub hash: Option<ssri::Integrity>,
pub meta: Option<serde_json::Value>,
pub ttl: Option<TTL>,
}
use std::fmt;
impl fmt::Debug for Frame {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Frame")
.field("id", &format!("{id}", id = self.id))
.field("topic", &self.topic)
.field("hash", &self.hash.as_ref().map(|x| format!("{x}")))
.field("meta", &self.meta)
.field("ttl", &self.ttl)
.finish()
}
}
impl<'de> Deserialize<'de> for FollowOption {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let s: String = Deserialize::deserialize(deserializer)?;
if s.is_empty() || s == "yes" {
Ok(FollowOption::On)
} else if let Ok(duration) = s.parse::<u64>() {
Ok(FollowOption::WithHeartbeat(Duration::from_millis(duration)))
} else {
match s.as_str() {
"true" => Ok(FollowOption::On),
"false" | "no" => Ok(FollowOption::Off),
_ => Err(serde::de::Error::custom("Invalid value for follow option")),
}
}
}
}
fn deserialize_bool<'de, D>(deserializer: D) -> Result<bool, D::Error>
where
D: Deserializer<'de>,
{
let s: String = Deserialize::deserialize(deserializer)?;
match s.as_str() {
"false" | "no" | "0" => Ok(false),
_ => Ok(true),
}
}
#[derive(PartialEq, Deserialize, Clone, Debug, Default, bon::Builder)]
pub struct ReadOptions {
#[serde(default)]
#[builder(default)]
pub follow: FollowOption,
#[serde(default, deserialize_with = "deserialize_bool")]
#[builder(default)]
pub new: bool,
#[serde(rename = "after")]
pub after: Option<Scru128Id>,
pub from: Option<Scru128Id>,
pub limit: Option<usize>,
pub last: Option<usize>,
pub topic: Option<String>,
}
#[derive(Clone, Debug, PartialEq)]
pub enum Pattern {
Exact(String),
Prefix(String),
}
impl Pattern {
fn matches(&self, topic: &str) -> bool {
match self {
Pattern::Exact(t) => t == topic,
Pattern::Prefix(p) => topic.starts_with(p.as_str()),
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum TopicFilter {
All,
Patterns(Vec<Pattern>),
}
impl TopicFilter {
pub fn parse(spec: &str) -> TopicFilter {
let mut patterns = Vec::new();
for part in spec.split(',') {
let part = part.trim();
if part.is_empty() {
continue;
}
if part == "*" {
return TopicFilter::All;
}
if let Some(prefix) = part.strip_suffix(".*") {
patterns.push(Pattern::Prefix(format!("{prefix}.")));
} else {
patterns.push(Pattern::Exact(part.to_string()));
}
}
if patterns.is_empty() {
TopicFilter::All
} else {
TopicFilter::Patterns(patterns)
}
}
pub fn from_option(spec: Option<&str>) -> TopicFilter {
match spec {
Some(s) => TopicFilter::parse(s),
None => TopicFilter::All,
}
}
pub fn matches(&self, topic: &str) -> bool {
match self {
TopicFilter::All => true,
TopicFilter::Patterns(patterns) => patterns.iter().any(|p| p.matches(topic)),
}
}
}
struct MergeById<'a> {
iters: Vec<Box<dyn Iterator<Item = Frame> + 'a>>,
heap: std::collections::BinaryHeap<MergeEntry>,
descending: bool,
last_emitted: Option<Scru128Id>,
}
struct MergeEntry {
key: u128,
idx: usize,
frame: Frame,
}
impl PartialEq for MergeEntry {
fn eq(&self, other: &Self) -> bool {
self.key == other.key && self.idx == other.idx
}
}
impl Eq for MergeEntry {}
impl PartialOrd for MergeEntry {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for MergeEntry {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.key.cmp(&other.key).then(self.idx.cmp(&other.idx))
}
}
fn merge_key(id: &Scru128Id, descending: bool) -> u128 {
let raw = u128::from_be_bytes(id.to_bytes());
if descending {
raw
} else {
!raw
}
}
impl<'a> MergeById<'a> {
fn new(mut iters: Vec<Box<dyn Iterator<Item = Frame> + 'a>>, descending: bool) -> Self {
let mut heap = std::collections::BinaryHeap::with_capacity(iters.len());
for (idx, iter) in iters.iter_mut().enumerate() {
if let Some(frame) = iter.next() {
heap.push(MergeEntry {
key: merge_key(&frame.id, descending),
idx,
frame,
});
}
}
MergeById {
iters,
heap,
descending,
last_emitted: None,
}
}
}
impl Iterator for MergeById<'_> {
type Item = Frame;
fn next(&mut self) -> Option<Frame> {
loop {
let entry = self.heap.pop()?;
if let Some(frame) = self.iters[entry.idx].next() {
self.heap.push(MergeEntry {
key: merge_key(&frame.id, self.descending),
idx: entry.idx,
frame,
});
}
if self.last_emitted == Some(entry.frame.id) {
continue;
}
self.last_emitted = Some(entry.frame.id);
return Some(entry.frame);
}
}
}
impl ReadOptions {
pub fn from_query(query: Option<&str>) -> Result<Self, crate::error::Error> {
match query {
Some(q) => Ok(serde_urlencoded::from_str(q)?),
None => Ok(Self::default()),
}
}
pub fn to_query_string(&self) -> String {
let mut params = Vec::new();
match self.follow {
FollowOption::Off => {}
FollowOption::On => params.push(("follow", "true".to_string())),
FollowOption::WithHeartbeat(duration) => {
params.push(("follow", duration.as_millis().to_string()));
}
}
if self.new {
params.push(("new", "true".to_string()));
}
if let Some(after) = self.after {
params.push(("after", after.to_string()));
}
if let Some(from) = self.from {
params.push(("from", from.to_string()));
}
if let Some(limit) = self.limit {
params.push(("limit", limit.to_string()));
}
if let Some(last) = self.last {
params.push(("last", last.to_string()));
}
if let Some(topic) = &self.topic {
params.push(("topic", topic.clone()));
}
if params.is_empty() {
String::new()
} else {
url::form_urlencoded::Serializer::new(String::new())
.extend_pairs(params)
.finish()
}
}
}
#[derive(Default, PartialEq, Clone, Debug)]
pub enum FollowOption {
#[default]
Off,
On,
WithHeartbeat(Duration),
}
#[derive(Debug)]
enum GCTask {
CheckLastTTL {
topic: String,
keep: u32,
},
Sweep,
Drain(tokio::sync::oneshot::Sender<()>),
}
enum Removal {
Id(Scru128Id),
Indexed {
id: Scru128Id,
topic: String,
expires_at: Option<u64>,
},
}
impl Removal {
fn id(&self) -> Scru128Id {
match self {
Removal::Id(id) | Removal::Indexed { id, .. } => *id,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
struct SweepStart {
key: [u8; 24],
inclusive: bool,
}
impl SweepStart {
fn bound(&self) -> Bound<[u8; 24]> {
if self.inclusive {
Bound::Included(self.key)
} else {
Bound::Excluded(self.key)
}
}
fn rank(&self) -> ([u8; 24], u8) {
(self.key, u8::from(!self.inclusive))
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
struct TrimFloor {
id: Scru128Id,
inclusive: bool,
}
#[derive(Clone, Debug, PartialEq, Eq, bon::Builder)]
pub struct StoreOptions {
#[builder(default)]
pub fsync: Fsync,
#[builder(default = Duration::from_millis(1000))]
pub ttl_sweep: Duration,
}
impl Default for StoreOptions {
fn default() -> Self {
StoreOptions::builder().build()
}
}
#[derive(Clone)]
pub struct Store {
pub path: PathBuf,
db: Database,
stream: Keyspace,
idx_topic: Keyspace,
idx_expiry: Keyspace,
sweep_start: Arc<Mutex<Option<SweepStart>>>,
trim_floor: Arc<Mutex<HashMap<String, TrimFloor>>>,
broadcast_tx: broadcast::Sender<Frame>,
gc_tx: UnboundedSender<GCTask>,
workers: Option<Arc<Workers>>,
append_lock: Arc<Mutex<()>>,
rt: Option<tokio::runtime::Handle>,
base_engine: Option<Arc<EngineState>>,
fsync: Arc<FsyncState>,
stats: Arc<StoreStats>,
}
#[derive(Debug, Default)]
pub struct StoreStats {
pub appends: AtomicUsize,
pub frames_scanned: AtomicUsize,
pub stream_reads: AtomicUsize,
pub removed_frames: AtomicUsize,
pub remove_commits: AtomicUsize,
pub gc_drains: AtomicUsize,
pub gc_nanos: AtomicU64,
}
#[derive(Debug, Clone, serde::Serialize)]
pub struct StoreStatsSnapshot {
pub appends: u64,
pub frames_scanned: u64,
pub stream_reads: u64,
pub removed_frames: u64,
pub remove_commits: u64,
pub gc_drains: u64,
pub gc_nanos: u64,
}
impl StoreStats {
fn snapshot(&self) -> StoreStatsSnapshot {
let n = |c: &AtomicUsize| c.load(Ordering::Relaxed) as u64;
StoreStatsSnapshot {
appends: n(&self.appends),
frames_scanned: n(&self.frames_scanned),
stream_reads: n(&self.stream_reads),
removed_frames: n(&self.removed_frames),
remove_commits: n(&self.remove_commits),
gc_drains: n(&self.gc_drains),
gc_nanos: self.gc_nanos.load(Ordering::Relaxed),
}
}
}
#[derive(Debug, Clone, serde::Serialize)]
pub struct KeyspaceRates {
pub block_cache_hit: Option<f64>,
pub data_block_cache_hit: Option<f64>,
pub index_block_cache_hit: Option<f64>,
pub filter_block_cache_hit: Option<f64>,
pub table_file_cache_hit: Option<f64>,
pub filter_efficiency: Option<f64>,
}
fn rate(observations: usize, value: f64) -> Option<f64> {
(observations > 0).then_some(value)
}
#[derive(Debug, Clone, serde::Serialize)]
pub struct KeyspaceMetrics {
pub keyspace: &'static str,
pub rates: KeyspaceRates,
pub filter_queries: usize,
pub io_skipped_by_filter: usize,
pub block_loads: usize,
pub block_load_io: usize,
pub data_block_io: u64,
pub index_block_io: u64,
pub filter_block_io: u64,
}
impl KeyspaceMetrics {
fn read(keyspace: &'static str, ks: &Keyspace) -> Self {
let m = ks.metrics();
Self {
keyspace,
rates: KeyspaceRates {
block_cache_hit: rate(m.block_loads(), m.block_cache_hit_rate()),
data_block_cache_hit: rate(
m.data_block_load_count(),
m.data_block_cache_hit_rate(),
),
index_block_cache_hit: rate(
m.index_block_load_count(),
m.index_block_cache_hit_rate(),
),
filter_block_cache_hit: rate(
m.filter_block_load_count(),
m.filter_block_cache_hit_rate(),
),
table_file_cache_hit: rate(m.block_loads(), m.table_file_cache_hit_rate()),
filter_efficiency: rate(m.filter_queries(), m.filter_efficiency()),
},
filter_queries: m.filter_queries(),
io_skipped_by_filter: m.io_skipped_by_filter(),
block_loads: m.block_loads(),
block_load_io: m.block_load_io_count(),
data_block_io: m.data_block_io(),
index_block_io: m.index_block_io(),
filter_block_io: m.filter_block_io(),
}
}
}
struct Workers {
gc: Option<std::thread::JoinHandle<()>>,
fsync: Option<Tick>,
sweep: Option<Tick>,
}
impl Drop for Workers {
fn drop(&mut self) {
drop(self.fsync.take());
drop(self.sweep.take());
if let Some(handle) = self.gc.take() {
let _ = handle.join();
}
}
}
struct Tick {
stop: Arc<AtomicBool>,
handle: Option<std::thread::JoinHandle<()>>,
}
impl Tick {
fn spawn(interval: Duration, mut run: impl FnMut() + Send + 'static) -> Tick {
let stop = Arc::new(AtomicBool::new(false));
let handle = std::thread::spawn({
let stop = stop.clone();
move || loop {
std::thread::park_timeout(interval);
if stop.load(Ordering::Acquire) {
break;
}
run();
}
});
Tick {
stop,
handle: Some(handle),
}
}
}
impl Drop for Tick {
fn drop(&mut self) {
self.stop.store(true, Ordering::Release);
if let Some(handle) = self.handle.take() {
handle.thread().unpark();
let _ = handle.join();
}
}
}
struct FsyncState {
mode: Fsync,
dirty: AtomicBool,
}
impl Store {
pub fn new(path: PathBuf) -> Result<Store, StoreError> {
Store::open(path, StoreOptions::default())
}
pub fn with_fsync(path: PathBuf, fsync: Fsync) -> Result<Store, StoreError> {
Store::open(path, StoreOptions::builder().fsync(fsync).build())
}
pub fn open(path: PathBuf, options: StoreOptions) -> Result<Store, StoreError> {
let fsync = options.fsync;
let db = match Database::builder(path.join("fjall"))
.cache_size(32 * 1024 * 1024) .worker_threads(2)
.open()
{
Ok(db) => db,
Err(FjallError::Locked) => return Err(StoreError::Locked),
Err(e) => return Err(StoreError::Other(e)),
};
let stream_opts = || {
KeyspaceCreateOptions::default()
.max_memtable_size(8 * 1024 * 1024) .data_block_size_policy(BlockSizePolicy::all(16 * 1024)) .data_block_hash_ratio_policy(HashRatioPolicy::all(8.0))
.expect_point_read_hits(true)
};
let idx_opts = || {
KeyspaceCreateOptions::default()
.max_memtable_size(8 * 1024 * 1024) .data_block_size_policy(BlockSizePolicy::all(16 * 1024)) .data_block_hash_ratio_policy(HashRatioPolicy::all(0.0)) .expect_point_read_hits(true)
};
let idx_topic_opts =
|| idx_opts().data_block_restart_interval_policy(RestartIntervalPolicy::all(32));
let stream = db.keyspace("stream", stream_opts).unwrap();
let idx_topic = db.keyspace("idx_topic", idx_topic_opts).unwrap();
let idx_expiry = db.keyspace("idx_expiry", idx_opts).unwrap();
let (broadcast_tx, _) = broadcast::channel(FOLLOW_BUFFER);
let (gc_tx, gc_rx) = mpsc::unbounded_channel();
let fsync_state = Arc::new(FsyncState {
mode: fsync,
dirty: AtomicBool::new(false),
});
let mut store = Store {
path: path.clone(),
db,
stream,
idx_topic,
idx_expiry,
sweep_start: Arc::new(Mutex::new(None)),
trim_floor: Arc::new(Mutex::new(HashMap::new())),
broadcast_tx,
gc_tx,
workers: None,
append_lock: Arc::new(Mutex::new(())),
rt: tokio::runtime::Handle::try_current().ok(),
base_engine: None,
fsync: fsync_state,
stats: Arc::new(StoreStats::default()),
};
let gc = spawn_gc_worker(gc_rx, store.clone());
let fsync_tick = match fsync {
Fsync::Interval(interval) => Some(spawn_fsync_tick(
store.db.clone(),
store.fsync.clone(),
interval,
)),
Fsync::Always | Fsync::Never => None,
};
let sweep_tick = if options.ttl_sweep.is_zero() {
None
} else {
Some(spawn_sweep_tick(store.gc_tx.clone(), options.ttl_sweep))
};
store.workers = Some(Arc::new(Workers {
gc: Some(gc),
fsync: fsync_tick,
sweep: sweep_tick,
}));
Ok(store)
}
pub fn stats(&self) -> StoreStatsSnapshot {
self.stats.snapshot()
}
pub fn metrics(&self) -> Vec<KeyspaceMetrics> {
vec![
KeyspaceMetrics::read("stream", &self.stream),
KeyspaceMetrics::read("idx_topic", &self.idx_topic),
KeyspaceMetrics::read("idx_expiry", &self.idx_expiry),
]
}
pub fn runtime(&self) -> Option<tokio::runtime::Handle> {
self.rt.clone()
}
pub fn fsync(&self) -> Fsync {
self.fsync.mode
}
pub fn flush(&self) -> Result<(), crate::error::Error> {
self.fsync.dirty.store(false, Ordering::Release);
self.db.persist(PersistMode::SyncAll)?;
Ok(())
}
fn after_commit(&self) -> Result<(), crate::error::Error> {
match self.fsync.mode {
Fsync::Always => self.db.persist(PersistMode::SyncAll)?,
Fsync::Interval(_) => self.fsync.dirty.store(true, Ordering::Release),
Fsync::Never => {}
}
Ok(())
}
pub fn with_base_engine(mut self, base: EngineState) -> Self {
self.base_engine = Some(Arc::new(base));
self
}
pub fn base_engine(&self) -> Option<&EngineState> {
self.base_engine.as_deref()
}
pub async fn wait_for_gc(&self) {
let (tx, rx) = tokio::sync::oneshot::channel();
let _ = self.gc_tx.send(GCTask::Drain(tx));
let _ = rx.await;
}
#[cfg(test)]
fn sweep(&self) {
let _ = self.gc_tx.send(GCTask::Sweep);
}
#[cfg(test)]
fn idx_expiry_len(&self) -> usize {
self.idx_expiry.len().unwrap()
}
#[tracing::instrument(skip(self))]
pub fn read(&self, options: ReadOptions) -> tokio::sync::mpsc::Receiver<Frame> {
let (tx, rx) = tokio::sync::mpsc::channel(100);
let should_follow = matches!(
options.follow,
FollowOption::On | FollowOption::WithHeartbeat(_)
);
let broadcast_rx = if should_follow {
Some(self.broadcast_tx.subscribe())
} else {
None
};
let done_rx = if !options.new {
let (done_tx, done_rx) = tokio::sync::oneshot::channel();
let tx_clone = tx.clone();
let store = self.clone();
let options = options.clone();
let should_follow_clone = should_follow;
std::thread::spawn(move || {
let mut last_id = None;
let mut count = 0;
let filter = TopicFilter::from_option(options.topic.as_deref());
if let Some(last_n) = options.last {
let iter = store.iter_for_filter_rev(filter);
let mut frames: Vec<Frame> = Vec::with_capacity(last_n);
for frame in iter {
if is_expired(&frame) {
continue;
}
frames.push(frame);
if frames.len() >= last_n {
break;
}
}
for frame in frames.into_iter().rev() {
last_id = Some(frame.id);
count += 1;
if tx_clone.blocking_send(frame).is_err() {
return;
}
}
} else {
let start_bound = options
.from
.map(|id| (id, true))
.or_else(|| options.after.map(|id| (id, false)));
let iter = store.iter_for_filter(filter, start_bound);
for frame in iter {
if is_expired(&frame) {
continue;
}
last_id = Some(frame.id);
if let Some(limit) = options.limit {
if count >= limit {
return; }
}
if tx_clone.blocking_send(frame).is_err() {
return;
}
count += 1;
}
}
if should_follow_clone {
let threshold = Frame::builder("xs.threshold")
.id(scru128::new())
.ttl(TTL::Ephemeral)
.build();
if tx_clone.blocking_send(threshold).is_err() {
return;
}
}
let _ = done_tx.send((last_id, count));
});
Some(done_rx)
} else {
None
};
if let Some(broadcast_rx) = broadcast_rx {
let handle = self
.rt
.clone()
.expect("read() with follow needs a runtime handle");
{
let tx = tx.clone();
let limit = options.limit;
handle.spawn(async move {
let (last_id, mut count) = match done_rx {
Some(done_rx) => match done_rx.await {
Ok((id, count)) => (id, count),
Err(_) => return, },
None => (None, 0),
};
let filter = TopicFilter::from_option(options.topic.as_deref());
let mut broadcast_rx = broadcast_rx;
loop {
tokio::select! {
_ = tx.closed() => break,
r = broadcast_rx.recv() => match r {
Ok(frame) => {
if !filter.matches(&frame.topic) {
continue;
}
if let Some(last_scanned_id) = last_id {
if frame.id <= last_scanned_id {
continue;
}
}
if tx.send(frame).await.is_err() {
break;
}
if let Some(limit) = limit {
count += 1;
if count >= limit {
break;
}
}
}
Err(broadcast::error::RecvError::Lagged(skipped)) => {
tracing::warn!(
skipped,
"follow fell behind the live buffer, ending the read"
);
break;
}
Err(broadcast::error::RecvError::Closed) => break,
}
}
}
});
}
if let FollowOption::WithHeartbeat(duration) = options.follow {
let heartbeat_tx = tx;
handle.spawn(async move {
loop {
tokio::select! {
_ = heartbeat_tx.closed() => break,
_ = tokio::time::sleep(duration) => {
let frame = Frame::builder("xs.pulse")
.id(scru128::new())
.ttl(TTL::Ephemeral)
.build();
if heartbeat_tx.send(frame).await.is_err() {
break;
}
}
}
}
});
}
}
rx
}
pub fn read_sync(&self, options: ReadOptions) -> impl Iterator<Item = Frame> + '_ {
let filter_expired = |frame: Frame| (!is_expired(&frame)).then_some(frame);
let filter = TopicFilter::from_option(options.topic.as_deref());
if let Some(last_n) = options.last {
let iter = self.iter_for_filter_rev(filter);
let mut frames: Vec<Frame> = Vec::with_capacity(last_n);
for frame in iter {
if let Some(frame) = filter_expired(frame) {
frames.push(frame);
if frames.len() >= last_n {
break;
}
}
}
frames.reverse();
Box::new(frames.into_iter()) as Box<dyn Iterator<Item = Frame> + '_>
} else {
let start_bound = options
.from
.map(|id| (id, true))
.or_else(|| options.after.map(|id| (id, false)));
Box::new(
self.iter_for_filter(filter, start_bound)
.filter_map(filter_expired)
.take(options.limit.unwrap_or(usize::MAX)),
)
}
}
pub fn nu_modules_at(
&self,
as_of: &Scru128Id,
) -> std::collections::HashMap<String, ssri::Integrity> {
let mut modules = std::collections::HashMap::new();
let options = ReadOptions::builder().follow(FollowOption::Off).build();
for frame in self.read_sync(options) {
if frame.id > *as_of {
break;
}
if let Some(hash) = frame.hash {
if let Some(name) = frame.topic.strip_prefix("xs.module.") {
if !name.is_empty() {
modules.insert(name.to_string(), hash);
}
}
}
}
modules
}
pub fn get(&self, id: &Scru128Id) -> Option<Frame> {
self.stats.stream_reads.fetch_add(1, Ordering::Relaxed);
self.stream
.get(id.to_bytes())
.unwrap()
.map(|value| deserialize_frame((id.as_bytes(), value)))
}
#[tracing::instrument(skip(self), fields(id = %id.to_string()))]
pub fn remove(&self, id: &Scru128Id) -> Result<(), crate::error::Error> {
self.remove_many([Removal::Id(*id)])
}
fn remove_many(
&self,
removals: impl IntoIterator<Item = Removal>,
) -> Result<(), crate::error::Error> {
let mut batch = self.db.batch();
for removal in removals {
match removal {
Removal::Id(id) => {
if let Some(frame) = self.get(&id) {
self.remove_frame_keys(
&mut batch,
&frame.id,
&frame.topic,
expires_at(&frame),
);
}
}
Removal::Indexed {
id,
topic,
expires_at,
} => self.remove_frame_keys(&mut batch, &id, &topic, expires_at),
}
}
if batch.is_empty() {
return Ok(());
}
batch.commit()?;
self.stats.remove_commits.fetch_add(1, Ordering::Relaxed);
if matches!(self.fsync.mode, Fsync::Interval(_)) {
self.fsync.dirty.store(true, Ordering::Release);
}
Ok(())
}
fn remove_frame_keys(
&self,
batch: &mut OwnedWriteBatch,
id: &Scru128Id,
topic: &str,
expires_at: Option<u64>,
) {
self.stats.removed_frames.fetch_add(1, Ordering::Relaxed);
let mut topic_key = idx_topic_key_prefix(topic);
topic_key.extend(id.as_bytes());
batch.remove(&self.stream, id.as_bytes());
batch.remove(&self.idx_topic, topic_key);
for prefix_key in idx_topic_prefix_keys(topic, id) {
batch.remove(&self.idx_topic, prefix_key);
}
if let Some(expires_at) = expires_at {
batch.remove(&self.idx_expiry, idx_expiry_key(expires_at, id));
}
}
fn expired_at(&self, now_ms: u64, limit: usize) -> Vec<Removal> {
let last = idx_expiry_key(now_ms, &Scru128Id::from_bytes([0xff; 16]));
let from = *self.sweep_start.lock().unwrap();
if from.is_some_and(|start| start.key > last) {
return Vec::new();
}
let start = from.map_or(Bound::Unbounded, |start| start.bound());
let mut removals = Vec::new();
let mut swept = None;
for guard in self
.idx_expiry
.range((start, Bound::Included(last)))
.take(limit)
{
let Ok((key, value)) = guard.into_inner() else {
break;
};
let Ok(key) = <[u8; 24]>::try_from(key.as_ref()) else {
break;
};
let (expires_at, id) = idx_expiry_parse_key(&key);
removals.push(Removal::Indexed {
id,
topic: String::from_utf8_lossy(&value).into_owned(),
expires_at: Some(expires_at),
});
swept = Some(key);
}
if let Some(key) = swept {
let mut start = self.sweep_start.lock().unwrap();
if *start == from {
*start = Some(SweepStart {
key,
inclusive: false,
});
}
}
removals
}
fn last_overflow(
&self,
topic: &str,
keep: u32,
pending: &HashSet<Scru128Id>,
limit: usize,
) -> Vec<Overflow> {
let floor = self.trim_floor.lock().unwrap().get(topic).copied();
let (low, top) = idx_topic_range(
idx_topic_key_prefix(topic),
floor.map(|floor| (floor.id, floor.inclusive)),
);
let high = if keep == 0 {
top
} else {
let mut seen = 0;
let mut hwm = None;
let mut oldest = None;
for guard in self.idx_topic.range((low.clone(), top)).rev() {
let Ok(key) = guard.key() else {
return Vec::new();
};
let id = idx_topic_frame_id_from_key(&key);
oldest = Some(id);
if pending.contains(&id) {
continue;
}
seen += 1;
if seen == keep {
hwm = Some(key.to_vec());
break;
}
}
match hwm {
Some(key) => Bound::Excluded(key),
None => {
if let Some(id) = oldest {
self.set_trim_floor(
topic,
floor,
TrimFloor {
id,
inclusive: true,
},
);
}
return Vec::new();
}
}
};
let mut overflow = Vec::new();
let mut trimmed = None;
for guard in self.idx_topic.range((low, high)) {
let Ok((key, value)) = guard.into_inner() else {
break;
};
let id = idx_topic_frame_id_from_key(&key);
if !pending.contains(&id) {
overflow.push(Overflow {
id,
expires_at: idx_topic_expiry_of(&value),
});
}
trimmed = Some(id);
if overflow.len() == limit {
break;
}
}
if let Some(id) = trimmed {
self.set_trim_floor(
topic,
floor,
TrimFloor {
id,
inclusive: false,
},
);
}
overflow
}
fn set_trim_floor(&self, topic: &str, from: Option<TrimFloor>, to: TrimFloor) {
let mut floors = self.trim_floor.lock().unwrap();
if floors.get(topic).copied() == from {
floors.insert(topic.to_string(), to);
}
}
fn note_indexed_expiry(&self, key: [u8; 24]) {
let mut start = self.sweep_start.lock().unwrap();
let Some(current) = *start else { return };
let candidate = SweepStart {
key,
inclusive: true,
};
if candidate.rank() < current.rank() {
*start = Some(candidate);
}
}
fn note_indexed_topic(&self, topic: &str, id: &Scru128Id) {
let mut floors = self.trim_floor.lock().unwrap();
if floors.is_empty() {
return;
}
if floors.get(topic).is_some_and(|floor| *id <= floor.id) {
floors.remove(topic);
}
}
fn reset_gc_resume(&self) {
*self.sweep_start.lock().unwrap() = None;
self.trim_floor.lock().unwrap().clear();
}
pub async fn cas_reader(&self, hash: ssri::Integrity) -> cacache::Result<cacache::Reader> {
cacache::Reader::open_hash(&self.path.join("cacache"), hash).await
}
pub fn cas_reader_sync(&self, hash: ssri::Integrity) -> cacache::Result<cacache::SyncReader> {
cacache::SyncReader::open_hash(self.path.join("cacache"), hash)
}
pub async fn cas_writer(&self) -> cacache::Result<cacache::Writer> {
cacache::WriteOpts::new()
.open_hash(&self.path.join("cacache"))
.await
}
pub fn cas_writer_sync(&self) -> cacache::Result<cacache::SyncWriter> {
cacache::WriteOpts::new().open_hash_sync(self.path.join("cacache"))
}
pub async fn cas_insert(&self, content: impl AsRef<[u8]>) -> cacache::Result<ssri::Integrity> {
cacache::write_hash(&self.path.join("cacache"), content).await
}
pub fn cas_insert_sync(&self, content: impl AsRef<[u8]>) -> cacache::Result<ssri::Integrity> {
cacache::write_hash_sync(self.path.join("cacache"), content)
}
pub async fn cas_insert_bytes(&self, bytes: &[u8]) -> cacache::Result<ssri::Integrity> {
self.cas_insert(bytes).await
}
pub fn cas_insert_bytes_sync(&self, bytes: &[u8]) -> cacache::Result<ssri::Integrity> {
self.cas_insert_sync(bytes)
}
pub async fn cas_read(&self, hash: &ssri::Integrity) -> cacache::Result<Vec<u8>> {
cacache::read_hash(&self.path.join("cacache"), hash).await
}
pub fn cas_read_sync(&self, hash: &ssri::Integrity) -> cacache::Result<Vec<u8>> {
cacache::read_hash_sync(self.path.join("cacache"), hash)
}
#[tracing::instrument(skip(self))]
pub fn insert_frame(&self, frame: &Frame) -> Result<(), crate::error::Error> {
let encoded: Vec<u8> = serde_json::to_vec(&frame).unwrap();
let topic_key = idx_topic_key_from_frame(frame)?;
let prefix_keys = idx_topic_prefix_keys(&frame.topic, &frame.id);
let idx_value = idx_topic_expiry_value(frame);
let superseded = self.get(&frame.id);
let mut batch = self.db.batch();
batch.insert(&self.stream, frame.id.as_bytes(), encoded);
batch.insert(&self.idx_topic, topic_key.clone(), idx_value);
for prefix_key in &prefix_keys {
batch.insert(&self.idx_topic, prefix_key, idx_value);
}
let expiry_key = expires_at(frame).map(|at| idx_expiry_key(at, &frame.id));
if let Some(key) = expiry_key {
batch.insert(&self.idx_expiry, key, frame.topic.as_bytes());
}
if let Some(old) = superseded.as_ref().filter(|old| old.topic != frame.topic) {
let mut old_key = idx_topic_key_prefix(&old.topic);
old_key.extend(frame.id.as_bytes());
for key in std::iter::once(old_key).chain(idx_topic_prefix_keys(&old.topic, &frame.id))
{
if key != topic_key && !prefix_keys.contains(&key) {
batch.remove(&self.idx_topic, key);
}
}
}
if let Some(stale) = superseded
.as_ref()
.and_then(expires_at)
.map(|at| idx_expiry_key(at, &frame.id))
.filter(|stale| Some(*stale) != expiry_key)
{
batch.remove(&self.idx_expiry, stale);
}
batch.commit()?;
if let Some(key) = expiry_key {
self.note_indexed_expiry(key);
}
self.note_indexed_topic(&frame.topic, &frame.id);
self.after_commit()
}
pub fn append(&self, mut frame: Frame) -> Result<Frame, crate::error::Error> {
let _guard = self.append_lock.lock().unwrap();
frame.id = scru128::new();
idx_topic_key_from_frame(&frame)?;
if frame.ttl != Some(TTL::Ephemeral) {
self.insert_frame(&frame)?;
if let Some(TTL::Last(n)) = frame.ttl {
let _ = self.gc_tx.send(GCTask::CheckLastTTL {
topic: frame.topic.clone(),
keep: n,
});
}
}
self.stats.appends.fetch_add(1, Ordering::Relaxed);
let _ = self.broadcast_tx.send(frame.clone());
Ok(frame)
}
fn iter_frames(
&self,
start: Option<(Scru128Id, bool)>,
) -> Box<dyn Iterator<Item = Frame> + '_> {
let range = match start {
Some((id, true)) => (Bound::Included(id.as_bytes().to_vec()), Bound::Unbounded),
Some((id, false)) => (Bound::Excluded(id.as_bytes().to_vec()), Bound::Unbounded),
None => (Bound::Unbounded, Bound::Unbounded),
};
let scanned = self.stats.clone();
Box::new(self.stream.range(range).filter_map(move |guard| {
scanned.frames_scanned.fetch_add(1, Ordering::Relaxed);
let (key, value) = guard.into_inner().ok()?;
Some(deserialize_frame((key, value)))
}))
}
fn iter_frames_rev(&self) -> Box<dyn Iterator<Item = Frame> + '_> {
let scanned = self.stats.clone();
Box::new(self.stream.iter().rev().filter_map(move |guard| {
scanned.frames_scanned.fetch_add(1, Ordering::Relaxed);
let (key, value) = guard.into_inner().ok()?;
Some(deserialize_frame((key, value)))
}))
}
fn iter_frames_by_topic_rev(&self, topic: &str) -> Box<dyn Iterator<Item = Frame> + '_> {
let prefix = idx_topic_key_prefix(topic);
let now = now_ms();
let scanned = self.stats.clone();
Box::new(
self.idx_topic
.prefix(prefix)
.rev()
.filter_map(move |guard| {
scanned.frames_scanned.fetch_add(1, Ordering::Relaxed);
self.get(&idx_topic_hit_id(guard, now)?)
}),
)
}
fn iter_frames_by_topic_prefix_rev(
&self,
prefix: &str,
) -> Box<dyn Iterator<Item = Frame> + '_> {
let mut index_prefix = Vec::with_capacity(prefix.len() + 1);
index_prefix.extend(prefix.as_bytes());
index_prefix.push(NULL_DELIMITER);
let now = now_ms();
let scanned = self.stats.clone();
Box::new(
self.idx_topic
.prefix(index_prefix)
.rev()
.filter_map(move |guard| {
scanned.frames_scanned.fetch_add(1, Ordering::Relaxed);
self.get(&idx_topic_hit_id(guard, now)?)
}),
)
}
fn iter_frames_by_topic(
&self,
topic: &str,
start: Option<(Scru128Id, bool)>,
) -> Box<dyn Iterator<Item = Frame> + '_> {
let range = idx_topic_range(idx_topic_key_prefix(topic), start);
let now = now_ms();
let scanned = self.stats.clone();
Box::new(self.idx_topic.range(range).filter_map(move |guard| {
scanned.frames_scanned.fetch_add(1, Ordering::Relaxed);
self.get(&idx_topic_hit_id(guard, now)?)
}))
}
fn iter_frames_by_topic_prefix(
&self,
prefix: &str,
start: Option<(Scru128Id, bool)>,
) -> Box<dyn Iterator<Item = Frame> + '_> {
let mut index_prefix = Vec::with_capacity(prefix.len() + 1);
index_prefix.extend(prefix.as_bytes());
index_prefix.push(NULL_DELIMITER);
let range = idx_topic_range(index_prefix, start);
let now = now_ms();
let scanned = self.stats.clone();
Box::new(self.idx_topic.range(range).filter_map(move |guard| {
scanned.frames_scanned.fetch_add(1, Ordering::Relaxed);
self.get(&idx_topic_hit_id(guard, now)?)
}))
}
fn iter_for_pattern(
&self,
pattern: Pattern,
start: Option<(Scru128Id, bool)>,
) -> Box<dyn Iterator<Item = Frame> + '_> {
match pattern {
Pattern::Exact(topic) => self.iter_frames_by_topic(&topic, start),
Pattern::Prefix(prefix) => self.iter_frames_by_topic_prefix(&prefix, start),
}
}
fn iter_for_pattern_rev(&self, pattern: Pattern) -> Box<dyn Iterator<Item = Frame> + '_> {
match pattern {
Pattern::Exact(topic) => self.iter_frames_by_topic_rev(&topic),
Pattern::Prefix(prefix) => self.iter_frames_by_topic_prefix_rev(&prefix),
}
}
fn iter_for_filter(
&self,
filter: TopicFilter,
start: Option<(Scru128Id, bool)>,
) -> Box<dyn Iterator<Item = Frame> + '_> {
match filter {
TopicFilter::All => self.iter_frames(start),
TopicFilter::Patterns(mut patterns) if patterns.len() == 1 => {
self.iter_for_pattern(patterns.remove(0), start)
}
TopicFilter::Patterns(patterns) => {
let iters = patterns
.into_iter()
.map(|p| self.iter_for_pattern(p, start))
.collect();
Box::new(MergeById::new(iters, false))
}
}
}
fn iter_for_filter_rev(&self, filter: TopicFilter) -> Box<dyn Iterator<Item = Frame> + '_> {
match filter {
TopicFilter::All => self.iter_frames_rev(),
TopicFilter::Patterns(mut patterns) if patterns.len() == 1 => {
self.iter_for_pattern_rev(patterns.remove(0))
}
TopicFilter::Patterns(patterns) => {
let iters = patterns
.into_iter()
.map(|p| self.iter_for_pattern_rev(p))
.collect();
Box::new(MergeById::new(iters, true))
}
}
}
}
fn spawn_gc_worker(
mut gc_rx: UnboundedReceiver<GCTask>,
mut store: Store,
) -> std::thread::JoinHandle<()> {
let (closed_tx, _) = mpsc::unbounded_channel();
store.gc_tx = closed_tx;
const MAX_TASKS_PER_DRAIN: usize = 1024;
const MAX_SWEEP_PER_DRAIN: usize = 4096;
const MAX_TRIM_PER_DRAIN: usize = 4096;
std::thread::spawn(move || {
let mut carried: Vec<GCTask> = Vec::new();
loop {
if carried.is_empty() {
match gc_rx.blocking_recv() {
Some(task) => carried.push(task),
None => break,
}
}
let drain_started = std::time::Instant::now();
let mut pending = HashSet::new();
let mut removals = Vec::new();
let mut drains = Vec::new();
let mut sweep_again = false;
let mut trim_again: HashMap<String, u32> = HashMap::new();
let mut trims: HashMap<String, u32> = HashMap::new();
let mut taken = 0;
while taken < MAX_TASKS_PER_DRAIN {
let Some(task) = carried.pop().or_else(|| gc_rx.try_recv().ok()) else {
break;
};
match task {
GCTask::CheckLastTTL { topic, keep } => {
trims
.entry(topic)
.and_modify(|k| *k = (*k).min(keep))
.or_insert(keep);
}
GCTask::Sweep => {
let expired = store.expired_at(now_ms(), MAX_SWEEP_PER_DRAIN);
sweep_again |= expired.len() == MAX_SWEEP_PER_DRAIN;
for removal in expired {
if pending.insert(removal.id()) {
removals.push(removal);
}
}
}
GCTask::Drain(tx) => drains.push(tx),
}
taken += 1;
}
for (topic, keep) in trims {
let overflow = store.last_overflow(&topic, keep, &pending, MAX_TRIM_PER_DRAIN);
let again = overflow.len() == MAX_TRIM_PER_DRAIN;
for frame in overflow {
if pending.insert(frame.id) {
removals.push(Removal::Indexed {
id: frame.id,
topic: topic.clone(),
expires_at: frame.expires_at,
});
}
}
if again {
trim_again.insert(topic, keep);
}
}
if let Err(e) = store.remove_many(removals) {
tracing::error!("gc remove failed: {e}");
store.reset_gc_resume();
}
store.stats.gc_drains.fetch_add(1, Ordering::Relaxed);
store
.stats
.gc_nanos
.fetch_add(drain_started.elapsed().as_nanos() as u64, Ordering::Relaxed);
for tx in drains {
let _ = tx.send(());
}
carried.extend(
trim_again
.into_iter()
.map(|(topic, keep)| GCTask::CheckLastTTL { topic, keep }),
);
if sweep_again {
carried.push(GCTask::Sweep);
}
}
})
}
fn spawn_fsync_tick(db: Database, state: Arc<FsyncState>, interval: Duration) -> Tick {
Tick::spawn(interval, move || {
if state.dirty.swap(false, Ordering::AcqRel) {
if let Err(e) = db.persist(PersistMode::SyncAll) {
tracing::error!("fsync failed: {e}");
}
}
})
}
fn spawn_sweep_tick(gc_tx: UnboundedSender<GCTask>, interval: Duration) -> Tick {
Tick::spawn(interval, move || {
let _ = gc_tx.send(GCTask::Sweep);
})
}
fn now_ms() -> u64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_millis() as u64
}
fn expires_at(frame: &Frame) -> Option<u64> {
match frame.ttl.as_ref() {
Some(TTL::Time(ttl)) => Some(frame.id.timestamp().saturating_add(ttl.as_millis() as u64)),
_ => None,
}
}
fn is_expired(frame: &Frame) -> bool {
matches!(expires_at(frame), Some(at) if now_ms() >= at)
}
fn idx_expiry_key(expires_at: u64, id: &Scru128Id) -> [u8; 24] {
let mut key = [0u8; 24];
key[..8].copy_from_slice(&expires_at.to_be_bytes());
key[8..].copy_from_slice(id.as_bytes());
key
}
fn idx_expiry_parse_key(key: &[u8]) -> (u64, Scru128Id) {
let expires_at = u64::from_be_bytes(key[..8].try_into().unwrap());
let id = Scru128Id::from_bytes(key[8..24].try_into().unwrap());
(expires_at, id)
}
const FOLLOW_BUFFER: usize = 4096;
const NULL_DELIMITER: u8 = 0;
const MAX_TOPIC_LENGTH: usize = 255;
pub fn validate_topic(topic: &str) -> Result<(), crate::error::Error> {
if topic.is_empty() {
return Err("Topic cannot be empty".to_string().into());
}
if topic.len() > MAX_TOPIC_LENGTH {
return Err(format!("Topic exceeds max length of {MAX_TOPIC_LENGTH} bytes").into());
}
if topic.ends_with('.') {
return Err("Topic cannot end with '.'".to_string().into());
}
if topic.contains("..") {
return Err("Topic cannot contain consecutive dots".to_string().into());
}
let bytes = topic.as_bytes();
let first = bytes[0];
if !first.is_ascii_alphabetic() && first != b'_' {
return Err("Topic must start with a-z, A-Z, or _".to_string().into());
}
for &b in bytes {
if !b.is_ascii_alphanumeric() && b != b'_' && b != b'-' && b != b'.' {
return Err(format!(
"Topic contains invalid character: '{}'. Allowed: a-z A-Z 0-9 _ - .",
b as char
)
.into());
}
}
Ok(())
}
pub fn validate_topic_query(spec: &str) -> Result<(), crate::error::Error> {
let mut seen = false;
for part in spec.split(',') {
let part = part.trim();
if part.is_empty() {
continue;
}
seen = true;
validate_topic_pattern(part)?;
}
if !seen {
return Err("Topic query cannot be empty".to_string().into());
}
Ok(())
}
fn validate_topic_pattern(topic: &str) -> Result<(), crate::error::Error> {
if topic == "*" {
return Ok(());
}
if let Some(prefix) = topic.strip_suffix(".*") {
if prefix.is_empty() {
return Err("Wildcard '.*' requires a prefix".to_string().into());
}
validate_topic(prefix)
} else {
validate_topic(topic)
}
}
fn idx_topic_prefix_keys(topic: &str, frame_id: &scru128::Scru128Id) -> Vec<Vec<u8>> {
let mut keys = Vec::new();
let mut pos = 0;
while let Some(dot_pos) = topic[pos..].find('.') {
let prefix = &topic[..pos + dot_pos + 1]; let mut key = Vec::with_capacity(prefix.len() + 1 + 16);
key.extend(prefix.as_bytes());
key.push(NULL_DELIMITER);
key.extend(frame_id.as_bytes());
keys.push(key);
pos += dot_pos + 1;
}
keys
}
const IDX_TOPIC_NEVER: u64 = u64::MAX;
#[derive(Debug, Clone, Copy)]
struct Overflow {
id: Scru128Id,
expires_at: Option<u64>,
}
#[inline]
fn idx_topic_expiry_of(value: &[u8]) -> Option<u64> {
match <[u8; 8]>::try_from(value) {
Ok(bytes) => match u64::from_be_bytes(bytes) {
IDX_TOPIC_NEVER => None,
at => Some(at),
},
Err(_) => None,
}
}
fn idx_topic_expiry_value(frame: &Frame) -> [u8; 8] {
expires_at(frame).unwrap_or(IDX_TOPIC_NEVER).to_be_bytes()
}
#[inline]
fn idx_topic_is_expired(value: &[u8], now: u64) -> bool {
match <[u8; 8]>::try_from(value) {
Ok(bytes) => {
let expires_at = u64::from_be_bytes(bytes);
expires_at != IDX_TOPIC_NEVER && now >= expires_at
}
Err(_) => false,
}
}
#[inline]
fn idx_topic_hit_id(guard: fjall::Guard, now: u64) -> Option<Scru128Id> {
let (key, value) = guard.into_inner().ok()?;
if idx_topic_is_expired(&value, now) {
return None;
}
Some(idx_topic_frame_id_from_key(&key))
}
fn idx_topic_range(
prefix: Vec<u8>,
start: Option<(Scru128Id, bool)>,
) -> (Bound<Vec<u8>>, Bound<Vec<u8>>) {
fn key_at(prefix: &[u8], id: &[u8; 16]) -> Vec<u8> {
let mut key = Vec::with_capacity(prefix.len() + 16);
key.extend_from_slice(prefix);
key.extend_from_slice(id);
key
}
let hi = Bound::Included(key_at(&prefix, &[0xff; 16]));
let lo = match start {
Some((id, true)) => Bound::Included(key_at(&prefix, id.as_bytes())),
Some((id, false)) => Bound::Excluded(key_at(&prefix, id.as_bytes())),
None => Bound::Included(prefix),
};
(lo, hi)
}
fn idx_topic_key_prefix(topic: &str) -> Vec<u8> {
let mut v = Vec::with_capacity(topic.len() + 1); v.extend(topic.as_bytes()); v.push(NULL_DELIMITER); v
}
pub(crate) fn idx_topic_key_from_frame(frame: &Frame) -> Result<Vec<u8>, crate::error::Error> {
validate_topic(&frame.topic)?;
let mut v = idx_topic_key_prefix(&frame.topic);
v.extend(frame.id.as_bytes());
Ok(v)
}
fn idx_topic_frame_id_from_key(key: &[u8]) -> Scru128Id {
let frame_id_bytes = &key[key.len() - 16..];
Scru128Id::from_bytes(frame_id_bytes.try_into().unwrap())
}
fn deserialize_frame<B1: AsRef<[u8]> + std::fmt::Debug, B2: AsRef<[u8]>>(
record: (B1, B2),
) -> Frame {
serde_json::from_slice(record.1.as_ref()).unwrap_or_else(|e| {
let key_bytes = record.0.as_ref();
if key_bytes.len() == 16 {
if let Ok(bytes) = key_bytes.try_into() {
let id = Scru128Id::from_bytes(bytes);
eprintln!("CORRUPTED_RECORD_ID: {id}");
}
}
let key = std::str::from_utf8(record.0.as_ref()).unwrap();
let value = std::str::from_utf8(record.1.as_ref()).unwrap();
panic!("Failed to deserialize frame: {e} {key} {value}")
})
}