Skip to content

🎧 Your Personal Streaming Service

Refactored PurgeInactive as a "generic" function.

Deluan3790aa45e4f5
diff --git a/domain/album.go b/domain/album.go
index 1097baebc..322be2ac7 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) error
GetAllIds() (*[]string, error)
GetStarred(QueryOptions) (*Albums, error)
}
diff --git a/domain/artist.go b/domain/artist.go
index e4a03d8c8..b6b184c56 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) error
}
type Artists []Artist
diff --git a/domain/mediafile.go b/domain/mediafile.go
index 935f55d80..72582223f 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) error
}
diff --git a/domain/playlist.go b/domain/playlist.go
index c2c656cd4..2a8e49784 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) error
}
type Playlists []Playlist
diff --git a/persistence/album_repository.go b/persistence/album_repository.go
index def92fa1b..e7fd8b2ba 100644
--- a/persistence/album_repository.go
+++ b/persistence/album_repository.go
@@ -57,21 +57,10 @@ func (r *albumRepository) GetAllIds() (*[]string, error) {
return &ids, nil
}
-func (r *albumRepository) PurgeInactive(active *domain.Albums) error {
- currentIds, err := r.getAllIds()
- if err != nil {
- return err
- }
- for _, a := range *active {
- currentIds[a.Id] = false
- }
- inactiveIds := make(map[string]bool)
- for id, inactive := range currentIds {
- if inactive {
- inactiveIds[id] = true
- }
- }
- return r.DeleteAll(inactiveIds)
+func (r *albumRepository) PurgeInactive(active domain.Albums) error {
+ return r.purgeInactive(active, func(e interface{}) string {
+ return e.(domain.Album).Id
+ })
}
func (r *albumRepository) GetStarred(options domain.QueryOptions) (*domain.Albums, error) {
diff --git a/persistence/artist_repository.go b/persistence/artist_repository.go
index f91054ed2..0023227ca 100644
--- a/persistence/artist_repository.go
+++ b/persistence/artist_repository.go
@@ -34,21 +34,10 @@ func (r *artistRepository) GetByName(name string) (*domain.Artist, error) {
return r.Get(id)
}
-func (r *artistRepository) PurgeInactive(active *domain.Artists) error {
- currentIds, err := r.getAllIds()
- if err != nil {
- return err
- }
- for _, a := range *active {
- currentIds[a.Id] = false
- }
- inactiveIds := make(map[string]bool)
- for id, inactive := range currentIds {
- if inactive {
- inactiveIds[id] = true
- }
- }
- return r.DeleteAll(inactiveIds)
+func (r *artistRepository) PurgeInactive(active domain.Artists) error {
+ return r.purgeInactive(active, func(e interface{}) string {
+ return e.(domain.Artist).Id
+ })
}
var _ domain.ArtistRepository = (*artistRepository)(nil)
diff --git a/persistence/ledis_repository.go b/persistence/ledis_repository.go
index 36d9f09c5..8fa3ddc8a 100644
--- a/persistence/ledis_repository.go
+++ b/persistence/ledis_repository.go
@@ -76,7 +76,29 @@ func (r *ledisRepository) getAllIds() (map[string]bool, error) {
return m, err
}
-func (r *ledisRepository) DeleteAll(ids map[string]bool) error {
+type getIdFunc func(e interface{}) string
+
+func (r *ledisRepository) purgeInactive(activeList interface{}, getId getIdFunc) error {
+ currentIds, err := r.getAllIds()
+ if err != nil {
+ return err
+ }
+ reflected := reflect.ValueOf(activeList).Elem()
+ for i := 0; i < reflected.Len(); i++ {
+ a := reflected.Index(i)
+ id := getId(a.Interface())
+ currentIds[id] = false
+ }
+ inactiveIds := make(map[string]bool)
+ for id, inactive := range currentIds {
+ if inactive {
+ inactiveIds[id] = true
+ }
+ }
+ return r.removeAll(inactiveIds)
+}
+
+func (r *ledisRepository) removeAll(ids map[string]bool) error {
allKey := r.table + "s:all"
keys := make([][]byte, len(ids))
@@ -99,6 +121,14 @@ func (r *ledisRepository) DeleteAll(ids map[string]bool) error {
}
}
+ // Delete table:idx:* (ZSet)
+ for idx := range r.indexes {
+ idxName := fmt.Sprintf("%s:idx:%s", r.table, idx)
+ if _, err := Db().ZRem([]byte(idxName), []byte(id)); err != nil {
+ return err
+ }
+ }
+
// Delete record table:id:* (KV)
if err := r.deleteRecord(id); err != nil {
return err
@@ -144,9 +174,6 @@ func (r *ledisRepository) saveOrUpdate(id string, entity interface{}) error {
for idx, fn := range r.indexes {
idxName := fmt.Sprintf("%s:idx:%s", r.table, idx)
- if _, err := Db().ZRem([]byte(idxName), []byte(id)); err != nil {
- return err
- }
score := calcScore(entity, fn)
sidx := ledis.ScorePair{score, []byte(id)}
if _, err = Db().ZAdd([]byte(idxName), sidx); err != nil {
diff --git a/persistence/ledis_repository_test.go b/persistence/ledis_repository_test.go
index 2f4b7b47a..029141542 100644
--- a/persistence/ledis_repository_test.go
+++ b/persistence/ledis_repository_test.go
@@ -233,7 +233,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
- err := repo.DeleteAll(ids)
+ 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 4060a6cf2..265ee048d 100644
--- a/persistence/mediafile_repository.go
+++ b/persistence/mediafile_repository.go
@@ -43,21 +43,10 @@ func (r *mediaFileRepository) FindByAlbum(albumId string) (*domain.MediaFiles, e
return &mfs, err
}
-func (r *mediaFileRepository) PurgeInactive(active *domain.MediaFiles) error {
- currentIds, err := r.getAllIds()
- if err != nil {
- return err
- }
- for _, a := range *active {
- currentIds[a.Id] = false
- }
- inactiveIds := make(map[string]bool)
- for id, inactive := range currentIds {
- if inactive {
- inactiveIds[id] = true
- }
- }
- return r.DeleteAll(inactiveIds)
+func (r *mediaFileRepository) PurgeInactive(active domain.MediaFiles) error {
+ return r.purgeInactive(active, func(e interface{}) string {
+ return e.(domain.MediaFile).Id
+ })
}
var _ domain.MediaFileRepository = (*mediaFileRepository)(nil)
diff --git a/persistence/playlist_repository.go b/persistence/playlist_repository.go
index 0617c8581..cad5556de 100644
--- a/persistence/playlist_repository.go
+++ b/persistence/playlist_repository.go
@@ -35,21 +35,10 @@ func (r *playlistRepository) GetAll(options domain.QueryOptions) (*domain.Playli
return &as, err
}
-func (r *playlistRepository) PurgeInactive(active *domain.Playlists) error {
- currentIds, err := r.getAllIds()
- if err != nil {
- return err
- }
- for _, a := range *active {
- currentIds[a.Id] = false
- }
- inactiveIds := make(map[string]bool)
- for id, inactive := range currentIds {
- if inactive {
- inactiveIds[id] = true
- }
- }
- return r.DeleteAll(inactiveIds)
+func (r *playlistRepository) PurgeInactive(active domain.Playlists) error {
+ return r.purgeInactive(active, func(e interface{}) string {
+ return e.(domain.Playlist).Id
+ })
}
var _ domain.PlaylistRepository = (*playlistRepository)(nil)
diff --git a/scanner/importer.go b/scanner/importer.go
index d7b2a0558..da0f9c3e6 100644
--- a/scanner/importer.go
+++ b/scanner/importer.go
@@ -2,12 +2,11 @@ package scanner
import (
"fmt"
+ "os"
"strconv"
"strings"
"time"
- "os"
-
"github.com/astaxie/beego"
"github.com/deluan/gosonic/consts"
"github.com/deluan/gosonic/domain"
@@ -212,16 +211,16 @@ func (i *Importer) importLibrary() (err error) {
}
beego.Debug("Purging old data")
- if err := i.mfRepo.PurgeInactive(&mfs); err != nil {
+ if err := i.mfRepo.PurgeInactive(mfs); err != nil {
beego.Error(err)
}
- if err := i.albumRepo.PurgeInactive(&als); err != nil {
+ if err := i.albumRepo.PurgeInactive(als); err != nil {
beego.Error(err)
}
- if err := i.artistRepo.PurgeInactive(&ars); err != nil {
+ if err := i.artistRepo.PurgeInactive(ars); err != nil {
beego.Error(err)
}
- if err := i.plsRepo.PurgeInactive(&pls); err != nil {
+ if err := i.plsRepo.PurgeInactive(pls); err != nil {
beego.Error(err)
}