navidrome/scanner2/scan_context.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

47 lines
929 B
Go
Raw Normal View History

2023-12-16 23:08:21 +01:00
package scanner2
import (
2023-12-20 03:31:04 +01:00
"context"
"fmt"
"sync"
"sync/atomic"
2023-12-16 23:08:21 +01:00
"time"
"github.com/navidrome/navidrome/model"
)
type scanContext struct {
2023-12-20 03:31:04 +01:00
lib model.Library
ds model.DataStore
startTime time.Time
lastUpdates map[string]time.Time
lock sync.RWMutex
2023-12-23 02:13:11 +01:00
fullRescan bool
numFolders atomic.Int64
2023-12-16 23:08:21 +01:00
}
2023-12-23 02:13:11 +01:00
func newScannerContext(ctx context.Context, ds model.DataStore, lib model.Library, fullRescan bool) (*scanContext, error) {
2023-12-20 03:31:04 +01:00
lastUpdates, err := ds.Folder(ctx).GetLastUpdates(lib)
if err != nil {
return nil, fmt.Errorf("error getting last updates: %w", err)
}
2023-12-16 23:08:21 +01:00
return &scanContext{
2023-12-20 03:31:04 +01:00
lib: lib,
ds: ds,
startTime: time.Now(),
lastUpdates: lastUpdates,
2023-12-23 02:13:11 +01:00
fullRescan: fullRescan,
2023-12-20 03:31:04 +01:00
}, nil
}
func (s *scanContext) getLastUpdatedInDB(id string) time.Time {
s.lock.RLock()
defer s.lock.RUnlock()
t, ok := s.lastUpdates[id]
if !ok {
return time.Time{}
2023-12-16 23:08:21 +01:00
}
2023-12-20 03:31:04 +01:00
return t
2023-12-16 23:08:21 +01:00
}