chadtmiller.com/group-host
atmosphere group host
0
0
A record read again keeps its place in the index
crates/group-host/src/store.rs+17−3
| @@ -485,7 +485,7 @@ const FORGET_OWN_REPO: &[&str] = &[ | ||
]; | ||
const INDEXED_STATE: &str = "SELECT rev, state FROM index_repo WHERE space = ?1 AND did = ?2"; | ||
const INDEX_STATE: &str = "INSERT INTO index_repo (space, did, rev, state) VALUES (?1, ?2, ?3, ?4) ON CONFLICT (space, did) DO UPDATE SET rev = ?3, state = ?4"; | ||
const UNINDEX_REPO: &str = "DELETE FROM index_record WHERE space = ?1 AND did = ?2"; | ||
const INDEXED_PATHS: &str = "SELECT path, cid FROM index_record WHERE space = ?1 AND did = ?2"; | ||
const UNINDEX: &str = "DELETE FROM index_record WHERE space = ?1 AND did = ?2 AND path = ?3"; | ||
const INDEX: &str = "INSERT INTO index_record (space, did, path, cid, value, indexed_at) VALUES (?1, ?2, ?3, ?4, ?5, ?6)"; | ||
const INDEXED: &str = "SELECT seq, did, path, cid, value, indexed_at FROM index_record WHERE space = ?1 AND (?2 IS NULL OR substr(path, 1, length(?2) + 1) = ?2 || '/') AND seq < ?3 ORDER BY seq DESC LIMIT ?4"; | ||
| @@ -1417,11 +1417,25 @@ impl Store { | ||
) -> Result<()> { | ||
let mut conn = self.conn(); | ||
let tx = conn.transaction()?; | ||
// A record read again as it was keeps its place, and when it was | ||
// first seen: newest first is by when each came, not when it was | ||
// last read. | ||
let held: BTreeMap<String, String> = { | ||
let mut q = tx.prepare(INDEXED_PATHS)?; | ||
let rows = q.query_map([space, did], |r| Ok((r.get(0)?, r.get(1)?))); | ||
rows?.collect::<rusqlite::Result<_>>()? | ||
}; | ||
if whole { | ||
let cleared = tx.execute(UNINDEX_REPO, [space, did]); | ||
cleared?; | ||
for path in held.keys().filter(|p| !changes.contains_key(*p)) { | ||
let gone = tx.execute(UNINDEX, [space, did, path]); | ||
gone?; | ||
} | ||
} | ||
for (path, put) in changes { | ||
let same = put.as_ref().map(|(cid, _)| cid.to_string()); | ||
if same.is_some() && held.get(path) == same.as_ref() { | ||
continue; | ||
} | ||
let gone = tx.execute(UNINDEX, [space, did, path]); | ||
gone?; | ||
if let Some((cid, value)) = put { | ||
crates/group-host/tests/index.rs+52−0
| @@ -829,3 +829,55 @@ async fn fetches_no_file_too_big_or_not_asked_for() { | ||
let got = read(&h, &lxm, &fetch, &[auth(&h, &alice, &lxm)]).await; | ||
assert_eq!(got.status, 400); | ||
} | ||
| ||
#[tokio::test] | ||
async fn keeps_a_record_read_again_where_it_was() { | ||
use group_host::store::SpaceHead; | ||
let h = start(&[], false).await; | ||
let [alice, _, _] = people(&h).await; | ||
let (_, space) = group_with(&h, &alice, "did:plc:writer").await; | ||
let store = &h.host.store; | ||
let put = |v: &str| { | ||
let value = v.as_bytes().to_vec(); | ||
Some((Cid::of_raw(&value), value)) | ||
}; | ||
let head = |rev: &str| SpaceHead { | ||
rev: rev.into(), | ||
state: vec![0; 2048], | ||
}; | ||
let first = BTreeMap::from([ | ||
("a.b.c/one".to_string(), put("one")), | ||
("a.b.c/two".to_string(), put("two")), | ||
]); | ||
store | ||
.index(&space, "did:plc:writer", &head("3l1"), true, &first, 100) | ||
.unwrap(); | ||
// Read whole again: one unchanged, one changed, one new, none gone | ||
// but what is not there. | ||
let second = BTreeMap::from([ | ||
("a.b.c/one".to_string(), put("one")), | ||
("a.b.c/two".to_string(), put("two, edited")), | ||
("a.b.c/three".to_string(), put("three")), | ||
]); | ||
store | ||
.index(&space, "did:plc:writer", &head("3l2"), true, &second, 200) | ||
.unwrap(); | ||
let listed = store.indexed(&space, None, None, 10).unwrap(); | ||
let order: Vec<(&str, i64)> = listed | ||
.iter() | ||
.map(|r| (r.rkey.as_str(), r.indexed_at)) | ||
.collect(); | ||
// Newest first, the unchanged one where it was, first seen at 100. | ||
assert_eq!(order, [("two", 200), ("three", 200), ("one", 100)]); | ||
// A whole read without one takes it out. | ||
let third = BTreeMap::from([("a.b.c/one".to_string(), put("one"))]); | ||
store | ||
.index(&space, "did:plc:writer", &head("3l3"), true, &third, 300) | ||
.unwrap(); | ||
let listed = store.indexed(&space, None, None, 10).unwrap(); | ||
assert_eq!(listed.len(), 1); | ||
assert_eq!( | ||
(listed[0].rkey.as_str(), listed[0].indexed_at), | ||
("one", 100) | ||
); | ||
} | ||
docs/design/spaces.md+3−2
| @@ -153,8 +153,9 @@ Details: | ||
| ||
The `fyi.opensocial.*` methods answer from the index. | ||
`fyi.opensocial.listSpaceRecords` brings the index up to date, waiting at | ||
most 5 seconds, then answers newest first; its `createdAt` is when the | ||
index read a record. It and `getSpaceBlob` ask the access record on every | ||
most 5 seconds, then answers newest first: by when the index first saw | ||
each record, its `createdAt`, which reading a repo again does not move. | ||
It and `getSpaceBlob` ask the access record on every | ||
call, so the index never decides who sees what. The caller is known by | ||
service auth or the console's session; one with neither reads only what | ||
is readable by `"public"`. A space's images are also served at | ||