diff --git a/persistence/album_repository.go b/persistence/album_repository.go index 6aa26ff03..42b12f9e2 100644 --- a/persistence/album_repository.go +++ b/persistence/album_repository.go @@ -5,7 +5,7 @@ import ( ) type albumRepository struct { - baseRepository + ledisRepository } func NewAlbumRepository() domain.AlbumRepository { diff --git a/persistence/artist_repository.go b/persistence/artist_repository.go index 93f5ef098..b61899633 100644 --- a/persistence/artist_repository.go +++ b/persistence/artist_repository.go @@ -5,7 +5,7 @@ import ( ) type artistRepository struct { - baseRepository + ledisRepository } func NewArtistRepository() domain.ArtistRepository { diff --git a/persistence/index_repository.go b/persistence/index_repository.go index 8cbf6d3dc..f8f1118a0 100644 --- a/persistence/index_repository.go +++ b/persistence/index_repository.go @@ -8,7 +8,7 @@ import ( ) type artistIndexRepository struct { - baseRepository + ledisRepository } func NewArtistIndexRepository() domain.ArtistIndexRepository { diff --git a/persistence/base_repository.go b/persistence/ledis_repository.go similarity index 76% rename from persistence/base_repository.go rename to persistence/ledis_repository.go index 7b1ebe4e7..77e07c842 100644 --- a/persistence/base_repository.go +++ b/persistence/ledis_repository.go @@ -9,13 +9,13 @@ import ( "strings" ) -type baseRepository struct { +type ledisRepository struct { table string entityType reflect.Type fieldNames []string } -func (r *baseRepository) init(table string, entity interface{}) { +func (r *ledisRepository) init(table string, entity interface{}) { r.table = table r.entityType = reflect.TypeOf(entity).Elem() @@ -29,22 +29,22 @@ func (r *baseRepository) init(table string, entity interface{}) { } // TODO Use annotations to specify fields to be used -func (r *baseRepository) NewId(fields ...string) string { +func (r *ledisRepository) 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) { +func (r *ledisRepository) CountAll() (int, error) { ids, err := db().SMembers([]byte(r.table + "s:all")) return len(ids), err } -func (r *baseRepository) Exists(id string) (bool, error) { +func (r *ledisRepository) Exists(id string) (bool, error) { res, err := db().SIsMember([]byte(r.table+"s:all"), []byte(id)) return res != 0, err } -func (r *baseRepository) saveOrUpdate(id string, entity interface{}) error { +func (r *ledisRepository) saveOrUpdate(id string, entity interface{}) error { recordPrefix := fmt.Sprintf("%s:%s:", r.table, id) allKey := r.table + "s:all" @@ -73,7 +73,7 @@ func (r *baseRepository) saveOrUpdate(id string, entity interface{}) error { } // TODO Optimize -func (r *baseRepository) getParent(entity interface{}) (table string, id string) { +func (r *ledisRepository) getParent(entity interface{}) (table string, id string) { dt := reflect.TypeOf(entity).Elem() for i := 0; i < dt.NumField(); i++ { f := dt.Field(i) @@ -86,7 +86,7 @@ func (r *baseRepository) getParent(entity interface{}) (table string, id string) return "", "" } -func (r *baseRepository) getFieldKeys(id string) [][]byte { +func (r *ledisRepository) 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 { @@ -95,11 +95,11 @@ func (r *baseRepository) getFieldKeys(id string) [][]byte { return fieldKeys } -func (r *baseRepository) newInstance() interface{} { +func (r *ledisRepository) newInstance() interface{} { return reflect.New(r.entityType).Interface() } -func (r *baseRepository) readEntity(id string) (interface{}, error) { +func (r *ledisRepository) readEntity(id string) (interface{}, error) { entity := r.newInstance() fieldKeys := r.getFieldKeys(id) @@ -112,7 +112,7 @@ func (r *baseRepository) readEntity(id string) (interface{}, error) { return entity, err } -func (r *baseRepository) toEntity(response [][]byte, entity interface{}) error { +func (r *ledisRepository) toEntity(response [][]byte, entity interface{}) error { var record = make(map[string]interface{}, len(response)) for i, v := range response { if len(v) > 0 { @@ -127,18 +127,18 @@ func (r *baseRepository) toEntity(response [][]byte, entity interface{}) error { return utils.ToStruct(record, entity) } -func (r *baseRepository) loadAll(entities interface{}, sortBy string, alpha bool) error { +func (r *ledisRepository) loadAll(entities interface{}, sortBy string, alpha bool) error { setName := r.table + "s:all" return r.loadFromSet(setName, entities, sortBy, alpha) } -func (r *baseRepository) loadChildren(parentTable string, parentId string, entities interface{}, sortBy string, alpha bool) error { +func (r *ledisRepository) loadChildren(parentTable string, parentId string, entities interface{}, sortBy string, alpha bool) error { setName := fmt.Sprintf("%s:%s:%ss", parentTable, parentId, r.table) return r.loadFromSet(setName, entities, sortBy, alpha) } // TODO Optimize it! Probably very slow (and confusing!) -func (r *baseRepository) loadFromSet(setName string, entities interface{}, sortBy string, alpha bool) error { +func (r *ledisRepository) loadFromSet(setName string, entities interface{}, sortBy string, alpha bool) error { reflected := reflect.ValueOf(entities).Elem() var sortKey []byte = nil if sortBy != "" { diff --git a/persistence/base_repository_test.go b/persistence/ledis_repository_test.go similarity index 98% rename from persistence/base_repository_test.go rename to persistence/ledis_repository_test.go index ca67623dc..899fc9210 100644 --- a/persistence/base_repository_test.go +++ b/persistence/ledis_repository_test.go @@ -19,8 +19,8 @@ func shouldBeEqual(actualStruct interface{}, expectedStruct ...interface{}) stri return ShouldEqual(actual, expected) } -func createRepo() *baseRepository { - repo := &baseRepository{} +func createRepo() *ledisRepository { + repo := &ledisRepository{} repo.init("test", &TestEntity{}) return repo } diff --git a/persistence/mediafile_repository.go b/persistence/mediafile_repository.go index 0a2747fd6..51f3f1fab 100644 --- a/persistence/mediafile_repository.go +++ b/persistence/mediafile_repository.go @@ -6,7 +6,7 @@ import ( ) type mediaFileRepository struct { - baseRepository + ledisRepository } func NewMediaFileRepository() domain.MediaFileRepository { diff --git a/persistence/property_repository.go b/persistence/property_repository.go index cf8166e75..6667b2c4f 100644 --- a/persistence/property_repository.go +++ b/persistence/property_repository.go @@ -6,7 +6,7 @@ import ( ) type propertyRepository struct { - baseRepository + ledisRepository } func NewPropertyRepository() domain.PropertyRepository {