Files
RenewCMS/internal/infrastructure/storage/readCountingStorage.go
T

43 lines
962 B
Go

package storage
import (
domain "RenewCMS/internal/domain/image"
"sync/atomic"
)
type ReadCountingStorage struct {
domain.Storage
opened atomic.Int64
}
var _ domain.Storage = &ReadCountingStorage{}
func NewReadCountingStorage(inner domain.Storage) *ReadCountingStorage {
return &ReadCountingStorage{Storage: inner}
}
func (s *ReadCountingStorage) Open(storedName string) (domain.Blob, error) {
blob, err := s.Storage.Open(storedName)
if err == nil {
s.opened.Add(1)
}
return blob, err
}
func (s *ReadCountingStorage) Opened() int {
return int(s.opened.Load())
}
type Logger interface {
Printf(format string, args ...any)
}
func RunBackfillReportingReads(inner domain.Storage, logger Logger, run func(domain.Storage) error) error {
counting := NewReadCountingStorage(inner)
err := run(counting)
if opened := counting.Opened(); opened > 0 {
logger.Printf("image metadata backfill read %d images from the bucket", opened)
}
return err
}