Skip to content

Commit

Permalink
Cache space lookup indexes in memory
Browse files Browse the repository at this point in the history
  • Loading branch information
aduffeck committed Jun 16, 2023
1 parent 539c44e commit 4e1cf9b
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 20 deletions.
4 changes: 3 additions & 1 deletion pkg/storage/utils/decomposedfs/decomposedfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import (
"github.com/cs3org/reva/v2/pkg/storage/utils/decomposedfs/lookup"
"github.com/cs3org/reva/v2/pkg/storage/utils/decomposedfs/metadata"
"github.com/cs3org/reva/v2/pkg/storage/utils/decomposedfs/migrator"
"github.com/cs3org/reva/v2/pkg/storage/utils/decomposedfs/mtimesyncedcache"
"github.com/cs3org/reva/v2/pkg/storage/utils/decomposedfs/node"
"github.com/cs3org/reva/v2/pkg/storage/utils/decomposedfs/options"
"github.com/cs3org/reva/v2/pkg/storage/utils/decomposedfs/tree"
Expand Down Expand Up @@ -99,7 +100,8 @@ type Decomposedfs struct {
stream events.Stream
cache cache.StatCache

UserCache *ttlcache.Cache
UserCache *ttlcache.Cache
spaceIDCache mtimesyncedcache.Cache[string, map[string]string]
}

// NewDefault returns an instance with default components
Expand Down
98 changes: 79 additions & 19 deletions pkg/storage/utils/decomposedfs/spaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,17 +299,37 @@ func (fs *Decomposedfs) ListStorageSpaces(ctx context.Context, filter []*provide
matches := map[string]struct{}{}

if requestedUserID != nil {
path := filepath.Join(fs.o.Root, "indexes", "by-user-id", requestedUserID.GetOpaqueId(), nodeID)
m, err := filepath.Glob(path)
indexPath := filepath.Join(fs.o.Root, "indexes", "by-user-id", requestedUserID.GetOpaqueId())
fi, err := os.Stat(indexPath)
if err != nil {
return nil, err
}
for _, match := range m {
link, err := os.Readlink(match)
allMatches, err := fs.spaceIDCache.LoadOrStore("by-user-id:"+requestedUserID.GetOpaqueId(), fi.ModTime(), func() (map[string]string, error) {
path := filepath.Join(fs.o.Root, "indexes", "by-user-id", requestedUserID.GetOpaqueId(), "*")
m, err := filepath.Glob(path)
if err != nil {
continue
return nil, err
}
matches := map[string]string{}
for _, match := range m {
link, err := os.Readlink(match)
if err != nil {
continue
}
matches[match] = link
}
matches[link] = struct{}{}
return matches, nil
})
if err != nil {
return nil, err
}

if nodeID == spaceIDAny {
for _, match := range allMatches {
matches[match] = struct{}{}
}
} else {
matches[allMatches[nodeID]] = struct{}{}
}

// get Groups for userid
Expand All @@ -323,35 +343,75 @@ func (fs *Decomposedfs) ListStorageSpaces(ctx context.Context, filter []*provide
}

for _, group := range user.Groups {
path := filepath.Join(fs.o.Root, "indexes", "by-group-id", group, nodeID)
m, err := filepath.Glob(path)
indexPath := filepath.Join(fs.o.Root, "indexes", "by-group-id", group)
fi, err := os.Stat(indexPath)
if err != nil {
return nil, err
continue
}
for _, match := range m {
link, err := os.Readlink(match)
allMatches, err := fs.spaceIDCache.LoadOrStore("by-group-id:"+group, fi.ModTime(), func() (map[string]string, error) {
path := filepath.Join(fs.o.Root, "indexes", "by-group-id", group, "*")
m, err := filepath.Glob(path)
if err != nil {
continue
return nil, err
}
matches := map[string]string{}
for _, match := range m {
link, err := os.Readlink(match)
if err != nil {
continue
}
matches[match] = link
}
matches[link] = struct{}{}
return matches, nil
})
if err != nil {
return nil, err
}

if nodeID == spaceIDAny {
for _, match := range allMatches {
matches[match] = struct{}{}
}
} else {
matches[allMatches[nodeID]] = struct{}{}
}
}

}

if requestedUserID == nil {
for spaceType := range spaceTypes {
path := filepath.Join(fs.o.Root, "indexes", "by-type", spaceType, nodeID)
m, err := filepath.Glob(path)
indexPath := filepath.Join(fs.o.Root, "indexes", "by-type", spaceType)
fi, err := os.Stat(indexPath)
if err != nil {
return nil, err
}
for _, match := range m {
link, err := os.Readlink(match)
allMatches, err := fs.spaceIDCache.LoadOrStore("by-type:"+spaceType, fi.ModTime(), func() (map[string]string, error) {
path := filepath.Join(fs.o.Root, "indexes", "by-type", spaceType, "*")
m, err := filepath.Glob(path)
if err != nil {
continue
return nil, err
}
matches := map[string]string{}
for _, match := range m {
link, err := os.Readlink(match)
if err != nil {
continue
}
matches[match] = link
}
return matches, nil
})
if err != nil {
return nil, err
}

if nodeID == spaceIDAny {
for _, match := range allMatches {
matches[match] = struct{}{}
}
matches[link] = struct{}{}
} else {
matches[allMatches[nodeID]] = struct{}{}
}
}
}
Expand Down

0 comments on commit 4e1cf9b

Please sign in to comment.