From 2583f608e869a3f3cc6edf80220981cedbb18c80 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Mon, 18 Apr 2022 21:13:35 +0300 Subject: [PATCH] [#1318] metabase: Add `DropGraves` method Method clean up Graveyard and should be called by the routine that handles tombstone expiration. Signed-off-by: Pavel Karpy --- .../metabase/graveyard.go | 22 ++++++++ .../metabase/graveyard_test.go | 52 +++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/pkg/local_object_storage/metabase/graveyard.go b/pkg/local_object_storage/metabase/graveyard.go index e4c1d30709..127f9ba65a 100644 --- a/pkg/local_object_storage/metabase/graveyard.go +++ b/pkg/local_object_storage/metabase/graveyard.go @@ -229,3 +229,25 @@ func graveFromKV(k, v []byte) (TombstonedObject, error) { tomb: tomb, }, nil } + +// DropGraves deletes tombstoned objects from the +// graveyard bucket. +// +// Returns any error appeared during deletion process. +func (db *DB) DropGraves(tss []TombstonedObject) error { + return db.boltDB.Update(func(tx *bbolt.Tx) error { + bkt := tx.Bucket(graveyardBucketName) + if bkt == nil { + return nil + } + + for _, ts := range tss { + err := bkt.Delete(addressKey(ts.Address())) + if err != nil { + return err + } + } + + return nil + }) +} diff --git a/pkg/local_object_storage/metabase/graveyard_test.go b/pkg/local_object_storage/metabase/graveyard_test.go index 6030101e41..3a14d16c0e 100644 --- a/pkg/local_object_storage/metabase/graveyard_test.go +++ b/pkg/local_object_storage/metabase/graveyard_test.go @@ -369,6 +369,58 @@ func TestDB_IterateOverGarbage_Offset(t *testing.T) { require.False(t, iWasCalled) } +func TestDB_DropGraves(t *testing.T) { + db := newDB(t) + + // generate and put 2 objects + obj1 := generateObject(t) + obj2 := generateObject(t) + + var err error + + err = putBig(db, obj1) + require.NoError(t, err) + + err = putBig(db, obj2) + require.NoError(t, err) + + inhumePrm := new(meta.InhumePrm) + + // inhume with tombstone + addrTombstone := generateAddress() + + _, err = db.Inhume(inhumePrm. + WithAddresses(object.AddressOf(obj1), object.AddressOf(obj2)). + WithTombstoneAddress(addrTombstone), + ) + require.NoError(t, err) + + buriedTS := make([]meta.TombstonedObject, 0) + iterGravePRM := new(meta.GraveyardIterationPrm) + var counter int + + err = db.IterateOverGraveyard(iterGravePRM.SetHandler(func(tomstoned meta.TombstonedObject) error { + buriedTS = append(buriedTS, tomstoned) + counter++ + + return nil + })) + require.NoError(t, err) + require.Equal(t, 2, counter) + + err = db.DropGraves(buriedTS) + require.NoError(t, err) + + counter = 0 + + err = db.IterateOverGraveyard(iterGravePRM.SetHandler(func(_ meta.TombstonedObject) error { + counter++ + return nil + })) + require.NoError(t, err) + require.Zero(t, counter) +} + func equalAddresses(aa1 []*addressSDK.Address, aa2 []*addressSDK.Address) bool { if len(aa1) != len(aa2) { return false