Skip to content

World's first open-source enterprise world model.

Binding a pushed document's declared phrases counts the base's signatures only for keys not already settled (#1310)

Szabo Bogdan5d915e2abf2a
diff --git a/crates/utopia-server/src/phrase_alignment.rs b/crates/utopia-server/src/phrase_alignment.rs
index 8082f7be..bdce2848 100644
--- a/crates/utopia-server/src/phrase_alignment.rs
+++ b/crates/utopia-server/src/phrase_alignment.rs
@@ -237,6 +237,18 @@ pub(crate) async fn bind_declared(
.into_iter()
.map(|b| (b.key(), b))
.collect();
+ // 判过的先剔掉:库级的签名是整库分组,每次推送都数一遍太贵(#1296)
+ let keys: HashSet<SignatureKey> = keys
+ .into_iter()
+ .filter(|k| {
+ !existing
+ .get(k)
+ .is_some_and(|b| settled(b, &keyed(k), &props, &closure))
+ })
+ .collect();
+ if keys.is_empty() {
+ return Ok(0);
+ }
let mut written = 0;
for s in phrase_bindings::signatures(pool, kb_id)
.await?
@@ -246,12 +258,6 @@ pub(crate) async fn bind_declared(
let Some(p) = declared(s, &props, &closure) else {
continue;
};
- if existing
- .get(&s.key())
- .is_some_and(|b| settled(b, s, &props, &closure))
- {
- continue;
- }
if phrase_bindings::decide(
pool,
kb_id,
@@ -277,6 +283,25 @@ pub(crate) async fn bind_declared(
/// 签名的键:短语 + 两端的类 + 宾语是不是字面值(与 `PhraseSignature::key` 同形)
type SignatureKey = (String, Option<Uuid>, Option<Uuid>, bool);
+
+/// 只有键的签名:`settled` 只看键上的这几格,不必为它把库里的签名数一遍
+fn keyed(
+ (phrase, subject_type_id, object_type_id, object_is_value): &SignatureKey,
+) -> PhraseSignature {
+ PhraseSignature {
+ phrase: phrase.clone(),
+ subject_type_id: *subject_type_id,
+ subject_type_key: None,
+ object_type_id: *object_type_id,
+ object_type_key: None,
+ object_is_value: *object_is_value,
+ count: 0,
+ examples: Vec::new(),
+ quotes: Vec::new(),
+ example_statement_ids: Vec::new(),
+ }
+}
+
/// 每条签名此刻的候选与指纹
type Considered<'a> = HashMap<SignatureKey, (Vec<&'a RelationTypeView>, String)>;
diff --git a/crates/utopia-server/src/phrase_alignment_declared_tests.rs b/crates/utopia-server/src/phrase_alignment_declared_tests.rs
index ac2a8cce..ca07dd70 100644
--- a/crates/utopia-server/src/phrase_alignment_declared_tests.rs
+++ b/crates/utopia-server/src/phrase_alignment_declared_tests.rs
@@ -86,8 +86,8 @@ impl Rig {
}))
}
- /// 推一篇文档并抽取:一条陈述,写成 [主语, 它的类别词, 短语, 宾语, 它的类别词]
- async fn push(&self, external_id: &str, said: [&str; 5]) -> anyhow::Result<()> {
+ /// 推一篇文档并抽取:一条陈述,写成 [主语, 它的类别词, 短语, 宾语, 它的类别词]。返回文档
+ async fn push(&self, external_id: &str, said: [&str; 5]) -> anyhow::Result<Uuid> {
let [subject, subject_kind, phrase, object, object_kind] = said;
let payload: Value = json!({
"external_id": external_id,
@@ -118,7 +118,8 @@ impl Rig {
doc,
utopia_core::models::Proposer::default(),
)
- .await
+ .await?;
+ Ok(doc)
}
async fn bindings(&self) -> anyhow::Result<Vec<phrase_bindings::Binding>> {
@@ -274,6 +275,34 @@ async fn only_a_phrase_naming_one_relation_that_fits_both_ends_binds() -> anyhow
rig.cleanup().await
}
+/// 签名都已判过的推送不数库里的签名(#1296)。一条连接的池上 sqlx 缓存它跑过的每条语句,
+/// 没跑的不在 `pg_prepared_statements` 里
+#[tokio::test]
+async fn a_push_whose_signatures_are_settled_skips_the_base_signatures() -> anyhow::Result<()> {
+ let Some(rig) = Rig::new().await? else {
+ return Ok(());
+ };
+ rig.push("obs-1", ["S-1", "statement", "validated_by", "T-1", "test"])
+ .await?;
+ let second = rig
+ .push("obs-2", ["S-2", "statement", "validated_by", "T-2", "test"])
+ .await?;
+ let one = sqlx::postgres::PgPoolOptions::new()
+ .max_connections(1)
+ .connect_with((*rig.pool.connect_options()).clone())
+ .await?;
+ assert_eq!(bind_declared(&one, rig.kb, second).await?, 0);
+ let grouped: Vec<String> = sqlx::query_scalar(
+ "SELECT statement FROM pg_prepared_statements
+ WHERE statement LIKE '%WITH live AS%' AND statement NOT LIKE '%pg_prepared_statements%'",
+ )
+ .fetch_all(&one)
+ .await?;
+ assert_eq!(grouped, Vec::<String>::new());
+ one.close().await;
+ rig.cleanup().await
+}
+
#[tokio::test]
async fn a_person_decision_is_not_overwritten_by_the_ontology() -> anyhow::Result<()> {
let Some(rig) = Rig::new().await? else {