diff --git a/server/ctrladmin/handlers.go b/server/ctrladmin/handlers.go index b7d5c28..999dc53 100644 --- a/server/ctrladmin/handlers.go +++ b/server/ctrladmin/handlers.go @@ -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"}, diff --git a/server/ctrlsubsonic/handlers_common.go b/server/ctrlsubsonic/handlers_common.go index 8332122..0fbb056 100644 --- a/server/ctrlsubsonic/handlers_common.go +++ b/server/ctrlsubsonic/handlers_common.go @@ -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) } }() diff --git a/server/scanner/scanner.go b/server/scanner/scanner.go index 0019737..5bc897f 100644 --- a/server/scanner/scanner.go +++ b/server/scanner/scanner.go @@ -74,11 +74,8 @@ type Scanner struct { 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{}, + db: db, + musicPath: musicPath, } } @@ -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() { - s.seenTracks = map[int]struct{}{} - s.seenFolders = map[int]struct{}{} - s.curFolders = &stack.Stack{} - s.seenTracksNew = 0 - s.seenTracksErr = 0 - }() + // 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 diff --git a/server/scanner/scanner_test.go b/server/scanner/scanner_test.go index 86250b7..aee0e06 100644 --- a/server/scanner/scanner_test.go +++ b/server/scanner/scanner_test.go @@ -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{}) } } diff --git a/server/server.go b/server/server.go index d66b41d..861dcf3 100644 --- a/server/server.go +++ b/server/server.go @@ -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) } }