Refactoring to a cleaner architecture
This commit is contained in:
@@ -0,0 +1 @@
|
||||
#-short
|
||||
@@ -0,0 +1,28 @@
|
||||
package persistence
|
||||
|
||||
import (
|
||||
"github.com/deluan/gosonic/domain"
|
||||
)
|
||||
|
||||
type Album struct {
|
||||
BaseRepository
|
||||
}
|
||||
|
||||
func NewAlbumRepository() *Album {
|
||||
r := &Album{}
|
||||
r.init("album", &domain.Album{})
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *Album) Put(m *domain.Album) error {
|
||||
if m.Id == "" {
|
||||
m.Id = r.NewId(m.ArtistId, m.Name)
|
||||
}
|
||||
return r.saveOrUpdate(m.Id, m)
|
||||
}
|
||||
|
||||
func (r *Album) Get(id string) (*domain.Album, error) {
|
||||
var rec interface{}
|
||||
rec, err := r.readEntity(id)
|
||||
return rec.(*domain.Album), err
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package persistence
|
||||
|
||||
import (
|
||||
"github.com/deluan/gosonic/domain"
|
||||
)
|
||||
|
||||
type Artist struct {
|
||||
BaseRepository
|
||||
}
|
||||
|
||||
func NewArtistRepository() *Artist {
|
||||
r := &Artist{}
|
||||
r.init("artist", &domain.Artist{})
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *Artist) Put(m *domain.Artist) error {
|
||||
if m.Id == "" {
|
||||
m.Id = r.NewId(m.Name)
|
||||
}
|
||||
return r.saveOrUpdate(m.Id, m)
|
||||
}
|
||||
|
||||
func (r *Artist) Get(id string) (*domain.Artist, error) {
|
||||
var rec interface{}
|
||||
rec, err := r.readEntity(id)
|
||||
return rec.(*domain.Artist), err
|
||||
}
|
||||
|
||||
func (r *Artist) GetByName(name string) (*domain.Artist, error) {
|
||||
id := r.NewId(name)
|
||||
return r.Get(id)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,152 @@
|
||||
package persistence
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"crypto/md5"
|
||||
"strings"
|
||||
"github.com/deluan/gosonic/utils"
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type BaseRepository struct {
|
||||
table string
|
||||
entityType reflect.Type
|
||||
fieldNames []string
|
||||
}
|
||||
|
||||
func (r *BaseRepository) init(table string, entity interface{}) {
|
||||
r.table = table
|
||||
r.entityType = reflect.TypeOf(entity).Elem()
|
||||
|
||||
h, _ := utils.ToMap(entity)
|
||||
r.fieldNames = make([]string, len(h))
|
||||
i := 0
|
||||
for k := range h {
|
||||
r.fieldNames[i] = k
|
||||
i++
|
||||
}
|
||||
}
|
||||
|
||||
// TODO Use annotations to specify fields to be used
|
||||
func (r *BaseRepository) NewId(fields ...string) string {
|
||||
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) {
|
||||
ids, err := db().SMembers([]byte(r.table + "s:all"))
|
||||
return len(ids), err
|
||||
}
|
||||
|
||||
func (r *BaseRepository) saveOrUpdate(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) getFieldKeys(id string) [][]byte {
|
||||
recordPrefix := fmt.Sprintf("%s:%s:", r.table, id)
|
||||
var fieldKeys = make([][]byte, len(r.fieldNames))
|
||||
for i, n := range r.fieldNames {
|
||||
fieldKeys[i] = []byte(recordPrefix + n)
|
||||
}
|
||||
return fieldKeys
|
||||
}
|
||||
|
||||
func (r* BaseRepository) newInstance() interface{} {
|
||||
return reflect.New(r.entityType).Interface()
|
||||
}
|
||||
|
||||
func (r *BaseRepository) readEntity(id string) (interface{}, error) {
|
||||
entity := r.newInstance()
|
||||
|
||||
fieldKeys := r.getFieldKeys(id)
|
||||
|
||||
res, err := db().MGet(fieldKeys...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = r.toEntity(res, entity)
|
||||
return entity, err
|
||||
}
|
||||
|
||||
func (r *BaseRepository) toEntity(response [][]byte, entity interface{}) error {
|
||||
var record = make(map[string]interface{}, len(response))
|
||||
for i, v := range response {
|
||||
var value interface{}
|
||||
if err := json.Unmarshal(v, &value); err != nil {
|
||||
return err
|
||||
}
|
||||
record[string(r.fieldNames[i])] = value
|
||||
}
|
||||
|
||||
return utils.ToStruct(record, entity)
|
||||
}
|
||||
|
||||
// TODO Optimize it! Probably very slow (and confusing!)
|
||||
func (r *BaseRepository) loadAll(entities interface{}, sortBy string) error {
|
||||
total, err := r.CountAll()
|
||||
if (err != nil) {
|
||||
return err
|
||||
}
|
||||
|
||||
reflected := reflect.ValueOf(entities).Elem()
|
||||
var sortKey []byte = nil
|
||||
if sortBy != "" {
|
||||
sortKey = []byte(fmt.Sprintf("%s:*:%s", r.table, sortBy))
|
||||
}
|
||||
setName := r.table + "s:all"
|
||||
response, err := db().XSSort([]byte(setName), 0, 0, true, false, sortKey, r.getFieldKeys("*"))
|
||||
if (err != nil) {
|
||||
return err
|
||||
}
|
||||
numFields := len(r.fieldNames)
|
||||
for i := 0; i < total; i++ {
|
||||
start := i * numFields
|
||||
entity := reflect.New(r.entityType).Interface()
|
||||
|
||||
if err := r.toEntity(response[start:start + numFields], entity); err != nil {
|
||||
return err
|
||||
}
|
||||
reflected.Set(reflect.Append(reflected, reflect.ValueOf(entity).Elem()))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
package persistence
|
||||
|
||||
import (
|
||||
"testing"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"github.com/deluan/gosonic/tests"
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type TestEntity struct {
|
||||
Id string
|
||||
Name string
|
||||
}
|
||||
|
||||
func shouldBeEqual(actualStruct interface{}, expectedStruct ...interface{}) string {
|
||||
actual := fmt.Sprintf("%#v", actualStruct)
|
||||
expected := fmt.Sprintf("%#v", expectedStruct[0])
|
||||
return ShouldEqual(actual, expected)
|
||||
}
|
||||
|
||||
func createRepo() *BaseRepository{
|
||||
repo := &BaseRepository{}
|
||||
repo.init("test", &TestEntity{})
|
||||
return repo
|
||||
}
|
||||
|
||||
func TestBaseRepository(t *testing.T) {
|
||||
tests.Init(t, false)
|
||||
|
||||
Convey("Subject: NewId", t, func() {
|
||||
repo := createRepo()
|
||||
|
||||
Convey("When I call NewId with a name", func() {
|
||||
Id := repo.NewId("a name")
|
||||
Convey("Then it should return a new Id", func() {
|
||||
So(Id, ShouldNotBeEmpty)
|
||||
})
|
||||
})
|
||||
|
||||
Convey("When I call NewId with the same name twice", func() {
|
||||
FirstId := repo.NewId("a name")
|
||||
SecondId := repo.NewId("a name")
|
||||
|
||||
Convey("Then it should return the same Id each time", func() {
|
||||
So(FirstId, ShouldEqual, SecondId)
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
Convey("When I call NewId with different names", func() {
|
||||
FirstId := repo.NewId("first name")
|
||||
SecondId := repo.NewId("second name")
|
||||
|
||||
Convey("Then it should return different Ids", func() {
|
||||
So(FirstId, ShouldNotEqual, SecondId)
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
Convey("Subject: saveOrUpdate/loadEntity/CountAll", t, func() {
|
||||
|
||||
Convey("Given an empty DB", func() {
|
||||
repo := createRepo()
|
||||
|
||||
Convey("When I save a new entity", func() {
|
||||
entity := &TestEntity{"123", "My Name"}
|
||||
err := repo.saveOrUpdate("123", entity)
|
||||
|
||||
Convey("Then the method shouldn't return any errors", func() {
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
|
||||
Convey("Then the number of entities should be 1", func() {
|
||||
count, _ := repo.CountAll()
|
||||
So(count, ShouldEqual, 1)
|
||||
})
|
||||
|
||||
Convey("And this entity should be equal to the the saved one", func() {
|
||||
actualEntity, _ := repo.readEntity("123")
|
||||
So(actualEntity, shouldBeEqual, entity)
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
Convey("Given a table with one entity", func() {
|
||||
repo := createRepo()
|
||||
entity := &TestEntity{"111", "One Name"}
|
||||
repo.saveOrUpdate(entity.Id, entity)
|
||||
|
||||
Convey("When I save an entity with a different Id", func() {
|
||||
newEntity := &TestEntity{"222", "Another Name"}
|
||||
repo.saveOrUpdate(newEntity.Id, newEntity)
|
||||
|
||||
Convey("Then the number of entities should be 2", func() {
|
||||
count, _ := repo.CountAll()
|
||||
So(count, ShouldEqual, 2)
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
Convey("When I save an entity with the same Id", func() {
|
||||
newEntity := &TestEntity{"111", "New Name"}
|
||||
repo.saveOrUpdate(newEntity.Id, newEntity)
|
||||
|
||||
Convey("Then the number of entities should be 1", func() {
|
||||
count, _ := repo.CountAll()
|
||||
So(count, ShouldEqual, 1)
|
||||
})
|
||||
|
||||
Convey("And the entity should be updated", func() {
|
||||
e, _ := repo.readEntity("111")
|
||||
actualEntity := e.(*TestEntity)
|
||||
So(actualEntity.Name, ShouldEqual, newEntity.Name)
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
Convey("Given a table with 3 entities", func() {
|
||||
repo := createRepo()
|
||||
for i := 1; i <= 3; i++ {
|
||||
e := &TestEntity{strconv.Itoa(i), fmt.Sprintf("Name %d", i)}
|
||||
repo.saveOrUpdate(e.Id, e)
|
||||
}
|
||||
|
||||
Convey("When I call loadAll", func() {
|
||||
var es = make([]TestEntity, 0)
|
||||
err := repo.loadAll(&es, "")
|
||||
Convey("Then It should not return any error", func() {
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
Convey("And I should get 3 entities", func() {
|
||||
So(len(es), ShouldEqual, 3)
|
||||
})
|
||||
Convey("And the values should be retrieved", func() {
|
||||
for _, e := range es {
|
||||
So(e.Id, ShouldBeIn, []string{"1", "2", "3"})
|
||||
So(e.Name, ShouldBeIn, []string{"Name 1", "Name 2", "Name 3"})
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reset(func() {
|
||||
dropDb()
|
||||
})
|
||||
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package persistence
|
||||
|
||||
import (
|
||||
"github.com/deluan/gosonic/domain"
|
||||
"errors"
|
||||
"sort"
|
||||
"github.com/deluan/gosonic/utils"
|
||||
)
|
||||
|
||||
type artistIndex struct {
|
||||
BaseRepository
|
||||
}
|
||||
|
||||
func NewArtistIndexRepository() domain.ArtistIndexRepository {
|
||||
r := &artistIndex{}
|
||||
r.init("index", &domain.ArtistIndex{})
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *artistIndex) Put(m *domain.ArtistIndex) error {
|
||||
if m.Id == "" {
|
||||
return errors.New("Id is not set")
|
||||
}
|
||||
sort.Sort(byArtistName(m.Artists))
|
||||
return r.saveOrUpdate(m.Id, m)
|
||||
}
|
||||
|
||||
func (r *artistIndex) Get(id string) (*domain.ArtistIndex, error) {
|
||||
var rec interface{}
|
||||
rec, err := r.readEntity(id)
|
||||
return rec.(*domain.ArtistIndex), err
|
||||
}
|
||||
|
||||
func (r *artistIndex) GetAll() ([]domain.ArtistIndex, error) {
|
||||
var indices = make([]domain.ArtistIndex, 0)
|
||||
err := r.loadAll(&indices, "")
|
||||
return indices, err
|
||||
}
|
||||
|
||||
type byArtistName []domain.ArtistInfo
|
||||
|
||||
func (a byArtistName) Len() int {
|
||||
return len(a)
|
||||
}
|
||||
func (a byArtistName) Swap(i, j int) {
|
||||
a[i], a[j] = a[j], a[i]
|
||||
}
|
||||
func (a byArtistName) Less(i, j int) bool {
|
||||
return utils.NoArticle(a[i].Artist) < utils.NoArticle(a[j].Artist)
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package persistence
|
||||
|
||||
import (
|
||||
"testing"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"github.com/deluan/gosonic/tests"
|
||||
"github.com/deluan/gosonic/domain"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func TestIndexRepository(t *testing.T) {
|
||||
|
||||
tests.Init(t, false)
|
||||
|
||||
Convey("Subject: NewIndexRepository", t, func() {
|
||||
repo := NewArtistIndexRepository()
|
||||
|
||||
Convey("It should be able to read and write to the database", func() {
|
||||
i := &domain.ArtistIndex{Id: "123"}
|
||||
|
||||
repo.Put(i)
|
||||
s,_ := repo.Get("123")
|
||||
|
||||
So(s, shouldBeEqual, i)
|
||||
})
|
||||
Convey("Method Put() should return error if Id is not set", func() {
|
||||
i := &domain.ArtistIndex{}
|
||||
|
||||
err := repo.Put(i)
|
||||
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
Convey("Given that I have 4 records", func() {
|
||||
for i := 1; i <= 4; i++ {
|
||||
e := &domain.ArtistIndex{Id: strconv.Itoa(i)}
|
||||
repo.Put(e)
|
||||
}
|
||||
|
||||
Convey("When I call GetAll()", func() {
|
||||
indices, err := repo.GetAll()
|
||||
Convey("Then It should not return any error", func() {
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
Convey("And It should return 4 entities", func() {
|
||||
So(indices, ShouldHaveLength, 4)
|
||||
})
|
||||
Convey("And the values should be retrieved", func() {
|
||||
for _, e := range indices {
|
||||
So(e.Id, ShouldBeIn, []string{"1", "2", "3", "4"})
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
Reset(func() {
|
||||
dropDb()
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package persistence
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/siddontang/ledisdb/ledis"
|
||||
"github.com/siddontang/ledisdb/config"
|
||||
)
|
||||
|
||||
var (
|
||||
_ledisInstance *ledis.Ledis
|
||||
_dbInstance *ledis.DB
|
||||
once sync.Once
|
||||
)
|
||||
|
||||
func db() *ledis.DB {
|
||||
once.Do(func() {
|
||||
config := config.NewConfigDefault()
|
||||
config.DataDir = beego.AppConfig.String("dbPath")
|
||||
l, _ := ledis.Open(config)
|
||||
instance, err := l.Select(0)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
_ledisInstance = l
|
||||
_dbInstance = instance
|
||||
})
|
||||
return _dbInstance
|
||||
}
|
||||
|
||||
|
||||
func dropDb() {
|
||||
db()
|
||||
_ledisInstance.FlushAll()
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package persistence
|
||||
|
||||
import (
|
||||
"github.com/deluan/gosonic/domain"
|
||||
)
|
||||
|
||||
type MediaFile struct {
|
||||
BaseRepository
|
||||
}
|
||||
|
||||
func NewMediaFileRepository() *MediaFile {
|
||||
r := &MediaFile{}
|
||||
r.init("mediafile", &domain.MediaFile{})
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *MediaFile) Put(m *domain.MediaFile) error {
|
||||
return r.saveOrUpdate(m.Id, m)
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package persistence
|
||||
|
||||
import (
|
||||
"github.com/deluan/gosonic/domain"
|
||||
"github.com/astaxie/beego"
|
||||
)
|
||||
|
||||
type MediaFolder struct {}
|
||||
|
||||
func NewMediaFolderRepository() *MediaFolder {
|
||||
return &MediaFolder{}
|
||||
}
|
||||
|
||||
|
||||
func (*MediaFolder) GetAll() ([]*domain.MediaFolder, error) {
|
||||
mediaFolder := domain.MediaFolder{Id: "0", Name: "iTunes Library", Path: beego.AppConfig.String("musicFolder")}
|
||||
result := make([]*domain.MediaFolder, 1)
|
||||
result[0] = &mediaFolder
|
||||
return result, nil
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package persistence
|
||||
|
||||
import (
|
||||
"github.com/deluan/gosonic/domain"
|
||||
"errors"
|
||||
)
|
||||
|
||||
type property struct {
|
||||
BaseRepository
|
||||
}
|
||||
|
||||
func NewPropertyRepository() *property {
|
||||
r := &property{}
|
||||
r.init("property", &domain.Property{})
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *property) Put(id string, value string) error {
|
||||
m := &domain.Property{Id: id, Value: value}
|
||||
if m.Id == "" {
|
||||
return errors.New("Id is required")
|
||||
}
|
||||
return r.saveOrUpdate(m.Id, m)
|
||||
}
|
||||
|
||||
func (r *property) Get(id string) (string, error) {
|
||||
var rec interface{}
|
||||
rec, err := r.readEntity(id)
|
||||
return rec.(*domain.Property).Value, err
|
||||
}
|
||||
|
||||
func (r*property) DefaultGet(id string, defaultValue string) (string, error) {
|
||||
v, err := r.Get(id)
|
||||
|
||||
if v == "" {
|
||||
v = defaultValue
|
||||
}
|
||||
|
||||
return v, err
|
||||
}
|
||||
Reference in New Issue
Block a user