zlayer-registry 0.12.0

OCI container registry client with blob caching
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
//! Persistent blob cache for OCI images using `SQLx` with `SQLite`
//!
//! This module provides a persistent blob cache backed by `SQLite` for durability.
//! Uses WAL mode for concurrent multi-process access.
//! Blobs are stored with metadata for LRU eviction.

use crate::cache::{compute_digest, validate_digest, BlobCacheBackend};
use crate::error::{CacheError, Result};
use async_trait::async_trait;
use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions};
use sqlx::SqlitePool;
use std::path::Path;
use std::str::FromStr;
use std::time::{SystemTime, UNIX_EPOCH};
use tracing::{debug, info};

/// Default database filename used when a directory is provided
const DEFAULT_DB_FILENAME: &str = "blob_cache.sqlite";

#[allow(clippy::cast_possible_wrap)]
fn current_timestamp() -> i64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_or(0, |d| d.as_secs() as i64)
}

/// Persistent blob cache for OCI images backed by `SQLite`
pub struct PersistentBlobCache {
    pool: SqlitePool,
    max_size_bytes: u64,
}

impl PersistentBlobCache {
    /// Create a new persistent cache at the given path
    ///
    /// If `path` is a directory, the cache database will be created as
    /// `blob_cache.sqlite` inside that directory. If `path` is a file path,
    /// it will be used directly as the database file.
    ///
    /// # Errors
    ///
    /// Returns an error if the database cannot be opened or the schema cannot be initialized.
    pub async fn open<P: AsRef<Path>>(path: P) -> Result<Self, CacheError> {
        let path = path.as_ref();

        // If the path is an existing directory, append the default database filename
        let db_path = if path.is_dir() {
            path.join(DEFAULT_DB_FILENAME)
        } else {
            path.to_path_buf()
        };

        // Ensure parent directory exists
        if let Some(parent) = db_path.parent() {
            std::fs::create_dir_all(parent)?;
        }

        // Build connection options with WAL mode and busy timeout
        let connect_options =
            SqliteConnectOptions::from_str(&format!("sqlite:{}?mode=rwc", db_path.display()))
                .map_err(|e| CacheError::Database(format!("invalid database path: {e}")))?
                .pragma("journal_mode", "WAL")
                .pragma("busy_timeout", "5000")
                .pragma("synchronous", "NORMAL")
                .create_if_missing(true);

        // Create connection pool
        let pool = SqlitePoolOptions::new()
            .max_connections(5)
            .connect_with(connect_options)
            .await
            .map_err(|e| CacheError::Database(format!("failed to open database: {e}")))?;

        // Initialize schema
        sqlx::query(
            r"
            CREATE TABLE IF NOT EXISTS blobs (
                digest TEXT PRIMARY KEY NOT NULL,
                data BLOB NOT NULL,
                size_bytes INTEGER NOT NULL,
                created_at INTEGER NOT NULL,
                last_accessed INTEGER NOT NULL
            )
            ",
        )
        .execute(&pool)
        .await
        .map_err(|e| CacheError::Database(format!("failed to create blobs table: {e}")))?;

        sqlx::query(
            r"
            CREATE INDEX IF NOT EXISTS idx_blobs_last_accessed ON blobs(last_accessed)
            ",
        )
        .execute(&pool)
        .await
        .map_err(|e| CacheError::Database(format!("failed to create index: {e}")))?;

        info!("Opened persistent blob cache at {:?}", db_path);

        Ok(Self {
            pool,
            max_size_bytes: 10 * 1024 * 1024 * 1024, // 10GB default
        })
    }

    /// Set maximum cache size in bytes
    #[must_use]
    pub fn with_max_size(mut self, max_size_bytes: u64) -> Self {
        self.max_size_bytes = max_size_bytes;
        self
    }

    /// Get a blob by digest
    ///
    /// # Errors
    ///
    /// Returns an error if the digest is invalid or the database query fails.
    pub async fn get(&self, digest: &str) -> Result<Option<Vec<u8>>, CacheError> {
        validate_digest(digest)?;

        let result: Option<Vec<u8>> = sqlx::query_scalar("SELECT data FROM blobs WHERE digest = ?")
            .bind(digest)
            .fetch_optional(&self.pool)
            .await
            .map_err(|e| CacheError::Database(format!("failed to get blob: {e}")))?;

        // Update last_accessed timestamp asynchronously (best effort)
        if result.is_some() {
            let _ = self.update_access_time(digest).await;
        }

        Ok(result)
    }

    /// Update the `last_accessed` timestamp for a blob
    async fn update_access_time(&self, digest: &str) -> Result<(), CacheError> {
        let now = current_timestamp();

        sqlx::query("UPDATE blobs SET last_accessed = ? WHERE digest = ?")
            .bind(now)
            .bind(digest)
            .execute(&self.pool)
            .await
            .map_err(|e| CacheError::Database(format!("failed to update access time: {e}")))?;

        Ok(())
    }

    /// Put a blob into the cache
    ///
    /// # Errors
    ///
    /// Returns an error if the digest is invalid, mismatches the data, or the database write fails.
    #[allow(clippy::cast_possible_wrap)]
    pub async fn put(&self, digest: &str, data: &[u8]) -> Result<(), CacheError> {
        validate_digest(digest)?;

        // Verify digest matches data (skip for manifest cache keys)
        if !digest.starts_with("manifest:") {
            let actual_digest = compute_digest(data);
            if actual_digest != digest {
                return Err(CacheError::Corrupted(format!(
                    "digest mismatch: expected {digest}, got {actual_digest}"
                )));
            }
        }

        let now = current_timestamp();
        let size_bytes = data.len() as i64;

        sqlx::query(
            r"
            INSERT OR REPLACE INTO blobs (digest, data, size_bytes, created_at, last_accessed)
            VALUES (?, ?, ?, ?, ?)
            ",
        )
        .bind(digest)
        .bind(data)
        .bind(size_bytes)
        .bind(now)
        .bind(now)
        .execute(&self.pool)
        .await
        .map_err(|e| CacheError::Database(format!("failed to insert blob: {e}")))?;

        debug!("Stored blob {} ({} bytes)", digest, data.len());

        // Evict if needed (after insert to avoid holding transaction too long)
        self.evict_if_needed().await?;

        Ok(())
    }

    /// Check if a blob exists in the cache
    ///
    /// # Errors
    ///
    /// Returns an error if the digest is invalid or the database query fails.
    pub async fn contains(&self, digest: &str) -> Result<bool, CacheError> {
        validate_digest(digest)?;

        let exists: bool =
            sqlx::query_scalar("SELECT EXISTS(SELECT 1 FROM blobs WHERE digest = ?)")
                .bind(digest)
                .fetch_one(&self.pool)
                .await
                .map_err(|e| CacheError::Database(format!("failed to check blob: {e}")))?;

        Ok(exists)
    }

    /// Delete a blob from the cache
    ///
    /// # Errors
    ///
    /// Returns an error if the digest is invalid or the database delete fails.
    pub async fn delete(&self, digest: &str) -> Result<(), CacheError> {
        validate_digest(digest)?;

        sqlx::query("DELETE FROM blobs WHERE digest = ?")
            .bind(digest)
            .execute(&self.pool)
            .await
            .map_err(|e| CacheError::Database(format!("failed to delete blob: {e}")))?;

        debug!("Deleted blob {}", digest);

        Ok(())
    }

    /// Return every `digest` column value whose name starts with `prefix`.
    ///
    /// # Errors
    ///
    /// Returns `CacheError::Database` on SQL failure.
    pub async fn keys_with_prefix(&self, prefix: &str) -> Result<Vec<String>, CacheError> {
        let pattern = format!("{prefix}%");
        let rows: Vec<(String,)> =
            sqlx::query_as("SELECT digest FROM blobs WHERE digest LIKE ? ESCAPE '\\'")
                .bind(pattern)
                .fetch_all(&self.pool)
                .await
                .map_err(|e| CacheError::Database(format!("failed to list keys: {e}")))?;
        Ok(rows.into_iter().map(|(s,)| s).collect())
    }

