~james/core

core/spindle/db/cache.go -rw-r--r-- 19.0 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 14 hours ago
                                                                                
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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
package db

import (
	"context"
	"database/sql"
	"fmt"
	"time"

	"tangled.org/core/spindle/quota"
)

type CacheEntry struct {
	ID           string
	StorageKey   string
	OwnerDID     string
	RepoDID      string
	Engine       string
	CacheKey     string
	CacheHash    string
	Checksum     string
	SizeBytes    int64
	RestoreCount int64
	State        string
	CreatedAt    time.Time
	LastUsedAt   time.Time
}

const cacheEntryColumns = `
	id, storage_key, owner_did, repo_did, engine, cache_key, cache_hash, checksum,
	size_bytes, restore_count, state, created_at, last_used_at`

func (d *DB) InsertCacheEntry(ctx context.Context, entry CacheEntry) error {
	_, err := d.ExecContext(ctx, `
		insert into cache_entries (
			id, storage_key, owner_did, repo_did, engine, cache_key, cache_hash, checksum,
			size_bytes, restore_count, state, created_at, last_used_at
		) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
		entry.ID,
		entry.StorageKey,
		entry.OwnerDID,
		entry.RepoDID,
		entry.Engine,
		entry.CacheKey,
		entry.CacheHash,
		entry.Checksum,
		entry.SizeBytes,
		entry.RestoreCount,
		entry.State,
		entry.CreatedAt.UnixNano(),
		entry.LastUsedAt.UnixNano(),
	)
	return err
}
func (d *DB) InsertCacheEntryWithinQuota(ctx context.Context, entry CacheEntry, maxEntries int64) (bool, error) {
	result, err := d.ExecContext(ctx, `
		insert into cache_entries (
			id, storage_key, owner_did, repo_did, engine, cache_key, cache_hash, checksum,
			size_bytes, restore_count, state, created_at, last_used_at
		)
		select ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
		where ? <= 0 or (
			select count(*) from cache_entries
			where owner_did = ? and state in ('pending', 'ready', 'deleting')
		) < ?`,
		entry.ID,
		entry.StorageKey,
		entry.OwnerDID,
		entry.RepoDID,
		entry.Engine,
		entry.CacheKey,
		entry.CacheHash,
		entry.Checksum,
		entry.SizeBytes,
		entry.RestoreCount,
		entry.State,
		entry.CreatedAt.UnixNano(),
		entry.LastUsedAt.UnixNano(),
		maxEntries,
		entry.OwnerDID,
		maxEntries,
	)
	if err != nil {
		return false, err
	}
	inserted, err := result.RowsAffected()
	return inserted == 1, err
}

func (d *DB) MarkCacheEntryReady(ctx context.Context, id string, sizeBytes int64, qs *QuotaStore, now time.Time) ([]CacheEntry, error) {
	return d.MarkCacheEntryReadyWithChecksum(ctx, id, sizeBytes, "", qs, 0, now)
}

// a nil qs charges nothing
func (d *DB) MarkCacheEntryReadyWithChecksum(ctx context.Context, id string, sizeBytes int64, checksum string, qs *QuotaStore, maxTotalBytes int64, now time.Time) ([]CacheEntry, error) {
	tx, err := d.BeginTx(ctx, nil)
	if err != nil {
		return nil, err
	}
	defer tx.Rollback()
	superseded, ready, err := markCacheEntryReady(ctx, tx, id, sizeBytes, checksum, qs, maxTotalBytes, now)
	if err != nil {
		return nil, err
	}
	if !ready {
		return nil, sql.ErrNoRows
	}
	if err := tx.Commit(); err != nil {
		return nil, err
	}
	return superseded, nil
}

type execQuerier interface {
	ExecContext(context.Context, string, ...any) (sql.Result, error)
	QueryRowContext(context.Context, string, ...any) *sql.Row
	QueryContext(context.Context, string, ...any) (*sql.Rows, error)
}

func markCacheEntryReady(ctx context.Context, tx execQuerier, id string, sizeBytes int64, checksum string, qs *QuotaStore, maxTotalBytes int64, now time.Time) ([]CacheEntry, bool, error) {
	current, err := scanCacheEntry(tx.QueryRowContext(ctx, `select `+cacheEntryColumns+` from cache_entries where id = ?`, id))
	if err == sql.ErrNoRows {
		return nil, false, nil
	}
	if err != nil {
		return nil, false, err
	}
	if current.State != "pending" {
		if current.State == "ready" {
			if current.SizeBytes != sizeBytes {
				return nil, false, fmt.Errorf("cache entry %q size changed from %d to %d", id, current.SizeBytes, sizeBytes)
			}
			return nil, true, nil
		}
		return nil, false, nil
	}

	rows, err := tx.QueryContext(ctx, `
		update cache_entries
		set state = 'deleting'
		where repo_did = ? and engine = ? and cache_key = ? and cache_hash = ?
			and state = 'ready'
		returning `+cacheEntryColumns, current.RepoDID, current.Engine, current.CacheKey, current.CacheHash)
	if err != nil {
		return nil, false, err
	}
	var superseded []CacheEntry
	for rows.Next() {
		entry, err := scanCacheEntry(rows)
		if err != nil {
			rows.Close()
			return nil, false, err
		}
		superseded = append(superseded, *entry)
	}
	if err := rows.Close(); err != nil {
		return nil, false, err
	}
	if err := rows.Err(); err != nil {
		return nil, false, err
	}

	if maxTotalBytes > 0 {
		evicted, fits, err := evictForTotal(ctx, tx, sizeBytes, maxTotalBytes)
		if err != nil {
			return nil, false, err
		}
		if !fits {
			return nil, false, unmarkCacheEntries(ctx, tx, superseded)
		}
		superseded = append(superseded, evicted...)
	}

	if qs != nil {
		// what this save replaces stops counting against it, and counts again if the charge is refused
		if _, err := tx.ExecContext(ctx, `savepoint replace_charge`); err != nil {
			return nil, false, err
		}
		for _, entry := range superseded {
			if err := unchargeCacheEntry(ctx, tx, entry.RepoDID, entry.ID); err != nil {
				return nil, false, err
			}
		}
		res, err := qs.ChargeTx(ctx, tx, quota.ReserveRequest{
			Kind:      quota.KindGenericCache,
			Key:       current.ID,
			Identity:  quota.Identity{OwnerDID: current.OwnerDID, RepoDID: current.RepoDID},
			Resources: quota.Resources{quota.ResourceCacheStorageBytes: sizeBytes},
		})
		if err != nil {
			return nil, false, err
		}
		if !res.Allowed {
			if _, err := tx.ExecContext(ctx, `rollback to replace_charge`); err != nil {
				return nil, false, err
			}
		}
		if _, err := tx.ExecContext(ctx, `release replace_charge`); err != nil {
			return nil, false, err
		}
		if !res.Allowed {
			return nil, false, unmarkCacheEntries(ctx, tx, superseded)
		}
	}

	updated, err := tx.QueryContext(ctx, `
		update cache_entries
		set state = 'ready', checksum = ?, size_bytes = ?, last_used_at = ?
		where id = ? and state = 'pending'
		returning id`, checksum, sizeBytes, now.UnixNano(), id)
	if err != nil {
		return nil, false, err
	}
	ready := updated.Next()
	updated.Close()
	return superseded, ready, nil
}

// a refused save still commits inside the mill's batch tx
func unmarkCacheEntries(ctx context.Context, tx execQuerier, entries []CacheEntry) error {
	for _, entry := range entries {
		if _, err := tx.ExecContext(ctx, `update cache_entries set state = 'ready' where id = ? and state = 'deleting'`, entry.ID); err != nil {
			return err
		}
	}
	return nil
}

const (
	readyCacheBytesQuery = `select coalesce(sum(size_bytes), 0) from cache_entries where state = 'ready'`
	// entries a restore holds stay out of eviction
	evictableCacheEntriesQuery = `select ` + cacheEntryColumns + ` from cache_entries
		where state = 'ready' and restore_count = 0
		order by last_used_at, created_at`
)

// evicts nothing when even every evictable entry would not make room
func evictForTotal(ctx context.Context, tx execQuerier, sizeBytes, maxTotalBytes int64) ([]CacheEntry, bool, error) {
	var used int64
	if err := tx.QueryRowContext(ctx, readyCacheBytesQuery).Scan(&used); err != nil {
		return nil, false, err
	}
	need := used + sizeBytes - maxTotalBytes
	if need <= 0 {
		return nil, true, nil
	}
	rows, err := tx.QueryContext(ctx, evictableCacheEntriesQuery)
	if err != nil {
		return nil, false, err
	}
	var evicted []CacheEntry
	for need > 0 && rows.Next() {
		entry, err := scanCacheEntry(rows)
		if err != nil {
			rows.Close()
			return nil, false, err
		}
		evicted = append(evicted, *entry)
		need -= entry.SizeBytes
	}
	if err := rows.Close(); err != nil {
		return nil, false, err
	}
	if err := rows.Err(); err != nil {
		return nil, false, err
	}
	if need > 0 {
		return nil, false, nil
	}
	for _, entry := range evicted {
		if _, err := tx.ExecContext(ctx, `update cache_entries set state = 'deleting' where id = ?`, entry.ID); err != nil {
			return nil, false, err
		}
	}
	return evicted, true, nil
}

func (tx *EventBatchTx) MarkCacheEntryReadyWithChecksum(ctx context.Context, id string, sizeBytes int64, checksum string, qs *QuotaStore, maxTotalBytes int64, now time.Time) ([]CacheEntry, bool, error) {
	return markCacheEntryReady(ctx, tx.tx, id, sizeBytes, checksum, qs, maxTotalBytes, now)
}

func (tx *EventBatchTx) TouchCacheEntry(ctx context.Context, id string, now time.Time) error {
	_, err := tx.tx.ExecContext(ctx, `
		update cache_entries
		set last_used_at = max(last_used_at, ?)
		where id = ? and state = 'ready'`, now.UnixNano(), id)
	return err
}

func (tx *EventBatchTx) DiscardCacheEntry(ctx context.Context, id string, pendingOnly bool) (*CacheEntry, error) {
	stateFilter := ""
	if pendingOnly {
		stateFilter = " and state in ('pending', 'deleting')"
	}
	current, err := scanCacheEntry(tx.tx.QueryRowContext(ctx, `
		update cache_entries
		set state = 'deleting'
		where id = ?`+stateFilter+`
		returning `+cacheEntryColumns, id))
	if err == sql.ErrNoRows {
		return nil, nil
	}
	return current, err
}

func (d *DB) FindCacheEntry(ctx context.Context, repoDID, engine, key, hash string) (*CacheEntry, error) {
	return scanCacheEntry(d.QueryRowContext(ctx, `
		select `+cacheEntryColumns+`
		from cache_entries
		where repo_did = ? and engine = ? and cache_key = ? and cache_hash = ? and state = 'ready'
		order by created_at desc
		limit 1`, repoDID, engine, key, hash))
}

func (d *DB) FindFallbackCacheEntry(ctx context.Context, repoDID, engine, key, excludeHash string) (*CacheEntry, error) {
	return scanCacheEntry(d.QueryRowContext(ctx, `
		select `+cacheEntryColumns+`
		from cache_entries
		where repo_did = ? and engine = ? and cache_key = ?
			and cache_hash <> ? and cache_hash <> '' and state = 'ready'
		order by created_at desc
		limit 1`, repoDID, engine, key, excludeHash))
}

func (d *DB) BeginCacheRestore(ctx context.Context, id string) (bool, error) {
	result, err := d.ExecContext(ctx, `update cache_entries set restore_count = restore_count + 1 where id = ? and state = 'ready'`, id)
	if err != nil {
		return false, err
	}
	n, err := result.RowsAffected()
	return n == 1, err
}

func (d *DB) EndCacheRestore(ctx context.Context, id string) error {
	_, err := d.ExecContext(ctx, `update cache_entries set restore_count = restore_count - 1 where id = ? and restore_count > 0`, id)
	return err
}

func (d *DB) CacheEntryPinned(ctx context.Context, id string) (bool, error) {
	var count int64
	err := d.QueryRowContext(ctx, `select restore_count from cache_entries where id = ?`, id).Scan(&count)
	if err == sql.ErrNoRows {
		return false, nil
	}
	return count > 0, err
}

func (d *DB) TouchCacheEntry(ctx context.Context, id string, now time.Time) error {
	_, err := d.ExecContext(ctx, `
		update cache_entries set last_used_at = max(last_used_at, ?) where id = ? and state = 'ready'`, now.UnixNano(), id)
	return err
}

func (d *DB) ClaimPendingCacheEntry(ctx context.Context, id string) (bool, error) {
	result, err := d.ExecContext(ctx, `
		update cache_entries set state = 'deleting' where id = ? and state = 'pending'`, id)
	if err != nil {
		return false, err
	}
	changed, err := result.RowsAffected()
	return changed == 1, err
}

func (d *DB) ClaimCacheEntry(ctx context.Context, id, expectedState string, expectedLastUsed time.Time) (bool, error) {
	result, err := d.ExecContext(ctx, `
		update cache_entries
		set state = 'deleting'
		where id = ? and state = ? and last_used_at = ? and restore_count = 0`,
		id, expectedState, expectedLastUsed.UnixNano())
	if err != nil {
		return false, err
	}
	changed, err := result.RowsAffected()
	return changed == 1, err
}

func (d *DB) ReadyCacheBytes(ctx context.Context) (int64, error) {
	var used int64
	err := d.QueryRowContext(ctx, readyCacheBytesQuery).Scan(&used)
	return used, err
}

func (d *DB) EvictableCacheEntries(ctx context.Context, limit int) ([]CacheEntry, error) {
	return d.queryCacheEntries(ctx, evictableCacheEntriesQuery+` limit ?`, limit)
}

func (d *DB) EvictableCacheEntriesOf(ctx context.Context, repoDID, ownerDID string, limit int) ([]CacheEntry, error) {
	return d.queryCacheEntries(ctx, `select `+cacheEntryColumns+` from cache_entries
		where state = 'ready' and restore_count = 0 and (repo_did = ? or owner_did = ?)
		order by last_used_at, created_at
		limit ?`, repoDID, ownerDID, limit)
}

func (d *DB) GetCacheEntry(ctx context.Context, id string) (*CacheEntry, error) {
	return scanCacheEntry(d.QueryRowContext(ctx, `select `+cacheEntryColumns+` from cache_entries where id = ?`, id))
}

func (d *DB) RestoreCacheEntryState(ctx context.Context, id, state string) error {
	_, err := d.ExecContext(ctx, `
		update cache_entries set state = ? where id = ? and state = 'deleting'`, state, id)
	return err
}

func (d *DB) ExpiredCacheEntries(ctx context.Context, readyBefore, pendingBefore time.Time, limit int) ([]CacheEntry, error) {
	return d.queryCacheEntries(ctx, `
		select `+cacheEntryColumns+`
		from cache_entries
		where (state = 'ready' and last_used_at < ?)
			or (state in ('pending', 'deleting') and last_used_at < ?)
		order by last_used_at
		limit ?`,
		readyBefore.UnixNano(),
		pendingBefore.UnixNano(),
		limit,
	)
}

// discardPendingCacheEntriesForLease fences uploads that can arrive after a lease dies.
// Their tombstones let the cache pruner remove a late object without metadata.
func (d *DB) DiscardPendingCacheEntriesForLease(leaseID string, now time.Time) ([]string, error) {
	tx, err := d.Begin()
	if err != nil {
		return nil, err
	}
	defer tx.Rollback()

	rows, err := tx.Query(`
		select c.id, c.storage_key
		from cache_entries c
		join mill_cache_capabilities cap on cap.cache_id = c.id
		where cap.lease_id = ? and cap.action = 'save' and c.state = 'pending'`, leaseID)
	if err != nil {
		return nil, err
	}
	var keys []string
	var ids []string
	for rows.Next() {
		var id, key string
		if err := rows.Scan(&id, &key); err != nil {
			_ = rows.Close()
			return nil, err
		}
		ids = append(ids, id)
		keys = append(keys, key)
	}
	if err := rows.Err(); err != nil {
		_ = rows.Close()
		return nil, err
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	for i, id := range ids {
		if _, err := tx.Exec(`update cache_entries set state = 'deleting' where id = ? and state = 'pending'`, id); err != nil {
			return nil, err
		}
		if _, err := tx.Exec(`
			insert into cache_object_deletions (storage_key, created_at)
			values (?, ?)
			on conflict(storage_key) do nothing`, keys[i], now.UnixNano()); err != nil {
			return nil, err
		}
	}
	if err := tx.Commit(); err != nil {
		return nil, err
	}
	return keys, nil
}

func (d *DB) DeleteCacheEntry(ctx context.Context, id string) error {
	return d.deleteCacheEntries(ctx, `id = ?`, id)
}

func (d *DB) DeleteCacheEntriesByStorageKey(ctx context.Context, key string) error {
	return d.deleteCacheEntries(ctx, `storage_key = ? and restore_count = 0`, key)
}

func (d *DB) deleteCacheEntries(ctx context.Context, where string, args ...any) error {
	tx, err := d.BeginTx(ctx, nil)
	if err != nil {
		return err
	}
	defer tx.Rollback()
	rows, err := tx.QueryContext(ctx, `delete from cache_entries where `+where+` returning repo_did, id`, args...)
	if err != nil {
		return err
	}
	var gone [][2]string
	for rows.Next() {
		var repo, id string
		if err := rows.Scan(&repo, &id); err != nil {
			rows.Close()
			return err
		}
		gone = append(gone, [2]string{repo, id})
	}
	if err := rows.Close(); err != nil {
		return err
	}
	for _, entry := range gone {
		if err := unchargeCacheEntry(ctx, tx, entry[0], entry[1]); err != nil {
			return err
		}
	}
	return tx.Commit()
}

func unchargeCacheEntry(ctx context.Context, tx execQuerier, repoDID, id string) error {
	_, err := tx.ExecContext(ctx, `
		delete from quota_allocations where repo_did = ? and kind = ? and key = ?`,
		repoDID, quota.KindGenericCache, id)
	return err
}

type MillCacheCapability struct {
	Action     string
	CacheID    string
	StorageKey string
}

func (d *DB) SaveMillCacheCapabilities(leaseID string, capabilities []MillCacheCapability) error {
	tx, err := d.Begin()
	if err != nil {
		return err
	}
	defer tx.Rollback()
	if _, err := tx.Exec(`delete from mill_cache_capabilities where lease_id = ?`, leaseID); err != nil {
		return err
	}
	for _, capability := range capabilities {
		if capability.Action != "restore" && capability.Action != "save" {
			return fmt.Errorf("cache capability has invalid action %q", capability.Action)
		}
		if capability.CacheID == "" || capability.StorageKey == "" {
			return fmt.Errorf("cache capability has empty identity")
		}
		if _, err := tx.Exec(`
			insert into mill_cache_capabilities (
				lease_id, action, cache_id, storage_key
			) values (?, ?, ?, ?)`,
			leaseID,
			capability.Action,
			capability.CacheID,
			capability.StorageKey,
		); err != nil {
			return err
		}
	}
	return tx.Commit()
}

func (tx *EventBatchTx) ConsumeMillCacheCapability(ctx context.Context, leaseID, action, cacheID string) (string, bool, error) {
	var storageKey string
	err := tx.tx.QueryRowContext(ctx, `
		delete from mill_cache_capabilities
		where lease_id = ? and action = ? and cache_id = ?
		returning storage_key`,
		leaseID, action, cacheID,
	).Scan(&storageKey)
	if err == sql.ErrNoRows {
		return "", false, nil
	}
	if err != nil {
		return "", false, err
	}
	return storageKey, true, nil
}

func (tx *EventBatchTx) QueueCacheObjectDeletion(ctx context.Context, storageKey string, now time.Time) error {
	_, err := tx.tx.ExecContext(ctx, `
		insert into cache_object_deletions (storage_key, created_at)
		values (?, ?)
		on conflict(storage_key) do nothing`,
		storageKey, now.UnixNano(),
	)
	return err
}

func (d *DB) PendingCacheObjectDeletions(ctx context.Context, limit int) ([]string, error) {
	rows, err := d.QueryContext(ctx, `
		select storage_key
		from cache_object_deletions
		order by created_at
		limit ?`, limit)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var keys []string
	for rows.Next() {
		var key string
		if err := rows.Scan(&key); err != nil {
			return nil, err
		}
		keys = append(keys, key)
	}
	return keys, rows.Err()
}

func (d *DB) CacheEntriesByStorageKey(ctx context.Context, key string) ([]CacheEntry, error) {
	return d.queryCacheEntries(ctx, `select `+cacheEntryColumns+` from cache_entries where storage_key = ?`, key)
}

func (d *DB) queryCacheEntries(ctx context.Context, query string, args ...any) ([]CacheEntry, error) {
	rows, err := d.QueryContext(ctx, query, args...)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var entries []CacheEntry
	for rows.Next() {
		entry, err := scanCacheEntry(rows)
		if err != nil {
			return nil, err
		}
		entries = append(entries, *entry)
	}
	return entries, rows.Err()
}

func (d *DB) CompleteCacheObjectDeletion(ctx context.Context, storageKey string) error {
	_, err := d.ExecContext(ctx, `delete from cache_object_deletions where storage_key = ?`, storageKey)
	return err
}

type cacheEntryScanner interface {
	Scan(dest ...any) error
}

func scanCacheEntry(row cacheEntryScanner) (*CacheEntry, error) {
	var entry CacheEntry
	var createdAt, lastUsedAt int64
	if err := row.Scan(
		&entry.ID,
		&entry.StorageKey,
		&entry.OwnerDID,
		&entry.RepoDID,
		&entry.Engine,
		&entry.CacheKey,
		&entry.CacheHash,
		&entry.Checksum,
		&entry.SizeBytes,
		&entry.RestoreCount,
		&entry.State,
		&createdAt,
		&lastUsedAt,
	); err != nil {
		return nil, err
	}
	entry.CreatedAt = time.Unix(0, createdAt)
	entry.LastUsedAt = time.Unix(0, lastUsedAt)
	return &entry, nil
}