From 206a7aa3955ebbd4685e432cd0cf5b4427583d91 Mon Sep 17 00:00:00 2001 From: Denis Kirillov Date: Tue, 28 Jun 2022 15:56:41 +0300 Subject: [PATCH] [#488] Change cache key types Signed-off-by: Denis Kirillov --- api/cache/accessbox.go | 4 ++-- api/cache/cache_test.go | 6 +++--- api/cache/objects.go | 8 +++----- api/cache/objectslist.go | 7 +++---- api/cache/objectslist_test.go | 26 +++++++++++++------------- 5 files changed, 24 insertions(+), 27 deletions(-) diff --git a/api/cache/accessbox.go b/api/cache/accessbox.go index ef9c2763..e23dbd84 100644 --- a/api/cache/accessbox.go +++ b/api/cache/accessbox.go @@ -50,7 +50,7 @@ func NewAccessBoxCache(config *Config) *AccessBoxCache { // Get returns a cached object. 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 { return nil } @@ -67,5 +67,5 @@ func (o *AccessBoxCache) Get(address oid.Address) *accessbox.Box { // Put stores an object to cache. func (o *AccessBoxCache) Put(address oid.Address, box *accessbox.Box) error { - return o.cache.Set(address.EncodeToString(), box) + return o.cache.Set(address, box) } diff --git a/api/cache/cache_test.go b/api/cache/cache_test.go index 4e97535f..0f66215a 100644 --- a/api/cache/cache_test.go +++ b/api/cache/cache_test.go @@ -26,7 +26,7 @@ func TestAccessBoxCacheType(t *testing.T) { require.Equal(t, box, val) require.Equal(t, 0, observedLog.Len()) - err = cache.cache.Set(addr.EncodeToString(), "tmp") + err = cache.cache.Set(addr, "tmp") require.NoError(t, err) assertInvalidCacheEntry(t, cache.Get(addr), observedLog) } @@ -82,7 +82,7 @@ func TestObjectCacheType(t *testing.T) { require.Equal(t, objInfo, val) require.Equal(t, 0, observedLog.Len()) - err = cache.cache.Set(addr.EncodeToString(), "tmp") + err = cache.cache.Set(addr, "tmp") require.NoError(t, err) assertInvalidCacheEntry(t, cache.GetObject(addr), observedLog) } @@ -92,7 +92,7 @@ func TestObjectsListCacheType(t *testing.T) { cache := NewObjectsListCache(DefaultObjectsListConfig(logger)) cnrID := cidtest.ID() - key := ObjectsListKey{cid: cnrID.EncodeToString(), prefix: "obj"} + key := ObjectsListKey{cid: cnrID, prefix: "obj"} objIDs := []oid.ID{oidtest.ID()} err := cache.Put(key, objIDs) diff --git a/api/cache/objects.go b/api/cache/objects.go index a1d0cc1e..440cb5c3 100644 --- a/api/cache/objects.go +++ b/api/cache/objects.go @@ -40,7 +40,7 @@ func New(config *Config) *ObjectsCache { // GetObject returns a cached object info. 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 { return nil } @@ -57,12 +57,10 @@ func (o *ObjectsCache) GetObject(address oid.Address) *data.ObjectInfo { // PutObject puts an object info to cache. func (o *ObjectsCache) PutObject(obj *data.ObjectInfo) error { - cnrID := obj.CID.EncodeToString() - objID := obj.ID.EncodeToString() - return o.cache.Set(cnrID+"/"+objID, obj) + return o.cache.Set(obj.Address(), obj) } // Delete deletes an object from cache. func (o *ObjectsCache) Delete(address oid.Address) bool { - return o.cache.Remove(address.EncodeToString()) + return o.cache.Remove(address) } diff --git a/api/cache/objectslist.go b/api/cache/objectslist.go index d7649fdf..db9d3bc2 100644 --- a/api/cache/objectslist.go +++ b/api/cache/objectslist.go @@ -34,7 +34,7 @@ type ( // ObjectsListKey is a key to find a ObjectsListCache's entry. ObjectsListKey struct { - cid string + cid cid.ID prefix string latestOnly bool } @@ -106,7 +106,6 @@ func (l *ObjectsListCache) PutVersions(key ObjectsListKey, versions []*data.Node // CleanCacheEntriesContainingObject deletes entries containing specified object. func (l *ObjectsListCache) CleanCacheEntriesContainingObject(objectName string, cnr cid.ID) { - cidStr := cnr.EncodeToString() keys := l.cache.Keys(true) for _, key := range keys { k, ok := key.(ObjectsListKey) @@ -115,7 +114,7 @@ func (l *ObjectsListCache) CleanCacheEntriesContainingObject(objectName string, zap.String("expected", fmt.Sprintf("%T", k))) continue } - if cidStr == k.cid && strings.HasPrefix(objectName, k.prefix) { + if cnr.Equals(k.cid) && strings.HasPrefix(objectName, k.prefix) { 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. func CreateObjectsListCacheKey(cnr cid.ID, prefix string, latestOnly bool) ObjectsListKey { p := ObjectsListKey{ - cid: cnr.EncodeToString(), + cid: cnr, prefix: prefix, latestOnly: latestOnly, } diff --git a/api/cache/objectslist_test.go b/api/cache/objectslist_test.go index 672869c8..2a842ca2 100644 --- a/api/cache/objectslist_test.go +++ b/api/cache/objectslist_test.go @@ -4,7 +4,7 @@ import ( "testing" "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" oidtest "github.com/nspcc-dev/neofs-sdk-go/object/id/test" "github.com/stretchr/testify/require" @@ -24,9 +24,9 @@ func getTestObjectsListConfig() *Config { func TestObjectsListCache(t *testing.T) { var ( - listSize = 10 - ids []oid.ID - userKey = "key" + listSize = 10 + ids []oid.ID + cidKey, cidKey2 = cidtest.ID(), cidtest.ID() ) for i := 0; i < listSize; i++ { @@ -37,7 +37,7 @@ func TestObjectsListCache(t *testing.T) { var ( config = getTestObjectsListConfig() cache = NewObjectsListCache(config) - cacheKey = ObjectsListKey{cid: userKey} + cacheKey = ObjectsListKey{cid: cidKey} ) 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) { var ( cache = NewObjectsListCache(getTestObjectsListConfig()) - cacheKey = ObjectsListKey{cid: userKey} + cacheKey = ObjectsListKey{cid: cidKey} ) err := cache.Put(cacheKey, ids) require.NoError(t, err) @@ -69,7 +69,7 @@ func TestObjectsListCache(t *testing.T) { t.Run("get cache with prefix", func(t *testing.T) { cacheKey := ObjectsListKey{ - cid: userKey, + cid: cidKey, prefix: "dir", } @@ -88,12 +88,12 @@ func TestObjectsListCache(t *testing.T) { t.Run("get cache with other prefix", func(t *testing.T) { var ( cacheKey = ObjectsListKey{ - cid: userKey, + cid: cidKey, prefix: "dir", } newKey = ObjectsListKey{ - cid: "key", + cid: cidKey, prefix: "obj", } ) @@ -109,10 +109,10 @@ func TestObjectsListCache(t *testing.T) { t.Run("get cache with non-existing key", func(t *testing.T) { var ( cacheKey = ObjectsListKey{ - cid: userKey, + cid: cidKey, } newKey = ObjectsListKey{ - cid: "asdf", + cid: cidKey2, } ) @@ -127,13 +127,13 @@ func TestObjectsListCache(t *testing.T) { func TestCleanCacheEntriesChangedWithPutObject(t *testing.T) { var ( - id cid.ID + id = cidtest.ID() oids = []oid.ID{oidtest.ID()} keys []ObjectsListKey ) 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) {