    /// Get current cache size in bytes
    ///
    /// # Errors
    ///
    /// Returns an error if the database query fails.
    #[allow(clippy::cast_sign_loss)]
    pub async fn size(&self) -> Result<u64, CacheError> {
        let total: Option<i64> = sqlx::query_scalar("SELECT SUM(size_bytes) FROM blobs")
            .fetch_one(&self.pool)
            .await
            .map_err(|e| CacheError::Database(format!("failed to get cache size: {e}")))?;

        Ok(total.unwrap_or(0) as u64)
    }

    /// Get number of blobs in the cache
    ///
    /// # Errors
    ///
    /// Returns an error if the database query fails.
    #[allow(clippy::cast_sign_loss)]
    pub async fn blob_count(&self) -> Result<u64, CacheError> {
        let count: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM blobs")
            .fetch_one(&self.pool)
            .await
            .map_err(|e| CacheError::Database(format!("failed to get blob count: {e}")))?;

        Ok(count as u64)
    }

    /// Clear all blobs from the cache
    ///
    /// # Errors
    ///
    /// Returns an error if the database delete fails.
    pub async fn clear(&self) -> Result<(), CacheError> {
        sqlx::query("DELETE FROM blobs")
            .execute(&self.pool)
            .await
            .map_err(|e| CacheError::Database(format!("failed to clear cache: {e}")))?;

        info!("Cleared all blobs from cache");

        Ok(())
    }

    /// Evict blobs using LRU if cache is over size limit
    #[allow(
        clippy::cast_precision_loss,
        clippy::cast_possible_truncation,
        clippy::cast_sign_loss
    )]
    async fn evict_if_needed(&self) -> Result<(), CacheError> {
        let current_size = self.size().await?;
        if current_size <= self.max_size_bytes {
            return Ok(());
        }

        // Target: evict until we're at 90% capacity
        let target_size = (self.max_size_bytes as f64 * 0.9) as u64;
        let to_evict = current_size.saturating_sub(target_size);

        info!(
            "Cache size {} exceeds limit {}, evicting {} bytes",
            current_size, self.max_size_bytes, to_evict
        );

        // Get oldest entries sorted by last_accessed
        let entries: Vec<(String, i64)> =
            sqlx::query_as("SELECT digest, size_bytes FROM blobs ORDER BY last_accessed ASC")
                .fetch_all(&self.pool)
                .await
                .map_err(|e| {
                    CacheError::Database(format!("failed to get blobs for eviction: {e}"))
                })?;

        // Evict oldest entries until we reach target
        let mut evicted_size = 0u64;
        let mut evicted_count = 0u64;
        let mut digests_to_delete = Vec::new();

        for (digest, size_bytes) in entries {
            if evicted_size >= to_evict {
                break;
            }

            digests_to_delete.push(digest);
            #[allow(clippy::cast_sign_loss)]
            {
                evicted_size += size_bytes as u64;
            }
            evicted_count += 1;
        }

        // Delete in batch for efficiency
        for digest in &digests_to_delete {
            sqlx::query("DELETE FROM blobs WHERE digest = ?")
                .bind(digest)
                .execute(&self.pool)
                .await
                .map_err(|e| CacheError::Database(format!("failed to delete blob: {e}")))?;
        }

        info!(
            "Evicted {} blobs ({} bytes) from cache",
            evicted_count, evicted_size
        );

        Ok(())
    }
}

#[async_trait]
impl BlobCacheBackend for PersistentBlobCache {
    async fn get(&self, digest: &str) -> Result<Option<Vec<u8>>, CacheError> {
        PersistentBlobCache::get(self, digest).await
    }

    async fn put(&self, digest: &str, data: &[u8]) -> Result<(), CacheError> {
        PersistentBlobCache::put(self, digest, data).await
    }

    async fn contains(&self, digest: &str) -> Result<bool, CacheError> {
        PersistentBlobCache::contains(self, digest).await
    }

    async fn delete(&self, digest: &str) -> Result<(), CacheError> {
        // Delegate to the inherent method (already async).
        PersistentBlobCache::delete(self, digest).await
    }

