Rework for performances

This commit is contained in:
Quentin 2021-04-23 22:22:26 +02:00
parent 2089b0e809
commit b477c898ce
Signed by: quentin
GPG key ID: A98E9B769E4FF428
2 changed files with 45 additions and 61 deletions

View file

@ -26,18 +26,15 @@ func cmdHead(config *configCollect) {
log.Println("Proposing following HEAD:\n"+rc.Head.Content.String()) log.Println("Proposing following HEAD:\n"+rc.Head.Content.String())
} }
func cmdLs(config *configCollect) {
rf := NewRepoFs(config)
rf.EntryNode.Walk(ExpandOnlyDirectory, -1)
stackFolders := []*DirNode{rf.EntryNode} type LsWalker struct { }
for len(stackFolders) > 0 { func (lw* LsWalker) onDir(dn *DirNode) {
fold := stackFolders[0] log.Println(dn.String())
stackFolders = append(stackFolders[1:], fold.Folders...) }
func (lw* LsWalker) onFile(fn *FileNode) {
log.Println(fold.String()) log.Println(fn.String())
for _, file := range fold.Files { }
log.Println(file.String()) func cmdLs(config *configCollect) {
} en := NewEntryNode(config)
} en.Walk(new(LsWalker))
} }

83
fs.go
View file

@ -43,9 +43,6 @@ type DirNode struct {
Config *configCollect Config *configCollect
Ent *DirEnt Ent *DirEnt
Elem DirElem Elem DirElem
Folders []*DirNode
Files []*FileNode
Parent *DirNode
AbsolutePath string AbsolutePath string
} }
@ -56,34 +53,27 @@ type FileNode struct {
AbsolutePath string AbsolutePath string
} }
type RepoFs struct { type TreeObserver interface {
Config *configCollect onDir(dir *DirNode)
EntryNode *DirNode onFile(file *FileNode)
} }
func NewRepoFs(config *configCollect) *RepoFs { func NewEntryNode(config *configCollect) *DirNode {
rf := new(RepoFs) entryNode := new(DirNode)
rf.Config = config entryNode.Ent = new(DirEnt)
rf.EntryNode = new(DirNode) entryNode.Ent.Id = config.DirId
rf.EntryNode.Ent = new(DirEnt) entryNode.Ent.Name = ""
rf.EntryNode.Ent.Id = config.DirId entryNode.AbsolutePath = "/"
rf.EntryNode.Ent.Name = "" entryNode.Config = config
rf.EntryNode.AbsolutePath = "/"
rf.EntryNode.Files = make([]*FileNode,0)
rf.EntryNode.Folders = make([]*DirNode,0)
rf.EntryNode.Config = config
return rf return entryNode
} }
func NewDirNode(parent *DirNode, ent *DirEnt) *DirNode { func NewDirNode(parent *DirNode, ent *DirEnt) *DirNode {
dn := new(DirNode) dn := new(DirNode)
dn.Ent = ent dn.Ent = ent
dn.Config = parent.Config dn.Config = parent.Config
dn.Files = make([]*FileNode,0) dn.AbsolutePath = parent.AbsolutePath + ent.Name + "/"
dn.Folders = make([]*DirNode,0)
dn.Parent = parent
dn.AbsolutePath = dn.Parent.AbsolutePath + ent.Name + "/"
return dn return dn
} }
@ -118,16 +108,6 @@ func (dn* DirNode) Parse() {
jdec := json.NewDecoder(zfile) jdec := json.NewDecoder(zfile)
err = jdec.Decode(&dn.Elem) err = jdec.Decode(&dn.Elem)
if err != nil { log.Fatal(err) } if err != nil { log.Fatal(err) }
for _, el := range dn.Elem.Entries {
if IsDir(el.Mode) {
dn.Folders = append(dn.Folders, NewDirNode(dn, el))
} else if IsRegular(el.Mode) {
dn.Files = append(dn.Files, NewFileNode(dn, el))
} else {
log.Fatal("Unknown mode", el.Mode, "for", el.Name, "in object id", dn.Ent.Id)
}
}
} }
func (fn* FileNode) Parse() { func (fn* FileNode) Parse() {
@ -145,29 +125,36 @@ func (fn* FileNode) Parse() {
if err != nil { log.Fatal(err) } if err != nil { log.Fatal(err) }
} }
type ExpandStrat int func (dn* DirNode) Children() ([]*DirNode, []*FileNode) {
const ( folders := make([]*DirNode,0)
ExpandOnlyDirectory ExpandStrat = iota files := make([]*FileNode, 0)
ExpandAll
)
func (dn* DirNode) Walk(strat ExpandStrat, depth int) { for _, el := range dn.Elem.Entries {
if IsDir(el.Mode) {
folders = append(folders, NewDirNode(dn, el))
} else if IsRegular(el.Mode) {
files = append(files, NewFileNode(dn, el))
} else {
log.Fatal("Unknown mode", el.Mode, "for", el.Name, "in object id", dn.Ent.Id)
}
}
return folders, files
}
func (dn* DirNode) Walk(tobs TreeObserver) {
qNode := []*DirNode{dn} qNode := []*DirNode{dn}
for len(qNode) > 0 && depth != 0{ for len(qNode) > 0 {
if depth > 0 { depth = depth - 1 }
cursor := qNode[0] cursor := qNode[0]
qNode = qNode[1:] qNode = qNode[1:]
cursor.Parse() cursor.Parse()
dir, file := cursor.Children()
qNode = append(dir, qNode...)
for _, ndn := range dn.Folders { tobs.onDir(cursor)
qNode = append(qNode, ndn) for _, f := range file {
} tobs.onFile(f)
if ExpandAll == strat {
for _, nfn := range dn.Files {
nfn.Parse()
}
} }
} }
} }