forked from TrueCloudLab/frostfs-s3-gw
[#488] Change cache key types
Signed-off-by: Denis Kirillov <denis@nspcc.ru>
This commit is contained in:
parent
e104855633
commit
206a7aa395
5 changed files with 24 additions and 27 deletions
4
api/cache/accessbox.go
vendored
4
api/cache/accessbox.go
vendored
|
@ -50,7 +50,7 @@ func NewAccessBoxCache(config *Config) *AccessBoxCache {
|
||||||
|
|
||||||
// Get returns a cached object.
|
// Get returns a cached object.
|
||||||
func (o *AccessBoxCache) Get(address oid.Address) *accessbox.Box {
|
func (o *AccessBoxCache) Get(address oid.Address) *accessbox.Box {
|
||||||
entry, err := o.cache.Get(address.EncodeToString())
|
entry, err := o.cache.Get(address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -67,5 +67,5 @@ func (o *AccessBoxCache) Get(address oid.Address) *accessbox.Box {
|
||||||
|
|
||||||
// Put stores an object to cache.
|
// Put stores an object to cache.
|
||||||
func (o *AccessBoxCache) Put(address oid.Address, box *accessbox.Box) error {
|
func (o *AccessBoxCache) Put(address oid.Address, box *accessbox.Box) error {
|
||||||
return o.cache.Set(address.EncodeToString(), box)
|
return o.cache.Set(address, box)
|
||||||
}
|
}
|
||||||
|
|
6
api/cache/cache_test.go
vendored
6
api/cache/cache_test.go
vendored
|
@ -26,7 +26,7 @@ func TestAccessBoxCacheType(t *testing.T) {
|
||||||
require.Equal(t, box, val)
|
require.Equal(t, box, val)
|
||||||
require.Equal(t, 0, observedLog.Len())
|
require.Equal(t, 0, observedLog.Len())
|
||||||
|
|
||||||
err = cache.cache.Set(addr.EncodeToString(), "tmp")
|
err = cache.cache.Set(addr, "tmp")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assertInvalidCacheEntry(t, cache.Get(addr), observedLog)
|
assertInvalidCacheEntry(t, cache.Get(addr), observedLog)
|
||||||
}
|
}
|
||||||
|
@ -82,7 +82,7 @@ func TestObjectCacheType(t *testing.T) {
|
||||||
require.Equal(t, objInfo, val)
|
require.Equal(t, objInfo, val)
|
||||||
require.Equal(t, 0, observedLog.Len())
|
require.Equal(t, 0, observedLog.Len())
|
||||||
|
|
||||||
err = cache.cache.Set(addr.EncodeToString(), "tmp")
|
err = cache.cache.Set(addr, "tmp")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assertInvalidCacheEntry(t, cache.GetObject(addr), observedLog)
|
assertInvalidCacheEntry(t, cache.GetObject(addr), observedLog)
|
||||||
}
|
}
|
||||||
|
@ -92,7 +92,7 @@ func TestObjectsListCacheType(t *testing.T) {
|
||||||
cache := NewObjectsListCache(DefaultObjectsListConfig(logger))
|
cache := NewObjectsListCache(DefaultObjectsListConfig(logger))
|
||||||
|
|
||||||
cnrID := cidtest.ID()
|
cnrID := cidtest.ID()
|
||||||
key := ObjectsListKey{cid: cnrID.EncodeToString(), prefix: "obj"}
|
key := ObjectsListKey{cid: cnrID, prefix: "obj"}
|
||||||
objIDs := []oid.ID{oidtest.ID()}
|
objIDs := []oid.ID{oidtest.ID()}
|
||||||
|
|
||||||
err := cache.Put(key, objIDs)
|
err := cache.Put(key, objIDs)
|
||||||
|
|
8
api/cache/objects.go
vendored
8
api/cache/objects.go
vendored
|
@ -40,7 +40,7 @@ func New(config *Config) *ObjectsCache {
|
||||||
|
|
||||||
// GetObject returns a cached object info.
|
// GetObject returns a cached object info.
|
||||||
func (o *ObjectsCache) GetObject(address oid.Address) *data.ObjectInfo {
|
func (o *ObjectsCache) GetObject(address oid.Address) *data.ObjectInfo {
|
||||||
entry, err := o.cache.Get(address.EncodeToString())
|
entry, err := o.cache.Get(address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -57,12 +57,10 @@ func (o *ObjectsCache) GetObject(address oid.Address) *data.ObjectInfo {
|
||||||
|
|
||||||
// PutObject puts an object info to cache.
|
// PutObject puts an object info to cache.
|
||||||
func (o *ObjectsCache) PutObject(obj *data.ObjectInfo) error {
|
func (o *ObjectsCache) PutObject(obj *data.ObjectInfo) error {
|
||||||
cnrID := obj.CID.EncodeToString()
|
return o.cache.Set(obj.Address(), obj)
|
||||||
objID := obj.ID.EncodeToString()
|
|
||||||
return o.cache.Set(cnrID+"/"+objID, obj)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete deletes an object from cache.
|
// Delete deletes an object from cache.
|
||||||
func (o *ObjectsCache) Delete(address oid.Address) bool {
|
func (o *ObjectsCache) Delete(address oid.Address) bool {
|
||||||
return o.cache.Remove(address.EncodeToString())
|
return o.cache.Remove(address)
|
||||||
}
|
}
|
||||||
|
|
7
api/cache/objectslist.go
vendored
7
api/cache/objectslist.go
vendored
|
@ -34,7 +34,7 @@ type (
|
||||||
|
|
||||||
// ObjectsListKey is a key to find a ObjectsListCache's entry.
|
// ObjectsListKey is a key to find a ObjectsListCache's entry.
|
||||||
ObjectsListKey struct {
|
ObjectsListKey struct {
|
||||||
cid string
|
cid cid.ID
|
||||||
prefix string
|
prefix string
|
||||||
latestOnly bool
|
latestOnly bool
|
||||||
}
|
}
|
||||||
|
@ -106,7 +106,6 @@ func (l *ObjectsListCache) PutVersions(key ObjectsListKey, versions []*data.Node
|
||||||
|
|
||||||
// CleanCacheEntriesContainingObject deletes entries containing specified object.
|
// CleanCacheEntriesContainingObject deletes entries containing specified object.
|
||||||
func (l *ObjectsListCache) CleanCacheEntriesContainingObject(objectName string, cnr cid.ID) {
|
func (l *ObjectsListCache) CleanCacheEntriesContainingObject(objectName string, cnr cid.ID) {
|
||||||
cidStr := cnr.EncodeToString()
|
|
||||||
keys := l.cache.Keys(true)
|
keys := l.cache.Keys(true)
|
||||||
for _, key := range keys {
|
for _, key := range keys {
|
||||||
k, ok := key.(ObjectsListKey)
|
k, ok := key.(ObjectsListKey)
|
||||||
|
@ -115,7 +114,7 @@ func (l *ObjectsListCache) CleanCacheEntriesContainingObject(objectName string,
|
||||||
zap.String("expected", fmt.Sprintf("%T", k)))
|
zap.String("expected", fmt.Sprintf("%T", k)))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if cidStr == k.cid && strings.HasPrefix(objectName, k.prefix) {
|
if cnr.Equals(k.cid) && strings.HasPrefix(objectName, k.prefix) {
|
||||||
l.cache.Remove(k)
|
l.cache.Remove(k)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -124,7 +123,7 @@ func (l *ObjectsListCache) CleanCacheEntriesContainingObject(objectName string,
|
||||||
// CreateObjectsListCacheKey returns ObjectsListKey with the given CID, prefix and latestOnly flag.
|
// CreateObjectsListCacheKey returns ObjectsListKey with the given CID, prefix and latestOnly flag.
|
||||||
func CreateObjectsListCacheKey(cnr cid.ID, prefix string, latestOnly bool) ObjectsListKey {
|
func CreateObjectsListCacheKey(cnr cid.ID, prefix string, latestOnly bool) ObjectsListKey {
|
||||||
p := ObjectsListKey{
|
p := ObjectsListKey{
|
||||||
cid: cnr.EncodeToString(),
|
cid: cnr,
|
||||||
prefix: prefix,
|
prefix: prefix,
|
||||||
latestOnly: latestOnly,
|
latestOnly: latestOnly,
|
||||||
}
|
}
|
||||||
|
|
22
api/cache/objectslist_test.go
vendored
22
api/cache/objectslist_test.go
vendored
|
@ -4,7 +4,7 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test"
|
||||||
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
||||||
oidtest "github.com/nspcc-dev/neofs-sdk-go/object/id/test"
|
oidtest "github.com/nspcc-dev/neofs-sdk-go/object/id/test"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
@ -26,7 +26,7 @@ func TestObjectsListCache(t *testing.T) {
|
||||||
var (
|
var (
|
||||||
listSize = 10
|
listSize = 10
|
||||||
ids []oid.ID
|
ids []oid.ID
|
||||||
userKey = "key"
|
cidKey, cidKey2 = cidtest.ID(), cidtest.ID()
|
||||||
)
|
)
|
||||||
|
|
||||||
for i := 0; i < listSize; i++ {
|
for i := 0; i < listSize; i++ {
|
||||||
|
@ -37,7 +37,7 @@ func TestObjectsListCache(t *testing.T) {
|
||||||
var (
|
var (
|
||||||
config = getTestObjectsListConfig()
|
config = getTestObjectsListConfig()
|
||||||
cache = NewObjectsListCache(config)
|
cache = NewObjectsListCache(config)
|
||||||
cacheKey = ObjectsListKey{cid: userKey}
|
cacheKey = ObjectsListKey{cid: cidKey}
|
||||||
)
|
)
|
||||||
|
|
||||||
err := cache.Put(cacheKey, ids)
|
err := cache.Put(cacheKey, ids)
|
||||||
|
@ -54,7 +54,7 @@ func TestObjectsListCache(t *testing.T) {
|
||||||
t.Run("get cache with empty prefix", func(t *testing.T) {
|
t.Run("get cache with empty prefix", func(t *testing.T) {
|
||||||
var (
|
var (
|
||||||
cache = NewObjectsListCache(getTestObjectsListConfig())
|
cache = NewObjectsListCache(getTestObjectsListConfig())
|
||||||
cacheKey = ObjectsListKey{cid: userKey}
|
cacheKey = ObjectsListKey{cid: cidKey}
|
||||||
)
|
)
|
||||||
err := cache.Put(cacheKey, ids)
|
err := cache.Put(cacheKey, ids)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -69,7 +69,7 @@ func TestObjectsListCache(t *testing.T) {
|
||||||
|
|
||||||
t.Run("get cache with prefix", func(t *testing.T) {
|
t.Run("get cache with prefix", func(t *testing.T) {
|
||||||
cacheKey := ObjectsListKey{
|
cacheKey := ObjectsListKey{
|
||||||
cid: userKey,
|
cid: cidKey,
|
||||||
prefix: "dir",
|
prefix: "dir",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,12 +88,12 @@ func TestObjectsListCache(t *testing.T) {
|
||||||
t.Run("get cache with other prefix", func(t *testing.T) {
|
t.Run("get cache with other prefix", func(t *testing.T) {
|
||||||
var (
|
var (
|
||||||
cacheKey = ObjectsListKey{
|
cacheKey = ObjectsListKey{
|
||||||
cid: userKey,
|
cid: cidKey,
|
||||||
prefix: "dir",
|
prefix: "dir",
|
||||||
}
|
}
|
||||||
|
|
||||||
newKey = ObjectsListKey{
|
newKey = ObjectsListKey{
|
||||||
cid: "key",
|
cid: cidKey,
|
||||||
prefix: "obj",
|
prefix: "obj",
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
@ -109,10 +109,10 @@ func TestObjectsListCache(t *testing.T) {
|
||||||
t.Run("get cache with non-existing key", func(t *testing.T) {
|
t.Run("get cache with non-existing key", func(t *testing.T) {
|
||||||
var (
|
var (
|
||||||
cacheKey = ObjectsListKey{
|
cacheKey = ObjectsListKey{
|
||||||
cid: userKey,
|
cid: cidKey,
|
||||||
}
|
}
|
||||||
newKey = ObjectsListKey{
|
newKey = ObjectsListKey{
|
||||||
cid: "asdf",
|
cid: cidKey2,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -127,13 +127,13 @@ func TestObjectsListCache(t *testing.T) {
|
||||||
|
|
||||||
func TestCleanCacheEntriesChangedWithPutObject(t *testing.T) {
|
func TestCleanCacheEntriesChangedWithPutObject(t *testing.T) {
|
||||||
var (
|
var (
|
||||||
id cid.ID
|
id = cidtest.ID()
|
||||||
oids = []oid.ID{oidtest.ID()}
|
oids = []oid.ID{oidtest.ID()}
|
||||||
keys []ObjectsListKey
|
keys []ObjectsListKey
|
||||||
)
|
)
|
||||||
|
|
||||||
for _, p := range []string{"", "dir/", "dir/lol/"} {
|
for _, p := range []string{"", "dir/", "dir/lol/"} {
|
||||||
keys = append(keys, ObjectsListKey{cid: id.EncodeToString(), prefix: p})
|
keys = append(keys, ObjectsListKey{cid: id, prefix: p})
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Run("put object to the root of the bucket", func(t *testing.T) {
|
t.Run("put object to the root of the bucket", func(t *testing.T) {
|
||||||
|
|
Loading…
Reference in a new issue