Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: Sort constraint definitions before comparing #727

Merged
merged 1 commit into from
Jan 18, 2024
Merged
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
19 changes: 13 additions & 6 deletions crates/core/src/db/update.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::fmt;
use std::borrow::Cow;
use std::collections::{BTreeMap, HashMap};
use std::collections::{BTreeMap, BTreeSet, HashMap};

use anyhow::Context;
use spacetimedb_primitives::Constraints;
Expand All @@ -11,7 +11,7 @@ use crate::execution_context::ExecutionContext;

use super::datastore::locking_tx_datastore::MutTxId;
use super::relational_db::RelationalDB;
use spacetimedb_sats::db::def::{TableDef, TableSchema};
use spacetimedb_sats::db::def::{ConstraintDef, TableDef, TableSchema};
use spacetimedb_sats::hash::Hash;

#[derive(thiserror::Error, Debug)]
Expand Down Expand Up @@ -182,14 +182,21 @@ fn equiv(a: &TableDef, b: &TableDef) -> bool {
table_type,
table_access,
} = a;

// Constraints may not be in the same order, depending on whether the
// `TableDef` was loaded from the db catalog or the module.
fn as_set(constraints: &[ConstraintDef]) -> BTreeSet<&ConstraintDef> {
constraints
.iter()
.filter(|c| c.constraints != Constraints::unset())
.collect()
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be better to do this change inside TableDef?:

pub struct TableDef {
    pub table_name: String,

    pub constraints: BtreeSet<ConstraintDef>,
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we actually want to store them like that? We might just want this function as a way to compare them. I think I'm going to propose that we merge this and consider that for future work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It threw me off a bit to realize that there can be unset constraints in the table def, so we would still need an element-wise comparison if it was a set already.

table_name == &b.table_name
&& table_type == &b.table_type
&& table_access == &b.table_access
&& columns == &b.columns
&& constraints
.iter()
.filter(|c| c.constraints != Constraints::unset())
.eq(b.constraints.iter().filter(|c| c.constraints != Constraints::unset()))
&& as_set(constraints) == as_set(&b.constraints)
}

#[cfg(test)]
Expand Down