Skip to content

Commit

Permalink
fix bug where showing entry details would result in a crash
Browse files Browse the repository at this point in the history
The issue would present itself whenever the selected path was not present in all snapshots
  • Loading branch information
drdo committed Aug 13, 2024
1 parent 660c761 commit 470bdb4
Showing 1 changed file with 48 additions and 39 deletions.
87 changes: 48 additions & 39 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,59 +183,68 @@ impl Cache {
path_id: PathId,
) -> Result<Option<EntryDetails>, Error> {
let raw_path_id = path_id.0;
let run_query =
|table: &str| -> Result<(String, usize, DateTime<Utc>), Error> {
let snapshot_hash = table.strip_prefix("entries_").unwrap();
let stmt_str = format!(
"SELECT \
let run_query = |table: &str| -> Result<
Option<(String, usize, DateTime<Utc>)>,
Error,
> {
let snapshot_hash = table.strip_prefix("entries_").unwrap();
let stmt_str = format!(
"SELECT \
hash, \
size, \
time \
FROM \"{table}\" \
JOIN paths ON path_id = paths.id \
JOIN snapshots ON hash = '{snapshot_hash}' \
WHERE path_id = {raw_path_id}\n"
);
let mut stmt = self.conn.prepare(&stmt_str)?;
let (hash, size, timestamp) = stmt.query_row([], |row| {
Ok((row.get("hash")?, row.get("size")?, row.get("time")?))
})?;
let time = timestamp_to_datetime(timestamp)?;
Ok((hash, size, time))
};
);
let mut stmt = self.conn.prepare(&stmt_str)?;
stmt.query_row([], |row| {
Ok((row.get("hash")?, row.get("size")?, row.get("time")?))
})
.optional()?
.map(|(hash, size, timestamp)| {
Ok((hash, size, timestamp_to_datetime(timestamp)?))
})
.transpose()
};

let mut entries_tables = self.entries_tables()?;
let mut details = match entries_tables.next() {
None => return Ok(None),
Some(table) => {
let (hash, size, time) = run_query(&table)?;
EntryDetails {
max_size: size,
max_size_snapshot_hash: hash.clone(),
first_seen: time,
first_seen_snapshot_hash: hash.clone(),
last_seen: time,
last_seen_snapshot_hash: hash,
let mut details = loop {
match entries_tables.next() {
None => return Ok(None),
Some(table) => {
if let Some((hash, size, time)) = run_query(&table)? {
break EntryDetails {
max_size: size,
max_size_snapshot_hash: hash.clone(),
first_seen: time,
first_seen_snapshot_hash: hash.clone(),
last_seen: time,
last_seen_snapshot_hash: hash,
};
}
}
}
};
let mut max_size_time = details.first_seen; // Time of the max_size snapshot
for table in entries_tables {
let (hash, size, time) = run_query(&table)?;
if size > details.max_size
|| (size == details.max_size && time > max_size_time)
{
details.max_size = size;
details.max_size_snapshot_hash = hash.clone();
max_size_time = time;
}
if time < details.first_seen {
details.first_seen = time;
details.first_seen_snapshot_hash = hash.clone();
}
if time > details.last_seen {
details.last_seen = time;
details.last_seen_snapshot_hash = hash;
if let Some((hash, size, time)) = run_query(&table)? {
if size > details.max_size
|| (size == details.max_size && time > max_size_time)
{
details.max_size = size;
details.max_size_snapshot_hash = hash.clone();
max_size_time = time;
}
if time < details.first_seen {
details.first_seen = time;
details.first_seen_snapshot_hash = hash.clone();
}
if time > details.last_seen {
details.last_seen = time;
details.last_seen_snapshot_hash = hash;
}
}
}
Ok(Some(details))
Expand Down

0 comments on commit 470bdb4

Please sign in to comment.