Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use filepath walk for flatfs query #34

Merged
merged 1 commit into from
Dec 14, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 25 additions & 43 deletions flatfs/flatfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"time"

Expand Down Expand Up @@ -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] == '.' {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

. but no ..?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the intent of this check was to ignore the random dotfiles that os'es (OSX) will randomly decide to put in folders. Other than that, i'm not certain.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh name[0], we're good.
On Sun, Dec 13, 2015 at 22:10 Jeromy Johnson notifications@github.com
wrote:

In flatfs/flatfs.go
#34 (comment):

  • 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] == '.' {
    

I think the intent of this check was to ignore the random dotfiles that
os'es (OSX) will randomly decide to put in folders. Other than that, i'm
not certain.


Reply to this email directly or view it on GitHub
https://github.com/jbenet/go-datastore/pull/34/files#r47458423.

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 {
Expand Down