frostfs-node/pkg/local_object_storage/metabase/v2/exists.go
Alex Vanin e478c0d024 [#199] Refactor metabase internal structure
Accoring to MetaBase-Plan-B storage engine specification.

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
2020-12-11 17:19:37 +03:00

43 lines
1.1 KiB
Go

package meta
import (
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
"go.etcd.io/bbolt"
)
// Exists returns ErrAlreadyRemoved if addr was marked as removed. Otherwise it
// returns true if addr is in primary index or false if it is not.
func (db *DB) Exists(addr *objectSDK.Address) (exists bool, err error) {
err = db.boltDB.View(func(tx *bbolt.Tx) error {
// check graveyard first
if inGraveyard(tx, addr) {
return ErrAlreadyRemoved
}
// if graveyard is empty, then check if object exists in primary bucket
primaryBucket := tx.Bucket(primaryBucketName(addr.ContainerID()))
if primaryBucket == nil {
return nil
}
// using `get` as `exists`: https://github.com/boltdb/bolt/issues/321
val := primaryBucket.Get(objectKey(addr.ObjectID()))
exists = len(val) != 0
return nil
})
return exists, err
}
// inGraveyard returns true if object was marked as removed.
func inGraveyard(tx *bbolt.Tx, addr *objectSDK.Address) bool {
graveyard := tx.Bucket(graveyardBucketName)
if graveyard == nil {
return false
}
tombstone := graveyard.Get(addressKey(addr))
return len(tombstone) != 0
}