Co-authored-by: Graham Steffaniak <graham.steffaniak@autodesk.com>
This commit is contained in:
Graham Steffaniak 2023-10-22 19:23:38 -05:00 committed by GitHub
parent 9b57eb1060
commit 8e4629a0c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 53 deletions

View File

@ -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

View File

@ -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()

View File

@ -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
}
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
}

View File

@ -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