Skip to content

Commit

Permalink
[nspcc-dev#112] Fix comments from PR
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 28, 2021
1 parent 17b49f7 commit 8702d1e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 43 deletions.
18 changes: 1 addition & 17 deletions api/handler/put.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package handler

import (
"context"
"encoding/xml"
"fmt"
"net/http"
Expand All @@ -13,7 +12,6 @@ import (
"github.com/nspcc-dev/neofs-node/pkg/policy"
"github.com/nspcc-dev/neofs-s3-gw/api"
"github.com/nspcc-dev/neofs-s3-gw/api/layer"
"github.com/nspcc-dev/neofs-s3-gw/creds/accessbox"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -107,7 +105,7 @@ func (h *handler) CreateBucketHandler(w http.ResponseWriter, r *http.Request) {
return
}

p.BoxData, err = getBoxData(r.Context())
p.BoxData, err = layer.GetBoxData(r.Context())
if err != nil {
h.registerAndSendError(w, r, err, "could not get boxData")
return
Expand Down Expand Up @@ -172,17 +170,3 @@ func parseBasicACL(basicACL string) (uint32, error) {
return uint32(value), nil
}
}

func getBoxData(ctx context.Context) (*accessbox.Box, error) {
var boxData *accessbox.Box
data, ok := ctx.Value(api.BoxData).(*accessbox.Box)
if !ok || data == nil {
return nil, fmt.Errorf("couldn't get box data from context")
}

boxData = data
if boxData.Gate == nil {
boxData.Gate = &accessbox.GateData{}
}
return boxData, nil
}
3 changes: 1 addition & 2 deletions api/layer/layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,10 @@ 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,
cache: cache,
cache: newListObjectsCache(),
}
}

Expand Down
23 changes: 13 additions & 10 deletions api/layer/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ 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 @@ -272,7 +271,7 @@ func (n *layer) ListObjectsV2(ctx context.Context, p *ListObjectsParamsV2) (*Lis
return nil, err
}

if data, ok := ctx.Value(api.BoxData).(*accessbox.Box); ok && data != nil {
if data, err := GetBoxData(ctx); data != nil && err == nil {
accessKey = data.Gate.AccessKey
} else {
return nil, fmt.Errorf("couldn't get access key from the accessbox: %s", err)
Expand All @@ -281,7 +280,7 @@ func (n *layer) ListObjectsV2(ctx context.Context, p *ListObjectsParamsV2) (*Lis
cacheKey = createKey(accessKey, bkt.CID)

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

Expand All @@ -297,20 +296,15 @@ func (n *layer) ListObjectsV2(ctx context.Context, p *ListObjectsParamsV2) (*Lis
}

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

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

restObjects := allObjects[p.MaxKeys:]
n.cache.PutCache(cacheKey, restObjects)
n.cache.Put(cacheKey, restObjects)
result.NextContinuationToken = restObjects[0].id.String()

allObjects = allObjects[:p.MaxKeys]
Expand Down Expand Up @@ -381,3 +375,12 @@ func trimStartAfter(startAfter string, objects []*ObjectInfo) []*ObjectInfo {
}
return objects
}

func trimAfterObjectID(id string, objects []*ObjectInfo) []*ObjectInfo {
for i, obj := range objects {
if obj.ID().String() == id {
return objects[i:]
}
}
return objects
}
22 changes: 8 additions & 14 deletions api/layer/object_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
// ObjectsListV2Cache provides interface for cache of ListObjectsV2 in a layer struct.
type (
ObjectsListV2Cache interface {
GetCache(token string, key string) []*ObjectInfo
PutCache(key string, objects []*ObjectInfo)
Get(token string, key string) []*ObjectInfo
Put(key string, objects []*ObjectInfo)
}
)

Expand All @@ -40,26 +40,21 @@ type (
mtx sync.RWMutex
}
cache struct {
list []*ObjectInfo
created time.Time
list []*ObjectInfo
}
)

func newListObjectsCache() *listObjectsCache {
c := listObjectsCache{}
c.caches = make(map[string]cache)
return &c
return &listObjectsCache{
caches: make(map[string]cache),
}
}

func (l *listObjectsCache) GetCache(token, key string) []*ObjectInfo {
func (l *listObjectsCache) Get(token, key string) []*ObjectInfo {
l.mtx.RLock()
defer l.mtx.RUnlock()
if val, ok := l.caches[key]; ok {
for i, obj := range val.list {
if obj.ID().String() == token {
return val.list[i:]
}
}
return trimAfterObjectID(token, val.list)
}

return nil
Expand All @@ -72,7 +67,6 @@ func (l *listObjectsCache) PutCache(key string, objects []*ObjectInfo) {
l.mtx.Lock()
defer l.mtx.Unlock()
c.list = objects
c.created = time.Now()
l.caches[key] = c
time.AfterFunc(defaultCacheLifetime, func() {
l.mtx.Lock()
Expand Down
19 changes: 19 additions & 0 deletions api/layer/util.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package layer

import (
"context"
"fmt"
"os"
"strconv"
"strings"
"time"

"github.com/nspcc-dev/neofs-api-go/pkg/object"
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
"github.com/nspcc-dev/neofs-s3-gw/api"
"github.com/nspcc-dev/neofs-s3-gw/creds/accessbox"
)

type (
Expand Down Expand Up @@ -172,3 +176,18 @@ func (o *ObjectInfo) ID() *object.ID { return o.id }

// IsDir allows to check if object is a directory.
func (o *ObjectInfo) IsDir() bool { return o.isDir }

// GetBoxData extracts accessbox.Box from context.
func GetBoxData(ctx context.Context) (*accessbox.Box, error) {
var boxData *accessbox.Box
data, ok := ctx.Value(api.BoxData).(*accessbox.Box)
if !ok || data == nil {
return nil, fmt.Errorf("couldn't get box data from context")
}

boxData = data
if boxData.Gate == nil {
boxData.Gate = &accessbox.GateData{}
}
return boxData, nil
}

0 comments on commit 8702d1e

Please sign in to comment.