add some small performance stuff

This commit is contained in:
sentriz
2019-07-04 14:09:24 +01:00
parent f4b766e86d
commit e3ef5fa5e2
2 changed files with 14 additions and 7 deletions

View File

@@ -53,8 +53,8 @@ func decoded(in string) string {
func withTx(db *gorm.DB, cb func(tx *gorm.DB)) { func withTx(db *gorm.DB, cb func(tx *gorm.DB)) {
tx := db.Begin() tx := db.Begin()
defer tx.Commit()
cb(tx) cb(tx)
tx.Commit()
} }
type Scanner struct { type Scanner struct {
@@ -233,6 +233,7 @@ func (s *Scanner) handleFolder(it *item) error {
s.curFolders.Push(folder) s.curFolders.Push(folder)
}() }()
err := s.db. err := s.db.
Select("id, updated_at").
Where(model.Album{ Where(model.Album{
LeftPath: it.directory, LeftPath: it.directory,
RightPath: it.filename, RightPath: it.filename,
@@ -261,6 +262,7 @@ func (s *Scanner) handleTrack(it *item) error {
// set track basics // set track basics
track := &model.Track{} track := &model.Track{}
err := s.trTx. err := s.trTx.
Select("id, updated_at").
Where(model.Track{ Where(model.Track{
AlbumID: s.curFolders.PeekID(), AlbumID: s.curFolders.PeekID(),
Filename: it.filename, Filename: it.filename,
@@ -295,7 +297,6 @@ func (s *Scanner) handleTrack(it *item) error {
track.Bitrate = trTags.Bitrate() // from the file instead of tags track.Bitrate = trTags.Bitrate() // from the file instead of tags
// //
// set album artist basics // set album artist basics
artist := &model.Artist{}
artistName := func() string { artistName := func() string {
if ret := trTags.AlbumArtist(); ret != "" { if ret := trTags.AlbumArtist(); ret != "" {
return ret return ret
@@ -305,7 +306,9 @@ func (s *Scanner) handleTrack(it *item) error {
} }
return "Unknown Artist" return "Unknown Artist"
}() }()
artist := &model.Artist{}
err = s.trTx. err = s.trTx.
Select("id").
Where("name = ?", artistName). Where("name = ?", artistName).
First(artist). First(artist).
Error Error

View File

@@ -7,7 +7,11 @@ import (
) )
func egAlbum(id int) *model.Album { func egAlbum(id int) *model.Album {
return &model.Album{IDBase: model.IDBase{id}} return &model.Album{
IDBase: model.IDBase{
ID: id,
},
}
} }
func TestFolderStack(t *testing.T) { func TestFolderStack(t *testing.T) {
@@ -19,8 +23,8 @@ func TestFolderStack(t *testing.T) {
expected := "[6, 5, 4, 3, ]" expected := "[6, 5, 4, 3, ]"
actual := sta.String() actual := sta.String()
if expected != actual { if expected != actual {
t.Errorf("first stack: expected string %q, got %q", t.Errorf("first stack: expected string "+
expected, actual) "%q, got %q", expected, actual)
} }
// //
sta = &Stack{} sta = &Stack{}
@@ -34,7 +38,7 @@ func TestFolderStack(t *testing.T) {
expected = "[6, 5, 4, 27, ]" expected = "[6, 5, 4, 27, ]"
actual = sta.String() actual = sta.String()
if expected != actual { if expected != actual {
t.Errorf("second stack: expected string %q, got %q", t.Errorf("second stack: expected string "+
expected, actual) "%q, got %q", expected, actual)
} }
} }