From 0cd05fdca5ed58d915fc80b694f5efbe9a154e95 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Mon, 9 Nov 2020 13:09:09 +0300 Subject: [PATCH] [#158] metabase: Implement operation of deleting a group of objects Signed-off-by: Leonard Lyubich --- pkg/local_object_storage/metabase/delete.go | 37 +++++++++++++------ .../metabase/delete_test.go | 15 ++++++++ 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/pkg/local_object_storage/metabase/delete.go b/pkg/local_object_storage/metabase/delete.go index 0690b706..eb63ef7d 100644 --- a/pkg/local_object_storage/metabase/delete.go +++ b/pkg/local_object_storage/metabase/delete.go @@ -10,18 +10,7 @@ var tombstoneBucket = []byte("tombstones") // Delete marks object as deleted. func (db *DB) Delete(addr *object.Address) error { - return db.boltDB.Update(func(tx *bbolt.Tx) error { - bucket, err := tx.CreateBucketIfNotExists(tombstoneBucket) - if err != nil { - return errors.Wrapf(err, "(%T) could not create tombstone bucket", db) - } - - if err := bucket.Put(addressKey(addr), nil); err != nil { - return errors.Wrapf(err, "(%T) could not put to tombstone bucket", db) - } - - return nil - }) + return db.delete(addr) } func objectRemoved(tx *bbolt.Tx, addr []byte) bool { @@ -29,3 +18,27 @@ func objectRemoved(tx *bbolt.Tx, addr []byte) bool { return tombstoneBucket != nil && tombstoneBucket.Get(addr) != nil } + +// DeleteObjects marks list of objects as deleted. +func (db *DB) DeleteObjects(list ...*object.Address) { + if err := db.delete(list...); err != nil { + // TODO: log error + } +} + +func (db *DB) delete(list ...*object.Address) error { + return db.boltDB.Update(func(tx *bbolt.Tx) error { + bucket, err := tx.CreateBucketIfNotExists(tombstoneBucket) + if err != nil { + return errors.Wrapf(err, "(%T) could not create tombstone bucket", db) + } + + for i := range list { + if err := bucket.Put(addressKey(list[i]), nil); err != nil { + return errors.Wrapf(err, "(%T) could not put to tombstone bucket", db) + } + } + + return nil + }) +} diff --git a/pkg/local_object_storage/metabase/delete_test.go b/pkg/local_object_storage/metabase/delete_test.go index f065f238..49a72c8b 100644 --- a/pkg/local_object_storage/metabase/delete_test.go +++ b/pkg/local_object_storage/metabase/delete_test.go @@ -54,3 +54,18 @@ func BenchmarkDB_Delete(b *testing.B) { } }) } + +func TestDB_DeleteObjects(t *testing.T) { + db := newDB(t) + defer releaseDB(db) + + o1 := generateObject(t, testPrm{}) + o2 := generateObject(t, testPrm{}) + + require.NoError(t, db.Put(o1)) + require.NoError(t, db.Put(o2)) + + db.DeleteObjects(o1.Address(), o2.Address()) + + testSelect(t, db, object.SearchFilters{}) +}