This commit is contained in:
yves committed 2026-11-20 23:17:15 +01:00
1 parent 10af480218
commit 47b73e6935
11 files changed
+470 -28

No files matched your search

+63 -12
View File
@@ -44,7 +44,9 @@ use meow_crypto::password::KdfParams;
use rusqlite::{Connection, OptionalExtension};
use serde::{Deserialize, Serialize};
pub use model::{Batch, Content, HistoryEntry, HistoryPage, OutboxItem, PendingAck, RetiredMailbox, SpaceId, State};
pub use model::{
Batch, Content, HistoryEntry, HistoryPage, OutboxItem, PendingAck, RetiredMailbox, Settings, SpaceId, State, Status,
};
#[derive(Debug, thiserror::Error)]
pub enum Error {
@@ -131,6 +133,7 @@ struct Meta {
servers: Vec<meow_core::server::ServerInfo>,
pending_acks: Vec<PendingAck>,
retired_mailboxes: Vec<RetiredMailbox>,
settings: Settings,
}
// ───────────────────────────── keys ─────────────────────────────
@@ -448,6 +451,7 @@ impl Store {
state.servers = m.servers;
state.pending_acks = m.pending_acks;
state.retired_mailboxes = m.retired_mailboxes;
state.settings = m.settings;
}
for e in &sp.index.contacts {
let bytes = blocks::get(&self.conn, &sp.key, &e.state)?;
@@ -492,6 +496,7 @@ impl Store {
servers: state.servers.clone(),
pending_acks: state.pending_acks.clone(),
retired_mailboxes: state.retired_mailboxes.clone(),
settings: state.settings,
})?;
let h = meow_crypto::sha256(&[&meta]);
if sp.written.get("meta") != Some(&h) || meta_ref.is_none() {
@@ -564,21 +569,34 @@ impl Store {
}
// History and files, per conversation.
type Pending = (ContactId, Vec<HistoryEntry>, Vec<(Vec<u8>, Vec<u8>)>);
let mut by_contact: Vec<Pending> = Vec::new();
#[derive(Default)]
struct Pending {
history: Vec<HistoryEntry>,
files: Vec<(Vec<u8>, Vec<u8>)>,
status: Vec<(Vec<u8>, Status)>,
}
fn slot<'a>(v: &'a mut Vec<(ContactId, Pending)>, c: &ContactId) -> &'a mut Pending {
let i = match v.iter().position(|(x, _)| x == c) {
Some(i) => i,
None => {
v.push((c.clone(), Pending::default()));
v.len() - 1
}
};
&mut v[i].1
}
let mut by_contact: Vec<(ContactId, Pending)> = Vec::new();
for h in batch.history {
match by_contact.iter_mut().find(|(c, _, _)| *c == h.contact) {
Some(slot) => slot.1.push(h),
None => by_contact.push((h.contact.clone(), vec![h], vec![])),
}
let c = h.contact.clone();
slot(&mut by_contact, &c).history.push(h);
}
for (c, mid, data) in batch.files {
match by_contact.iter_mut().find(|(x, _, _)| *x == c) {
Some(slot) => slot.2.push((mid, data)),
None => by_contact.push((c, vec![], vec![(mid, data)])),
}
slot(&mut by_contact, &c).files.push((mid, data));
}
for (cid, history, files) in by_contact {
for (c, mid, st) in batch.status {
slot(&mut by_contact, &c).status.push((mid, st));
}
for (cid, Pending { history, files, status }) in by_contact {
let entry = contacts_idx
.iter_mut()
.find(|e| e.id == cid)
@@ -591,6 +609,7 @@ impl Store {
dir.files.push((mid, r));
}
}
update_status(&tx, &sp.key, &mut w, &mut dir, status)?;
entry.dir = w.put_value(&dir, Some(&entry.dir))?;
}
@@ -758,3 +777,35 @@ fn append_history(
dir.pages.push(r);
Ok(())
}
/// Apply status changes, searching segments from the latest one (where nearly all
/// updates land). Only rewritten segments change blocks.
fn update_status(
conn: &Connection,
key: &Key,
w: &mut Writer,
dir: &mut Dir,
mut updates: Vec<(Vec<u8>, Status)>,
) -> Result<()> {
for i in (0..dir.pages.len()).rev() {
if updates.is_empty() {
break;
}
let mut page: Vec<HistoryEntry> = blocks::get_value(conn, key, &dir.pages[i])?;
let mut changed = false;
updates.retain(|(mid, st)| match page.iter_mut().find(|e| e.id == *mid) {
Some(e) => {
if *st > e.status {
e.status = *st;
changed = true;
}
false
}
None => true,
});
if changed {
dir.pages[i] = w.put_value(&page, Some(&dir.pages[i]))?;
}
}
Ok(())
}