Removed Storm persistence. Too slow and SQL implementation is actually more versatile
This commit is contained in:
parent
66e9377795
commit
0629be5800
|
@ -1,110 +0,0 @@
|
|||
package db_storm
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/asdine/storm/q"
|
||||
"github.com/cloudsonic/sonic-server/domain"
|
||||
)
|
||||
|
||||
type _Album struct {
|
||||
ID string ``
|
||||
Name string `storm:"index"`
|
||||
ArtistID string `storm:"index"`
|
||||
CoverArtPath string ``
|
||||
CoverArtId string ``
|
||||
Artist string `storm:"index"`
|
||||
AlbumArtist string ``
|
||||
Year int `storm:"index"`
|
||||
Compilation bool ``
|
||||
Starred bool `storm:"index"`
|
||||
PlayCount int `storm:"index"`
|
||||
PlayDate time.Time `storm:"index"`
|
||||
SongCount int ``
|
||||
Duration int ``
|
||||
Rating int `storm:"index"`
|
||||
Genre string ``
|
||||
StarredAt time.Time `storm:"index"`
|
||||
CreatedAt time.Time `storm:"index"`
|
||||
UpdatedAt time.Time ``
|
||||
}
|
||||
|
||||
type albumRepository struct {
|
||||
stormRepository
|
||||
}
|
||||
|
||||
func NewAlbumRepository() domain.AlbumRepository {
|
||||
r := &albumRepository{}
|
||||
r.init(&_Album{})
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *albumRepository) Put(a *domain.Album) error {
|
||||
ta := _Album(*a)
|
||||
return Db().Save(&ta)
|
||||
}
|
||||
|
||||
func (r *albumRepository) Get(id string) (*domain.Album, error) {
|
||||
ta := &_Album{}
|
||||
err := r.getByID(id, ta)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
a := domain.Album(*ta)
|
||||
return &a, err
|
||||
}
|
||||
|
||||
func (r *albumRepository) FindByArtist(artistId string) (domain.Albums, error) {
|
||||
var albums []_Album
|
||||
err := r.execute(q.Eq("ArtistID", artistId), &albums)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return r.toAlbums(albums)
|
||||
}
|
||||
|
||||
func (r *albumRepository) GetAll(options ...domain.QueryOptions) (domain.Albums, error) {
|
||||
var all []_Album
|
||||
err := r.getAll(&all, options...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return r.toAlbums(all)
|
||||
}
|
||||
|
||||
func (r *albumRepository) toAlbums(all []_Album) (domain.Albums, error) {
|
||||
result := make(domain.Albums, len(all))
|
||||
for i, a := range all {
|
||||
result[i] = domain.Album(a)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (r *albumRepository) GetAllIds() ([]string, error) {
|
||||
var all []_Album
|
||||
err := r.getAll(&all)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := make([]string, len(all))
|
||||
for i, a := range all {
|
||||
result[i] = domain.Album(a).ID
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (r *albumRepository) PurgeInactive(activeList domain.Albums) ([]string, error) {
|
||||
return r.purgeInactive(activeList)
|
||||
}
|
||||
|
||||
func (r *albumRepository) GetStarred(options ...domain.QueryOptions) (domain.Albums, error) {
|
||||
var starred []_Album
|
||||
err := r.execute(q.Eq("Starred", true), &starred, options...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return r.toAlbums(starred)
|
||||
}
|
||||
|
||||
var _ domain.AlbumRepository = (*albumRepository)(nil)
|
||||
var _ = domain.Album(_Album{})
|
|
@ -1,67 +0,0 @@
|
|||
package db_storm
|
||||
|
||||
import (
|
||||
"github.com/cloudsonic/sonic-server/domain"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("AlbumRepository", func() {
|
||||
var repo domain.AlbumRepository
|
||||
|
||||
BeforeEach(func() {
|
||||
repo = NewAlbumRepository()
|
||||
})
|
||||
|
||||
Describe("GetAll", func() {
|
||||
It("returns all records", func() {
|
||||
Expect(repo.GetAll(domain.QueryOptions{})).To(Equal(testAlbums))
|
||||
})
|
||||
|
||||
It("returns all records sorted", func() {
|
||||
Expect(repo.GetAll(domain.QueryOptions{SortBy: "Name"})).To(Equal(domain.Albums{
|
||||
{ID: "2", Name: "Abbey Road", Artist: "The Beatles", ArtistID: "1"},
|
||||
{ID: "3", Name: "Radioactivity", Artist: "Kraftwerk", ArtistID: "2", Starred: true},
|
||||
{ID: "1", Name: "Sgt Peppers", Artist: "The Beatles", ArtistID: "1"},
|
||||
}))
|
||||
})
|
||||
|
||||
It("returns all records sorted desc", func() {
|
||||
Expect(repo.GetAll(domain.QueryOptions{SortBy: "Name", Desc: true})).To(Equal(domain.Albums{
|
||||
{ID: "1", Name: "Sgt Peppers", Artist: "The Beatles", ArtistID: "1"},
|
||||
{ID: "3", Name: "Radioactivity", Artist: "Kraftwerk", ArtistID: "2", Starred: true},
|
||||
{ID: "2", Name: "Abbey Road", Artist: "The Beatles", ArtistID: "1"},
|
||||
}))
|
||||
})
|
||||
|
||||
It("paginates the result", func() {
|
||||
Expect(repo.GetAll(domain.QueryOptions{Offset: 1, Size: 1})).To(Equal(domain.Albums{
|
||||
{ID: "2", Name: "Abbey Road", Artist: "The Beatles", ArtistID: "1"},
|
||||
}))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("GetAllIds", func() {
|
||||
It("returns all records", func() {
|
||||
Expect(repo.GetAllIds()).To(Equal([]string{"1", "2", "3"}))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("GetStarred", func() {
|
||||
It("returns all starred records", func() {
|
||||
Expect(repo.GetStarred(domain.QueryOptions{})).To(Equal(domain.Albums{
|
||||
{ID: "3", Name: "Radioactivity", Artist: "Kraftwerk", ArtistID: "2", Starred: true},
|
||||
}))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("FindByArtist", func() {
|
||||
It("returns all records from a given ArtistID", func() {
|
||||
Expect(repo.FindByArtist("1")).To(Equal(domain.Albums{
|
||||
{ID: "1", Name: "Sgt Peppers", Artist: "The Beatles", ArtistID: "1"},
|
||||
{ID: "2", Name: "Abbey Road", Artist: "The Beatles", ArtistID: "1"},
|
||||
}))
|
||||
})
|
||||
})
|
||||
|
||||
})
|
|
@ -1,44 +0,0 @@
|
|||
package db_storm
|
||||
|
||||
import (
|
||||
"github.com/cloudsonic/sonic-server/domain"
|
||||
)
|
||||
|
||||
// This is used to isolate Storm's struct tags from the domain, to keep it agnostic of persistence details
|
||||
type _Artist struct {
|
||||
ID string
|
||||
Name string `storm:"index"`
|
||||
AlbumCount int
|
||||
}
|
||||
|
||||
type artistRepository struct {
|
||||
stormRepository
|
||||
}
|
||||
|
||||
func NewArtistRepository() domain.ArtistRepository {
|
||||
r := &artistRepository{}
|
||||
r.init(&_Artist{})
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *artistRepository) Put(a *domain.Artist) error {
|
||||
ta := _Artist(*a)
|
||||
return Db().Save(&ta)
|
||||
}
|
||||
|
||||
func (r *artistRepository) Get(id string) (*domain.Artist, error) {
|
||||
ta := &_Artist{}
|
||||
err := r.getByID(id, ta)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
a := domain.Artist(*ta)
|
||||
return &a, nil
|
||||
}
|
||||
|
||||
func (r *artistRepository) PurgeInactive(activeList domain.Artists) ([]string, error) {
|
||||
return r.purgeInactive(activeList)
|
||||
}
|
||||
|
||||
var _ domain.ArtistRepository = (*artistRepository)(nil)
|
||||
var _ = domain.Artist(_Artist{})
|
|
@ -1,45 +0,0 @@
|
|||
package db_storm
|
||||
|
||||
import (
|
||||
"github.com/cloudsonic/sonic-server/domain"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("ArtistRepository", func() {
|
||||
var repo domain.ArtistRepository
|
||||
|
||||
BeforeEach(func() {
|
||||
repo = NewArtistRepository()
|
||||
})
|
||||
|
||||
It("saves and retrieves data", func() {
|
||||
Expect(repo.Get("1")).To(Equal(&domain.Artist{ID: "1", Name: "Saara Saara"}))
|
||||
})
|
||||
|
||||
It("returns ErrNotFound when the ID does not exist", func() {
|
||||
_, err := repo.Get("999")
|
||||
Expect(err).To(MatchError(domain.ErrNotFound))
|
||||
})
|
||||
|
||||
Describe("PurgeInactive", func() {
|
||||
var data domain.Artists
|
||||
|
||||
BeforeEach(func() {
|
||||
for _, a := range data {
|
||||
repo.Put(&a)
|
||||
}
|
||||
})
|
||||
|
||||
It("purges inactive records", func() {
|
||||
active := domain.Artists{{ID: "1"}, {ID: "3"}}
|
||||
Expect(repo.PurgeInactive(active)).To(Equal([]string{"2"}))
|
||||
})
|
||||
|
||||
It("doesn't delete anything if all is active", func() {
|
||||
active := domain.Artists{{ID: "1"}, {ID: "2"}, {ID: "3"}}
|
||||
Expect(repo.PurgeInactive(active)).To(BeEmpty())
|
||||
})
|
||||
})
|
||||
|
||||
})
|
|
@ -1,53 +0,0 @@
|
|||
package db_storm
|
||||
|
||||
import (
|
||||
"github.com/asdine/storm"
|
||||
"github.com/cloudsonic/sonic-server/domain"
|
||||
"github.com/cloudsonic/sonic-server/log"
|
||||
"github.com/cloudsonic/sonic-server/scanner"
|
||||
)
|
||||
|
||||
var (
|
||||
checkSumBucket = "_Checksums"
|
||||
)
|
||||
|
||||
type checkSumRepository struct {
|
||||
data map[string]string
|
||||
}
|
||||
|
||||
func NewCheckSumRepository() scanner.CheckSumRepository {
|
||||
r := &checkSumRepository{}
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *checkSumRepository) loadData() error {
|
||||
loadedData := make(map[string]string)
|
||||
err := Db().Get(checkSumBucket, checkSumBucket, &loadedData)
|
||||
if err == storm.ErrNotFound {
|
||||
return domain.ErrNotFound
|
||||
}
|
||||
log.Debug("Loaded checksums", "total", len(loadedData))
|
||||
r.data = loadedData
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *checkSumRepository) Get(id string) (string, error) {
|
||||
if r.data == nil {
|
||||
err := r.loadData()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
return r.data[id], nil
|
||||
}
|
||||
|
||||
func (r *checkSumRepository) SetData(newSums map[string]string) error {
|
||||
err := Db().Set(checkSumBucket, checkSumBucket, newSums)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.data = newSums
|
||||
return nil
|
||||
}
|
||||
|
||||
var _ scanner.CheckSumRepository = (*checkSumRepository)(nil)
|
|
@ -1,28 +0,0 @@
|
|||
package db_storm
|
||||
|
||||
import (
|
||||
"github.com/cloudsonic/sonic-server/scanner"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("ChecksumRepository", func() {
|
||||
var repo scanner.CheckSumRepository
|
||||
|
||||
BeforeEach(func() {
|
||||
Db().Drop(checkSumBucket)
|
||||
repo = NewCheckSumRepository()
|
||||
repo.SetData(map[string]string{
|
||||
"a": "AAA", "b": "BBB",
|
||||
})
|
||||
})
|
||||
|
||||
It("can retrieve data", func() {
|
||||
Expect(repo.Get("b")).To(Equal("BBB"))
|
||||
})
|
||||
|
||||
It("persists data", func() {
|
||||
newRepo := NewCheckSumRepository()
|
||||
Expect(newRepo.Get("b")).To(Equal("BBB"))
|
||||
})
|
||||
})
|
|
@ -1,59 +0,0 @@
|
|||
package db_storm
|
||||
|
||||
import (
|
||||
"github.com/cloudsonic/sonic-server/domain"
|
||||
)
|
||||
|
||||
type _ArtistIndex struct {
|
||||
ID string
|
||||
Artists domain.ArtistInfos
|
||||
}
|
||||
|
||||
type artistIndexRepository struct {
|
||||
stormRepository
|
||||
}
|
||||
|
||||
func NewArtistIndexRepository() domain.ArtistIndexRepository {
|
||||
r := &artistIndexRepository{}
|
||||
r.init(&_ArtistIndex{})
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *artistIndexRepository) Put(i *domain.ArtistIndex) error {
|
||||
ti := _ArtistIndex(*i)
|
||||
return Db().Save(&ti)
|
||||
}
|
||||
|
||||
func (r *artistIndexRepository) Get(id string) (*domain.ArtistIndex, error) {
|
||||
ta := &_ArtistIndex{}
|
||||
err := r.getByID(id, ta)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
a := domain.ArtistIndex(*ta)
|
||||
return &a, err
|
||||
}
|
||||
|
||||
func (r *artistIndexRepository) GetAll() (domain.ArtistIndexes, error) {
|
||||
var all []_ArtistIndex
|
||||
err := r.getAll(&all)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return r.toArtistIndexes(all)
|
||||
}
|
||||
|
||||
func (r *artistIndexRepository) toArtistIndexes(all []_ArtistIndex) (domain.ArtistIndexes, error) {
|
||||
result := make(domain.ArtistIndexes, len(all))
|
||||
for i, a := range all {
|
||||
result[i] = domain.ArtistIndex(a)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (r *artistIndexRepository) DeleteAll() error {
|
||||
return Db().Drop(&_ArtistIndex{})
|
||||
}
|
||||
|
||||
var _ domain.ArtistIndexRepository = (*artistIndexRepository)(nil)
|
||||
var _ = domain.ArtistIndex(_ArtistIndex{})
|
|
@ -1,107 +0,0 @@
|
|||
package db_storm
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/asdine/storm/q"
|
||||
"github.com/cloudsonic/sonic-server/domain"
|
||||
)
|
||||
|
||||
type _MediaFile struct {
|
||||
ID string ``
|
||||
Path string ``
|
||||
Title string ``
|
||||
Album string ``
|
||||
Artist string ``
|
||||
ArtistID string ``
|
||||
AlbumArtist string ``
|
||||
AlbumID string `storm:"index"`
|
||||
HasCoverArt bool ``
|
||||
TrackNumber int ``
|
||||
DiscNumber int ``
|
||||
Year int ``
|
||||
Size string ``
|
||||
Suffix string ``
|
||||
Duration int ``
|
||||
BitRate int ``
|
||||
Genre string ``
|
||||
Compilation bool ``
|
||||
PlayCount int ``
|
||||
PlayDate time.Time ``
|
||||
Rating int ``
|
||||
Starred bool `storm:"index"`
|
||||
StarredAt time.Time ``
|
||||
CreatedAt time.Time ``
|
||||
UpdatedAt time.Time ``
|
||||
}
|
||||
|
||||
type mediaFileRepository struct {
|
||||
stormRepository
|
||||
}
|
||||
|
||||
func NewMediaFileRepository() domain.MediaFileRepository {
|
||||
r := &mediaFileRepository{}
|
||||
r.init(&_MediaFile{})
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *mediaFileRepository) Put(m *domain.MediaFile) error {
|
||||
tm := _MediaFile(*m)
|
||||
return Db().Save(&tm)
|
||||
}
|
||||
|
||||
func (r *mediaFileRepository) Get(id string) (*domain.MediaFile, error) {
|
||||
tm := &_MediaFile{}
|
||||
err := r.getByID(id, tm)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
a := domain.MediaFile(*tm)
|
||||
return &a, nil
|
||||
}
|
||||
|
||||
func (r *mediaFileRepository) toMediaFiles(all []_MediaFile) (domain.MediaFiles, error) {
|
||||
result := make(domain.MediaFiles, len(all))
|
||||
for i, m := range all {
|
||||
result[i] = domain.MediaFile(m)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (r *mediaFileRepository) FindByAlbum(albumId string) (domain.MediaFiles, error) {
|
||||
var mfs []_MediaFile
|
||||
err := r.execute(q.Eq("AlbumID", albumId), &mfs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return r.toMediaFiles(mfs)
|
||||
}
|
||||
|
||||
func (r *mediaFileRepository) GetStarred(options ...domain.QueryOptions) (domain.MediaFiles, error) {
|
||||
var starred []_MediaFile
|
||||
err := r.execute(q.Eq("Starred", true), &starred, options...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return r.toMediaFiles(starred)
|
||||
}
|
||||
|
||||
func (r *mediaFileRepository) GetAllIds() ([]string, error) {
|
||||
var all []_MediaFile
|
||||
err := r.getAll(&all)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := make([]string, len(all))
|
||||
for i, m := range all {
|
||||
result[i] = domain.MediaFile(m).ID
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (r *mediaFileRepository) PurgeInactive(activeList domain.MediaFiles) ([]string, error) {
|
||||
return r.purgeInactive(activeList)
|
||||
}
|
||||
|
||||
var _ domain.MediaFileRepository = (*mediaFileRepository)(nil)
|
||||
var _ = domain.MediaFile(_MediaFile{})
|
|
@ -1,65 +0,0 @@
|
|||
package db_storm
|
||||
|
||||
import (
|
||||
"github.com/cloudsonic/sonic-server/domain"
|
||||
)
|
||||
|
||||
type _Playlist struct {
|
||||
ID string
|
||||
Name string
|
||||
Comment string
|
||||
FullPath string
|
||||
Duration int
|
||||
Owner string
|
||||
Public bool
|
||||
Tracks []string
|
||||
}
|
||||
|
||||
type playlistRepository struct {
|
||||
stormRepository
|
||||
}
|
||||
|
||||
func NewPlaylistRepository() domain.PlaylistRepository {
|
||||
r := &playlistRepository{}
|
||||
r.init(&_Playlist{})
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *playlistRepository) Put(p *domain.Playlist) error {
|
||||
tp := _Playlist(*p)
|
||||
return Db().Save(&tp)
|
||||
}
|
||||
|
||||
func (r *playlistRepository) Get(id string) (*domain.Playlist, error) {
|
||||
tp := &_Playlist{}
|
||||
err := r.getByID(id, tp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
a := domain.Playlist(*tp)
|
||||
return &a, err
|
||||
}
|
||||
|
||||
func (r *playlistRepository) GetAll(options ...domain.QueryOptions) (domain.Playlists, error) {
|
||||
var all []_Playlist
|
||||
err := r.getAll(&all, options...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return r.toPlaylists(all)
|
||||
}
|
||||
|
||||
func (r *playlistRepository) toPlaylists(all []_Playlist) (domain.Playlists, error) {
|
||||
result := make(domain.Playlists, len(all))
|
||||
for i, p := range all {
|
||||
result[i] = domain.Playlist(p)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (r *playlistRepository) PurgeInactive(activeList domain.Playlists) ([]string, error) {
|
||||
return r.purgeInactive(activeList)
|
||||
}
|
||||
|
||||
var _ domain.PlaylistRepository = (*playlistRepository)(nil)
|
||||
var _ = domain.Playlist(_Playlist{})
|
|
@ -1,42 +0,0 @@
|
|||
package db_storm
|
||||
|
||||
import (
|
||||
"github.com/asdine/storm"
|
||||
"github.com/cloudsonic/sonic-server/domain"
|
||||
)
|
||||
|
||||
const propertyBucket = "Property"
|
||||
|
||||
type propertyRepository struct {
|
||||
}
|
||||
|
||||
func NewPropertyRepository() domain.PropertyRepository {
|
||||
r := &propertyRepository{}
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *propertyRepository) Put(id string, value string) error {
|
||||
return Db().Set(propertyBucket, id, value)
|
||||
}
|
||||
|
||||
func (r *propertyRepository) Get(id string) (string, error) {
|
||||
var value string
|
||||
err := Db().Get(propertyBucket, id, &value)
|
||||
if err == storm.ErrNotFound {
|
||||
return value, domain.ErrNotFound
|
||||
}
|
||||
return value, err
|
||||
}
|
||||
|
||||
func (r *propertyRepository) DefaultGet(id string, defaultValue string) (string, error) {
|
||||
value, err := r.Get(id)
|
||||
if err == domain.ErrNotFound {
|
||||
return defaultValue, nil
|
||||
}
|
||||
if err != nil {
|
||||
return defaultValue, err
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
var _ domain.PropertyRepository = (*propertyRepository)(nil)
|
|
@ -1,30 +0,0 @@
|
|||
package db_storm
|
||||
|
||||
import (
|
||||
"github.com/cloudsonic/sonic-server/domain"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("PropertyRepository", func() {
|
||||
var repo domain.PropertyRepository
|
||||
|
||||
BeforeEach(func() {
|
||||
Db().Drop(propertyBucket)
|
||||
repo = NewPropertyRepository()
|
||||
})
|
||||
|
||||
It("saves and retrieves data", func() {
|
||||
Expect(repo.Put("1", "test")).To(BeNil())
|
||||
Expect(repo.Get("1")).To(Equal("test"))
|
||||
})
|
||||
|
||||
It("returns default if data is not found", func() {
|
||||
Expect(repo.DefaultGet("2", "default")).To(Equal("default"))
|
||||
})
|
||||
|
||||
It("returns value if found", func() {
|
||||
Expect(repo.Put("3", "test")).To(BeNil())
|
||||
Expect(repo.DefaultGet("3", "default")).To(Equal("test"))
|
||||
})
|
||||
})
|
|
@ -1,33 +0,0 @@
|
|||
package db_storm
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path"
|
||||
"sync"
|
||||
|
||||
"github.com/asdine/storm"
|
||||
"github.com/cloudsonic/sonic-server/conf"
|
||||
"github.com/cloudsonic/sonic-server/log"
|
||||
)
|
||||
|
||||
var (
|
||||
_dbInstance *storm.DB
|
||||
once sync.Once
|
||||
)
|
||||
|
||||
func Db() *storm.DB {
|
||||
once.Do(func() {
|
||||
err := os.MkdirAll(conf.Sonic.DbPath, 0700)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
dbPath := path.Join(conf.Sonic.DbPath, "storm.db")
|
||||
instance, err := storm.Open(dbPath)
|
||||
log.Debug("Opening Storm DB from: " + dbPath)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
_dbInstance = instance
|
||||
})
|
||||
return _dbInstance
|
||||
}
|
|
@ -1,132 +0,0 @@
|
|||
package db_storm
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
|
||||
"github.com/asdine/storm"
|
||||
"github.com/asdine/storm/index"
|
||||
"github.com/asdine/storm/q"
|
||||
"github.com/cloudsonic/sonic-server/domain"
|
||||
)
|
||||
|
||||
type stormRepository struct {
|
||||
bucket interface{}
|
||||
}
|
||||
|
||||
func (r *stormRepository) init(entity interface{}) {
|
||||
r.bucket = entity
|
||||
if err := Db().Init(r.bucket); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *stormRepository) CountAll() (int64, error) {
|
||||
c, err := Db().Count(r.bucket)
|
||||
return int64(c), err
|
||||
}
|
||||
|
||||
func (r *stormRepository) Exists(id string) (bool, error) {
|
||||
err := Db().One("ID", id, r.bucket)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return err != storm.ErrNotFound, nil
|
||||
}
|
||||
|
||||
func (r *stormRepository) extractID(record interface{}) string {
|
||||
v := reflect.ValueOf(record).Elem()
|
||||
id := v.FieldByName("ID").String()
|
||||
return id
|
||||
}
|
||||
|
||||
func (r *stormRepository) getByID(id string, ta interface{}) error {
|
||||
err := Db().One("ID", id, ta)
|
||||
if err == storm.ErrNotFound {
|
||||
return domain.ErrNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *stormRepository) purgeInactive(activeList interface{}) (deleted []string, err error) {
|
||||
reflected := reflect.ValueOf(activeList)
|
||||
totalActive := reflected.Len()
|
||||
activeIDs := make([]string, totalActive)
|
||||
for i := 0; i < totalActive; i++ {
|
||||
item := reflected.Index(i)
|
||||
activeIDs[i] = item.FieldByName("ID").String()
|
||||
}
|
||||
|
||||
query := Db().Select(q.Not(q.In("ID", activeIDs)))
|
||||
|
||||
// Collect IDs that will be deleted
|
||||
err = query.Each(r.bucket, func(record interface{}) error {
|
||||
id := r.extractID(record)
|
||||
deleted = append(deleted, id)
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(deleted) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
err = query.Delete(r.bucket)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return deleted, nil
|
||||
}
|
||||
|
||||
func (r *stormRepository) execute(matcher q.Matcher, result interface{}, options ...domain.QueryOptions) error {
|
||||
query := Db().Select(matcher)
|
||||
if len(options) > 0 {
|
||||
query = addQueryOptions(query, options[0])
|
||||
}
|
||||
err := query.Find(result)
|
||||
if err == storm.ErrNotFound {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *stormRepository) getAll(all interface{}, options ...domain.QueryOptions) (err error) {
|
||||
o := domain.QueryOptions{}
|
||||
if len(options) > 0 {
|
||||
o = options[0]
|
||||
}
|
||||
if o.SortBy != "" {
|
||||
err = Db().AllByIndex(o.SortBy, all, stormOptions(o))
|
||||
} else {
|
||||
err = Db().All(all, stormOptions(o))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func stormOptions(options ...domain.QueryOptions) func(*index.Options) {
|
||||
o := domain.QueryOptions{}
|
||||
if len(options) > 0 {
|
||||
o = options[0]
|
||||
}
|
||||
return func(opts *index.Options) {
|
||||
opts.Reverse = o.Desc
|
||||
opts.Skip = o.Offset
|
||||
if o.Size > 0 {
|
||||
opts.Limit = o.Size
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func addQueryOptions(q storm.Query, o domain.QueryOptions) storm.Query {
|
||||
if o.SortBy != "" {
|
||||
q = q.OrderBy(o.SortBy)
|
||||
}
|
||||
if o.Desc {
|
||||
q = q.Reverse()
|
||||
}
|
||||
if o.Size > 0 {
|
||||
q = q.Limit(o.Size)
|
||||
}
|
||||
return q.Skip(o.Offset)
|
||||
}
|
|
@ -1,49 +0,0 @@
|
|||
package db_storm
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/cloudsonic/sonic-server/conf"
|
||||
"github.com/cloudsonic/sonic-server/domain"
|
||||
"github.com/cloudsonic/sonic-server/log"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
func TestStormPersistence(t *testing.T) {
|
||||
log.SetLevel(log.LevelCritical)
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "Storm Persistence Suite")
|
||||
}
|
||||
|
||||
var testAlbums = domain.Albums{
|
||||
{ID: "1", Name: "Sgt Peppers", Artist: "The Beatles", ArtistID: "1"},
|
||||
{ID: "2", Name: "Abbey Road", Artist: "The Beatles", ArtistID: "1"},
|
||||
{ID: "3", Name: "Radioactivity", Artist: "Kraftwerk", ArtistID: "2", Starred: true},
|
||||
}
|
||||
var testArtists = domain.Artists{
|
||||
{ID: "1", Name: "Saara Saara"},
|
||||
{ID: "2", Name: "Kraftwerk"},
|
||||
{ID: "3", Name: "The Beatles"},
|
||||
}
|
||||
|
||||
var _ = Describe("Initialize test DB", func() {
|
||||
BeforeSuite(func() {
|
||||
conf.Sonic.DbPath, _ = ioutil.TempDir("", "cloudsonic_tests")
|
||||
os.MkdirAll(conf.Sonic.DbPath, 0700)
|
||||
Db().Drop(&_Album{})
|
||||
albumRepo := NewAlbumRepository()
|
||||
for _, a := range testAlbums {
|
||||
albumRepo.Put(&a)
|
||||
}
|
||||
|
||||
Db().Drop(&_Artist{})
|
||||
artistRepo := NewArtistRepository()
|
||||
for _, a := range testArtists {
|
||||
artistRepo.Put(&a)
|
||||
}
|
||||
})
|
||||
|
||||
})
|
|
@ -1,19 +0,0 @@
|
|||
package db_storm
|
||||
|
||||
import (
|
||||
"github.com/cloudsonic/sonic-server/persistence"
|
||||
"github.com/google/wire"
|
||||
)
|
||||
|
||||
var Set = wire.NewSet(
|
||||
NewPropertyRepository,
|
||||
NewArtistRepository,
|
||||
NewAlbumRepository,
|
||||
NewMediaFileRepository,
|
||||
NewArtistIndexRepository,
|
||||
NewPlaylistRepository,
|
||||
NewCheckSumRepository,
|
||||
persistence.NewNowPlayingRepository,
|
||||
persistence.NewMediaFolderRepository,
|
||||
wire.Value(persistence.ProviderIdentifier("storm")),
|
||||
)
|
27
wire_gen.go
27
wire_gen.go
|
@ -13,7 +13,6 @@ import (
|
|||
"github.com/cloudsonic/sonic-server/persistence"
|
||||
"github.com/cloudsonic/sonic-server/persistence/db_ledis"
|
||||
"github.com/cloudsonic/sonic-server/persistence/db_sql"
|
||||
"github.com/cloudsonic/sonic-server/persistence/db_storm"
|
||||
"github.com/cloudsonic/sonic-server/scanner"
|
||||
"github.com/deluan/gomate"
|
||||
"github.com/deluan/gomate/ledis"
|
||||
|
@ -110,30 +109,6 @@ func createLedisDBProvider() *Provider {
|
|||
return provider
|
||||
}
|
||||
|
||||
func createStormProvider() *Provider {
|
||||
albumRepository := db_storm.NewAlbumRepository()
|
||||
artistRepository := db_storm.NewArtistRepository()
|
||||
checkSumRepository := db_storm.NewCheckSumRepository()
|
||||
artistIndexRepository := db_storm.NewArtistIndexRepository()
|
||||
mediaFileRepository := db_storm.NewMediaFileRepository()
|
||||
mediaFolderRepository := persistence.NewMediaFolderRepository()
|
||||
nowPlayingRepository := persistence.NewNowPlayingRepository()
|
||||
playlistRepository := db_storm.NewPlaylistRepository()
|
||||
propertyRepository := db_storm.NewPropertyRepository()
|
||||
provider := &Provider{
|
||||
AlbumRepository: albumRepository,
|
||||
ArtistRepository: artistRepository,
|
||||
CheckSumRepository: checkSumRepository,
|
||||
ArtistIndexRepository: artistIndexRepository,
|
||||
MediaFileRepository: mediaFileRepository,
|
||||
MediaFolderRepository: mediaFolderRepository,
|
||||
NowPlayingRepository: nowPlayingRepository,
|
||||
PlaylistRepository: playlistRepository,
|
||||
PropertyRepository: propertyRepository,
|
||||
}
|
||||
return provider
|
||||
}
|
||||
|
||||
// wire_injectors.go:
|
||||
|
||||
type Provider struct {
|
||||
|
@ -157,8 +132,6 @@ func createPersistenceProvider(provider persistence.ProviderIdentifier) *Provide
|
|||
switch provider {
|
||||
case "sql":
|
||||
return createSQLProvider()
|
||||
case "storm":
|
||||
return createStormProvider()
|
||||
default:
|
||||
return createLedisDBProvider()
|
||||
}
|
||||
|
|
|
@ -10,7 +10,6 @@ import (
|
|||
"github.com/cloudsonic/sonic-server/persistence"
|
||||
"github.com/cloudsonic/sonic-server/persistence/db_ledis"
|
||||
"github.com/cloudsonic/sonic-server/persistence/db_sql"
|
||||
"github.com/cloudsonic/sonic-server/persistence/db_storm"
|
||||
"github.com/cloudsonic/sonic-server/scanner"
|
||||
"github.com/deluan/gomate"
|
||||
"github.com/deluan/gomate/ledis"
|
||||
|
@ -56,8 +55,6 @@ func createPersistenceProvider(provider persistence.ProviderIdentifier) *Provide
|
|||
switch provider {
|
||||
case "sql":
|
||||
return createSQLProvider()
|
||||
case "storm":
|
||||
return createStormProvider()
|
||||
default:
|
||||
return createLedisDBProvider()
|
||||
}
|
||||
|
@ -77,13 +74,6 @@ func createLedisDBProvider() *Provider {
|
|||
))
|
||||
}
|
||||
|
||||
func createStormProvider() *Provider {
|
||||
panic(wire.Build(
|
||||
db_storm.Set,
|
||||
wire.Struct(new(Provider), "*"),
|
||||
))
|
||||
}
|
||||
|
||||
func newDB() gomate.DB {
|
||||
return ledis.NewEmbeddedDB(db_ledis.Db())
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue