Skip to content

Commit

Permalink
Optimize Readdir
Browse files Browse the repository at this point in the history
  • Loading branch information
suzaku committed Jan 22, 2021
1 parent 21f365a commit 0da0ac5
Showing 1 changed file with 35 additions and 18 deletions.
53 changes: 35 additions & 18 deletions pkg/meta/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -1167,10 +1167,6 @@ func (r *redisMeta) Readdir(ctx Context, inode Ino, plus uint8, entries *[]*Entr
if err := r.GetAttr(ctx, inode, &attr); err != 0 {
return err
}
vals, err := r.rdb.HGetAll(ctx, r.entryKey(inode)).Result()
if err != nil {
return errno(err)
}
*entries = []*Entry{
{
Inode: inode,
Expand All @@ -1185,26 +1181,47 @@ func (r *redisMeta) Readdir(ctx Context, inode Ino, plus uint8, entries *[]*Entr
Attr: &Attr{Typ: TypeDirectory},
})
}
vals, err := r.rdb.HGetAll(ctx, r.entryKey(inode)).Result()
if err != nil {
return errno(err)
}
newEntries := make([]Entry, len(vals))
newAttrs := make([]Attr, len(newEntries))
var i int
for name, val := range vals {
typ, inode := r.parseEntry([]byte(val))
*entries = append(*entries, &Entry{
Inode: inode,
Name: []byte(name),
Attr: &Attr{Typ: typ},
})
ent := newEntries[i]
ent.Inode = inode
ent.Name = []byte(name)
attr := newAttrs[i]
attr.Typ = typ
ent.Attr = &attr
*entries = append(*entries, &ent)
i++
}
if plus != 0 {
var keys []string
for _, e := range *entries {
keys = append(keys, r.inodeKey(e.Inode))
}
rs, _ := r.rdb.MGet(ctx, keys...).Result()
for i, re := range rs {
if re != nil {
if a, ok := re.(string); ok {
r.parseAttr([]byte(a), (*entries)[i].Attr)
batchSize := 4096
if batchSize > len(*entries) {
batchSize = len(*entries)
}
keysBatch := make([]string, 0, batchSize)
for i := 0; i < len(*entries); i += batchSize {
end := i + batchSize
if end > len(*entries) {
end = len(*entries)
}
for _, e := range (*entries)[i:end] {
keysBatch = append(keysBatch, r.inodeKey(e.Inode))
}
rs, _ := r.rdb.MGet(ctx, keysBatch...).Result()
for j, re := range rs {
if re != nil {
if a, ok := re.(string); ok {
r.parseAttr([]byte(a), (*entries)[i+j].Attr)
}
}
}
keysBatch = keysBatch[:0]
}
}
return 0
Expand Down

0 comments on commit 0da0ac5

Please sign in to comment.