2021-02-16 07:57:43 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-02-16 07:57:43 +00:00
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
addressSDK "github.com/nspcc-dev/neofs-sdk-go/object/address"
|
2021-02-16 07:57:43 +00:00
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
2021-04-08 14:19:31 +00:00
|
|
|
// DeletedObject represents descriptor of the object that was
|
|
|
|
// marked to be deleted.
|
|
|
|
type DeletedObject struct {
|
2022-01-26 12:11:13 +00:00
|
|
|
addr *addressSDK.Address
|
2021-02-16 07:57:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Address returns buried object address.
|
2021-04-08 14:19:31 +00:00
|
|
|
func (g *DeletedObject) Address() *addressSDK.Address {
|
2021-02-16 07:57:43 +00:00
|
|
|
return g.addr
|
|
|
|
}
|
|
|
|
|
2021-04-08 14:19:31 +00:00
|
|
|
// Handler is a DeletedObject handling function.
|
|
|
|
type Handler func(*DeletedObject) error
|
|
|
|
|
|
|
|
// IterateOverGarbage iterates over all objects
|
|
|
|
// marked with GC mark.
|
|
|
|
//
|
|
|
|
// If h returns ErrInterruptIterator, nil returns immediately.
|
|
|
|
// Returns other errors of h directly.
|
|
|
|
func (db *DB) IterateOverGarbage(h Handler) error {
|
|
|
|
return db.boltDB.View(func(tx *bbolt.Tx) error {
|
|
|
|
return db.iterateDeletedObj(tx, withGC, h)
|
|
|
|
})
|
|
|
|
}
|
2021-02-16 07:57:43 +00:00
|
|
|
|
|
|
|
// IterateOverGraveyard iterates over all graves in DB.
|
|
|
|
//
|
2021-02-19 08:19:28 +00:00
|
|
|
// If h returns ErrInterruptIterator, nil returns immediately.
|
|
|
|
// Returns other errors of h directly.
|
2021-04-08 14:19:31 +00:00
|
|
|
func (db *DB) IterateOverGraveyard(h Handler) error {
|
2021-02-16 07:57:43 +00:00
|
|
|
return db.boltDB.View(func(tx *bbolt.Tx) error {
|
2021-04-08 14:19:31 +00:00
|
|
|
return db.iterateDeletedObj(tx, grave, h)
|
2021-02-16 07:57:43 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-08 14:19:31 +00:00
|
|
|
type deletedType uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
_ deletedType = iota
|
|
|
|
grave
|
|
|
|
withGC
|
|
|
|
)
|
|
|
|
|
|
|
|
func (db *DB) iterateDeletedObj(tx *bbolt.Tx, t deletedType, h Handler) error {
|
|
|
|
var bkt *bbolt.Bucket
|
|
|
|
switch t {
|
|
|
|
case grave:
|
|
|
|
bkt = tx.Bucket(graveyardBucketName)
|
|
|
|
case withGC:
|
|
|
|
bkt = tx.Bucket(garbageBucketName)
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("metabase: unknown iteration object type: %d", t))
|
|
|
|
}
|
|
|
|
|
|
|
|
if bkt == nil {
|
2021-02-16 07:57:43 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-08 14:19:31 +00:00
|
|
|
// iterate over all deleted objects
|
|
|
|
err := bkt.ForEach(func(k, v []byte) error {
|
|
|
|
// parse deleted object
|
|
|
|
delObj, err := deletedObjectFromKV(k, v)
|
2021-02-16 07:57:43 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("could not parse Grave: %w", err)
|
2021-02-16 07:57:43 +00:00
|
|
|
}
|
|
|
|
|
2021-04-08 14:19:31 +00:00
|
|
|
// handler object
|
|
|
|
return h(delObj)
|
2021-02-16 07:57:43 +00:00
|
|
|
})
|
2021-02-19 08:19:28 +00:00
|
|
|
|
|
|
|
if errors.Is(err, ErrInterruptIterator) {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2021-02-16 07:57:43 +00:00
|
|
|
}
|
|
|
|
|
2021-04-08 14:19:31 +00:00
|
|
|
func deletedObjectFromKV(k, _ []byte) (*DeletedObject, error) {
|
2021-02-16 07:57:43 +00:00
|
|
|
addr, err := addressFromKey(k)
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not parse address: %w", err)
|
2021-02-16 07:57:43 +00:00
|
|
|
}
|
|
|
|
|
2021-04-08 14:19:31 +00:00
|
|
|
return &DeletedObject{
|
|
|
|
addr: addr,
|
2021-02-16 07:57:43 +00:00
|
|
|
}, nil
|
|
|
|
}
|