Using sorted sets

This commit is contained in:
Deluan
2016-03-05 19:40:36 -05:00
parent 3db18d8bd6
commit d2701ea67c
3 changed files with 13 additions and 11 deletions
+1 -1
View File
@@ -97,7 +97,7 @@ func (c *GetMusicDirectoryController) buildAlbumDir(al *domain.Album, tracks []d
func (c *GetMusicDirectoryController) isArtist(id string) bool { func (c *GetMusicDirectoryController) isArtist(id string) bool {
found, err := c.artistRepo.Exists(id) found, err := c.artistRepo.Exists(id)
if err != nil { if err != nil {
beego.Error("Error searching for Artist:", err) beego.Error("Error searching for Artist", id, " - ", err)
c.SendError(responses.ERROR_GENERIC, "Internal Error") c.SendError(responses.ERROR_GENERIC, "Internal Error")
} }
return found return found
+1 -1
View File
@@ -2,7 +2,7 @@ package domain
type BaseRepository interface { type BaseRepository interface {
NewId(fields ...string) string NewId(fields ...string) string
CountAll() (int, error) CountAll() (int64, error)
Exists(id string) (bool, error) Exists(id string) (bool, error)
} }
+11 -9
View File
@@ -4,10 +4,11 @@ import (
"crypto/md5" "crypto/md5"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/deluan/gosonic/domain"
"github.com/deluan/gosonic/utils" "github.com/deluan/gosonic/utils"
"github.com/siddontang/ledisdb/ledis"
"reflect" "reflect"
"strings" "strings"
"github.com/deluan/gosonic/domain"
) )
type ledisRepository struct { type ledisRepository struct {
@@ -35,14 +36,14 @@ func (r *ledisRepository) NewId(fields ...string) string {
return fmt.Sprintf("%x", md5.Sum([]byte(s))) return fmt.Sprintf("%x", md5.Sum([]byte(s)))
} }
func (r *ledisRepository) CountAll() (int, error) { func (r *ledisRepository) CountAll() (int64, error) {
ids, err := db().SMembers([]byte(r.table + "s:all")) size, err := db().ZCard([]byte(r.table + "s:all"))
return len(ids), err return size, err
} }
func (r *ledisRepository) Exists(id string) (bool, error) { func (r *ledisRepository) Exists(id string) (bool, error) {
res, err := db().SIsMember([]byte(r.table+"s:all"), []byte(id)) res, _ := db().ZScore([]byte(r.table+"s:all"), []byte(id))
return res != 0, err return res != ledis.InvalidScore, nil
} }
func (r *ledisRepository) saveOrUpdate(id string, entity interface{}) error { func (r *ledisRepository) saveOrUpdate(id string, entity interface{}) error {
@@ -62,13 +63,14 @@ func (r *ledisRepository) saveOrUpdate(id string, entity interface{}) error {
} }
if _, err = db().SAdd([]byte(allKey), []byte(id)); err != nil { sid := ledis.ScorePair{0, []byte(id)}
if _, err = db().ZAdd([]byte(allKey), sid); err != nil {
return err return err
} }
if parentTable, parentId := r.getParent(entity); parentTable != "" { if parentTable, parentId := r.getParent(entity); parentTable != "" {
parentCollectionKey := fmt.Sprintf("%s:%s:%ss", parentTable, parentId, r.table) parentCollectionKey := fmt.Sprintf("%s:%s:%ss", parentTable, parentId, r.table)
_, err = db().SAdd([]byte(parentCollectionKey), []byte(id)) _, err = db().ZAdd([]byte(parentCollectionKey), sid)
} }
return nil return nil
} }
@@ -150,7 +152,7 @@ func (r *ledisRepository) loadFromSet(setName string, entities interface{}, qo .
if o.SortBy != "" { if o.SortBy != "" {
sortKey = []byte(fmt.Sprintf("%s:*:%s", r.table, o.SortBy)) sortKey = []byte(fmt.Sprintf("%s:*:%s", r.table, o.SortBy))
} }
response, err := db().XSSort([]byte(setName), o.Offset, o.Size, o.Alpha, o.Desc, sortKey, r.getFieldKeys("*")) response, err := db().XZSort([]byte(setName), o.Offset, o.Size, o.Alpha, o.Desc, sortKey, r.getFieldKeys("*"))
if err != nil { if err != nil {
return err return err
} }