Removing purged ids from the search index

This commit is contained in:
Deluan 2016-03-18 19:50:21 -04:00
parent 3790aa45e4
commit 508bf7152f
12 changed files with 44 additions and 22 deletions

View File

@ -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)
}

View File

@ -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

View File

@ -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)
}

View File

@ -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

View File

@ -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

View File

@ -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
})

View File

@ -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
})

View File

@ -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))

View File

@ -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)

View File

@ -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
})

View File

@ -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
})

View File

@ -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)
}