diff --git a/domain/album.go b/domain/album.go index 322be2ac7..bef266f17 100644 --- a/domain/album.go +++ b/domain/album.go @@ -29,7 +29,7 @@ type AlbumRepository interface { Get(id string) (*Album, error) FindByArtist(artistId string) (*Albums, error) GetAll(QueryOptions) (*Albums, error) - PurgeInactive(active Albums) error + PurgeInactive(active Albums) ([]string, error) GetAllIds() (*[]string, error) GetStarred(QueryOptions) (*Albums, error) } diff --git a/domain/artist.go b/domain/artist.go index b6b184c56..9c4210bf0 100644 --- a/domain/artist.go +++ b/domain/artist.go @@ -10,7 +10,7 @@ type ArtistRepository interface { Put(m *Artist) error Get(id string) (*Artist, error) GetByName(name string) (*Artist, error) - PurgeInactive(active Artists) error + PurgeInactive(active Artists) ([]string, error) } type Artists []Artist diff --git a/domain/mediafile.go b/domain/mediafile.go index 72582223f..5a0a0e6a6 100644 --- a/domain/mediafile.go +++ b/domain/mediafile.go @@ -48,5 +48,5 @@ type MediaFileRepository interface { Put(m *MediaFile) error Get(id string) (*MediaFile, error) FindByAlbum(albumId string) (*MediaFiles, error) - PurgeInactive(active MediaFiles) error + PurgeInactive(active MediaFiles) ([]string, error) } diff --git a/domain/playlist.go b/domain/playlist.go index 2a8e49784..33bca9a3a 100644 --- a/domain/playlist.go +++ b/domain/playlist.go @@ -12,7 +12,7 @@ type PlaylistRepository interface { Put(m *Playlist) error Get(id string) (*Playlist, error) GetAll(options QueryOptions) (*Playlists, error) - PurgeInactive(active Playlists) error + PurgeInactive(active Playlists) ([]string, error) } type Playlists []Playlist diff --git a/engine/search.go b/engine/search.go index c9f63eea8..94132f6f2 100644 --- a/engine/search.go +++ b/engine/search.go @@ -17,6 +17,10 @@ type Search interface { IndexAlbum(al *domain.Album) error IndexMediaFile(mf *domain.MediaFile) error + RemoveArtist(ids []string) error + RemoveAlbum(ids []string) error + RemoveMediaFile(ids []string) error + SearchArtist(q string, offset int, size int) (Results, error) SearchAlbum(q string, offset int, size int) (Results, error) SearchSong(q string, offset int, size int) (Results, error) @@ -63,6 +67,18 @@ func (s search) IndexMediaFile(mf *domain.MediaFile) error { return s.idxSong.Index(mf.Id, sanitize.Accents(strings.ToLower(mf.Title))) } +func (s search) RemoveArtist(ids []string) error { + return s.idxArtist.Remove(ids...) +} + +func (s search) RemoveAlbum(ids []string) error { + return s.idxAlbum.Remove(ids...) +} + +func (s search) RemoveMediaFile(ids []string) error { + return s.idxSong.Remove(ids...) +} + func (s search) SearchArtist(q string, offset int, size int) (Results, error) { q = sanitize.Accents(strings.ToLower(strings.TrimSuffix(q, "*"))) min := offset diff --git a/persistence/album_repository.go b/persistence/album_repository.go index e7fd8b2ba..dded4b361 100644 --- a/persistence/album_repository.go +++ b/persistence/album_repository.go @@ -57,7 +57,7 @@ func (r *albumRepository) GetAllIds() (*[]string, error) { return &ids, nil } -func (r *albumRepository) PurgeInactive(active domain.Albums) error { +func (r *albumRepository) PurgeInactive(active domain.Albums) ([]string, error) { return r.purgeInactive(active, func(e interface{}) string { return e.(domain.Album).Id }) diff --git a/persistence/artist_repository.go b/persistence/artist_repository.go index 0023227ca..a7c9c2d31 100644 --- a/persistence/artist_repository.go +++ b/persistence/artist_repository.go @@ -34,7 +34,7 @@ func (r *artistRepository) GetByName(name string) (*domain.Artist, error) { return r.Get(id) } -func (r *artistRepository) PurgeInactive(active domain.Artists) error { +func (r *artistRepository) PurgeInactive(active domain.Artists) ([]string, error) { return r.purgeInactive(active, func(e interface{}) string { return e.(domain.Artist).Id }) diff --git a/persistence/ledis_repository.go b/persistence/ledis_repository.go index 8fa3ddc8a..7bb887d8f 100644 --- a/persistence/ledis_repository.go +++ b/persistence/ledis_repository.go @@ -78,32 +78,33 @@ func (r *ledisRepository) getAllIds() (map[string]bool, error) { type getIdFunc func(e interface{}) string -func (r *ledisRepository) purgeInactive(activeList interface{}, getId getIdFunc) error { +func (r *ledisRepository) purgeInactive(activeList interface{}, getId getIdFunc) ([]string, error) { currentIds, err := r.getAllIds() if err != nil { - return err + return nil, err } reflected := reflect.ValueOf(activeList).Elem() - for i := 0; i < reflected.Len(); i++ { + totalActive := reflected.Len() + for i := 0; i < totalActive; i++ { a := reflected.Index(i) id := getId(a.Interface()) currentIds[id] = false } - inactiveIds := make(map[string]bool) + inactiveIds := make([]string, 0, len(currentIds)-totalActive) for id, inactive := range currentIds { if inactive { - inactiveIds[id] = true + inactiveIds = append(inactiveIds, id) } } - return r.removeAll(inactiveIds) + return inactiveIds, r.removeAll(inactiveIds) } -func (r *ledisRepository) removeAll(ids map[string]bool) error { +func (r *ledisRepository) removeAll(ids []string) error { allKey := r.table + "s:all" keys := make([][]byte, len(ids)) i := 0 - for id := range ids { + for _, id := range ids { // Delete from parent:parentId:table (ZSet) if r.parentTable != "" { parentKey := []byte(fmt.Sprintf("%s:%s:%s", r.table, id, r.parentIdField)) diff --git a/persistence/ledis_repository_test.go b/persistence/ledis_repository_test.go index 029141542..ab102af05 100644 --- a/persistence/ledis_repository_test.go +++ b/persistence/ledis_repository_test.go @@ -231,8 +231,7 @@ func TestBaseRepository(t *testing.T) { }) Convey("When I call DeletaAll with one of the entities", func() { - ids := make(map[string]bool) - ids["1"] = true + ids := []string{"1"} err := repo.removeAll(ids) Convey("Then It should not return any error", func() { So(err, ShouldBeNil) diff --git a/persistence/mediafile_repository.go b/persistence/mediafile_repository.go index 265ee048d..397473026 100644 --- a/persistence/mediafile_repository.go +++ b/persistence/mediafile_repository.go @@ -43,7 +43,7 @@ func (r *mediaFileRepository) FindByAlbum(albumId string) (*domain.MediaFiles, e return &mfs, err } -func (r *mediaFileRepository) PurgeInactive(active domain.MediaFiles) error { +func (r *mediaFileRepository) PurgeInactive(active domain.MediaFiles) ([]string, error) { return r.purgeInactive(active, func(e interface{}) string { return e.(domain.MediaFile).Id }) diff --git a/persistence/playlist_repository.go b/persistence/playlist_repository.go index cad5556de..ef9efd4c3 100644 --- a/persistence/playlist_repository.go +++ b/persistence/playlist_repository.go @@ -35,7 +35,7 @@ func (r *playlistRepository) GetAll(options domain.QueryOptions) (*domain.Playli return &as, err } -func (r *playlistRepository) PurgeInactive(active domain.Playlists) error { +func (r *playlistRepository) PurgeInactive(active domain.Playlists) ([]string, error) { return r.purgeInactive(active, func(e interface{}) string { return e.(domain.Playlist).Id }) diff --git a/scanner/importer.go b/scanner/importer.go index da0f9c3e6..159bcf3b7 100644 --- a/scanner/importer.go +++ b/scanner/importer.go @@ -211,16 +211,22 @@ func (i *Importer) importLibrary() (err error) { } beego.Debug("Purging old data") - if err := i.mfRepo.PurgeInactive(mfs); err != nil { + if deleted, err := i.mfRepo.PurgeInactive(mfs); err != nil { beego.Error(err) + } else { + i.search.RemoveMediaFile(deleted) } - if err := i.albumRepo.PurgeInactive(als); err != nil { + if deleted, err := i.albumRepo.PurgeInactive(als); err != nil { beego.Error(err) + } else { + i.search.RemoveAlbum(deleted) } - if err := i.artistRepo.PurgeInactive(ars); err != nil { + if deleted, err := i.artistRepo.PurgeInactive(ars); err != nil { beego.Error(err) + } else { + i.search.RemoveArtist(deleted) } - if err := i.plsRepo.PurgeInactive(pls); err != nil { + if _, err := i.plsRepo.PurgeInactive(pls); err != nil { beego.Error(err) }