Skip to content

Commit

Permalink
[nspcc-dev#112] api:Add usages of cache in ListObjectsV2
Browse files Browse the repository at this point in the history
Signed-off-by: Angira Kekteeva <kira@nspcc.ru>
  • Loading branch information
masterSplinter01 committed Jul 26, 2021
1 parent c8ffeb6 commit 6a163a9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
11 changes: 7 additions & 4 deletions api/layer/layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import (

type (
layer struct {
pool pool.Pool
log *zap.Logger
pool pool.Pool
log *zap.Logger
cache ObjectsListV2Cache
}

// Params stores basic API parameters.
Expand Down Expand Up @@ -127,9 +128,11 @@ const (
// NewLayer creates instance of layer. It checks credentials
// and establishes gRPC connection with node.
func NewLayer(log *zap.Logger, conns pool.Pool) Client {
cache := newListObjectsCache()
return &layer{
pool: conns,
log: log,
pool: conns,
log: log,
cache: cache,
}
}

Expand Down
32 changes: 29 additions & 3 deletions api/layer/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package layer
import (
"context"
"errors"
"fmt"
"io"
"net/url"
"sort"
Expand All @@ -13,6 +14,7 @@ import (
cid "github.com/nspcc-dev/neofs-api-go/pkg/container/id"
"github.com/nspcc-dev/neofs-api-go/pkg/object"
"github.com/nspcc-dev/neofs-s3-gw/api"
"github.com/nspcc-dev/neofs-s3-gw/creds/accessbox"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -258,6 +260,8 @@ func (n *layer) ListObjectsV2(ctx context.Context, p *ListObjectsParamsV2) (*Lis
result ListObjectsInfoV2
allObjects []*ObjectInfo
bkt *BucketInfo
accessKey string
cacheKey string
)

if p.MaxKeys == 0 {
Expand All @@ -268,9 +272,19 @@ func (n *layer) ListObjectsV2(ctx context.Context, p *ListObjectsParamsV2) (*Lis
return nil, err
}

if p.ContinuationToken != "" {
// find cache with continuation token
if data, ok := ctx.Value(api.GateData).(*accessbox.GateData); ok && data != nil {
accessKey = data.AccessKey
} else {
return nil, fmt.Errorf("couldn't get access key from the accessbox: %s", err)
}

cacheKey = createKey(&accessKey, bkt.CID)

if p.ContinuationToken != "" {
allObjects = n.cache.GetCache(p.ContinuationToken, cacheKey)
}

if allObjects == nil {
allObjects, err = n.listSortedAllObjects(ctx, allObjectParams{
Bucket: bkt,
Prefix: p.Prefix,
Expand All @@ -280,13 +294,25 @@ func (n *layer) ListObjectsV2(ctx context.Context, p *ListObjectsParamsV2) (*Lis
if err != nil {
return nil, err
}

if p.ContinuationToken != "" {
for i, obj := range allObjects {
if obj.ID().String() == p.ContinuationToken {
allObjects = allObjects[i:]
break
}
}
}
}

if len(allObjects) > p.MaxKeys {
result.IsTruncated = true

restObjects := allObjects[p.MaxKeys:]
n.cache.PutCache(createKey(&accessKey, bkt.CID), restObjects)
result.NextContinuationToken = restObjects[0].id.String()

allObjects = allObjects[:p.MaxKeys]
// add creating of cache here
}

for _, ov := range allObjects {
Expand Down

0 comments on commit 6a163a9

Please sign in to comment.