Implement protocol to notice server rollback
This commit is contained in:
parent
a392b95352
commit
bb41d63286
1 changed files with 402 additions and 8 deletions
|
|
@ -9,6 +9,8 @@ use anyhow::{Context as _, Result, ensure};
|
|||
|
||||
use crate::consts::MAX_EVENTS_PAGE_BYTES;
|
||||
|
||||
const EMPTY_HISTORY_CHECKPOINT: &str = "0:empty";
|
||||
|
||||
impl Database {
|
||||
pub async fn get_missing_document(
|
||||
tx: &mut Transaction<'_>,
|
||||
|
|
@ -236,3 +238,45 @@ impl Database {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Database {
|
||||
pub async fn history_checkpoint(&self, vault: &VaultId) -> Result<String> {
|
||||
let mut tx = self.create_readonly_transaction(vault).await?;
|
||||
|
||||
let row: Option<(i64, String)> = sqlx::query_as(
|
||||
"SELECT event_id, event_token FROM events ORDER BY event_id DESC LIMIT 1",
|
||||
)
|
||||
.fetch_optional(&mut *tx)
|
||||
.await?;
|
||||
|
||||
Ok(row.map_or_else(
|
||||
|| EMPTY_HISTORY_CHECKPOINT.to_owned(),
|
||||
|(id, token)| format!("{id}:{token}"),
|
||||
))
|
||||
}
|
||||
|
||||
pub async fn contains_checkpoint(&self, vault: &VaultId, checkpoint: &str) -> Result<bool> {
|
||||
if checkpoint == EMPTY_HISTORY_CHECKPOINT {
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
let Some((id, token)) = checkpoint.split_once(':') else {
|
||||
return Ok(false);
|
||||
};
|
||||
|
||||
let Ok(id) = id.parse::<i64>() else {
|
||||
return Ok(false);
|
||||
};
|
||||
|
||||
let mut tx = self.create_readonly_transaction(vault).await?;
|
||||
let exists: bool = sqlx::query_scalar(
|
||||
"SELECT EXISTS(SELECT 1 FROM events WHERE event_id = ? AND event_token = ?)",
|
||||
)
|
||||
.bind(id)
|
||||
.bind(token)
|
||||
.fetch_one(&mut *tx)
|
||||
.await?;
|
||||
|
||||
Ok(exists)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue