55//! exceeded, returning the evicted snapshot IDs so the caller can clean up
66//! backend resources.
77
8+ use std:: num:: NonZeroU64 ;
89use std:: path:: Path ;
910use std:: time:: { SystemTime , UNIX_EPOCH } ;
1011
@@ -28,7 +29,7 @@ use crate::types::SnapshotId;
2829pub struct ImageRegistry {
2930 #[ debug( skip) ]
3031 conn : Mutex < Connection > ,
31- capacity : u64 ,
32+ capacity : NonZeroU64 ,
3233}
3334
3435/// Returns the current Unix epoch in seconds.
@@ -52,7 +53,7 @@ impl ImageRegistry {
5253 ///
5354 /// Returns an error if the database cannot be opened or the schema cannot
5455 /// be applied.
55- pub fn open ( path : & Path , capacity : u64 ) -> Result < Self > {
56+ pub fn open ( path : & Path , capacity : NonZeroU64 ) -> Result < Self > {
5657 if let Some ( parent) = path. parent ( ) {
5758 std:: fs:: create_dir_all ( parent) ?;
5859 }
@@ -176,11 +177,12 @@ impl ImageRegistry {
176177 /// its capacity. Returns the snapshot IDs of evicted entries.
177178 fn evict_overflow ( & self ) -> Vec < SnapshotId > {
178179 let count = self . len ( ) ;
179- if count <= self . capacity {
180+ let capacity = self . capacity . get ( ) ;
181+ if count <= capacity {
180182 return Vec :: new ( ) ;
181183 }
182184
183- let overflow = count - self . capacity ;
185+ let overflow = count - capacity;
184186
185187 let Ok ( conn) = self . conn . lock ( ) else {
186188 return Vec :: new ( ) ;
@@ -221,6 +223,7 @@ mod tests {
221223 fn open_temp ( capacity : u64 ) -> ( ImageRegistry , tempfile:: TempDir ) {
222224 let dir = tempfile:: tempdir ( ) . expect ( "failed to create temp dir" ) ;
223225 let db_path = dir. path ( ) . join ( "registry.db" ) ;
226+ let capacity = NonZeroU64 :: new ( capacity) . expect ( "capacity must be non-zero" ) ;
224227 let registry = ImageRegistry :: open ( & db_path, capacity) . expect ( "failed to open registry" ) ;
225228 ( registry, dir)
226229 }
@@ -294,14 +297,16 @@ mod tests {
294297 let dir = tempfile:: tempdir ( ) . expect ( "failed to create temp dir" ) ;
295298 let db_path = dir. path ( ) . join ( "registry.db" ) ;
296299
300+ let capacity = NonZeroU64 :: new ( 10 ) . expect ( "capacity must be non-zero" ) ;
301+
297302 {
298- let reg = ImageRegistry :: open ( & db_path, 10 ) . expect ( "open" ) ;
303+ let reg = ImageRegistry :: open ( & db_path, capacity ) . expect ( "open" ) ;
299304 reg. put ( "persistent" , & SnapshotId ( "snap-persist" . into ( ) ) ) ;
300305 assert_eq ! ( reg. len( ) , 1 ) ;
301306 // reg is dropped here, closing the connection.
302307 }
303308
304- let reg2 = ImageRegistry :: open ( & db_path, 10 ) . expect ( "reopen" ) ;
309+ let reg2 = ImageRegistry :: open ( & db_path, capacity ) . expect ( "reopen" ) ;
305310 assert_eq ! ( reg2. len( ) , 1 ) ;
306311 let got = reg2. get ( "persistent" ) ;
307312 assert_eq ! ( got, Some ( SnapshotId ( "snap-persist" . into( ) ) ) ) ;
0 commit comments