satellite/metainfo/metabase: use txutil.WithTx in delete
Change-Id: I75965c4dcf57479ace2367f5d3069d785628e86a
This commit is contained in:
parent
bc460cd62d
commit
8182d8a726
@ -14,6 +14,7 @@ import (
|
||||
|
||||
"storj.io/common/storj"
|
||||
"storj.io/storj/private/dbutil/pgutil"
|
||||
"storj.io/storj/private/dbutil/txutil"
|
||||
"storj.io/storj/private/tagsql"
|
||||
)
|
||||
|
||||
@ -101,17 +102,7 @@ func (db *DB) DeleteObjectExactVersion(ctx context.Context, opts DeleteObjectExa
|
||||
return DeleteObjectResult{}, err
|
||||
}
|
||||
|
||||
tx, err := db.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return DeleteObjectResult{}, Error.New("failed BeginTx: %w", err)
|
||||
}
|
||||
committed := false
|
||||
defer func() {
|
||||
if !committed {
|
||||
err = errs.Combine(err, Error.Wrap(tx.Rollback()))
|
||||
}
|
||||
}()
|
||||
|
||||
err = txutil.WithTx(ctx, db.db, nil, func(ctx context.Context, tx tagsql.Tx) error {
|
||||
rows, err := tx.Query(ctx, `
|
||||
DELETE FROM objects
|
||||
WHERE
|
||||
@ -130,34 +121,33 @@ func (db *DB) DeleteObjectExactVersion(ctx context.Context, opts DeleteObjectExa
|
||||
`, opts.ProjectID, opts.BucketName, []byte(opts.ObjectKey), opts.Version)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return DeleteObjectResult{}, storj.ErrObjectNotFound.Wrap(Error.Wrap(err))
|
||||
return storj.ErrObjectNotFound.Wrap(Error.Wrap(err))
|
||||
}
|
||||
return DeleteObjectResult{}, Error.New("unable to delete object: %w", err)
|
||||
return Error.New("unable to delete object: %w", err)
|
||||
}
|
||||
|
||||
result.Objects, err = scanObjectDeletion(opts.ObjectLocation, rows)
|
||||
if err != nil {
|
||||
return DeleteObjectResult{}, err
|
||||
return err
|
||||
}
|
||||
|
||||
if len(result.Objects) == 0 {
|
||||
return DeleteObjectResult{}, storj.ErrObjectNotFound.Wrap(Error.New("no rows deleted"))
|
||||
return storj.ErrObjectNotFound.Wrap(Error.New("no rows deleted"))
|
||||
}
|
||||
|
||||
segmentInfos, err := deleteSegments(ctx, tx, result.Objects)
|
||||
if err != nil {
|
||||
return DeleteObjectResult{}, err
|
||||
return err
|
||||
}
|
||||
|
||||
if len(segmentInfos) != 0 {
|
||||
result.Segments = segmentInfos
|
||||
}
|
||||
|
||||
err, committed = tx.Commit(), true
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return DeleteObjectResult{}, Error.New("unable to commit tx: %w", err)
|
||||
return DeleteObjectResult{}, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@ -169,17 +159,7 @@ func (db *DB) DeleteObjectLatestVersion(ctx context.Context, opts DeleteObjectLa
|
||||
return DeleteObjectResult{}, err
|
||||
}
|
||||
|
||||
tx, err := db.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return DeleteObjectResult{}, Error.New("failed BeginTx: %w", err)
|
||||
}
|
||||
committed := false
|
||||
defer func() {
|
||||
if !committed {
|
||||
err = errs.Combine(err, Error.Wrap(tx.Rollback()))
|
||||
}
|
||||
}()
|
||||
|
||||
err = txutil.WithTx(ctx, db.db, nil, func(ctx context.Context, tx tagsql.Tx) error {
|
||||
// TODO different sql for Postgres and CockroachDB
|
||||
// version ONLY for cockroachdb
|
||||
// Postgres doesn't support ORDER BY and LIMIT in DELETE
|
||||
@ -220,34 +200,34 @@ func (db *DB) DeleteObjectLatestVersion(ctx context.Context, opts DeleteObjectLa
|
||||
`, opts.ProjectID, opts.BucketName, []byte(opts.ObjectKey))
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return DeleteObjectResult{}, storj.ErrObjectNotFound.Wrap(Error.Wrap(err))
|
||||
return storj.ErrObjectNotFound.Wrap(Error.Wrap(err))
|
||||
}
|
||||
return DeleteObjectResult{}, Error.New("unable to delete object: %w", err)
|
||||
return Error.New("unable to delete object: %w", err)
|
||||
}
|
||||
|
||||
result.Objects, err = scanObjectDeletion(opts.ObjectLocation, rows)
|
||||
if err != nil {
|
||||
return DeleteObjectResult{}, err
|
||||
return err
|
||||
}
|
||||
|
||||
if len(result.Objects) == 0 {
|
||||
return DeleteObjectResult{}, storj.ErrObjectNotFound.Wrap(Error.New("no rows deleted"))
|
||||
return storj.ErrObjectNotFound.Wrap(Error.New("no rows deleted"))
|
||||
}
|
||||
|
||||
segmentInfos, err := deleteSegments(ctx, tx, result.Objects)
|
||||
if err != nil {
|
||||
return DeleteObjectResult{}, err
|
||||
return err
|
||||
}
|
||||
|
||||
if len(segmentInfos) != 0 {
|
||||
result.Segments = segmentInfos
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
err, committed = tx.Commit(), true
|
||||
if err != nil {
|
||||
return DeleteObjectResult{}, Error.New("unable to commit tx: %w", err)
|
||||
return DeleteObjectResult{}, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@ -258,18 +238,7 @@ func (db *DB) DeleteObjectAllVersions(ctx context.Context, opts DeleteObjectAllV
|
||||
if err := opts.Verify(); err != nil {
|
||||
return DeleteObjectResult{}, err
|
||||
}
|
||||
|
||||
tx, err := db.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return DeleteObjectResult{}, Error.New("failed BeginTx: %w", err)
|
||||
}
|
||||
committed := false
|
||||
defer func() {
|
||||
if !committed {
|
||||
err = errs.Combine(err, Error.Wrap(tx.Rollback()))
|
||||
}
|
||||
}()
|
||||
|
||||
err = txutil.WithTx(ctx, db.db, nil, func(ctx context.Context, tx tagsql.Tx) error {
|
||||
rows, err := tx.Query(ctx, `
|
||||
DELETE FROM objects
|
||||
WHERE
|
||||
@ -287,34 +256,34 @@ func (db *DB) DeleteObjectAllVersions(ctx context.Context, opts DeleteObjectAllV
|
||||
`, opts.ProjectID, opts.BucketName, []byte(opts.ObjectKey))
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return DeleteObjectResult{}, storj.ErrObjectNotFound.Wrap(Error.Wrap(err))
|
||||
return storj.ErrObjectNotFound.Wrap(Error.Wrap(err))
|
||||
}
|
||||
return DeleteObjectResult{}, Error.New("unable to delete object: %w", err)
|
||||
return Error.New("unable to delete object: %w", err)
|
||||
}
|
||||
|
||||
result.Objects, err = scanObjectDeletion(opts.ObjectLocation, rows)
|
||||
if err != nil {
|
||||
return DeleteObjectResult{}, err
|
||||
return err
|
||||
}
|
||||
|
||||
if len(result.Objects) == 0 {
|
||||
return DeleteObjectResult{}, storj.ErrObjectNotFound.Wrap(Error.New("no rows deleted"))
|
||||
return storj.ErrObjectNotFound.Wrap(Error.New("no rows deleted"))
|
||||
}
|
||||
|
||||
segmentInfos, err := deleteSegments(ctx, tx, result.Objects)
|
||||
if err != nil {
|
||||
return DeleteObjectResult{}, err
|
||||
return err
|
||||
}
|
||||
|
||||
if len(segmentInfos) != 0 {
|
||||
result.Segments = segmentInfos
|
||||
}
|
||||
|
||||
err, committed = tx.Commit(), true
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return DeleteObjectResult{}, Error.New("unable to commit tx: %w", err)
|
||||
return DeleteObjectResult{}, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@ -331,17 +300,7 @@ func (db *DB) DeleteObjectsAllVersions(ctx context.Context, opts DeleteObjectsAl
|
||||
return DeleteObjectResult{}, err
|
||||
}
|
||||
|
||||
tx, err := db.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return DeleteObjectResult{}, Error.New("failed BeginTx: %w", err)
|
||||
}
|
||||
committed := false
|
||||
defer func() {
|
||||
if !committed {
|
||||
err = errs.Combine(err, Error.Wrap(tx.Rollback()))
|
||||
}
|
||||
}()
|
||||
|
||||
err = txutil.WithTx(ctx, db.db, nil, func(ctx context.Context, tx tagsql.Tx) error {
|
||||
// It is aleady verified that all object locations are in the same bucket
|
||||
projectID := opts.Locations[0].ProjectID
|
||||
bucketName := opts.Locations[0].BucketName
|
||||
@ -375,35 +334,34 @@ func (db *DB) DeleteObjectsAllVersions(ctx context.Context, opts DeleteObjectsAl
|
||||
`, projectID, bucketName, pgutil.ByteaArray(objectKeys))
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return DeleteObjectResult{}, storj.ErrObjectNotFound.Wrap(Error.Wrap(err))
|
||||
return storj.ErrObjectNotFound.Wrap(Error.Wrap(err))
|
||||
}
|
||||
return DeleteObjectResult{}, Error.New("unable to delete object: %w", err)
|
||||
return Error.New("unable to delete object: %w", err)
|
||||
}
|
||||
|
||||
result.Objects, err = scanMultipleObjectsDeletion(rows)
|
||||
if err != nil {
|
||||
return DeleteObjectResult{}, err
|
||||
return err
|
||||
}
|
||||
|
||||
if len(result.Objects) == 0 {
|
||||
// nothing was delete, no error
|
||||
return DeleteObjectResult{}, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
segmentInfos, err := deleteSegments(ctx, tx, result.Objects)
|
||||
if err != nil {
|
||||
return DeleteObjectResult{}, err
|
||||
return err
|
||||
}
|
||||
|
||||
if len(segmentInfos) != 0 {
|
||||
result.Segments = segmentInfos
|
||||
}
|
||||
|
||||
err, committed = tx.Commit(), true
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return DeleteObjectResult{}, Error.New("unable to commit tx: %w", err)
|
||||
return DeleteObjectResult{}, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@ -460,6 +418,10 @@ func scanMultipleObjectsDeletion(rows tagsql.Rows) (objects []Object, err error)
|
||||
return nil, Error.New("unable to delete object: %w", err)
|
||||
}
|
||||
|
||||
if len(objects) == 0 {
|
||||
objects = nil
|
||||
}
|
||||
|
||||
return objects, nil
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user