scanner: add scanoptions struct
This commit is contained in:
@@ -22,9 +22,9 @@ func firstExisting(or string, strings ...string) string {
|
||||
return or
|
||||
}
|
||||
|
||||
func doScan(scanFunc func() error) {
|
||||
func doScan(scanner *scanner.Scanner, opts scanner.ScanOptions) {
|
||||
go func() {
|
||||
if err := scanFunc(); err != nil {
|
||||
if err := scanner.Start(opts); err != nil {
|
||||
log.Printf("error while scanning: %v\n", err)
|
||||
}
|
||||
}()
|
||||
@@ -278,7 +278,7 @@ func (c *Controller) ServeUpdateLastFMAPIKeyDo(r *http.Request) *Response {
|
||||
}
|
||||
|
||||
func (c *Controller) ServeStartScanIncDo(r *http.Request) *Response {
|
||||
defer doScan(c.Scanner.StartInc)
|
||||
defer doScan(c.Scanner, scanner.ScanOptions{})
|
||||
return &Response{
|
||||
redirect: "/admin/home",
|
||||
flashN: []string{"incremental scan started. refresh for results"},
|
||||
@@ -286,7 +286,7 @@ func (c *Controller) ServeStartScanIncDo(r *http.Request) *Response {
|
||||
}
|
||||
|
||||
func (c *Controller) ServeStartScanFullDo(r *http.Request) *Response {
|
||||
defer doScan(c.Scanner.StartFull)
|
||||
defer doScan(c.Scanner, scanner.ScanOptions{IsFull: true})
|
||||
return &Response{
|
||||
redirect: "/admin/home",
|
||||
flashN: []string{"full scan started. refresh for results"},
|
||||
|
||||
@@ -86,7 +86,7 @@ func (c *Controller) ServeGetMusicFolders(r *http.Request) *spec.Response {
|
||||
|
||||
func (c *Controller) ServeStartScan(r *http.Request) *spec.Response {
|
||||
go func() {
|
||||
if err := c.Scanner.StartInc(); err != nil {
|
||||
if err := c.Scanner.Start(scanner.ScanOptions{}); err != nil {
|
||||
log.Printf("error while scanning: %v\n", err)
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -76,9 +76,6 @@ func New(musicPath string, db *db.DB) *Scanner {
|
||||
return &Scanner{
|
||||
db: db,
|
||||
musicPath: musicPath,
|
||||
seenTracks: map[int]struct{}{},
|
||||
seenFolders: map[int]struct{}{},
|
||||
curFolders: &stack.Stack{},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,22 +126,25 @@ func (s *Scanner) cleanArtists() (int, error) {
|
||||
// ## begin entries
|
||||
// ## begin entries
|
||||
|
||||
func (s *Scanner) Start(isFull bool) error {
|
||||
type ScanOptions struct {
|
||||
IsFull bool
|
||||
Path string // TODO
|
||||
}
|
||||
|
||||
func (s *Scanner) Start(opts ScanOptions) error {
|
||||
if IsScanning() {
|
||||
return errors.New("already scanning")
|
||||
}
|
||||
unSet := SetScanning()
|
||||
defer unSet()
|
||||
// reset tracking variables when finished
|
||||
defer func() {
|
||||
// reset state vars for the new scan
|
||||
s.isFull = opts.IsFull
|
||||
s.seenTracks = map[int]struct{}{}
|
||||
s.seenFolders = map[int]struct{}{}
|
||||
s.curFolders = &stack.Stack{}
|
||||
s.seenTracksNew = 0
|
||||
s.seenTracksErr = 0
|
||||
}()
|
||||
// ** begin being walking
|
||||
s.isFull = isFull
|
||||
start := time.Now()
|
||||
err := godirwalk.Walk(s.musicPath, &godirwalk.Options{
|
||||
Callback: s.callbackItem,
|
||||
@@ -182,14 +182,6 @@ func (s *Scanner) Start(isFull bool) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Scanner) StartInc() error {
|
||||
return s.Start(false)
|
||||
}
|
||||
|
||||
func (s *Scanner) StartFull() error {
|
||||
return s.Start(true)
|
||||
}
|
||||
|
||||
// items are passed to the handle*() functions
|
||||
type item struct {
|
||||
fullPath string
|
||||
|
||||
@@ -40,17 +40,17 @@ func resetTablesPause(db *db.DB, b *testing.B) {
|
||||
func BenchmarkScanFresh(b *testing.B) {
|
||||
for n := 0; n < b.N; n++ {
|
||||
resetTablesPause(testScanner.db, b)
|
||||
testScanner.StartInc()
|
||||
testScanner.Start(ScanOptions{})
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkScanIncremental(b *testing.B) {
|
||||
// do a full scan and reset
|
||||
testScanner.StartInc()
|
||||
testScanner.Start(ScanOptions{})
|
||||
b.ResetTimer()
|
||||
// do the inc scans
|
||||
for n := 0; n < b.N; n++ {
|
||||
testScanner.StartInc()
|
||||
testScanner.Start(ScanOptions{})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -207,7 +207,7 @@ func (s *Server) StartScanTicker(dur time.Duration) (funcExecute, funcInterrupt)
|
||||
case <-done:
|
||||
return nil
|
||||
case <-ticker.C:
|
||||
if err := s.scanner.StartInc(); err != nil {
|
||||
if err := s.scanner.Start(scanner.ScanOptions{}); err != nil {
|
||||
log.Printf("error scanning: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user