Rework for performances
This commit is contained in:
parent
2089b0e809
commit
b477c898ce
2 changed files with 45 additions and 61 deletions
23
command.go
23
command.go
|
@ -26,18 +26,15 @@ func cmdHead(config *configCollect) {
|
|||
log.Println("Proposing following HEAD:\n"+rc.Head.Content.String())
|
||||
}
|
||||
|
||||
|
||||
type LsWalker struct { }
|
||||
func (lw* LsWalker) onDir(dn *DirNode) {
|
||||
log.Println(dn.String())
|
||||
}
|
||||
func (lw* LsWalker) onFile(fn *FileNode) {
|
||||
log.Println(fn.String())
|
||||
}
|
||||
func cmdLs(config *configCollect) {
|
||||
rf := NewRepoFs(config)
|
||||
rf.EntryNode.Walk(ExpandOnlyDirectory, -1)
|
||||
|
||||
stackFolders := []*DirNode{rf.EntryNode}
|
||||
for len(stackFolders) > 0 {
|
||||
fold := stackFolders[0]
|
||||
stackFolders = append(stackFolders[1:], fold.Folders...)
|
||||
|
||||
log.Println(fold.String())
|
||||
for _, file := range fold.Files {
|
||||
log.Println(file.String())
|
||||
}
|
||||
}
|
||||
en := NewEntryNode(config)
|
||||
en.Walk(new(LsWalker))
|
||||
}
|
||||
|
|
83
fs.go
83
fs.go
|
@ -43,9 +43,6 @@ type DirNode struct {
|
|||
Config *configCollect
|
||||
Ent *DirEnt
|
||||
Elem DirElem
|
||||
Folders []*DirNode
|
||||
Files []*FileNode
|
||||
Parent *DirNode
|
||||
AbsolutePath string
|
||||
}
|
||||
|
||||
|
@ -56,34 +53,27 @@ type FileNode struct {
|
|||
AbsolutePath string
|
||||
}
|
||||
|
||||
type RepoFs struct {
|
||||
Config *configCollect
|
||||
EntryNode *DirNode
|
||||
type TreeObserver interface {
|
||||
onDir(dir *DirNode)
|
||||
onFile(file *FileNode)
|
||||
}
|
||||
|
||||
func NewRepoFs(config *configCollect) *RepoFs {
|
||||
rf := new(RepoFs)
|
||||
rf.Config = config
|
||||
rf.EntryNode = new(DirNode)
|
||||
rf.EntryNode.Ent = new(DirEnt)
|
||||
rf.EntryNode.Ent.Id = config.DirId
|
||||
rf.EntryNode.Ent.Name = ""
|
||||
rf.EntryNode.AbsolutePath = "/"
|
||||
rf.EntryNode.Files = make([]*FileNode,0)
|
||||
rf.EntryNode.Folders = make([]*DirNode,0)
|
||||
rf.EntryNode.Config = config
|
||||
func NewEntryNode(config *configCollect) *DirNode {
|
||||
entryNode := new(DirNode)
|
||||
entryNode.Ent = new(DirEnt)
|
||||
entryNode.Ent.Id = config.DirId
|
||||
entryNode.Ent.Name = ""
|
||||
entryNode.AbsolutePath = "/"
|
||||
entryNode.Config = config
|
||||
|
||||
return rf
|
||||
return entryNode
|
||||
}
|
||||
|
||||
func NewDirNode(parent *DirNode, ent *DirEnt) *DirNode {
|
||||
dn := new(DirNode)
|
||||
dn.Ent = ent
|
||||
dn.Config = parent.Config
|
||||
dn.Files = make([]*FileNode,0)
|
||||
dn.Folders = make([]*DirNode,0)
|
||||
dn.Parent = parent
|
||||
dn.AbsolutePath = dn.Parent.AbsolutePath + ent.Name + "/"
|
||||
dn.AbsolutePath = parent.AbsolutePath + ent.Name + "/"
|
||||
|
||||
return dn
|
||||
}
|
||||
|
@ -118,16 +108,6 @@ func (dn* DirNode) Parse() {
|
|||
jdec := json.NewDecoder(zfile)
|
||||
err = jdec.Decode(&dn.Elem)
|
||||
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() {
|
||||
|
@ -145,29 +125,36 @@ func (fn* FileNode) Parse() {
|
|||
if err != nil { log.Fatal(err) }
|
||||
}
|
||||
|
||||
type ExpandStrat int
|
||||
const (
|
||||
ExpandOnlyDirectory ExpandStrat = iota
|
||||
ExpandAll
|
||||
)
|
||||
func (dn* DirNode) Children() ([]*DirNode, []*FileNode) {
|
||||
folders := make([]*DirNode,0)
|
||||
files := make([]*FileNode, 0)
|
||||
|
||||
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}
|
||||
for len(qNode) > 0 && depth != 0{
|
||||
if depth > 0 { depth = depth - 1 }
|
||||
for len(qNode) > 0 {
|
||||
|
||||
cursor := qNode[0]
|
||||
qNode = qNode[1:]
|
||||
cursor.Parse()
|
||||
dir, file := cursor.Children()
|
||||
qNode = append(dir, qNode...)
|
||||
|
||||
for _, ndn := range dn.Folders {
|
||||
qNode = append(qNode, ndn)
|
||||
}
|
||||
|
||||
if ExpandAll == strat {
|
||||
for _, nfn := range dn.Files {
|
||||
nfn.Parse()
|
||||
}
|
||||
tobs.onDir(cursor)
|
||||
for _, f := range file {
|
||||
tobs.onFile(f)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue