[#789] blobovnicza: Implement iterator
There is a need to be able to process all stored objects saved in `Blobovnicza`. Implement `Blobovnicza.Iterate` method which iterates over all objects. Implement `IterateObjects` helper function to simplify the code. Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
55c94a0152
commit
b618a44d69
2 changed files with 121 additions and 0 deletions
|
@ -2,6 +2,7 @@ package blobovnicza
|
|||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"math/rand"
|
||||
"os"
|
||||
|
@ -9,6 +10,7 @@ import (
|
|||
|
||||
cidtest "github.com/nspcc-dev/neofs-api-go/pkg/container/id/test"
|
||||
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
|
||||
objecttest "github.com/nspcc-dev/neofs-api-go/pkg/object/test"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/util/logger/test"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
@ -118,3 +120,57 @@ func TestBlobovnicza(t *testing.T) {
|
|||
|
||||
require.NoError(t, blz.Close())
|
||||
}
|
||||
|
||||
func TestIterateObjects(t *testing.T) {
|
||||
p := t.Name()
|
||||
|
||||
// create Blobovnicza instance
|
||||
blz := New(
|
||||
WithPath(p),
|
||||
WithObjectSizeLimit(1<<10),
|
||||
WithFullSizeLimit(100<<10),
|
||||
)
|
||||
|
||||
defer os.Remove(p)
|
||||
|
||||
// open Blobovnicza
|
||||
require.NoError(t, blz.Open())
|
||||
|
||||
// initialize Blobovnicza
|
||||
require.NoError(t, blz.Init())
|
||||
|
||||
const objNum = 5
|
||||
|
||||
mObjs := make(map[string][]byte)
|
||||
|
||||
for i := uint64(0); i < objNum; i++ {
|
||||
data := make([]byte, 8) // actual data doesn't really matter for test
|
||||
|
||||
binary.BigEndian.PutUint64(data, i)
|
||||
|
||||
mObjs[string(data)] = data
|
||||
}
|
||||
|
||||
var putPrm PutPrm
|
||||
|
||||
for _, v := range mObjs {
|
||||
putPrm.SetAddress(objecttest.Address())
|
||||
putPrm.SetMarshaledObject(v)
|
||||
|
||||
_, err := blz.Put(&putPrm)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
err := IterateObjects(blz, func(data []byte) error {
|
||||
v, ok := mObjs[string(data)]
|
||||
require.True(t, ok)
|
||||
|
||||
require.Equal(t, v, data)
|
||||
|
||||
delete(mObjs, string(data))
|
||||
|
||||
return nil
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Empty(t, mObjs)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue