Commit 024754e6 authored by Jeromy's avatar Jeromy

use filepath walk for flatfs query

parent bec407bc
...@@ -9,6 +9,7 @@ import ( ...@@ -9,6 +9,7 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"path" "path"
"path/filepath"
"strings" "strings"
"time" "time"
...@@ -314,52 +315,33 @@ func (fs *Datastore) Query(q query.Query) (query.Results, error) { ...@@ -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") return nil, errors.New("flatfs only supports listing all keys in random order")
} }
// TODO this dumb implementation gathers all keys into a single slice. reschan := make(chan query.Result)
root, err := os.Open(fs.path) go func() {
if err != nil { defer close(reschan)
return nil, err err := filepath.Walk(fs.path, func(path string, info os.FileInfo, err error) error {
}
defer root.Close()
var res []query.Entry if !info.Mode().IsRegular() || info.Name()[0] == '.' {
prefixes, err := root.Readdir(0) return nil
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
}
func (fs *Datastore) enumerateKeys(fi os.FileInfo, res []query.Entry) ([]query.Entry, error) { key, ok := fs.decode(info.Name())
if !fi.IsDir() || fi.Name()[0] == '.' { if !ok {
return res, nil log.Warning("failed to decode entry in flatfs")
} return nil
child, err := os.Open(path.Join(fs.path, fi.Name())) }
if err != nil {
return nil, err reschan <- query.Result{
} Entry: query.Entry{
defer child.Close() Key: key.String(),
objs, err := child.Readdir(0) },
if err != nil { }
return nil, err return nil
} })
for _, fi := range objs { if err != nil {
if !fi.Mode().IsRegular() || fi.Name()[0] == '.' { log.Warning("walk failed: ", err)
return res, nil
}
key, ok := fs.decode(fi.Name())
if !ok {
return res, nil
} }
res = append(res, query.Entry{Key: key.String()}) }()
} return query.ResultsWithChan(q, reschan), nil
return res, nil
} }
func (fs *Datastore) Close() error { func (fs *Datastore) Close() error {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment