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

Add deduping to mobile packet verifier #585

Merged
merged 3 commits into from
Aug 2, 2023

Conversation

bbalser
Copy link
Contributor

@bbalser bbalser commented Aug 1, 2023

No description provided.

@@ -0,0 +1,4 @@
CREATE TABLE event_ids (
event_id TEXT NOT NULL PRIMARY KEY,
Copy link
Contributor

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?

Copy link
Contributor Author

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>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
tx: &mut Transaction<'_, Postgres>,
txn: &mut Transaction<'_, Postgres>,

tx reads like a message channel sender

Comment on lines 6 to 31
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)
}
Copy link
Contributor

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?

Copy link
Contributor

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;

Copy link
Contributor

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

Copy link
Contributor

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;

Copy link
Contributor Author

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)")
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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)")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed 5c66469

@bbalser bbalser marked this pull request as ready for review August 1, 2023 18:42
@bbalser bbalser merged commit 76cd5ae into main Aug 2, 2023
1 check passed
@bbalser bbalser deleted the bbalser/mobile-packet-verifier-dedup branch August 2, 2023 13:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants