From 5c6c5a9c2b976f5aa6adb9248959d153bdcfaf5c Mon Sep 17 00:00:00 2001 From: Jeromy Date: Mon, 14 Dec 2015 10:00:54 -0800 Subject: [PATCH] update to new flatfs code License: MIT Signed-off-by: Jeromy --- Godeps/Godeps.json | 4 +- .../jbenet/go-datastore/flatfs/flatfs.go | 68 +++++++------------ 2 files changed, 27 insertions(+), 45 deletions(-) diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index a56ee039f96..85cd3335fa1 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -1,6 +1,6 @@ { "ImportPath": "github.com/ipfs/go-ipfs", - "GoVersion": "go1.5.1", + "GoVersion": "go1.5.2", "Packages": [ "./..." ], @@ -166,7 +166,7 @@ }, { "ImportPath": "github.com/jbenet/go-datastore", - "Rev": "bec407bccea1cfaf56ee946e947642e3ac5a9258" + "Rev": "19e39c85262aa4c796b26346f3e1937711ffe2bf" }, { "ImportPath": "github.com/jbenet/go-detect-race", diff --git a/Godeps/_workspace/src/github.com/jbenet/go-datastore/flatfs/flatfs.go b/Godeps/_workspace/src/github.com/jbenet/go-datastore/flatfs/flatfs.go index f85ad05ddb4..e2bbd39031b 100644 --- a/Godeps/_workspace/src/github.com/jbenet/go-datastore/flatfs/flatfs.go +++ b/Godeps/_workspace/src/github.com/jbenet/go-datastore/flatfs/flatfs.go @@ -9,6 +9,7 @@ import ( "io/ioutil" "os" "path" + "path/filepath" "strings" "time" @@ -314,52 +315,33 @@ func (fs *Datastore) Query(q query.Query) (query.Results, error) { return nil, errors.New("flatfs only supports listing all keys in random order") } - // TODO this dumb implementation gathers all keys into a single slice. - root, err := os.Open(fs.path) - if err != nil { - return nil, err - } - defer root.Close() + reschan := make(chan query.Result) + go func() { + defer close(reschan) + err := filepath.Walk(fs.path, func(path string, info os.FileInfo, err error) error { - var res []query.Entry - prefixes, err := root.Readdir(0) - if err != nil { - return nil, err - } - for _, fi := range prefixes { - var err error - res, err = fs.enumerateKeys(fi, res) - if err != nil { - return nil, err - } - } - return query.ResultsWithEntries(q, res), nil -} + if !info.Mode().IsRegular() || info.Name()[0] == '.' { + return nil + } -func (fs *Datastore) enumerateKeys(fi os.FileInfo, res []query.Entry) ([]query.Entry, error) { - if !fi.IsDir() || fi.Name()[0] == '.' { - return res, nil - } - child, err := os.Open(path.Join(fs.path, fi.Name())) - if err != nil { - return nil, err - } - defer child.Close() - objs, err := child.Readdir(0) - if err != nil { - return nil, err - } - for _, fi := range objs { - if !fi.Mode().IsRegular() || fi.Name()[0] == '.' { - return res, nil - } - key, ok := fs.decode(fi.Name()) - if !ok { - return res, nil + key, ok := fs.decode(info.Name()) + if !ok { + log.Warning("failed to decode entry in flatfs") + return nil + } + + reschan <- query.Result{ + Entry: query.Entry{ + Key: key.String(), + }, + } + return nil + }) + if err != nil { + log.Warning("walk failed: ", err) } - res = append(res, query.Entry{Key: key.String()}) - } - return res, nil + }() + return query.ResultsWithChan(q, reschan), nil } func (fs *Datastore) Close() error {