2021-10-07 14:50:36 +00:00
|
|
|
package blobstor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
2022-07-05 13:47:39 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/blobovniczatree"
|
2022-01-10 12:46:01 +00:00
|
|
|
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object"
|
2021-10-07 14:50:36 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCompression(t *testing.T) {
|
|
|
|
dir, err := os.MkdirTemp("", "neofs*")
|
|
|
|
require.NoError(t, err)
|
|
|
|
t.Cleanup(func() { _ = os.RemoveAll(dir) })
|
|
|
|
|
|
|
|
const (
|
|
|
|
smallSizeLimit = 512
|
|
|
|
objCount = 4
|
|
|
|
)
|
|
|
|
|
|
|
|
newBlobStor := func(t *testing.T, compress bool) *BlobStor {
|
|
|
|
bs := New(WithCompressObjects(compress),
|
|
|
|
WithRootPath(dir),
|
2021-11-15 10:07:18 +00:00
|
|
|
WithSmallSizeLimit(smallSizeLimit),
|
|
|
|
WithBlobovniczaShallowWidth(1)) // default width is 16, slow init
|
2022-06-28 13:42:50 +00:00
|
|
|
require.NoError(t, bs.Open(false))
|
2021-10-07 14:50:36 +00:00
|
|
|
require.NoError(t, bs.Init())
|
|
|
|
return bs
|
|
|
|
}
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
bigObj := make([]*objectSDK.Object, objCount)
|
|
|
|
smallObj := make([]*objectSDK.Object, objCount)
|
2021-10-07 14:50:36 +00:00
|
|
|
for i := 0; i < objCount; i++ {
|
|
|
|
bigObj[i] = testObject(smallSizeLimit * 2)
|
|
|
|
smallObj[i] = testObject(smallSizeLimit / 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
testGet := func(t *testing.T, b *BlobStor, i int) {
|
2022-07-05 13:47:39 +00:00
|
|
|
var prm blobovniczatree.GetSmallPrm
|
|
|
|
prm.SetAddress(object.AddressOf(smallObj[i]))
|
|
|
|
|
|
|
|
res1, err := b.GetSmall(prm)
|
2021-10-07 14:50:36 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, smallObj[i], res1.Object())
|
|
|
|
|
2022-05-23 14:15:16 +00:00
|
|
|
res2, err := b.GetBig(GetBigPrm{address: address{object.AddressOf(bigObj[i])}})
|
2021-10-07 14:50:36 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, bigObj[i], res2.Object())
|
|
|
|
}
|
|
|
|
|
|
|
|
testPut := func(t *testing.T, b *BlobStor, i int) {
|
2022-05-23 14:15:16 +00:00
|
|
|
var prm PutPrm
|
2021-10-07 14:50:36 +00:00
|
|
|
prm.SetObject(smallObj[i])
|
|
|
|
_, err = b.Put(prm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-05-23 14:15:16 +00:00
|
|
|
prm = PutPrm{}
|
2021-10-07 14:50:36 +00:00
|
|
|
prm.SetObject(bigObj[i])
|
|
|
|
_, err = b.Put(prm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put and Get uncompressed object
|
|
|
|
blobStor := newBlobStor(t, false)
|
|
|
|
testPut(t, blobStor, 0)
|
|
|
|
testGet(t, blobStor, 0)
|
|
|
|
require.NoError(t, blobStor.Close())
|
|
|
|
|
|
|
|
blobStor = newBlobStor(t, true)
|
|
|
|
testGet(t, blobStor, 0) // get uncompressed object with compress enabled
|
|
|
|
testPut(t, blobStor, 1)
|
|
|
|
testGet(t, blobStor, 1)
|
|
|
|
require.NoError(t, blobStor.Close())
|
|
|
|
|
|
|
|
blobStor = newBlobStor(t, false)
|
|
|
|
testGet(t, blobStor, 0) // get old uncompressed object
|
|
|
|
testGet(t, blobStor, 1) // get compressed object with compression disabled
|
|
|
|
testPut(t, blobStor, 2)
|
|
|
|
testGet(t, blobStor, 2)
|
|
|
|
require.NoError(t, blobStor.Close())
|
|
|
|
}
|
2022-01-10 12:46:01 +00:00
|
|
|
|
|
|
|
func TestBlobstor_needsCompression(t *testing.T) {
|
|
|
|
const smallSizeLimit = 512
|
|
|
|
newBlobStor := func(t *testing.T, compress bool, ct ...string) *BlobStor {
|
|
|
|
dir, err := os.MkdirTemp("", "neofs*")
|
|
|
|
require.NoError(t, err)
|
|
|
|
t.Cleanup(func() { _ = os.RemoveAll(dir) })
|
|
|
|
|
|
|
|
bs := New(WithCompressObjects(compress),
|
|
|
|
WithRootPath(dir),
|
|
|
|
WithSmallSizeLimit(smallSizeLimit),
|
|
|
|
WithBlobovniczaShallowWidth(1),
|
|
|
|
WithUncompressableContentTypes(ct))
|
2022-06-28 13:42:50 +00:00
|
|
|
require.NoError(t, bs.Open(false))
|
2022-01-10 12:46:01 +00:00
|
|
|
require.NoError(t, bs.Init())
|
|
|
|
return bs
|
|
|
|
}
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
newObjectWithCt := func(contentType string) *objectSDK.Object {
|
|
|
|
obj := testObject(smallSizeLimit + 1)
|
2022-01-10 12:46:01 +00:00
|
|
|
if contentType != "" {
|
2022-03-15 12:11:35 +00:00
|
|
|
var a objectSDK.Attribute
|
2022-01-10 12:46:01 +00:00
|
|
|
a.SetKey(objectSDK.AttributeContentType)
|
|
|
|
a.SetValue(contentType)
|
|
|
|
obj.SetAttributes(a)
|
|
|
|
}
|
2022-03-03 14:19:05 +00:00
|
|
|
return obj
|
2022-01-10 12:46:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("content-types specified", func(t *testing.T) {
|
|
|
|
b := newBlobStor(t, true, "audio/*", "*/x-mpeg", "*/mpeg", "application/x-midi")
|
|
|
|
|
|
|
|
obj := newObjectWithCt("video/mpeg")
|
2022-01-11 11:33:04 +00:00
|
|
|
require.False(t, b.NeedsCompression(obj))
|
2022-01-10 12:46:01 +00:00
|
|
|
|
|
|
|
obj = newObjectWithCt("audio/aiff")
|
2022-01-11 11:33:04 +00:00
|
|
|
require.False(t, b.NeedsCompression(obj))
|
2022-01-10 12:46:01 +00:00
|
|
|
|
|
|
|
obj = newObjectWithCt("application/x-midi")
|
2022-01-11 11:33:04 +00:00
|
|
|
require.False(t, b.NeedsCompression(obj))
|
2022-01-10 12:46:01 +00:00
|
|
|
|
|
|
|
obj = newObjectWithCt("text/plain")
|
2022-01-11 11:33:04 +00:00
|
|
|
require.True(t, b.NeedsCompression(obj))
|
2022-01-10 12:46:01 +00:00
|
|
|
|
|
|
|
obj = newObjectWithCt("")
|
2022-01-11 11:33:04 +00:00
|
|
|
require.True(t, b.NeedsCompression(obj))
|
2022-01-10 12:46:01 +00:00
|
|
|
})
|
|
|
|
t.Run("content-types omitted", func(t *testing.T) {
|
|
|
|
b := newBlobStor(t, true)
|
|
|
|
obj := newObjectWithCt("video/mpeg")
|
2022-01-11 11:33:04 +00:00
|
|
|
require.True(t, b.NeedsCompression(obj))
|
2022-01-10 12:46:01 +00:00
|
|
|
})
|
|
|
|
t.Run("compress disabled", func(t *testing.T) {
|
|
|
|
b := newBlobStor(t, false, "video/mpeg")
|
|
|
|
|
|
|
|
obj := newObjectWithCt("video/mpeg")
|
2022-01-11 11:33:04 +00:00
|
|
|
require.False(t, b.NeedsCompression(obj))
|
2022-01-10 12:46:01 +00:00
|
|
|
|
|
|
|
obj = newObjectWithCt("text/plain")
|
2022-01-11 11:33:04 +00:00
|
|
|
require.False(t, b.NeedsCompression(obj))
|
2022-01-10 12:46:01 +00:00
|
|
|
})
|
|
|
|
}
|