From db94ca355f4e0ad8c688134c6f80722fb02a9120 Mon Sep 17 00:00:00 2001 From: sentriz Date: Tue, 4 Jun 2019 15:46:43 +0100 Subject: [PATCH] merge left and right paths for benchmark --- model/model.go | 11 ++++------- scanner/folder_stack.go | 2 +- scanner/walk.go | 30 +++++++++++------------------- 3 files changed, 16 insertions(+), 27 deletions(-) diff --git a/model/model.go b/model/model.go index 09310f3..f4140d3 100644 --- a/model/model.go +++ b/model/model.go @@ -21,11 +21,9 @@ type Artist struct { type Track struct { IDBase CrudBase - Folder Folder - // TODO: try removing idx_folder_basename_ext - FolderID int `gorm:"not null; unique_index:idx_folder_filename_ext" sql:"default: null; type:int REFERENCES folders(id) ON DELETE CASCADE"` - Filename string `gorm:"not null; unique_index:idx_folder_filename_ext" sql:"default: null"` - Ext string `gorm:"not nill; unique_index:idx_folder_filename_ext" sql:"default: null"` + Folder Folder + FolderID int `gorm:"not null; unique_index:idx_folder_filename" sql:"default: null; type:int REFERENCES folders(id) ON DELETE CASCADE"` + Filename string `gorm:"not null; unique_index:idx_folder_filename" sql:"default: null"` Artist Artist ArtistID int `gorm:"not null; index" sql:"default: null; type:int REFERENCES artists(id) ON DELETE CASCADE"` ContentType string `gorm:"not null" sql:"default: null"` @@ -69,8 +67,7 @@ type Play struct { type Folder struct { IDBase CrudBase - LeftPath string `gorm:"unique_index:idx_left_path_right_path"` - RightPath string `gorm:"not null; unique_index:idx_left_path_right_path" sql:"default: null"` + Path string `gorm:"not null; unique_index" sql:"default: null"` Parent *Folder ParentID int `sql:"default: null; type:int REFERENCES folders(id) ON DELETE CASCADE"` AlbumArtist Artist diff --git a/scanner/folder_stack.go b/scanner/folder_stack.go index 26f4f5d..ca9381b 100644 --- a/scanner/folder_stack.go +++ b/scanner/folder_stack.go @@ -34,7 +34,7 @@ func (s *folderStack) Peek() *model.Folder { func (s *folderStack) String() string { paths := make([]string, len(*s)) for i, folder := range *s { - paths[i] = folder.RightPath + paths[i] = folder.Path } return fmt.Sprintf("[%s]", strings.Join(paths, " ")) } diff --git a/scanner/walk.go b/scanner/walk.go index ac85627..8543401 100644 --- a/scanner/walk.go +++ b/scanner/walk.go @@ -16,11 +16,10 @@ import ( type item struct { // // common - fullPath string - relPath string - directory string - filename string - stat os.FileInfo + fullPath string + relPath string + filename string + stat os.FileInfo // // track only ext string @@ -36,13 +35,12 @@ func (s *Scanner) callbackItem(fullPath string, info *godirwalk.Dirent) error { if err != nil { return errors.Wrap(err, "getting relative path") } - directory, filename := path.Split(relPath) + _, filename := path.Split(relPath) it := &item{ - fullPath: fullPath, - relPath: relPath, - directory: directory, - filename: filename, - stat: stat, + fullPath: fullPath, + relPath: relPath, + filename: filename, + stat: stat, } if info.IsDir() { return s.handleFolder(it) @@ -75,10 +73,7 @@ func (s *Scanner) callbackPost(fullPath string, info *godirwalk.Dirent) error { func (s *Scanner) handleFolder(it *item) error { var folder model.Folder err := s.tx. - Where(model.Folder{ - LeftPath: it.directory, - RightPath: it.filename, - }). + Where("path = ?", it.relPath). First(&folder). Error if !gorm.IsRecordNotFoundError(err) && @@ -87,8 +82,7 @@ func (s *Scanner) handleFolder(it *item) error { s.curFolders.Push(&folder) return nil } - folder.LeftPath = it.directory - folder.RightPath = it.filename + folder.Path = it.relPath s.tx.Save(&folder) folder.IsNew = true s.curFolders.Push(&folder) @@ -103,7 +97,6 @@ func (s *Scanner) handleTrack(it *item) error { Where(model.Track{ FolderID: s.curFolderID(), Filename: it.filename, - Ext: it.ext, }). First(&track). Error @@ -114,7 +107,6 @@ func (s *Scanner) handleTrack(it *item) error { return nil } track.Filename = it.filename - track.Ext = it.ext track.ContentType = it.mime track.Size = int(it.stat.Size()) track.FolderID = s.curFolderID()