Change semantics of ChecksumRepository

This commit is contained in:
Deluan 2020-01-15 08:54:00 -05:00
parent c404252b7f
commit 16c550a9d1
6 changed files with 37 additions and 37 deletions

View file

@ -1,6 +1,8 @@
package model package model
type CheckSumRepository interface { type ChecksumMap map[string]string
Get(id string) (string, error)
SetData(newSums map[string]string) error type ChecksumRepository interface {
GetData() (ChecksumMap, error)
SetData(newSums ChecksumMap) error
} }

View file

@ -2,12 +2,10 @@ package persistence
import ( import (
"github.com/astaxie/beego/orm" "github.com/astaxie/beego/orm"
"github.com/cloudsonic/sonic-server/log"
"github.com/cloudsonic/sonic-server/model" "github.com/cloudsonic/sonic-server/model"
) )
type checkSumRepository struct { type checkSumRepository struct {
data map[string]string
} }
const checkSumId = "1" const checkSumId = "1"
@ -17,40 +15,28 @@ type Checksum struct {
Sum string Sum string
} }
func NewCheckSumRepository() model.CheckSumRepository { func NewCheckSumRepository() model.ChecksumRepository {
r := &checkSumRepository{} r := &checkSumRepository{}
return r return r
} }
func (r *checkSumRepository) loadData() error { func (r *checkSumRepository) GetData() (model.ChecksumMap, error) {
loadedData := make(map[string]string) loadedData := make(map[string]string)
var all []Checksum var all []Checksum
_, err := Db().QueryTable(&Checksum{}).All(&all) _, err := Db().QueryTable(&Checksum{}).Limit(-1).All(&all)
if err != nil { if err != nil {
return err return nil, err
} }
for _, cks := range all { for _, cks := range all {
loadedData[cks.ID] = cks.Sum loadedData[cks.ID] = cks.Sum
} }
r.data = loadedData return loadedData, nil
log.Debug("Loaded checksums", "total", len(loadedData))
return nil
} }
func (r *checkSumRepository) Get(id string) (string, error) { func (r *checkSumRepository) SetData(newSums model.ChecksumMap) 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 := withTx(func(o orm.Ormer) error { err := withTx(func(o orm.Ormer) error {
_, err := Db().Raw("delete from checksum").Exec() _, err := Db().Raw("delete from checksum").Exec()
if err != nil { if err != nil {
@ -72,8 +58,7 @@ func (r *checkSumRepository) SetData(newSums map[string]string) error {
if err != nil { if err != nil {
return err return err
} }
r.data = newSums
return nil return nil
} }
var _ model.CheckSumRepository = (*checkSumRepository)(nil) var _ model.ChecksumRepository = (*checkSumRepository)(nil)

View file

@ -7,7 +7,7 @@ import (
) )
var _ = Describe("ChecksumRepository", func() { var _ = Describe("ChecksumRepository", func() {
var repo model.CheckSumRepository var repo model.ChecksumRepository
BeforeEach(func() { BeforeEach(func() {
Db().Delete(&Checksum{ID: checkSumId}) Db().Delete(&Checksum{ID: checkSumId})
@ -21,11 +21,15 @@ var _ = Describe("ChecksumRepository", func() {
}) })
It("can retrieve data", func() { It("can retrieve data", func() {
Expect(repo.Get("b")).To(Equal("BBB")) sums, err := repo.GetData()
Expect(err).To(BeNil())
Expect(sums["b"]).To(Equal("BBB"))
}) })
It("persists data", func() { It("persists data", func() {
newRepo := NewCheckSumRepository() newRepo := NewCheckSumRepository()
Expect(newRepo.Get("b")).To(Equal("BBB")) sums, err := newRepo.GetData()
Expect(err).To(BeNil())
Expect(sums["b"]).To(Equal("BBB"))
}) })
}) })

View file

@ -28,11 +28,12 @@ type ItunesScanner struct {
pplaylists map[string]plsRelation pplaylists map[string]plsRelation
pmediaFiles map[int]*model.MediaFile pmediaFiles map[int]*model.MediaFile
lastModifiedSince time.Time lastModifiedSince time.Time
checksumRepo model.CheckSumRepository checksumRepo model.ChecksumRepository
checksums model.ChecksumMap
newSums map[string]string newSums map[string]string
} }
func NewItunesScanner(checksumRepo model.CheckSumRepository) *ItunesScanner { func NewItunesScanner(checksumRepo model.ChecksumRepository) *ItunesScanner {
return &ItunesScanner{checksumRepo: checksumRepo} return &ItunesScanner{checksumRepo: checksumRepo}
} }
@ -51,6 +52,14 @@ func (s *ItunesScanner) ScanLibrary(lastModifiedSince time.Time, path string) (i
} }
log.Debug("Loaded tracks", "total", len(l.Tracks)) log.Debug("Loaded tracks", "total", len(l.Tracks))
s.checksums, err = s.checksumRepo.GetData()
if err != nil {
log.Error("Error loading checksums", err)
s.checksums = map[string]string{}
} else {
log.Debug("Loaded checksums", "total", len(s.checksums))
}
s.lastModifiedSince = lastModifiedSince s.lastModifiedSince = lastModifiedSince
s.mediaFiles = make(map[string]*model.MediaFile) s.mediaFiles = make(map[string]*model.MediaFile)
s.albums = make(map[string]*model.Album) s.albums = make(map[string]*model.Album)
@ -212,7 +221,7 @@ func (s *ItunesScanner) lastChangedDate(t *itl.Track) time.Time {
func (s *ItunesScanner) hasChanged(t *itl.Track) bool { func (s *ItunesScanner) hasChanged(t *itl.Track) bool {
id := t.PersistentID id := t.PersistentID
oldSum, _ := s.checksumRepo.Get(id) oldSum, _ := s.checksums[id]
newSum := s.newSums[id] newSum := s.newSums[id]
return oldSum != newSum return oldSum != newSum
} }

View file

@ -20,8 +20,8 @@ import (
func CreateApp(musicFolder string) *server.Server { func CreateApp(musicFolder string) *server.Server {
repositories := createPersistenceProvider() repositories := createPersistenceProvider()
checkSumRepository := repositories.CheckSumRepository checksumRepository := repositories.CheckSumRepository
itunesScanner := scanner_legacy.NewItunesScanner(checkSumRepository) itunesScanner := scanner_legacy.NewItunesScanner(checksumRepository)
mediaFileRepository := repositories.MediaFileRepository mediaFileRepository := repositories.MediaFileRepository
albumRepository := repositories.AlbumRepository albumRepository := repositories.AlbumRepository
artistRepository := repositories.ArtistRepository artistRepository := repositories.ArtistRepository
@ -58,7 +58,7 @@ func CreateSubsonicAPIRouter() *api.Router {
func createPersistenceProvider() *Repositories { func createPersistenceProvider() *Repositories {
albumRepository := persistence.NewAlbumRepository() albumRepository := persistence.NewAlbumRepository()
artistRepository := persistence.NewArtistRepository() artistRepository := persistence.NewArtistRepository()
checkSumRepository := persistence.NewCheckSumRepository() checksumRepository := persistence.NewCheckSumRepository()
artistIndexRepository := persistence.NewArtistIndexRepository() artistIndexRepository := persistence.NewArtistIndexRepository()
mediaFileRepository := persistence.NewMediaFileRepository() mediaFileRepository := persistence.NewMediaFileRepository()
mediaFolderRepository := persistence.NewMediaFolderRepository() mediaFolderRepository := persistence.NewMediaFolderRepository()
@ -68,7 +68,7 @@ func createPersistenceProvider() *Repositories {
repositories := &Repositories{ repositories := &Repositories{
AlbumRepository: albumRepository, AlbumRepository: albumRepository,
ArtistRepository: artistRepository, ArtistRepository: artistRepository,
CheckSumRepository: checkSumRepository, CheckSumRepository: checksumRepository,
ArtistIndexRepository: artistIndexRepository, ArtistIndexRepository: artistIndexRepository,
MediaFileRepository: mediaFileRepository, MediaFileRepository: mediaFileRepository,
MediaFolderRepository: mediaFolderRepository, MediaFolderRepository: mediaFolderRepository,
@ -84,7 +84,7 @@ func createPersistenceProvider() *Repositories {
type Repositories struct { type Repositories struct {
AlbumRepository model.AlbumRepository AlbumRepository model.AlbumRepository
ArtistRepository model.ArtistRepository ArtistRepository model.ArtistRepository
CheckSumRepository model.CheckSumRepository CheckSumRepository model.ChecksumRepository
ArtistIndexRepository model.ArtistIndexRepository ArtistIndexRepository model.ArtistIndexRepository
MediaFileRepository model.MediaFileRepository MediaFileRepository model.MediaFileRepository
MediaFolderRepository model.MediaFolderRepository MediaFolderRepository model.MediaFolderRepository

View file

@ -16,7 +16,7 @@ import (
type Repositories struct { type Repositories struct {
AlbumRepository model.AlbumRepository AlbumRepository model.AlbumRepository
ArtistRepository model.ArtistRepository ArtistRepository model.ArtistRepository
CheckSumRepository model.CheckSumRepository CheckSumRepository model.ChecksumRepository
ArtistIndexRepository model.ArtistIndexRepository ArtistIndexRepository model.ArtistIndexRepository
MediaFileRepository model.MediaFileRepository MediaFileRepository model.MediaFileRepository
MediaFolderRepository model.MediaFolderRepository MediaFolderRepository model.MediaFolderRepository