Skip to content

Commit

Permalink
perf: write to the idx cache (#2225)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellie authored Jul 2, 2024
1 parent 8b17690 commit 0def653
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
38 changes: 38 additions & 0 deletions crates/atuin-server-postgres/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::HashMap;
use std::fmt::Debug;
use std::ops::Range;

Expand Down Expand Up @@ -519,6 +520,15 @@ impl Database for Postgres {
async fn add_records(&self, user: &User, records: &[Record<EncryptedData>]) -> DbResult<()> {
let mut tx = self.pool.begin().await.map_err(fix_error)?;

// We won't have uploaded this data if it wasn't the max. Therefore, we can deduce the max
// idx without having to make further database queries. Doing the query on this small
// amount of data should be much, much faster.
//
// Worst case, say we get this wrong. We end up caching data that isn't actually the max
// idx, so clients upload again. The cache logic can be verified with a sql query anyway :)

let mut heads = HashMap::<(HostId, &str), u64>::new();

for i in records {
let id = atuin_common::utils::uuid_v7();

Expand All @@ -542,6 +552,34 @@ impl Database for Postgres {
.execute(&mut *tx)
.await
.map_err(fix_error)?;

// we're already iterating sooooo
heads
.entry((i.host.id, &i.tag))
.and_modify(|e| {
if i.idx > *e {
*e = i.idx
}
})
.or_insert(i.idx);
}

// we've built the map of heads for this push, so commit it to the database
for ((host, tag), idx) in heads {
sqlx::query(
"insert into store_idx_cache
(user_id, host, tag, idx)
values ($1, $2, $3, $4)
on conflict(user_id, host, tag) do update set idx = $4
",
)
.bind(user.id)
.bind(host)
.bind(tag)
.bind(idx as i64)
.execute(&mut *tx)
.await
.map_err(fix_error)?;
}

tx.commit().await.map_err(fix_error)?;
Expand Down
2 changes: 2 additions & 0 deletions crates/atuin-server/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ pub struct Mail {

/// Configuration for the postmark api client
/// This is what we use for Atuin Cloud, the forum, etc.
#[serde(default)]
pub postmark: Postmark,

#[serde(default)]
pub verification: MailVerification,
}

Expand Down

0 comments on commit 0def653

Please sign in to comment.