speed up scanning and delete folders
This commit is contained in:
@@ -41,6 +41,7 @@ type Scanner struct {
|
|||||||
db, tx *gorm.DB
|
db, tx *gorm.DB
|
||||||
musicPath string
|
musicPath string
|
||||||
seenTracks map[int]struct{}
|
seenTracks map[int]struct{}
|
||||||
|
seenFolders map[int]struct{}
|
||||||
seenTracksNew int
|
seenTracksNew int
|
||||||
seenTracksErr int
|
seenTracksErr int
|
||||||
curFolders folderStack
|
curFolders folderStack
|
||||||
@@ -49,10 +50,11 @@ type Scanner struct {
|
|||||||
|
|
||||||
func New(db *gorm.DB, musicPath string) *Scanner {
|
func New(db *gorm.DB, musicPath string) *Scanner {
|
||||||
return &Scanner{
|
return &Scanner{
|
||||||
db: db,
|
db: db,
|
||||||
musicPath: musicPath,
|
musicPath: musicPath,
|
||||||
seenTracks: make(map[int]struct{}),
|
seenTracks: make(map[int]struct{}),
|
||||||
curFolders: make(folderStack, 0),
|
seenFolders: make(map[int]struct{}),
|
||||||
|
curFolders: make(folderStack, 0),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,20 +135,30 @@ func (s *Scanner) Start() error {
|
|||||||
deleted++
|
deleted++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// delete folders not on filesystem
|
||||||
|
var folders []*model.Album
|
||||||
|
err = s.tx.
|
||||||
|
Select("id").
|
||||||
|
Find(&folders).
|
||||||
|
Error
|
||||||
|
for _, folder := range folders {
|
||||||
|
_, ok := s.seenFolders[folder.ID]
|
||||||
|
if !ok {
|
||||||
|
s.tx.Delete(folder)
|
||||||
|
}
|
||||||
|
}
|
||||||
// then, delete albums without tracks
|
// then, delete albums without tracks
|
||||||
s.tx.Exec(`
|
s.tx.Exec(`
|
||||||
DELETE FROM albums
|
DELETE FROM albums
|
||||||
WHERE tag_artist_id NOT NULL AND
|
WHERE tag_artist_id NOT NULL
|
||||||
(SELECT count(id)
|
AND NOT EXISTS (SELECT 1 FROM tracks
|
||||||
FROM tracks
|
WHERE tracks.album_id = albums.id)
|
||||||
WHERE album_id = albums.id) = 0;
|
|
||||||
`)
|
`)
|
||||||
// then, delete artists without albums
|
// then, delete artists without albums
|
||||||
s.tx.Exec(`
|
s.tx.Exec(`
|
||||||
DELETE FROM artists
|
DELETE FROM artists
|
||||||
WHERE (SELECT count(id)
|
WHERE NOT EXISTS (SELECT 1 from albums
|
||||||
FROM albums
|
WHERE albums.tag_artist_id = artists.id)
|
||||||
WHERE tag_artist_id = artists.id) = 0;
|
|
||||||
`)
|
`)
|
||||||
log.Printf("finished clean in %s, -%d tracks\n",
|
log.Printf("finished clean in %s, -%d tracks\n",
|
||||||
time.Since(start),
|
time.Since(start),
|
||||||
@@ -210,7 +222,12 @@ func (s *Scanner) callbackPost(fullPath string, info *godirwalk.Dirent) error {
|
|||||||
|
|
||||||
func (s *Scanner) handleFolder(it *item) error {
|
func (s *Scanner) handleFolder(it *item) error {
|
||||||
folder := &model.Album{}
|
folder := &model.Album{}
|
||||||
defer s.curFolders.Push(folder)
|
defer func() {
|
||||||
|
// folder's id will come from early return
|
||||||
|
// or save at the end
|
||||||
|
s.seenFolders[folder.ID] = struct{}{}
|
||||||
|
s.curFolders.Push(folder)
|
||||||
|
}()
|
||||||
err := s.tx.
|
err := s.tx.
|
||||||
Where(model.Album{
|
Where(model.Album{
|
||||||
LeftPath: it.directory,
|
LeftPath: it.directory,
|
||||||
|
|||||||
Reference in New Issue
Block a user