From 8e4629a0c43e90fa157aa34749084d579c4854e2 Mon Sep 17 00:00:00 2001 From: Graham Steffaniak <42989099+gtsteffaniak@users.noreply.github.com> Date: Sun, 22 Oct 2023 19:23:38 -0500 Subject: [PATCH] V0.2.1 (#64) Co-authored-by: Graham Steffaniak --- backend/index/indexing.go | 8 ++---- backend/index/indexing_test.go | 4 +-- backend/index/search_index.go | 22 +++++++-------- backend/index/search_index_test.go | 45 ++++++++---------------------- 4 files changed, 26 insertions(+), 53 deletions(-) diff --git a/backend/index/indexing.go b/backend/index/indexing.go index bfee12d3..0655fd36 100644 --- a/backend/index/indexing.go +++ b/backend/index/indexing.go @@ -11,10 +11,6 @@ import ( "github.com/gtsteffaniak/filebrowser/settings" ) -const ( - maxIndexSize = 1000 -) - type Index struct { Dirs []string Files []string @@ -34,8 +30,8 @@ func GetIndex() *Index { func Initialize(intervalMinutes uint32) { // Initialize the index indexes = Index{ - Dirs: make([]string, 0, maxIndexSize), - Files: make([]string, 0, maxIndexSize), + Dirs: []string{}, + Files: []string{}, } rootPath = settings.GlobalConfiguration.Server.Root var numFiles, numDirs int diff --git a/backend/index/indexing_test.go b/backend/index/indexing_test.go index f1d081e3..01f4e7eb 100644 --- a/backend/index/indexing_test.go +++ b/backend/index/indexing_test.go @@ -10,8 +10,8 @@ import ( func BenchmarkFillIndex(b *testing.B) { indexes = Index{ - Dirs: make([]string, 0, 1000), - Files: make([]string, 0, 1000), + Dirs: []string{}, + Files: []string{}, } b.ResetTimer() b.ReportAllocs() diff --git a/backend/index/search_index.go b/backend/index/search_index.go index 65a0942b..92e1b68f 100644 --- a/backend/index/search_index.go +++ b/backend/index/search_index.go @@ -21,8 +21,6 @@ func (si *Index) Search(search string, scope string, sourceSession string) ([]st if scope == "" { scope = "/" } - fileTypes := map[string]bool{} - runningHash := generateRandomHash(4) sessionInProgress.Store(sourceSession, runningHash) // Store the value in the sync.Map searchOptions := ParseSearch(search) @@ -55,20 +53,16 @@ func (si *Index) Search(search string, scope string, sourceSession string) ([]st if count > maxSearchResults { break } - pathName := scopedPathNameFilter(path, scope) + pathName := scopedPathNameFilter(path, scope, isDir) if pathName == "" { continue } - + fileTypes := map[string]bool{} matches, fileType := containsSearchTerm(path, searchTerm, *searchOptions, isDir, fileTypes) if !matches { continue } - if isDir { - fileListTypes[pathName+"/"] = fileType - } else { - fileListTypes[pathName] = fileType - } + fileListTypes[pathName] = fileType matching = append(matching, pathName) count++ } @@ -83,13 +77,17 @@ func (si *Index) Search(search string, scope string, sourceSession string) ([]st return matching, fileListTypes } -func scopedPathNameFilter(pathName string, scope string) string { +func scopedPathNameFilter(pathName string, scope string, isDir bool) string { scope = strings.TrimPrefix(scope, "/") pathName = strings.TrimPrefix(pathName, "/") + pathName = strings.TrimSuffix(pathName, "/") if strings.HasPrefix(pathName, scope) { - pathName = "/" + strings.TrimPrefix(pathName, scope) + pathName = strings.TrimPrefix(pathName, scope) + if isDir { + pathName = pathName + "/" + } } else { - pathName = "" + pathName = "" // return not matched } return pathName } diff --git a/backend/index/search_index_test.go b/backend/index/search_index_test.go index 19df1c02..ac0ab407 100644 --- a/backend/index/search_index_test.go +++ b/backend/index/search_index_test.go @@ -9,8 +9,8 @@ import ( func BenchmarkSearchAllIndexes(b *testing.B) { indexes = Index{ - Dirs: make([]string, 0, 1000), - Files: make([]string, 0, 1000), + Dirs: []string{}, + Files: []string{}, } // Create mock data createMockData(50, 3) // 1000 dirs, 3 files per dir @@ -101,37 +101,37 @@ func TestSearchIndexes(t *testing.T) { { search: "audio", scope: "/new/", - expectedResult: []string{"/test/audio.wav"}, + expectedResult: []string{"test/audio.wav"}, expectedTypes: map[string]map[string]bool{ - "/test/audio.wav": map[string]bool{"audio": true, "dir": false}, + "test/audio.wav": map[string]bool{"audio": true, "dir": false}, }, }, { search: "test", scope: "/", - expectedResult: []string{"/test"}, + expectedResult: []string{"test/"}, expectedTypes: map[string]map[string]bool{ - "/test/": map[string]bool{"dir": true}, + "test/": map[string]bool{"dir": true}, }, }, { search: "archive", scope: "/", - expectedResult: []string{"/new/test/path/archive.zip"}, + expectedResult: []string{"new/test/path/archive.zip"}, expectedTypes: map[string]map[string]bool{ - "/new/test/path/archive.zip": map[string]bool{"archive": true, "dir": false}, + "new/test/path/archive.zip": map[string]bool{"archive": true, "dir": false}, }, }, { search: "video", scope: "/", expectedResult: []string{ - "/new/test/video.mp4", - "/new/test/video.MP4", + "new/test/video.mp4", + "new/test/video.MP4", }, expectedTypes: map[string]map[string]bool{ - "/new/test/video.MP4": map[string]bool{"video": true, "dir": false}, - "/new/test/video.mp4": map[string]bool{"video": true, "dir": false}, + "new/test/video.MP4": map[string]bool{"video": true, "dir": false}, + "new/test/video.mp4": map[string]bool{"video": true, "dir": false}, }, }, } @@ -150,27 +150,6 @@ func TestSearchIndexes(t *testing.T) { } } -func Test_scopedPathNameFilter(t *testing.T) { - type args struct { - pathName string - scope string - } - tests := []struct { - name string - args args - want string - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := scopedPathNameFilter(tt.args.pathName, tt.args.scope); got != tt.want { - t.Errorf("scopedPathNameFilter() = %v, want %v", got, tt.want) - } - }) - } -} - func Test_isDoc(t *testing.T) { type args struct { extension string