diff --git a/pkg/local_object_storage/metabase/storage_id.go b/pkg/local_object_storage/metabase/storage_id.go index e8cca5b26..34f5b0fbb 100644 --- a/pkg/local_object_storage/metabase/storage_id.go +++ b/pkg/local_object_storage/metabase/storage_id.go @@ -52,3 +52,40 @@ func (db *DB) storageID(tx *bbolt.Tx, addr oid.Address) ([]byte, error) { return slice.Copy(storageID), nil } + +// UpdateStorageIDPrm groups the parameters of UpdateStorageID operation. +type UpdateStorageIDPrm struct { + addr oid.Address + id []byte +} + +// UpdateStorageIDRes groups the resulting values of UpdateStorageID operation. +type UpdateStorageIDRes struct{} + +// SetAddress is an UpdateStorageID option to set the object address to check. +func (p *UpdateStorageIDPrm) SetAddress(addr oid.Address) { + p.addr = addr +} + +// SetStorageID is an UpdateStorageID option to set the storage ID. +func (p *UpdateStorageIDPrm) SetStorageID(id []byte) { + p.id = id +} + +// UpdateStorageID updates storage descriptor for objects from the blobstor. +func (db *DB) UpdateStorageID(prm UpdateStorageIDPrm) (res UpdateStorageIDRes, err error) { + db.modeMtx.RLock() + defer db.modeMtx.RUnlock() + + currEpoch := db.epochState.CurrentEpoch() + + err = db.boltDB.Batch(func(tx *bbolt.Tx) error { + exists, err := db.exists(tx, prm.addr, currEpoch) + if !exists || err != nil { + return err + } + return updateStorageID(tx, prm.addr, prm.id) + }) + + return +} diff --git a/pkg/local_object_storage/metabase/storage_id_test.go b/pkg/local_object_storage/metabase/storage_id_test.go index aa22c2eca..155a8f418 100644 --- a/pkg/local_object_storage/metabase/storage_id_test.go +++ b/pkg/local_object_storage/metabase/storage_id_test.go @@ -9,7 +9,7 @@ import ( "github.com/stretchr/testify/require" ) -func TestDB_IsSmall(t *testing.T) { +func TestDB_StorageID(t *testing.T) { db := newDB(t) raw1 := generateObject(t) @@ -39,6 +39,23 @@ func TestDB_IsSmall(t *testing.T) { fetchedStorageID, err = metaStorageID(db, object.AddressOf(raw1)) require.NoError(t, err) require.Equal(t, storageID, fetchedStorageID) + + t.Run("update", func(t *testing.T) { + require.NoError(t, metaUpdateStorageID(db, object.AddressOf(raw2), storageID)) + + fetchedStorageID, err = metaStorageID(db, object.AddressOf(raw2)) + require.NoError(t, err) + require.Equal(t, storageID, fetchedStorageID) + }) +} + +func metaUpdateStorageID(db *meta.DB, addr oid.Address, id []byte) error { + var sidPrm meta.UpdateStorageIDPrm + sidPrm.SetAddress(addr) + sidPrm.SetStorageID(id) + + _, err := db.UpdateStorageID(sidPrm) + return err } func metaStorageID(db *meta.DB, addr oid.Address) ([]byte, error) {