-
Notifications
You must be signed in to change notification settings - Fork 21
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
Add deduping to mobile packet verifier #585
Conversation
@@ -0,0 +1,4 @@ | |||
CREATE TABLE event_ids ( | |||
event_id TEXT NOT NULL PRIMARY KEY, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what are these event_ids constituted from? are we pretty sure they'll be unique for the life of a record in this database without aggregating to some other piece of data like a hotspot pubkey or cbsd_id?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are unique according to Dmytro.
@@ -80,17 +70,29 @@ pub async fn accumulate_sessions( | |||
} | |||
|
|||
async fn verify_report( | |||
tx: &mut Transaction<'_, Postgres>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tx: &mut Transaction<'_, Postgres>, | |
txn: &mut Transaction<'_, Postgres>, |
tx reads like a message channel sender
pub async fn is_duplicate( | ||
tx: &mut Transaction<'_, Postgres>, | ||
event_id: String, | ||
) -> anyhow::Result<bool> { | ||
let result = | ||
sqlx::query_scalar::<_, bool>("SELECT EXISTS(SELECT 1 FROM event_ids WHERE event_id = $1)") | ||
.bind(event_id.clone()) | ||
.fetch_one(&mut *tx) | ||
.await?; | ||
|
||
Ok(result) | ||
} | ||
|
||
pub async fn record( | ||
tx: &mut Transaction<'_, Postgres>, | ||
event_id: String, | ||
received_timestamp: DateTime<Utc>, | ||
) -> anyhow::Result<()> { | ||
sqlx::query("INSERT INTO event_ids(event_id, received_timestamp) VALUES($1,$2)") | ||
.bind(event_id) | ||
.bind(received_timestamp) | ||
.execute(tx) | ||
.await | ||
.map(|_| ()) | ||
.map_err(anyhow::Error::from) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
worth the speed boost to make this an upsert statement returning whether or not the insert happened and then do it in a single query?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i've done this in a different app which attempts to do an upsert and then returns the boolean if the row was inserted or not
query_with_bindings.execute(&mut tx).await?.rows_affected() > 0;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @jeffgrunewald here, this should be an upsert
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is would the query would look like:
let is_duplicate = sqlx::query(
r#"
INSERT INTO event_ids (event_id, received_timestamp) VALUES ($1, $2)
ON CONFLICT DO NOTHING
"#
)
.bind(event_id)
.bind(received_timestamp)
.execute(tx)
.await?
.rows_affected() > 0;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, upsert is better 5c66469
event_id: String, | ||
received_timestamp: DateTime<Utc>, | ||
) -> anyhow::Result<()> { | ||
sqlx::query("INSERT INTO event_ids(event_id, received_timestamp) VALUES($1,$2)") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sqlx::query("INSERT INTO event_ids(event_id, received_timestamp) VALUES($1,$2)") | |
sqlx::query("INSERT INTO event_ids(event_id, received_timestamp) VALUES ($1, $2)") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed 5c66469
No description provided.