package database import ( "context" "database/sql" "fmt" "time" ) type SnapshotRepository struct { db *DB } func NewSnapshotRepository(db *DB) *SnapshotRepository { return &SnapshotRepository{db: db} } func (r *SnapshotRepository) Create(ctx context.Context, tx *sql.Tx, snapshot *Snapshot) error { query := ` INSERT INTO snapshots (id, hostname, vaultik_version, created_ts, file_count, chunk_count, blob_count, total_size, blob_size, compression_ratio) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ` var err error if tx != nil { _, err = tx.ExecContext(ctx, query, snapshot.ID, snapshot.Hostname, snapshot.VaultikVersion, snapshot.CreatedTS.Unix(), snapshot.FileCount, snapshot.ChunkCount, snapshot.BlobCount, snapshot.TotalSize, snapshot.BlobSize, snapshot.CompressionRatio) } else { _, err = r.db.ExecWithLock(ctx, query, snapshot.ID, snapshot.Hostname, snapshot.VaultikVersion, snapshot.CreatedTS.Unix(), snapshot.FileCount, snapshot.ChunkCount, snapshot.BlobCount, snapshot.TotalSize, snapshot.BlobSize, snapshot.CompressionRatio) } if err != nil { return fmt.Errorf("inserting snapshot: %w", err) } return nil } func (r *SnapshotRepository) UpdateCounts(ctx context.Context, tx *sql.Tx, snapshotID string, fileCount, chunkCount, blobCount, totalSize, blobSize int64) error { compressionRatio := 1.0 if totalSize > 0 { compressionRatio = float64(blobSize) / float64(totalSize) } query := ` UPDATE snapshots SET file_count = ?, chunk_count = ?, blob_count = ?, total_size = ?, blob_size = ?, compression_ratio = ? WHERE id = ? ` var err error if tx != nil { _, err = tx.ExecContext(ctx, query, fileCount, chunkCount, blobCount, totalSize, blobSize, compressionRatio, snapshotID) } else { _, err = r.db.ExecWithLock(ctx, query, fileCount, chunkCount, blobCount, totalSize, blobSize, compressionRatio, snapshotID) } if err != nil { return fmt.Errorf("updating snapshot: %w", err) } return nil } func (r *SnapshotRepository) GetByID(ctx context.Context, snapshotID string) (*Snapshot, error) { query := ` SELECT id, hostname, vaultik_version, created_ts, file_count, chunk_count, blob_count, total_size, blob_size, compression_ratio FROM snapshots WHERE id = ? ` var snapshot Snapshot var createdTSUnix int64 err := r.db.conn.QueryRowContext(ctx, query, snapshotID).Scan( &snapshot.ID, &snapshot.Hostname, &snapshot.VaultikVersion, &createdTSUnix, &snapshot.FileCount, &snapshot.ChunkCount, &snapshot.BlobCount, &snapshot.TotalSize, &snapshot.BlobSize, &snapshot.CompressionRatio, ) if err == sql.ErrNoRows { return nil, nil } if err != nil { return nil, fmt.Errorf("querying snapshot: %w", err) } snapshot.CreatedTS = time.Unix(createdTSUnix, 0) return &snapshot, nil } func (r *SnapshotRepository) ListRecent(ctx context.Context, limit int) ([]*Snapshot, error) { query := ` SELECT id, hostname, vaultik_version, created_ts, file_count, chunk_count, blob_count, total_size, blob_size, compression_ratio FROM snapshots ORDER BY created_ts DESC LIMIT ? ` rows, err := r.db.conn.QueryContext(ctx, query, limit) if err != nil { return nil, fmt.Errorf("querying snapshots: %w", err) } defer CloseRows(rows) var snapshots []*Snapshot for rows.Next() { var snapshot Snapshot var createdTSUnix int64 err := rows.Scan( &snapshot.ID, &snapshot.Hostname, &snapshot.VaultikVersion, &createdTSUnix, &snapshot.FileCount, &snapshot.ChunkCount, &snapshot.BlobCount, &snapshot.TotalSize, &snapshot.BlobSize, &snapshot.CompressionRatio, ) if err != nil { return nil, fmt.Errorf("scanning snapshot: %w", err) } snapshot.CreatedTS = time.Unix(createdTSUnix, 0) snapshots = append(snapshots, &snapshot) } return snapshots, rows.Err() }