Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions crates/utopia-store/src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1261,19 +1261,21 @@ pub async fn add_evidence_located(
let mut tx = pool.begin().await?;
let mut target = fact_id;
loop {
let live: Option<bool> =
sqlx::query_scalar("SELECT invalidated_at IS NULL FROM facts WHERE id = $1 FOR SHARE")
.bind(target)
.fetch_optional(&mut *tx)
.await?;
if live != Some(false) {
let Some((kb_id, false)) = sqlx::query_as::<_, (Uuid, bool)>(
"SELECT kb_id, invalidated_at IS NULL FROM facts WHERE id = $1 FOR SHARE",
)
.bind(target)
.fetch_optional(&mut *tx)
.await?
else {
break;
}
};
let next: Option<Uuid> = sqlx::query_scalar(
"SELECT id FROM facts WHERE supersedes = $1
"SELECT id FROM facts WHERE supersedes = $1 AND kb_id = $2
ORDER BY invalidated_at IS NULL DESC, recorded_at DESC LIMIT 1",
)
.bind(target)
.bind(kb_id)
.fetch_optional(&mut *tx)
.await?;
match next {
Expand Down
15 changes: 9 additions & 6 deletions crates/utopia-store/src/resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2467,17 +2467,19 @@ struct MergeRow {
/// 搬过的事实,连同从它们改写出来的每一行(顺着 supersedes 往下走到底,作废的也算)
async fn with_rewrites(
tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
kb_id: Uuid,
roots: &[Uuid],
) -> AppResult<Vec<Uuid>> {
Ok(sqlx::query_scalar(
"WITH RECURSIVE chain(id) AS (
SELECT unnest($1::uuid[])
UNION
SELECT f.id FROM facts f JOIN chain c ON f.supersedes = c.id
SELECT f.id FROM facts f JOIN chain c ON f.supersedes = c.id AND f.kb_id = $2
)
SELECT id FROM chain",
)
.bind(roots)
.bind(kb_id)
.fetch_all(&mut **tx)
.await?)
}
Expand Down Expand Up @@ -2525,19 +2527,19 @@ pub async fn revert_merge(pool: &PgPool, kb_id: Uuid, merge_id: Uuid) -> AppResu
.bind(m.target_id)
.fetch_all(&mut *tx)
.await?;
let touched: Vec<Uuid> = with_rewrites(&mut tx, &m.moved_subject_facts)
let touched: Vec<Uuid> = with_rewrites(&mut tx, kb_id, &m.moved_subject_facts)
.await?
.into_iter()
.chain(with_rewrites(&mut tx, &m.moved_object_facts).await?)
.chain(with_rewrites(&mut tx, kb_id, &m.moved_object_facts).await?)
.chain(m.invalidated_facts.iter().copied())
.collect();
let timelines =
crate::temporal::timelines_of(&mut *tx, kb_id, &touched, Some((&holders, m.source_id)))
.await?;
crate::temporal::lock_timelines(&mut tx, kb_id, &timelines).await?;
// 锁上之后再走一遍:等锁的时候,引擎可能刚从它们改写出新的一行
let subject_rows = with_rewrites(&mut tx, &m.moved_subject_facts).await?;
let object_rows = with_rewrites(&mut tx, &m.moved_object_facts).await?;
let subject_rows = with_rewrites(&mut tx, kb_id, &m.moved_subject_facts).await?;
let object_rows = with_rewrites(&mut tx, kb_id, &m.moved_object_facts).await?;

sqlx::query("UPDATE facts SET subject_id = $1 WHERE id = ANY($2) AND subject_id = ANY($3)")
.bind(m.source_id)
Expand Down Expand Up @@ -2582,13 +2584,14 @@ pub async fn revert_merge(pool: &PgPool, kb_id: Uuid, merge_id: Uuid) -> AppResu
"WITH RECURSIVE chain(fact_id, chunk_id) AS (
SELECT * FROM unnest($1::uuid[], $2::uuid[])
UNION
SELECT f.id, c.chunk_id FROM facts f JOIN chain c ON f.supersedes = c.fact_id
SELECT f.id, c.chunk_id FROM facts f JOIN chain c ON f.supersedes = c.fact_id AND f.kb_id = $3
)
DELETE FROM fact_evidence fe USING chain c
WHERE fe.fact_id = c.fact_id AND fe.chunk_id = c.chunk_id",
)
.bind(&m.copied_evidence_facts)
.bind(&m.copied_evidence_chunks)
.bind(kb_id)
.execute(&mut *tx)
.await?;
sqlx::query("UPDATE facts SET invalidated_at = NULL WHERE id = ANY($1)")
Expand Down
Loading