mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-03 20:47:35 +03:00
Change semantics of ChecksumRepository
This commit is contained in:
parent
c404252b7f
commit
16c550a9d1
6 changed files with 37 additions and 37 deletions
|
@ -1,6 +1,8 @@
|
|||
package model
|
||||
|
||||
type CheckSumRepository interface {
|
||||
Get(id string) (string, error)
|
||||
SetData(newSums map[string]string) error
|
||||
type ChecksumMap map[string]string
|
||||
|
||||
type ChecksumRepository interface {
|
||||
GetData() (ChecksumMap, error)
|
||||
SetData(newSums ChecksumMap) error
|
||||
}
|
||||
|
|
|
@ -2,12 +2,10 @@ package persistence
|
|||
|
||||
import (
|
||||
"github.com/astaxie/beego/orm"
|
||||
"github.com/cloudsonic/sonic-server/log"
|
||||
"github.com/cloudsonic/sonic-server/model"
|
||||
)
|
||||
|
||||
type checkSumRepository struct {
|
||||
data map[string]string
|
||||
}
|
||||
|
||||
const checkSumId = "1"
|
||||
|
@ -17,40 +15,28 @@ type Checksum struct {
|
|||
Sum string
|
||||
}
|
||||
|
||||
func NewCheckSumRepository() model.CheckSumRepository {
|
||||
func NewCheckSumRepository() model.ChecksumRepository {
|
||||
r := &checkSumRepository{}
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *checkSumRepository) loadData() error {
|
||||
func (r *checkSumRepository) GetData() (model.ChecksumMap, error) {
|
||||
loadedData := make(map[string]string)
|
||||
|
||||
var all []Checksum
|
||||
_, err := Db().QueryTable(&Checksum{}).All(&all)
|
||||
_, err := Db().QueryTable(&Checksum{}).Limit(-1).All(&all)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, cks := range all {
|
||||
loadedData[cks.ID] = cks.Sum
|
||||
}
|
||||
|
||||
r.data = loadedData
|
||||
log.Debug("Loaded checksums", "total", len(loadedData))
|
||||
return nil
|
||||
return loadedData, nil
|
||||
}
|
||||
|
||||
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 {
|
||||
func (r *checkSumRepository) SetData(newSums model.ChecksumMap) error {
|
||||
err := withTx(func(o orm.Ormer) error {
|
||||
_, err := Db().Raw("delete from checksum").Exec()
|
||||
if err != nil {
|
||||
|
@ -72,8 +58,7 @@ func (r *checkSumRepository) SetData(newSums map[string]string) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.data = newSums
|
||||
return nil
|
||||
}
|
||||
|
||||
var _ model.CheckSumRepository = (*checkSumRepository)(nil)
|
||||
var _ model.ChecksumRepository = (*checkSumRepository)(nil)
|
||||
|
|
|
@ -7,7 +7,7 @@ import (
|
|||
)
|
||||
|
||||
var _ = Describe("ChecksumRepository", func() {
|
||||
var repo model.CheckSumRepository
|
||||
var repo model.ChecksumRepository
|
||||
|
||||
BeforeEach(func() {
|
||||
Db().Delete(&Checksum{ID: checkSumId})
|
||||
|
@ -21,11 +21,15 @@ var _ = Describe("ChecksumRepository", 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() {
|
||||
newRepo := NewCheckSumRepository()
|
||||
Expect(newRepo.Get("b")).To(Equal("BBB"))
|
||||
sums, err := newRepo.GetData()
|
||||
Expect(err).To(BeNil())
|
||||
Expect(sums["b"]).To(Equal("BBB"))
|
||||
})
|
||||
})
|
||||
|
|
|
@ -28,11 +28,12 @@ type ItunesScanner struct {
|
|||
pplaylists map[string]plsRelation
|
||||
pmediaFiles map[int]*model.MediaFile
|
||||
lastModifiedSince time.Time
|
||||
checksumRepo model.CheckSumRepository
|
||||
checksumRepo model.ChecksumRepository
|
||||
checksums model.ChecksumMap
|
||||
newSums map[string]string
|
||||
}
|
||||
|
||||
func NewItunesScanner(checksumRepo model.CheckSumRepository) *ItunesScanner {
|
||||
func NewItunesScanner(checksumRepo model.ChecksumRepository) *ItunesScanner {
|
||||
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))
|
||||
|
||||
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.mediaFiles = make(map[string]*model.MediaFile)
|
||||
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 {
|
||||
id := t.PersistentID
|
||||
oldSum, _ := s.checksumRepo.Get(id)
|
||||
oldSum, _ := s.checksums[id]
|
||||
newSum := s.newSums[id]
|
||||
return oldSum != newSum
|
||||
}
|
||||
|
|
10
wire_gen.go
10
wire_gen.go
|
@ -20,8 +20,8 @@ import (
|
|||
|
||||
func CreateApp(musicFolder string) *server.Server {
|
||||
repositories := createPersistenceProvider()
|
||||
checkSumRepository := repositories.CheckSumRepository
|
||||
itunesScanner := scanner_legacy.NewItunesScanner(checkSumRepository)
|
||||
checksumRepository := repositories.CheckSumRepository
|
||||
itunesScanner := scanner_legacy.NewItunesScanner(checksumRepository)
|
||||
mediaFileRepository := repositories.MediaFileRepository
|
||||
albumRepository := repositories.AlbumRepository
|
||||
artistRepository := repositories.ArtistRepository
|
||||
|
@ -58,7 +58,7 @@ func CreateSubsonicAPIRouter() *api.Router {
|
|||
func createPersistenceProvider() *Repositories {
|
||||
albumRepository := persistence.NewAlbumRepository()
|
||||
artistRepository := persistence.NewArtistRepository()
|
||||
checkSumRepository := persistence.NewCheckSumRepository()
|
||||
checksumRepository := persistence.NewCheckSumRepository()
|
||||
artistIndexRepository := persistence.NewArtistIndexRepository()
|
||||
mediaFileRepository := persistence.NewMediaFileRepository()
|
||||
mediaFolderRepository := persistence.NewMediaFolderRepository()
|
||||
|
@ -68,7 +68,7 @@ func createPersistenceProvider() *Repositories {
|
|||
repositories := &Repositories{
|
||||
AlbumRepository: albumRepository,
|
||||
ArtistRepository: artistRepository,
|
||||
CheckSumRepository: checkSumRepository,
|
||||
CheckSumRepository: checksumRepository,
|
||||
ArtistIndexRepository: artistIndexRepository,
|
||||
MediaFileRepository: mediaFileRepository,
|
||||
MediaFolderRepository: mediaFolderRepository,
|
||||
|
@ -84,7 +84,7 @@ func createPersistenceProvider() *Repositories {
|
|||
type Repositories struct {
|
||||
AlbumRepository model.AlbumRepository
|
||||
ArtistRepository model.ArtistRepository
|
||||
CheckSumRepository model.CheckSumRepository
|
||||
CheckSumRepository model.ChecksumRepository
|
||||
ArtistIndexRepository model.ArtistIndexRepository
|
||||
MediaFileRepository model.MediaFileRepository
|
||||
MediaFolderRepository model.MediaFolderRepository
|
||||
|
|
|
@ -16,7 +16,7 @@ import (
|
|||
type Repositories struct {
|
||||
AlbumRepository model.AlbumRepository
|
||||
ArtistRepository model.ArtistRepository
|
||||
CheckSumRepository model.CheckSumRepository
|
||||
CheckSumRepository model.ChecksumRepository
|
||||
ArtistIndexRepository model.ArtistIndexRepository
|
||||
MediaFileRepository model.MediaFileRepository
|
||||
MediaFolderRepository model.MediaFolderRepository
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue