fs: cache recent dir for a bit

Change-Id: Id85344038d38dedc134435463d96d8b836edbcd7
This commit is contained in:
Brad Fitzpatrick 2014-01-31 11:50:22 +01:00
parent e2dc7a9256
commit e5ad2282c4
1 changed files with 21 additions and 5 deletions

View File

@ -39,9 +39,11 @@ type recentDir struct {
noXattr noXattr
fs *CamliFileSystem fs *CamliFileSystem
mu sync.Mutex mu sync.Mutex
ents map[string]*search.DescribedBlob // filename to blob meta ents map[string]*search.DescribedBlob // filename to blob meta
modTime map[string]time.Time // filename to permanode modtime modTime map[string]time.Time // filename to permanode modtime
lastReaddir time.Time
lastNames []string
} }
func (n *recentDir) Attr() fuse.Attr { func (n *recentDir) Attr() fuse.Attr {
@ -52,10 +54,22 @@ func (n *recentDir) Attr() fuse.Attr {
} }
} }
const recentSearchInterval = 10 * time.Second
func (n *recentDir) ReadDir(intr fs.Intr) ([]fuse.Dirent, fuse.Error) { func (n *recentDir) ReadDir(intr fs.Intr) ([]fuse.Dirent, fuse.Error) {
log.Printf("fs.recent: ReadDir / searching") var ents []fuse.Dirent
n.mu.Lock() n.mu.Lock()
defer n.mu.Unlock() defer n.mu.Unlock()
if n.lastReaddir.After(time.Now().Add(-recentSearchInterval)) {
log.Printf("fs.recent: ReadDir from cache")
for _, name := range n.lastNames {
ents = append(ents, fuse.Dirent{Name: name})
}
return ents, nil
}
log.Printf("fs.recent: ReadDir, doing search")
n.ents = make(map[string]*search.DescribedBlob) n.ents = make(map[string]*search.DescribedBlob)
n.modTime = make(map[string]time.Time) n.modTime = make(map[string]time.Time)
@ -67,7 +81,7 @@ func (n *recentDir) ReadDir(intr fs.Intr) ([]fuse.Dirent, fuse.Error) {
return nil, fuse.EIO return nil, fuse.EIO
} }
var ents []fuse.Dirent n.lastNames = nil
for _, ri := range res.Recent { for _, ri := range res.Recent {
modTime := ri.ModTime.Time() modTime := ri.ModTime.Time()
meta := res.Meta.Get(ri.BlobRef) meta := res.Meta.Get(ri.BlobRef)
@ -103,11 +117,13 @@ func (n *recentDir) ReadDir(intr fs.Intr) ([]fuse.Dirent, fuse.Error) {
n.ents[name] = ccMeta n.ents[name] = ccMeta
n.modTime[name] = modTime n.modTime[name] = modTime
log.Printf("fs.recent: name %q = %v (at %v -> %v)", name, ccMeta.BlobRef, ri.ModTime.Time(), modTime) log.Printf("fs.recent: name %q = %v (at %v -> %v)", name, ccMeta.BlobRef, ri.ModTime.Time(), modTime)
n.lastNames = append(n.lastNames, name)
ents = append(ents, fuse.Dirent{ ents = append(ents, fuse.Dirent{
Name: name, Name: name,
}) })
} }
log.Printf("fs.recent returning %d entries", len(ents)) log.Printf("fs.recent returning %d entries", len(ents))
n.lastReaddir = time.Now()
return ents, nil return ents, nil
} }