Adding A new Field to the FileNode -> Title

Previously During Searching the Connection with mpd client was utilised
to get the title for the files this was slowing the search with large
number of files. Now after adding the Title field to the struct the
Field is accessed instead of querying the server this has lead to faster
searches.

[[ Please Note the Title of a Folder would be the last accessed
track from the FileMap. ]]
This commit is contained in:
aditya-K2 2021-12-28 10:32:48 +05:30
parent a8ae5fb426
commit 09297a4974
3 changed files with 13 additions and 21 deletions

View File

@ -3,6 +3,8 @@ package client
import (
"fmt"
"strings"
"github.com/fhs/gompd/mpd"
)
type FileNode struct {
@ -10,6 +12,7 @@ type FileNode struct {
Path string
Parent *FileNode
AbsolutePath string
Title string
}
// Source Interface For Fuzzy Searching.
@ -17,12 +20,7 @@ type FileNodes []FileNode
func (f FileNodes) String(i int) string {
if len(f[i].Children) == 0 {
_s, err := CONN.ListAllInfo(f[i].AbsolutePath)
if err != nil {
NotificationServer.Send(fmt.Sprintf("Could Not Get Information About the Node %s", f[i].Path))
return f[i].Path
}
return _s[0]["Title"]
return f[i].Title
}
return f[i].Path
}
@ -31,11 +29,11 @@ func (f FileNodes) Len() int {
return len(f)
}
func (f *FileNode) AddChildren(path string) {
func (f *FileNode) AddChildren(path string, title string) {
if f.Path != "" {
f.Children = append(f.Children, FileNode{Children: make([]FileNode, 0), Path: path, Parent: f, AbsolutePath: f.AbsolutePath + "/" + path})
f.Children = append(f.Children, FileNode{Children: make([]FileNode, 0), Path: path, Parent: f, AbsolutePath: f.AbsolutePath + "/" + path, Title: title})
} else {
f.Children = append(f.Children, FileNode{Children: make([]FileNode, 0), Path: path, Parent: f, AbsolutePath: f.AbsolutePath + path})
f.Children = append(f.Children, FileNode{Children: make([]FileNode, 0), Path: path, Parent: f, AbsolutePath: f.AbsolutePath + path, Title: title})
}
}
@ -44,14 +42,14 @@ func (f *FileNode) AddChildNode(m FileNode) {
f.Children = append(f.Children, m)
}
func GenerateDirectoryTree(path []string) *FileNode {
func GenerateDirectoryTree(path []mpd.Attrs) *FileNode {
var head *FileNode = new(FileNode)
var head1 *FileNode = head
for i := range path {
sepPaths := strings.Split(path[i], "/")
sepPaths := strings.Split(path[i]["file"], "/")
for j := range sepPaths {
if len(head.Children) == 0 {
head.AddChildren(sepPaths[j])
head.AddChildren(sepPaths[j], path[i]["Title"])
head = &(head.Children[len(head.Children)-1])
} else {
var headIsChanged = false
@ -63,7 +61,7 @@ func GenerateDirectoryTree(path []string) *FileNode {
}
}
if !headIsChanged {
head.AddChildren(sepPaths[j])
head.AddChildren(sepPaths[j], path[i]["Title"])
head = &(head.Children[len(head.Children)-1])
}
}

View File

@ -1,7 +1,6 @@
package client
import (
"fmt"
"strings"
"github.com/aditya-K2/fuzzy"
@ -18,12 +17,7 @@ func UpdateBuffSearchView(inputTable *tview.Table, m fuzzy.Matches, f []FileNode
if len(f[v.Index].Children) != 0 {
inputTable.SetCellSimple(k, 0, utils.GetMatchedString(f[v.Index].Path, "#0000ff", "yellow", v.MatchedIndexes))
} else {
_s, err := CONN.ListAllInfo(f[v.Index].AbsolutePath)
if err != nil {
NotificationServer.Send(fmt.Sprintf("Could Not Add %s to the Table", f[v.Index].Path))
} else {
inputTable.SetCellSimple(k, 0, utils.GetMatchedString(_s[0]["Title"], "#fbff00", "green", v.MatchedIndexes))
}
inputTable.SetCellSimple(k, 0, utils.GetMatchedString(f[v.Index].Title, "#fbff00", "green", v.MatchedIndexes))
}
if k == 15 {
break

View File

@ -52,7 +52,7 @@ func main() {
// Connecting the Notification Server to the Main UI
notify.ConnectUI(UI)
fileMap, err := CONN.GetFiles()
fileMap, err := CONN.ListAllInfo("/")
// Generating the Directory Tree for File Navigation.
dirTree := client.GenerateDirectoryTree(fileMap)