Commit 27279083 authored by Kevin Atkinson's avatar Kevin Atkinson

Don't use filepath.Walk for Query method.

filepath.Walk() sorts the names and calls lstat() on each entry, both of
which are unnecessary when all we need are the key names in random order.

For large datastores can improve the performance by about 50%.
parent f5bb6094
......@@ -312,32 +312,7 @@ func (fs *Datastore) Query(q query.Query) (query.Results, error) {
reschan := make(chan query.Result, query.KeysOnlyBufSize)
go func() {
defer close(reschan)
err := filepath.Walk(fs.path, func(path string, info os.FileInfo, err error) error {
if os.IsNotExist(err) {
return nil
}
if err != nil {
log.Errorf("Walk func in Query got error: %v", err)
return err
}
if !info.Mode().IsRegular() || strings.HasPrefix(info.Name(), ".") {
return 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
})
err := fs.walkTopLevel(fs.path, reschan)
if err != nil {
log.Warning("walk failed: ", err)
}
......@@ -345,6 +320,54 @@ func (fs *Datastore) Query(q query.Query) (query.Results, error) {
return query.ResultsWithChan(q, reschan), nil
}
func (fs *Datastore) walkTopLevel(path string, reschan chan query.Result) error {
dir, err := os.Open(path)
if err != nil {
return err
}
names, err := dir.Readdirnames(-1)
if err != nil {
return err
}
for _, dir := range names {
err = fs.walk(filepath.Join(path, dir), reschan)
if err != nil {
return err
}
}
return nil
}
func (fs *Datastore) walk(path string, reschan chan query.Result) error {
dir, err := os.Open(path)
if err != nil {
return err
}
names, err := dir.Readdirnames(-1)
if err != nil {
return err
}
for _, fn := range names {
if len(fn) == 0 || fn[0] == '.' {
continue
}
key, ok := fs.decode(fn)
if !ok {
log.Warning("failed to decode entry in flatfs")
continue
}
reschan <- query.Result{
Entry: query.Entry{
Key: key.String(),
},
}
}
return nil
}
func (fs *Datastore) Close() error {
return nil
}
......
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