From 0d0ecf5b108a68e94d8c970ac5fc7ab523577fc5 Mon Sep 17 00:00:00 2001 From: Denis Kirillov Date: Mon, 24 Oct 2022 17:44:11 +0300 Subject: [PATCH] [#650] Drop unused object cache methods Signed-off-by: Denis Kirillov --- api/cache/cache_test.go | 11 +++--- api/cache/objectslist.go | 25 +----------- api/cache/objectslist_test.go | 72 +++++++++++++++++------------------ 3 files changed, 43 insertions(+), 65 deletions(-) diff --git a/api/cache/cache_test.go b/api/cache/cache_test.go index 3d46ad79..38cae725 100644 --- a/api/cache/cache_test.go +++ b/api/cache/cache_test.go @@ -6,7 +6,6 @@ import ( "github.com/nspcc-dev/neofs-s3-gw/api/data" "github.com/nspcc-dev/neofs-s3-gw/creds/accessbox" 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" "go.uber.org/zap" @@ -104,17 +103,17 @@ func TestObjectsListCacheType(t *testing.T) { cnrID := cidtest.ID() key := ObjectsListKey{cid: cnrID, prefix: "obj"} - objIDs := []oid.ID{oidtest.ID()} + versions := []*data.NodeVersion{{BaseNodeVersion: data.BaseNodeVersion{OID: oidtest.ID()}}} - err := cache.Put(key, objIDs) + err := cache.PutVersions(key, versions) require.NoError(t, err) - val := cache.Get(key) - require.Equal(t, objIDs, val) + val := cache.GetVersions(key) + require.Equal(t, versions, val) require.Equal(t, 0, observedLog.Len()) err = cache.cache.Set(key, "tmp") require.NoError(t, err) - assertInvalidCacheEntry(t, cache.Get(key), observedLog) + assertInvalidCacheEntry(t, cache.GetVersions(key), observedLog) err = cache.cache.Set("key", "tmp") require.NoError(t, err) diff --git a/api/cache/objectslist.go b/api/cache/objectslist.go index 976c762e..e9827ce5 100644 --- a/api/cache/objectslist.go +++ b/api/cache/objectslist.go @@ -9,7 +9,6 @@ import ( "github.com/bluele/gcache" "github.com/nspcc-dev/neofs-s3-gw/api/data" cid "github.com/nspcc-dev/neofs-sdk-go/container/id" - oid "github.com/nspcc-dev/neofs-sdk-go/object/id" "go.uber.org/zap" ) @@ -67,23 +66,6 @@ func NewObjectsListCache(config *Config) *ObjectsListCache { return &ObjectsListCache{cache: gc, logger: config.Logger} } -// Get returns a list of ObjectInfo. -func (l *ObjectsListCache) Get(key ObjectsListKey) []oid.ID { - entry, err := l.cache.Get(key) - if err != nil { - return nil - } - - result, ok := entry.([]oid.ID) - if !ok { - l.logger.Warn("invalid cache entry type", zap.String("actual", fmt.Sprintf("%T", entry)), - zap.String("expected", fmt.Sprintf("%T", result))) - return nil - } - - return result -} - // GetVersions returns a list of ObjectInfo. func (l *ObjectsListCache) GetVersions(key ObjectsListKey) []*data.NodeVersion { entry, err := l.cache.Get(key) @@ -93,17 +75,14 @@ func (l *ObjectsListCache) GetVersions(key ObjectsListKey) []*data.NodeVersion { result, ok := entry.([]*data.NodeVersion) if !ok { + l.logger.Warn("invalid cache entry type", zap.String("actual", fmt.Sprintf("%T", entry)), + zap.String("expected", fmt.Sprintf("%T", result))) return nil } return result } -// Put puts a list of objects to cache. -func (l *ObjectsListCache) Put(key ObjectsListKey, oids []oid.ID) error { - return l.cache.Set(key, oids) -} - // PutVersions puts a list of object versions to cache. func (l *ObjectsListCache) PutVersions(key ObjectsListKey, versions []*data.NodeVersion) error { return l.cache.Set(key, versions) diff --git a/api/cache/objectslist_test.go b/api/cache/objectslist_test.go index 2a842ca2..040eb18b 100644 --- a/api/cache/objectslist_test.go +++ b/api/cache/objectslist_test.go @@ -4,8 +4,8 @@ import ( "testing" "time" + "github.com/nspcc-dev/neofs-s3-gw/api/data" 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" "go.uber.org/zap" @@ -25,26 +25,26 @@ func getTestObjectsListConfig() *Config { func TestObjectsListCache(t *testing.T) { var ( listSize = 10 - ids []oid.ID + versions []*data.NodeVersion cidKey, cidKey2 = cidtest.ID(), cidtest.ID() ) for i := 0; i < listSize; i++ { - ids = append(ids, oidtest.ID()) + versions = append(versions, &data.NodeVersion{BaseNodeVersion: data.BaseNodeVersion{OID: oidtest.ID()}}) } t.Run("lifetime", func(t *testing.T) { var ( - config = getTestObjectsListConfig() - cache = NewObjectsListCache(config) - cacheKey = ObjectsListKey{cid: cidKey} + config = getTestObjectsListConfig() + cache = NewObjectsListCache(config) + listKey = ObjectsListKey{cid: cidKey} ) - err := cache.Put(cacheKey, ids) + err := cache.PutVersions(listKey, versions) require.NoError(t, err) condition := func() bool { - return cache.Get(cacheKey) == nil + return cache.GetVersions(listKey) == nil } require.Never(t, condition, config.Lifetime, time.Second) @@ -53,41 +53,41 @@ func TestObjectsListCache(t *testing.T) { t.Run("get cache with empty prefix", func(t *testing.T) { var ( - cache = NewObjectsListCache(getTestObjectsListConfig()) - cacheKey = ObjectsListKey{cid: cidKey} + cache = NewObjectsListCache(getTestObjectsListConfig()) + listKey = ObjectsListKey{cid: cidKey} ) - err := cache.Put(cacheKey, ids) + err := cache.PutVersions(listKey, versions) require.NoError(t, err) - actual := cache.Get(cacheKey) + actual := cache.GetVersions(listKey) - require.Equal(t, len(ids), len(actual)) - for i := range ids { - require.Equal(t, ids[i], actual[i]) + require.Equal(t, len(versions), len(actual)) + for i := range versions { + require.Equal(t, versions[i], actual[i]) } }) t.Run("get cache with prefix", func(t *testing.T) { - cacheKey := ObjectsListKey{ + listKey := ObjectsListKey{ cid: cidKey, prefix: "dir", } cache := NewObjectsListCache(getTestObjectsListConfig()) - err := cache.Put(cacheKey, ids) + err := cache.PutVersions(listKey, versions) require.NoError(t, err) - actual := cache.Get(cacheKey) + actual := cache.GetVersions(listKey) - require.Equal(t, len(ids), len(actual)) - for i := range ids { - require.Equal(t, ids[i], actual[i]) + require.Equal(t, len(versions), len(actual)) + for i := range versions { + require.Equal(t, versions[i], actual[i]) } }) t.Run("get cache with other prefix", func(t *testing.T) { var ( - cacheKey = ObjectsListKey{ + listKey = ObjectsListKey{ cid: cidKey, prefix: "dir", } @@ -99,16 +99,16 @@ func TestObjectsListCache(t *testing.T) { ) cache := NewObjectsListCache(getTestObjectsListConfig()) - err := cache.Put(cacheKey, ids) + err := cache.PutVersions(listKey, versions) require.NoError(t, err) - actual := cache.Get(newKey) + actual := cache.GetVersions(newKey) require.Nil(t, actual) }) t.Run("get cache with non-existing key", func(t *testing.T) { var ( - cacheKey = ObjectsListKey{ + listKey = ObjectsListKey{ cid: cidKey, } newKey = ObjectsListKey{ @@ -117,19 +117,19 @@ func TestObjectsListCache(t *testing.T) { ) cache := NewObjectsListCache(getTestObjectsListConfig()) - err := cache.Put(cacheKey, ids) + err := cache.PutVersions(listKey, versions) require.NoError(t, err) - actual := cache.Get(newKey) + actual := cache.GetVersions(newKey) require.Nil(t, actual) }) } func TestCleanCacheEntriesChangedWithPutObject(t *testing.T) { var ( - id = cidtest.ID() - oids = []oid.ID{oidtest.ID()} - keys []ObjectsListKey + id = cidtest.ID() + versions = []*data.NodeVersion{{BaseNodeVersion: data.BaseNodeVersion{OID: oidtest.ID()}}} + keys []ObjectsListKey ) for _, p := range []string{"", "dir/", "dir/lol/"} { @@ -141,12 +141,12 @@ func TestCleanCacheEntriesChangedWithPutObject(t *testing.T) { config.Lifetime = time.Minute cache := NewObjectsListCache(config) for _, k := range keys { - err := cache.Put(k, oids) + err := cache.PutVersions(k, versions) require.NoError(t, err) } cache.CleanCacheEntriesContainingObject("obj1", id) for _, k := range keys { - list := cache.Get(k) + list := cache.GetVersions(k) if k.prefix == "" { require.Nil(t, list) } else { @@ -160,12 +160,12 @@ func TestCleanCacheEntriesChangedWithPutObject(t *testing.T) { config.Lifetime = time.Minute cache := NewObjectsListCache(config) for _, k := range keys { - err := cache.Put(k, oids) + err := cache.PutVersions(k, versions) require.NoError(t, err) } cache.CleanCacheEntriesContainingObject("dir/obj", id) for _, k := range keys { - list := cache.Get(k) + list := cache.GetVersions(k) if k.prefix == "" || k.prefix == "dir/" { require.Nil(t, list) } else { @@ -179,12 +179,12 @@ func TestCleanCacheEntriesChangedWithPutObject(t *testing.T) { config.Lifetime = time.Minute cache := NewObjectsListCache(config) for _, k := range keys { - err := cache.Put(k, oids) + err := cache.PutVersions(k, versions) require.NoError(t, err) } cache.CleanCacheEntriesContainingObject("dir/lol/obj", id) for _, k := range keys { - list := cache.Get(k) + list := cache.GetVersions(k) require.Nil(t, list) } })