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
+1 -1
View File
@@ -29,7 +29,7 @@ type AlbumRepository interface {
Get(id string) (*Album, error) Get(id string) (*Album, error)
FindByArtist(artistId string) (*Albums, error) FindByArtist(artistId string) (*Albums, error)
GetAll(QueryOptions) (*Albums, error) GetAll(QueryOptions) (*Albums, error)
PurgeInactive(active Albums) error PurgeInactive(active Albums) ([]string, error)
GetAllIds() (*[]string, error) GetAllIds() (*[]string, error)
GetStarred(QueryOptions) (*Albums, error) GetStarred(QueryOptions) (*Albums, error)
} }
+1 -1
View File
@@ -10,7 +10,7 @@ type ArtistRepository interface {
Put(m *Artist) error Put(m *Artist) error
Get(id string) (*Artist, error) Get(id string) (*Artist, error)
GetByName(name string) (*Artist, error) GetByName(name string) (*Artist, error)
PurgeInactive(active Artists) error PurgeInactive(active Artists) ([]string, error)
} }
type Artists []Artist type Artists []Artist
+1 -1
View File
@@ -48,5 +48,5 @@ type MediaFileRepository interface {
Put(m *MediaFile) error Put(m *MediaFile) error
Get(id string) (*MediaFile, error) Get(id string) (*MediaFile, error)
FindByAlbum(albumId string) (*MediaFiles, error) FindByAlbum(albumId string) (*MediaFiles, error)
PurgeInactive(active MediaFiles) error PurgeInactive(active MediaFiles) ([]string, error)
} }
+1 -1
View File
@@ -12,7 +12,7 @@ type PlaylistRepository interface {
Put(m *Playlist) error Put(m *Playlist) error
Get(id string) (*Playlist, error) Get(id string) (*Playlist, error)
GetAll(options QueryOptions) (*Playlists, error) GetAll(options QueryOptions) (*Playlists, error)
PurgeInactive(active Playlists) error PurgeInactive(active Playlists) ([]string, error)
} }
type Playlists []Playlist type Playlists []Playlist
+16
View File
@@ -17,6 +17,10 @@ type Search interface {
IndexAlbum(al *domain.Album) error IndexAlbum(al *domain.Album) error
IndexMediaFile(mf *domain.MediaFile) 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) SearchArtist(q string, offset int, size int) (Results, error)
SearchAlbum(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) 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))) 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) { func (s search) SearchArtist(q string, offset int, size int) (Results, error) {
q = sanitize.Accents(strings.ToLower(strings.TrimSuffix(q, "*"))) q = sanitize.Accents(strings.ToLower(strings.TrimSuffix(q, "*")))
min := offset min := offset
+1 -1
View File
@@ -57,7 +57,7 @@ func (r *albumRepository) GetAllIds() (*[]string, error) {
return &ids, nil 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 r.purgeInactive(active, func(e interface{}) string {
return e.(domain.Album).Id return e.(domain.Album).Id
}) })
+1 -1
View File
@@ -34,7 +34,7 @@ func (r *artistRepository) GetByName(name string) (*domain.Artist, error) {
return r.Get(id) 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 r.purgeInactive(active, func(e interface{}) string {
return e.(domain.Artist).Id return e.(domain.Artist).Id
}) })
+9 -8
View File
@@ -78,32 +78,33 @@ func (r *ledisRepository) getAllIds() (map[string]bool, error) {
type getIdFunc func(e interface{}) string 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() currentIds, err := r.getAllIds()
if err != nil { if err != nil {
return err return nil, err
} }
reflected := reflect.ValueOf(activeList).Elem() 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) a := reflected.Index(i)
id := getId(a.Interface()) id := getId(a.Interface())
currentIds[id] = false currentIds[id] = false
} }
inactiveIds := make(map[string]bool) inactiveIds := make([]string, 0, len(currentIds)-totalActive)
for id, inactive := range currentIds { for id, inactive := range currentIds {
if inactive { 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" allKey := r.table + "s:all"
keys := make([][]byte, len(ids)) keys := make([][]byte, len(ids))
i := 0 i := 0
for id := range ids { for _, id := range ids {
// Delete from parent:parentId:table (ZSet) // Delete from parent:parentId:table (ZSet)
if r.parentTable != "" { if r.parentTable != "" {
parentKey := []byte(fmt.Sprintf("%s:%s:%s", r.table, id, r.parentIdField)) parentKey := []byte(fmt.Sprintf("%s:%s:%s", r.table, id, r.parentIdField))
+1 -2
View File
@@ -231,8 +231,7 @@ func TestBaseRepository(t *testing.T) {
}) })
Convey("When I call DeletaAll with one of the entities", func() { Convey("When I call DeletaAll with one of the entities", func() {
ids := make(map[string]bool) ids := []string{"1"}
ids["1"] = true
err := repo.removeAll(ids) err := repo.removeAll(ids)
Convey("Then It should not return any error", func() { Convey("Then It should not return any error", func() {
So(err, ShouldBeNil) So(err, ShouldBeNil)
+1 -1
View File
@@ -43,7 +43,7 @@ func (r *mediaFileRepository) FindByAlbum(albumId string) (*domain.MediaFiles, e
return &mfs, err 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 r.purgeInactive(active, func(e interface{}) string {
return e.(domain.MediaFile).Id return e.(domain.MediaFile).Id
}) })
+1 -1
View File
@@ -35,7 +35,7 @@ func (r *playlistRepository) GetAll(options domain.QueryOptions) (*domain.Playli
return &as, err 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 r.purgeInactive(active, func(e interface{}) string {
return e.(domain.Playlist).Id return e.(domain.Playlist).Id
}) })
+10 -4
View File
@@ -211,16 +211,22 @@ func (i *Importer) importLibrary() (err error) {
} }
beego.Debug("Purging old data") 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) 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) 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) 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) beego.Error(err)
} }