mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-23 15:20:33 +03:00
Refactored PurgeInactive as a "generic" function.
Also delete indexes when removing records
This commit is contained in:
parent
cac352b18c
commit
3790aa45e4
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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) {
|
||||
|
@ -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)
|
||||
|
@ -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 {
|
||||
|
@ -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)
|
||||
})
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user