Big LedisDB refactoring
This commit is contained in:
@@ -10,20 +10,20 @@ type Album struct {
|
||||
|
||||
func NewAlbumRepository() *Album {
|
||||
r := &Album{}
|
||||
r.key = "album"
|
||||
r.table = "album"
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *Album) Put(m *models.Album) (*models.Album, error) {
|
||||
func (r *Album) Put(m *models.Album) error {
|
||||
if m.Id == "" {
|
||||
m.Id = r.NewId(m.Name)
|
||||
}
|
||||
return m, r.saveOrUpdate(m.Id, m)
|
||||
return r.saveOrUpdate(m.Id, m)
|
||||
}
|
||||
|
||||
func (r *Album) Get(id string) (*models.Album, error) {
|
||||
rec := &models.Album{}
|
||||
err := readStruct(r.key, id, rec)
|
||||
err := r.loadEntity(id, rec)
|
||||
return rec, err
|
||||
}
|
||||
|
||||
|
||||
@@ -10,20 +10,20 @@ type Artist struct {
|
||||
|
||||
func NewArtistRepository() *Artist {
|
||||
r := &Artist{}
|
||||
r.key = "artist"
|
||||
r.table = "artist"
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *Artist) Put(m *models.Artist) (*models.Artist, error) { // TODO Return only error
|
||||
func (r *Artist) Put(m *models.Artist) error {
|
||||
if m.Id == "" {
|
||||
m.Id = r.NewId(m.Name)
|
||||
}
|
||||
return m, r.saveOrUpdate(m.Id, m)
|
||||
return r.saveOrUpdate(m.Id, m)
|
||||
}
|
||||
|
||||
func (r *Artist) Get(id string) (*models.Artist, error) {
|
||||
rec := &models.Artist{}
|
||||
err := readStruct(r.key, id, rec)
|
||||
err := r.loadEntity(id, rec)
|
||||
return rec, err
|
||||
}
|
||||
|
||||
|
||||
@@ -4,26 +4,100 @@ import (
|
||||
"fmt"
|
||||
"crypto/md5"
|
||||
"strings"
|
||||
"github.com/deluan/gosonic/utils"
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
|
||||
type BaseRepository struct {
|
||||
key string // TODO Rename to 'table'
|
||||
table string
|
||||
}
|
||||
|
||||
func (r *BaseRepository) NewId(fields ...string) string {
|
||||
s := fmt.Sprintf("%s\\%s", strings.ToUpper(r.key), strings.Join(fields, ""))
|
||||
s := fmt.Sprintf("%s\\%s", strings.ToUpper(r.table), strings.Join(fields, ""))
|
||||
return fmt.Sprintf("%x", md5.Sum([]byte(s)))
|
||||
}
|
||||
|
||||
func (r *BaseRepository) CountAll() (int, error) {
|
||||
return count(r.key)
|
||||
ids, err := db().SMembers([]byte(r.table + "s:all"))
|
||||
return len(ids), err
|
||||
}
|
||||
|
||||
func (r *BaseRepository) saveOrUpdate(id string, rec interface{}) error {
|
||||
return saveStruct(r.key, id, rec)
|
||||
return r.saveEntity(id, rec)
|
||||
}
|
||||
|
||||
func (r *BaseRepository) Dump() {
|
||||
}
|
||||
|
||||
func (r *BaseRepository) saveEntity(id string, entity interface{}) error {
|
||||
recordPrefix := fmt.Sprintf("%s:%s:", r.table, id)
|
||||
allKey := r.table + "s:all"
|
||||
|
||||
h, err := utils.ToMap(entity)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for f, v := range h {
|
||||
key := recordPrefix + f
|
||||
value, _ := json.Marshal(v)
|
||||
if err := db().Set([]byte(key), value); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if _, err = db().SAdd([]byte(allKey), []byte(id)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if parentTable, parentId := r.getParent(entity); parentTable != "" {
|
||||
parentCollectionKey := fmt.Sprintf("%s:%s:%ss", parentTable, parentId, r.table)
|
||||
_, err = db().SAdd([]byte(parentCollectionKey), []byte(id))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// TODO Optimize
|
||||
func (r *BaseRepository) getParent(entity interface{}) (table string, id string) {
|
||||
dt := reflect.TypeOf(entity).Elem()
|
||||
for i := 0; i < dt.NumField(); i++ {
|
||||
f := dt.Field(i)
|
||||
table := f.Tag.Get("parent")
|
||||
if table != "" {
|
||||
dv := reflect.ValueOf(entity).Elem()
|
||||
return table, dv.FieldByName(f.Name).String()
|
||||
}
|
||||
}
|
||||
return "", ""
|
||||
}
|
||||
|
||||
func (r *BaseRepository) loadEntity(id string, entity interface{}) error {
|
||||
recordPrefix := fmt.Sprintf("%s:%s:", r.table, id)
|
||||
|
||||
h, _ := utils.ToMap(entity)
|
||||
var fieldKeys = make([][]byte, len(h))
|
||||
var fieldNames = make([]string, len(h))
|
||||
i := 0
|
||||
for k, _ := range h {
|
||||
fieldNames[i] = k
|
||||
fieldKeys[i] = []byte(recordPrefix + k)
|
||||
i++
|
||||
}
|
||||
|
||||
res, err := db().MGet(fieldKeys...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var record = make(map[string]interface{}, len(res))
|
||||
for i, v := range res {
|
||||
var value interface{}
|
||||
if err := json.Unmarshal(v, &value); err != nil {
|
||||
return err
|
||||
}
|
||||
record[string(fieldNames[i])] = value
|
||||
}
|
||||
|
||||
return utils.ToStruct(record, entity)
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ type ArtistIndex struct {
|
||||
|
||||
func NewArtistIndexRepository() *ArtistIndex {
|
||||
r := &ArtistIndex{}
|
||||
r.key = "index"
|
||||
r.table = "index"
|
||||
return r
|
||||
}
|
||||
|
||||
|
||||
@@ -2,11 +2,9 @@ package repositories
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"encoding/json"
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/siddontang/ledisdb/ledis"
|
||||
"github.com/siddontang/ledisdb/config"
|
||||
"github.com/deluan/gosonic/utils"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -26,40 +24,4 @@ func db() *ledis.DB {
|
||||
_dbInstance = instance
|
||||
})
|
||||
return _dbInstance
|
||||
}
|
||||
|
||||
func saveStruct(key, id string, data interface{}) error {
|
||||
h, err := utils.ToMap(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var fvList = make([]ledis.FVPair, len(h))
|
||||
i := 0
|
||||
for f, v := range h {
|
||||
fvList[i].Field = []byte(f)
|
||||
fvList[i].Value, _ = json.Marshal(v)
|
||||
i++
|
||||
}
|
||||
kh := key + "_id_" + id
|
||||
ks := key + "_ids"
|
||||
db().SAdd([]byte(ks), []byte(id))
|
||||
return db().HMset([]byte(kh), fvList...)
|
||||
}
|
||||
|
||||
func readStruct(key, id string, rec interface{}) error {
|
||||
kh := key + "_id_" + id
|
||||
fvs, _ := db().HGetAll([]byte(kh))
|
||||
var m = make(map[string]interface{}, len(fvs))
|
||||
for _, fv := range fvs {
|
||||
var v interface{}
|
||||
json.Unmarshal(fv.Value, &v)
|
||||
m[string(fv.Field)] = v
|
||||
}
|
||||
|
||||
return utils.ToStruct(m, rec)
|
||||
}
|
||||
|
||||
func count(key string) (int, error) {
|
||||
ids, err := db().SMembers([]byte(key + "_ids"))
|
||||
return len(ids), err
|
||||
}
|
||||
@@ -10,7 +10,7 @@ type MediaFile struct {
|
||||
|
||||
func NewMediaFileRepository() *MediaFile {
|
||||
r := &MediaFile{}
|
||||
r.key = "mediafile"
|
||||
r.table = "mediafile"
|
||||
return r
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user