2021-02-16 15:56:59 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-02-16 15:56:59 +00:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
objectV2 "github.com/nspcc-dev/neofs-api-go/v2/object"
|
2021-11-10 07:08:33 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-05-31 17:00:41 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2021-02-16 15:56:59 +00:00
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ExpiredObject is a descriptor of expired object from DB.
|
|
|
|
type ExpiredObject struct {
|
|
|
|
typ object.Type
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
addr oid.Address
|
2021-02-16 15:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Type returns type of the expired object.
|
|
|
|
func (e *ExpiredObject) Type() object.Type {
|
|
|
|
return e.typ
|
|
|
|
}
|
|
|
|
|
|
|
|
// Address returns address of the expired object.
|
2022-05-31 17:00:41 +00:00
|
|
|
func (e *ExpiredObject) Address() oid.Address {
|
2021-02-16 15:56:59 +00:00
|
|
|
return e.addr
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExpiredObjectHandler is an ExpiredObject handling function.
|
|
|
|
type ExpiredObjectHandler func(*ExpiredObject) error
|
|
|
|
|
|
|
|
// ErrInterruptIterator is returned by iteration handlers
|
|
|
|
// as a "break" keyword.
|
|
|
|
var ErrInterruptIterator = errors.New("iterator is interrupted")
|
|
|
|
|
|
|
|
// IterateExpired iterates over all objects in DB which are out of date
|
2022-02-15 18:22:06 +00:00
|
|
|
// relative to epoch. Locked objects are not included (do not confuse
|
|
|
|
// with objects of type LOCK).
|
2021-02-16 15:56:59 +00:00
|
|
|
//
|
|
|
|
// If h returns ErrInterruptIterator, nil returns immediately.
|
|
|
|
// Returns other errors of h directly.
|
|
|
|
func (db *DB) IterateExpired(epoch uint64, h ExpiredObjectHandler) error {
|
|
|
|
return db.boltDB.View(func(tx *bbolt.Tx) error {
|
|
|
|
return db.iterateExpired(tx, epoch, h)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) iterateExpired(tx *bbolt.Tx, epoch uint64, h ExpiredObjectHandler) error {
|
|
|
|
err := tx.ForEach(func(name []byte, b *bbolt.Bucket) error {
|
|
|
|
cidBytes := cidFromAttributeBucket(name, objectV2.SysAttributeExpEpoch)
|
|
|
|
if cidBytes == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.ForEach(func(expKey, _ []byte) error {
|
|
|
|
bktExpired := b.Bucket(expKey)
|
|
|
|
if bktExpired == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-05 05:48:42 +00:00
|
|
|
expiresAfter, err := strconv.ParseUint(string(expKey), 10, 64)
|
2021-02-19 08:34:13 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("could not parse expiration epoch: %w", err)
|
2022-09-05 05:48:42 +00:00
|
|
|
} else if expiresAfter >= epoch {
|
2021-02-19 08:34:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-02-16 15:56:59 +00:00
|
|
|
|
2021-02-19 08:34:13 +00:00
|
|
|
return bktExpired.ForEach(func(idKey, _ []byte) error {
|
2022-05-31 17:00:41 +00:00
|
|
|
var id oid.ID
|
2021-02-16 15:56:59 +00:00
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
err = id.DecodeString(string(idKey))
|
2021-02-16 15:56:59 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("could not parse ID of expired object: %w", err)
|
2021-02-16 15:56:59 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
var cnrID cid.ID
|
2021-02-16 15:56:59 +00:00
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
err = cnrID.DecodeString(string(cidBytes))
|
2021-02-16 15:56:59 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("could not parse container ID of expired bucket: %w", err)
|
2021-02-16 15:56:59 +00:00
|
|
|
}
|
|
|
|
|
2022-02-15 18:22:06 +00:00
|
|
|
// Ignore locked objects.
|
|
|
|
//
|
|
|
|
// To slightly optimize performance we can check only REGULAR objects
|
|
|
|
// (only they can be locked), but it's more reliable.
|
2022-05-12 16:37:46 +00:00
|
|
|
if objectLocked(tx, cnrID, id) {
|
2022-02-15 18:22:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
var addr oid.Address
|
|
|
|
addr.SetContainer(cnrID)
|
|
|
|
addr.SetObject(id)
|
2021-02-16 15:56:59 +00:00
|
|
|
|
|
|
|
return h(&ExpiredObject{
|
2022-05-12 16:37:46 +00:00
|
|
|
typ: firstIrregularObjectType(tx, cnrID, idKey),
|
2021-02-16 15:56:59 +00:00
|
|
|
addr: addr,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
if errors.Is(err, ErrInterruptIterator) {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-17 12:22:13 +00:00
|
|
|
// IterateCoveredByTombstones iterates over all objects in DB which are covered
|
2022-02-15 18:24:12 +00:00
|
|
|
// by tombstone with string address from tss. Locked objects are not included
|
|
|
|
// (do not confuse with objects of type LOCK).
|
2021-02-17 12:22:13 +00:00
|
|
|
//
|
|
|
|
// If h returns ErrInterruptIterator, nil returns immediately.
|
|
|
|
// Returns other errors of h directly.
|
|
|
|
//
|
|
|
|
// Does not modify tss.
|
2022-05-31 17:00:41 +00:00
|
|
|
func (db *DB) IterateCoveredByTombstones(tss map[string]oid.Address, h func(oid.Address) error) error {
|
2021-02-17 12:22:13 +00:00
|
|
|
return db.boltDB.View(func(tx *bbolt.Tx) error {
|
|
|
|
return db.iterateCoveredByTombstones(tx, tss, h)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func (db *DB) iterateCoveredByTombstones(tx *bbolt.Tx, tss map[string]oid.Address, h func(oid.Address) error) error {
|
2021-02-17 12:22:13 +00:00
|
|
|
bktGraveyard := tx.Bucket(graveyardBucketName)
|
|
|
|
if bktGraveyard == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err := bktGraveyard.ForEach(func(k, v []byte) error {
|
|
|
|
if _, ok := tss[string(v)]; ok {
|
2022-05-31 17:00:41 +00:00
|
|
|
var addr oid.Address
|
|
|
|
|
|
|
|
err := decodeAddressFromKey(&addr, k)
|
2021-02-17 12:22:13 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("could not parse address of the object under tombstone: %w", err)
|
2021-02-17 12:22:13 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
if objectLocked(tx, addr.Container(), addr.Object()) {
|
2022-02-15 18:24:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-17 12:22:13 +00:00
|
|
|
return h(addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if errors.Is(err, ErrInterruptIterator) {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2022-08-25 15:58:18 +00:00
|
|
|
|
|
|
|
func iteratePhyObjects(tx *bbolt.Tx, f func(cid.ID, oid.ID) error) error {
|
|
|
|
var cid cid.ID
|
|
|
|
var oid oid.ID
|
|
|
|
|
|
|
|
return tx.ForEach(func(name []byte, b *bbolt.Bucket) error {
|
|
|
|
b58CID, postfix := parseContainerIDWithPostfix(&cid, name)
|
|
|
|
if len(b58CID) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch postfix {
|
|
|
|
case "",
|
|
|
|
storageGroupPostfix,
|
|
|
|
bucketNameSuffixLockers,
|
|
|
|
tombstonePostfix:
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.ForEach(func(k, v []byte) error {
|
|
|
|
if oid.DecodeString(string(k)) == nil {
|
|
|
|
return f(cid, oid)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|