From f634dab8509ce012c99dcba26392000424e49c82 Mon Sep 17 00:00:00 2001 From: Jesper Jensen Date: Sat, 7 Feb 2026 20:40:44 +0100 Subject: Start to peel apart the update loop I also moved over to passing borrows of the time. This should have been a separate commit, but i forgot. --- src/db.rs | 68 +++++++++++++++++++++++++++++++-------------------------------- 1 file changed, 34 insertions(+), 34 deletions(-) (limited to 'src/db.rs') diff --git a/src/db.rs b/src/db.rs index 457921a..ae6073f 100644 --- a/src/db.rs +++ b/src/db.rs @@ -4,13 +4,13 @@ use rusqlite::OptionalExtension; pub trait Db { fn get_image(&self, registry: &str, image: &str) -> Option<(i64, DateTime)>; - fn insert_image(&self, registry: &str, image: &str, last_checked: DateTime) -> i64; + fn insert_image(&self, registry: &str, image: &str, last_checked: &DateTime) -> i64; fn get_tags_sorted(&self, image_id: i64) -> Vec; fn delete_tag(&self, image_id: i64, tag: &str); - fn insert_tag(&self, image_id: i64, tag: &str, fetched_at: DateTime); - fn update_last_checked(&self, image_id: i64, last_checked: DateTime); + fn insert_tag(&self, image_id: i64, tag: &str, fetched_at: &DateTime); + fn update_last_checked(&self, image_id: i64, last_checked: &DateTime); fn get_tag_digest(&self, image_id: i64, tag: &str) -> Option; - fn update_tag_digest(&self, image_id: i64, tag: &str, digest: &str, fetched_at: DateTime); + fn update_tag_digest(&self, image_id: i64, tag: &str, digest: &str, fetched_at: &DateTime); } pub struct SqliteDb { @@ -113,7 +113,7 @@ impl Db for SqliteDb { ))).optional().unwrap(); } - fn insert_image(&self, registry: &str, image: &str, last_checked: DateTime) -> i64 { + fn insert_image(&self, registry: &str, image: &str, last_checked: &DateTime) -> i64 { let _timer = crate::metrics::get().db_query_duration.start_timer(); self.conn.execute(" INSERT INTO images(registry, image, last_checked) VALUES (?1, ?2, ?3) @@ -138,13 +138,13 @@ impl Db for SqliteDb { (image_id, tag)).unwrap(); } - fn insert_tag(&self, image_id: i64, tag: &str, fetched_at: DateTime) { + fn insert_tag(&self, image_id: i64, tag: &str, fetched_at: &DateTime) { let _timer = crate::metrics::get().db_query_duration.start_timer(); self.conn.execute("INSERT INTO tags(image_id, tag, fetched_at) VALUES (?1, ?2, ?3)", (image_id, tag, fetched_at)).unwrap(); } - fn update_last_checked(&self, image_id: i64, last_checked: DateTime) { + fn update_last_checked(&self, image_id: i64, last_checked: &DateTime) { let _timer = crate::metrics::get().db_query_duration.start_timer(); self.conn.execute("UPDATE images SET last_checked = ?1 WHERE id = ?2", (last_checked, image_id)).unwrap(); } @@ -156,7 +156,7 @@ impl Db for SqliteDb { ", (image_id, tag), |row| row.get::<_, String>(0)).optional().unwrap(); } - fn update_tag_digest(&self, image_id: i64, tag: &str, digest: &str, fetched_at: DateTime) { + fn update_tag_digest(&self, image_id: i64, tag: &str, digest: &str, fetched_at: &DateTime) { let _timer = crate::metrics::get().db_query_duration.start_timer(); self.conn.execute("UPDATE tags SET digest = ?1, fetched_at = ?2 WHERE image_id = ?3 AND tag = ?4", (digest, fetched_at, image_id, tag)).unwrap(); @@ -192,10 +192,10 @@ impl Db for StubDb { return None; } - fn insert_image(&self, registry: &str, image: &str, last_checked: DateTime) -> i64 { + fn insert_image(&self, registry: &str, image: &str, last_checked: &DateTime) -> i64 { let id = *self.next_id.borrow(); *self.next_id.borrow_mut() += 1; - self.images.borrow_mut().push((registry.to_string(), image.to_string(), id, last_checked)); + self.images.borrow_mut().push((registry.to_string(), image.to_string(), id, last_checked.clone())); return id; } @@ -214,14 +214,14 @@ impl Db for StubDb { self.tags.borrow_mut().retain(|(id, t, _, _)| !(*id == image_id && t == tag)); } - fn insert_tag(&self, image_id: i64, tag: &str, fetched_at: DateTime) { - self.tags.borrow_mut().push((image_id, tag.to_string(), None, fetched_at)); + fn insert_tag(&self, image_id: i64, tag: &str, fetched_at: &DateTime) { + self.tags.borrow_mut().push((image_id, tag.to_string(), None, fetched_at.clone())); } - fn update_last_checked(&self, image_id: i64, last_checked: DateTime) { + fn update_last_checked(&self, image_id: i64, last_checked: &DateTime) { for (_, _, id, lc) in self.images.borrow_mut().iter_mut() { if *id == image_id { - *lc = last_checked; + *lc = last_checked.clone(); return; } } @@ -236,11 +236,11 @@ impl Db for StubDb { return None; } - fn update_tag_digest(&self, image_id: i64, tag: &str, digest: &str, fetched_at: DateTime) { + fn update_tag_digest(&self, image_id: i64, tag: &str, digest: &str, fetched_at: &DateTime) { for (id, t, d, fa) in self.tags.borrow_mut().iter_mut() { if *id == image_id && t == tag { *d = Some(digest.to_string()); - *fa = fetched_at; + *fa = fetched_at.clone(); return; } } @@ -254,8 +254,8 @@ mod tests { fn test_insert_image_returns_incrementing_ids(db: &dyn Db) { let now = Utc.with_ymd_and_hms(2000, 1, 1, 0, 0, 0).unwrap(); - let id1 = db.insert_image("docker.io", "nginx", now); - let id2 = db.insert_image("docker.io", "redis", now); + let id1 = db.insert_image("docker.io", "nginx", &now); + let id2 = db.insert_image("docker.io", "redis", &now); assert!(id2 > id1); } @@ -265,7 +265,7 @@ mod tests { fn test_get_image_returns_inserted(db: &dyn Db) { let now = Utc.with_ymd_and_hms(2000, 1, 1, 0, 0, 0).unwrap(); - let id = db.insert_image("docker.io", "nginx", now); + let id = db.insert_image("docker.io", "nginx", &now); let (got_id, got_time) = db.get_image("docker.io", "nginx").unwrap(); assert_eq!(got_id, id); assert_eq!(got_time, now); @@ -273,24 +273,24 @@ mod tests { fn test_get_tags_returns_empty_for_no_tags(db: &dyn Db) { let now = Utc.with_ymd_and_hms(2000, 1, 1, 0, 0, 0).unwrap(); - let id = db.insert_image("docker.io", "nginx", now); + let id = db.insert_image("docker.io", "nginx", &now); assert_eq!(db.get_tags_sorted(id), Vec::::new()); } fn test_get_tags_returns_sorted(db: &dyn Db) { let now = Utc.with_ymd_and_hms(2000, 1, 1, 0, 0, 0).unwrap(); - let id = db.insert_image("docker.io", "nginx", now); - db.insert_tag(id, "2.0", now); - db.insert_tag(id, "1.0", now); - db.insert_tag(id, "latest", now); + let id = db.insert_image("docker.io", "nginx", &now); + db.insert_tag(id, "2.0", &now); + db.insert_tag(id, "1.0", &now); + db.insert_tag(id, "latest", &now); assert_eq!(db.get_tags_sorted(id), vec!["1.0", "2.0", "latest"]); } fn test_delete_tag_removes_tag(db: &dyn Db) { let now = Utc.with_ymd_and_hms(2000, 1, 1, 0, 0, 0).unwrap(); - let id = db.insert_image("docker.io", "nginx", now); - db.insert_tag(id, "1.0", now); - db.insert_tag(id, "2.0", now); + let id = db.insert_image("docker.io", "nginx", &now); + db.insert_tag(id, "1.0", &now); + db.insert_tag(id, "2.0", &now); db.delete_tag(id, "1.0"); assert_eq!(db.get_tags_sorted(id), vec!["2.0"]); } @@ -298,23 +298,23 @@ mod tests { fn test_update_last_checked(db: &dyn Db) { let t1 = Utc.with_ymd_and_hms(2000, 1, 1, 0, 0, 0).unwrap(); let t2 = Utc.with_ymd_and_hms(2000, 1, 1, 1, 0, 0).unwrap(); - let id = db.insert_image("docker.io", "nginx", t1); - db.update_last_checked(id, t2); + let id = db.insert_image("docker.io", "nginx", &t1); + db.update_last_checked(id, &t2); assert_eq!(db.get_image("docker.io", "nginx").unwrap().1, t2); } fn test_get_tag_digest_returns_none_when_unset(db: &dyn Db) { let now = Utc.with_ymd_and_hms(2000, 1, 1, 0, 0, 0).unwrap(); - let id = db.insert_image("docker.io", "nginx", now); - db.insert_tag(id, "1.0", now); + let id = db.insert_image("docker.io", "nginx", &now); + db.insert_tag(id, "1.0", &now); assert_eq!(db.get_tag_digest(id, "1.0"), None); } fn test_update_tag_digest(db: &dyn Db) { let now = Utc.with_ymd_and_hms(2000, 1, 1, 0, 0, 0).unwrap(); - let id = db.insert_image("docker.io", "nginx", now); - db.insert_tag(id, "1.0", now); - db.update_tag_digest(id, "1.0", "sha256:abc", now); + let id = db.insert_image("docker.io", "nginx", &now); + db.insert_tag(id, "1.0", &now); + db.update_tag_digest(id, "1.0", "sha256:abc", &now); assert_eq!(db.get_tag_digest(id, "1.0"), Some("sha256:abc".to_string())); } -- cgit v1.2.3