    async fn keys_with_prefix(&self, prefix: &str) -> Result<Vec<String>, CacheError> {
        PersistentBlobCache::keys_with_prefix(self, prefix).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cache::compute_digest;
    use tempfile::TempDir;

    async fn create_test_cache() -> (PersistentBlobCache, TempDir) {
        let temp_dir = TempDir::new().unwrap();
        let cache_path = temp_dir.path().join("test_cache.sqlite");
        let cache = PersistentBlobCache::open(&cache_path).await.unwrap();
        (cache, temp_dir)
    }

    #[tokio::test]
    async fn test_put_get() {
        let (cache, _temp) = create_test_cache().await;

        let data = b"test data";
        let digest = compute_digest(data);

        cache.put(&digest, data).await.unwrap();
        let retrieved = cache.get(&digest).await.unwrap();
        assert_eq!(retrieved, Some(data.to_vec()));
    }

    #[tokio::test]
    async fn test_contains() {
        let (cache, _temp) = create_test_cache().await;

        let data = b"test data";
        let digest = compute_digest(data);

        assert!(!cache.contains(&digest).await.unwrap());
        cache.put(&digest, data).await.unwrap();
        assert!(cache.contains(&digest).await.unwrap());
    }

    #[tokio::test]
    async fn test_delete() {
        let (cache, _temp) = create_test_cache().await;

        let data = b"test data";
        let digest = compute_digest(data);

        cache.put(&digest, data).await.unwrap();
        assert!(cache.contains(&digest).await.unwrap());

        cache.delete(&digest).await.unwrap();
        assert!(!cache.contains(&digest).await.unwrap());
    }

    #[tokio::test]
    async fn test_size() {
        let (cache, _temp) = create_test_cache().await;

        let data1 = b"test data 1";
        let digest1 = compute_digest(data1);
        let data2 = b"test data 2";
        let digest2 = compute_digest(data2);

        cache.put(&digest1, data1).await.unwrap();
        cache.put(&digest2, data2).await.unwrap();

        let size = cache.size().await.unwrap();
        assert_eq!(size, data1.len() as u64 + data2.len() as u64);
    }

    #[tokio::test]
    async fn test_clear() {
        let (cache, _temp) = create_test_cache().await;

        let data = b"test data";
        let digest = compute_digest(data);

        cache.put(&digest, data).await.unwrap();
        assert!(cache.contains(&digest).await.unwrap());

        cache.clear().await.unwrap();
        assert!(!cache.contains(&digest).await.unwrap());
        assert_eq!(cache.size().await.unwrap(), 0);
    }

    #[tokio::test]
    async fn test_persistence() {
        let temp_dir = TempDir::new().unwrap();
        let cache_path = temp_dir.path().join("persist_cache.sqlite");

        let data = b"persistent data";
        let digest = compute_digest(data);

        // Write data
        {
            let cache = PersistentBlobCache::open(&cache_path).await.unwrap();
            cache.put(&digest, data).await.unwrap();
        }

        // Reopen and verify data persists
        {
            let cache = PersistentBlobCache::open(&cache_path).await.unwrap();
            let retrieved = cache.get(&digest).await.unwrap();
            assert_eq!(retrieved, Some(data.to_vec()));
        }
    }

    #[tokio::test]
    async fn test_eviction() {
        let (cache, _temp) = create_test_cache().await;
        let cache = cache.with_max_size(100); // 100 bytes max

        // Insert data that exceeds limit
        for i in 0..20 {
            let data = format!("data_{i:02}");
            let digest = compute_digest(data.as_bytes());
            cache.put(&digest, data.as_bytes()).await.unwrap();

            // Add small delay to ensure different timestamps
            tokio::time::sleep(std::time::Duration::from_millis(10)).await;
        }

        // Cache should have evicted some entries
        let size = cache.size().await.unwrap();
        assert!(size <= 100, "Cache size {size} should be <= 100");
    }

    #[tokio::test]
    async fn test_invalid_digest() {
        let (cache, _temp) = create_test_cache().await;

        let result = cache.get("invalid_digest").await;
        assert!(result.is_err());

        let result = cache.put("md5:abc", b"data").await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_digest_mismatch() {
        let (cache, _temp) = create_test_cache().await;

        let data = b"test data";
        let wrong_digest =
            "sha256:0000000000000000000000000000000000000000000000000000000000000000";

        let result = cache.put(wrong_digest, data).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_open_with_directory_path() {
        // Test that opening with a directory path works (appends default filename)
        let temp_dir = TempDir::new().unwrap();

        // Pass the directory path instead of a file path
        let cache = PersistentBlobCache::open(temp_dir.path()).await.unwrap();

        // Verify cache works
        let data = b"test data for directory path";
        let digest = compute_digest(data);

        cache.put(&digest, data).await.unwrap();
        let retrieved = cache.get(&digest).await.unwrap();
        assert_eq!(retrieved, Some(data.to_vec()));

        // Verify the database file was created with the default name
        let expected_db_path = temp_dir.path().join(DEFAULT_DB_FILENAME);
        assert!(
            expected_db_path.exists(),
            "Database file should be created at {expected_db_path:?}"
        );
    }

    #[tokio::test]
    async fn test_lru_access_time_update() {
        let (cache, _temp) = create_test_cache().await;
        let cache = cache.with_max_size(150); // 150 bytes max

        // Create three blobs of 60 bytes each
        let data1 = vec![1u8; 60];
        let digest1 = compute_digest(&data1);
        let data2 = vec![2u8; 60];
        let digest2 = compute_digest(&data2);
        let data3 = vec![3u8; 60];
        let digest3 = compute_digest(&data3);

        // Add first two blobs (120 bytes total, under limit)
        cache.put(&digest1, &data1).await.unwrap();
        tokio::time::sleep(std::time::Duration::from_secs(1)).await; // Use full second sleeps for timestamp resolution
        cache.put(&digest2, &data2).await.unwrap();
        tokio::time::sleep(std::time::Duration::from_secs(1)).await;

        // Verify both exist
        assert!(cache.contains(&digest1).await.unwrap());
        assert!(cache.contains(&digest2).await.unwrap());

        // Access digest1 to update its access time (making it more recent than data2)
        let result = cache.get(&digest1).await.unwrap();
        assert!(result.is_some(), "data1 should exist");
        tokio::time::sleep(std::time::Duration::from_secs(1)).await;

        // Adding third blob brings us to 180 bytes (over 150 limit)
        // Eviction target is 90% of 150 = 135 bytes
        // Need to evict 180 - 135 = 45 bytes
        // Should evict data2 (60 bytes, the oldest), leaving data1 and data3
        cache.put(&digest3, &data3).await.unwrap();

        // Verify the eviction happened correctly
        let has_data1 = cache.contains(&digest1).await.unwrap();
        let has_data2 = cache.contains(&digest2).await.unwrap();
        let has_data3 = cache.contains(&digest3).await.unwrap();
        let final_size = cache.size().await.unwrap();

        // data3 (just added) should always remain
        assert!(
            has_data3,
            "data3 should remain (just added). data1={has_data1}, data2={has_data2}, data3={has_data3}, size={final_size}"
        );

        // At least one should be evicted
        assert!(
            !has_data1 || !has_data2,
            "At least one of data1 or data2 should be evicted. data1={has_data1}, data2={has_data2}, data3={has_data3}, size={final_size}"
        );

        // data2 (oldest) should be evicted before data1 (accessed recently)
        assert!(
            has_data1 || !has_data2,
            "LRU eviction failed: data1 (recently accessed) was evicted but data2 (oldest) was kept. data1={has_data1}, data2={has_data2}, data3={has_data3}, size={final_size}"
        );
    }

    #[tokio::test]
    async fn test_blob_count() {
        let (cache, _temp) = create_test_cache().await;

        assert_eq!(cache.blob_count().await.unwrap(), 0);

        let data1 = b"test data 1";
        let digest1 = compute_digest(data1);
        cache.put(&digest1, data1).await.unwrap();
        assert_eq!(cache.blob_count().await.unwrap(), 1);

        let data2 = b"test data 2";
        let digest2 = compute_digest(data2);
        cache.put(&digest2, data2).await.unwrap();
        assert_eq!(cache.blob_count().await.unwrap(), 2);

        cache.delete(&digest1).await.unwrap();
        assert_eq!(cache.blob_count().await.unwrap(), 1);
    }
}