2020-10-28 15:28:06 +00:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package metabase
|
|
|
|
|
|
|
|
import (
|
2020-11-03 15:51:03 +00:00
|
|
|
"bytes"
|
2020-10-28 15:28:06 +00:00
|
|
|
"context"
|
2020-11-03 15:51:03 +00:00
|
|
|
"sort"
|
2020-10-28 15:28:06 +00:00
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
|
|
|
"storj.io/common/storj"
|
2021-04-23 10:52:40 +01:00
|
|
|
"storj.io/private/dbutil/pgutil"
|
|
|
|
"storj.io/private/tagsql"
|
2020-10-28 15:28:06 +00:00
|
|
|
)
|
|
|
|
|
2023-07-19 10:09:53 +01:00
|
|
|
// DeletedSegmentInfo info about deleted segment.
|
|
|
|
type DeletedSegmentInfo struct {
|
|
|
|
RootPieceID storj.PieceID
|
|
|
|
Pieces Pieces
|
|
|
|
}
|
|
|
|
|
2020-10-28 15:28:06 +00:00
|
|
|
// DeleteObjectExactVersion contains arguments necessary for deleting an exact version of object.
|
|
|
|
type DeleteObjectExactVersion struct {
|
|
|
|
Version Version
|
|
|
|
ObjectLocation
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify delete object fields.
|
|
|
|
func (obj *DeleteObjectExactVersion) Verify() error {
|
|
|
|
if err := obj.ObjectLocation.Verify(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if obj.Version <= 0 {
|
|
|
|
return ErrInvalidRequest.New("Version invalid: %v", obj.Version)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteObjectResult result of deleting object.
|
|
|
|
type DeleteObjectResult struct {
|
2023-07-19 10:09:53 +01:00
|
|
|
Objects []Object
|
2022-03-23 13:16:46 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 15:51:03 +00:00
|
|
|
// DeleteObjectsAllVersions contains arguments necessary for deleting all versions of multiple objects from the same bucket.
|
|
|
|
type DeleteObjectsAllVersions struct {
|
|
|
|
Locations []ObjectLocation
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify delete objects fields.
|
|
|
|
func (delete *DeleteObjectsAllVersions) Verify() error {
|
|
|
|
if len(delete.Locations) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(delete.Locations) > 1000 {
|
|
|
|
return ErrInvalidRequest.New("cannot delete more than 1000 objects in a single request")
|
|
|
|
}
|
|
|
|
|
|
|
|
var errGroup errs.Group
|
|
|
|
for _, location := range delete.Locations {
|
|
|
|
errGroup.Add(location.Verify())
|
|
|
|
}
|
|
|
|
|
|
|
|
err := errGroup.Err()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify if all locations are in the same bucket
|
|
|
|
first := delete.Locations[0]
|
|
|
|
for _, item := range delete.Locations[1:] {
|
|
|
|
if first.ProjectID != item.ProjectID || first.BucketName != item.BucketName {
|
|
|
|
return ErrInvalidRequest.New("all objects must be in the same bucket")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-19 10:09:53 +01:00
|
|
|
var deleteObjectExactVersion = `
|
2022-02-11 21:10:10 +00:00
|
|
|
WITH deleted_objects AS (
|
|
|
|
DELETE FROM objects
|
|
|
|
WHERE
|
|
|
|
project_id = $1 AND
|
|
|
|
bucket_name = $2 AND
|
|
|
|
object_key = $3 AND
|
2022-03-14 15:53:00 +00:00
|
|
|
version = $4
|
2022-02-11 21:10:10 +00:00
|
|
|
RETURNING
|
2023-07-19 10:09:53 +01:00
|
|
|
version, stream_id, created_at, expires_at, status, segment_count, encrypted_metadata_nonce,
|
|
|
|
encrypted_metadata, encrypted_metadata_encrypted_key, total_plain_size, total_encrypted_size,
|
|
|
|
fixed_segment_size, encryption
|
2022-02-11 21:10:10 +00:00
|
|
|
), deleted_segments AS (
|
|
|
|
DELETE FROM segments
|
|
|
|
WHERE segments.stream_id IN (SELECT deleted_objects.stream_id FROM deleted_objects)
|
2023-07-19 10:09:53 +01:00
|
|
|
RETURNING segments.stream_id
|
2022-02-11 21:10:10 +00:00
|
|
|
)
|
|
|
|
SELECT
|
2023-07-19 10:09:53 +01:00
|
|
|
version, stream_id, created_at, expires_at, status, segment_count, encrypted_metadata_nonce,
|
|
|
|
encrypted_metadata, encrypted_metadata_encrypted_key, total_plain_size, total_encrypted_size,
|
|
|
|
fixed_segment_size, encryption
|
|
|
|
FROM deleted_objects`
|
|
|
|
|
|
|
|
var deleteObjectLastCommitted = `
|
2022-08-31 12:49:30 +01:00
|
|
|
WITH deleted_objects AS (
|
|
|
|
DELETE FROM objects
|
|
|
|
WHERE
|
|
|
|
project_id = $1 AND
|
|
|
|
bucket_name = $2 AND
|
|
|
|
object_key = $3 AND
|
|
|
|
version IN (SELECT version FROM objects WHERE
|
|
|
|
project_id = $1 AND
|
|
|
|
bucket_name = $2 AND
|
|
|
|
object_key = $3 AND
|
|
|
|
status = ` + committedStatus + ` AND
|
|
|
|
(expires_at IS NULL OR expires_at > now())
|
|
|
|
ORDER BY version DESC
|
|
|
|
)
|
|
|
|
RETURNING
|
|
|
|
version, stream_id,
|
|
|
|
created_at, expires_at,
|
|
|
|
status, segment_count,
|
|
|
|
encrypted_metadata_nonce, encrypted_metadata, encrypted_metadata_encrypted_key,
|
|
|
|
total_plain_size, total_encrypted_size, fixed_segment_size,
|
|
|
|
encryption
|
|
|
|
), deleted_segments AS (
|
|
|
|
DELETE FROM segments
|
|
|
|
WHERE segments.stream_id IN (SELECT deleted_objects.stream_id FROM deleted_objects)
|
2023-07-19 10:09:53 +01:00
|
|
|
RETURNING segments.stream_id
|
2022-04-04 15:31:30 +01:00
|
|
|
)
|
|
|
|
SELECT
|
2023-07-19 10:09:53 +01:00
|
|
|
version, stream_id, created_at, expires_at, status, segment_count, encrypted_metadata_nonce,
|
|
|
|
encrypted_metadata, encrypted_metadata_encrypted_key, total_plain_size, total_encrypted_size,
|
|
|
|
fixed_segment_size, encryption
|
|
|
|
FROM deleted_objects`
|
2022-03-23 13:16:46 +00:00
|
|
|
|
2020-10-28 15:28:06 +00:00
|
|
|
// DeleteObjectExactVersion deletes an exact object version.
|
2022-05-17 16:25:48 +01:00
|
|
|
func (db *DB) DeleteObjectExactVersion(
|
|
|
|
ctx context.Context, opts DeleteObjectExactVersion,
|
|
|
|
) (result DeleteObjectResult, err error) {
|
2023-07-19 10:09:53 +01:00
|
|
|
result, err = db.deleteObjectExactVersion(ctx, opts, db.db)
|
|
|
|
if err != nil {
|
|
|
|
return DeleteObjectResult{}, err
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
2022-05-17 16:25:48 +01:00
|
|
|
|
2023-07-19 10:09:53 +01:00
|
|
|
type stmt interface {
|
|
|
|
QueryContext(ctx context.Context, query string, args ...interface{}) (tagsql.Rows, error)
|
2022-05-17 16:25:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// implementation of DB.DeleteObjectExactVersion for re-use internally in metabase package.
|
2023-07-19 10:09:53 +01:00
|
|
|
func (db *DB) deleteObjectExactVersion(ctx context.Context, opts DeleteObjectExactVersion, stmt stmt) (result DeleteObjectResult, err error) {
|
2020-10-28 15:28:06 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
if err := opts.Verify(); err != nil {
|
|
|
|
return DeleteObjectResult{}, err
|
|
|
|
}
|
2022-02-11 21:10:10 +00:00
|
|
|
|
2022-05-17 16:25:48 +01:00
|
|
|
err = withRows(
|
2023-07-19 10:09:53 +01:00
|
|
|
stmt.QueryContext(ctx, deleteObjectExactVersion,
|
|
|
|
opts.ProjectID, []byte(opts.BucketName), opts.ObjectKey, opts.Version),
|
2022-05-17 16:25:48 +01:00
|
|
|
)(func(rows tagsql.Rows) error {
|
2023-07-19 10:09:53 +01:00
|
|
|
result.Objects, err = db.scanObjectDeletion(ctx, opts.ObjectLocation, rows)
|
2022-05-17 16:25:48 +01:00
|
|
|
return err
|
2022-03-23 13:16:46 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2023-07-19 10:09:53 +01:00
|
|
|
return DeleteObjectResult{}, err
|
2022-03-23 13:16:46 +00:00
|
|
|
}
|
|
|
|
|
2023-07-19 10:09:53 +01:00
|
|
|
mon.Meter("object_delete").Mark(len(result.Objects))
|
|
|
|
for _, object := range result.Objects {
|
|
|
|
mon.Meter("segment_delete").Mark(int(object.SegmentCount))
|
2022-03-23 13:16:46 +00:00
|
|
|
}
|
2022-05-17 16:25:48 +01:00
|
|
|
|
2023-07-19 10:09:53 +01:00
|
|
|
return result, nil
|
2022-03-23 13:16:46 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 23:17:05 +00:00
|
|
|
// DeletePendingObject contains arguments necessary for deleting a pending object.
|
|
|
|
type DeletePendingObject struct {
|
2021-05-04 14:51:40 +01:00
|
|
|
ObjectStream
|
2020-12-01 23:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify verifies delete pending object fields validity.
|
|
|
|
func (opts *DeletePendingObject) Verify() error {
|
2021-05-04 14:51:40 +01:00
|
|
|
if err := opts.ObjectStream.Verify(); err != nil {
|
2020-12-01 23:17:05 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeletePendingObject deletes a pending object with specified version and streamID.
|
|
|
|
func (db *DB) DeletePendingObject(ctx context.Context, opts DeletePendingObject) (result DeleteObjectResult, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
if err := opts.Verify(); err != nil {
|
|
|
|
return DeleteObjectResult{}, err
|
|
|
|
}
|
|
|
|
|
2021-07-28 14:44:22 +01:00
|
|
|
err = withRows(db.db.QueryContext(ctx, `
|
2021-03-11 14:45:00 +00:00
|
|
|
WITH deleted_objects AS (
|
|
|
|
DELETE FROM objects
|
|
|
|
WHERE
|
|
|
|
project_id = $1 AND
|
|
|
|
bucket_name = $2 AND
|
|
|
|
object_key = $3 AND
|
|
|
|
version = $4 AND
|
|
|
|
stream_id = $5 AND
|
|
|
|
status = `+pendingStatus+`
|
|
|
|
RETURNING
|
2023-07-19 10:09:53 +01:00
|
|
|
version, stream_id, created_at, expires_at, status, segment_count,
|
2021-03-11 14:45:00 +00:00
|
|
|
encrypted_metadata_nonce, encrypted_metadata, encrypted_metadata_encrypted_key,
|
2023-07-19 10:09:53 +01:00
|
|
|
total_plain_size, total_encrypted_size, fixed_segment_size, encryption
|
2021-03-11 14:45:00 +00:00
|
|
|
), deleted_segments AS (
|
|
|
|
DELETE FROM segments
|
2022-02-22 14:41:20 +00:00
|
|
|
WHERE segments.stream_id IN (SELECT deleted_objects.stream_id FROM deleted_objects)
|
2023-07-19 10:09:53 +01:00
|
|
|
RETURNING segments.stream_id
|
2021-03-11 14:45:00 +00:00
|
|
|
)
|
|
|
|
SELECT
|
2023-07-19 10:09:53 +01:00
|
|
|
version, stream_id, created_at, expires_at, status, segment_count,
|
|
|
|
encrypted_metadata_nonce, encrypted_metadata, encrypted_metadata_encrypted_key,
|
|
|
|
total_plain_size, total_encrypted_size, fixed_segment_size, encryption
|
2021-03-11 14:45:00 +00:00
|
|
|
FROM deleted_objects
|
2021-10-04 14:43:07 +01:00
|
|
|
`, opts.ProjectID, []byte(opts.BucketName), opts.ObjectKey, opts.Version, opts.StreamID))(func(rows tagsql.Rows) error {
|
2023-07-19 10:09:53 +01:00
|
|
|
result.Objects, err = db.scanObjectDeletion(ctx, opts.Location(), rows)
|
2021-03-11 14:45:00 +00:00
|
|
|
return err
|
2020-12-01 23:17:05 +00:00
|
|
|
})
|
2021-03-11 14:45:00 +00:00
|
|
|
|
2020-12-01 23:17:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return DeleteObjectResult{}, err
|
|
|
|
}
|
2021-03-11 14:45:00 +00:00
|
|
|
|
|
|
|
if len(result.Objects) == 0 {
|
2023-04-19 10:54:16 +01:00
|
|
|
return DeleteObjectResult{}, ErrObjectNotFound.Wrap(Error.New("no rows deleted"))
|
2021-03-11 14:45:00 +00:00
|
|
|
}
|
|
|
|
|
2021-06-04 14:21:09 +01:00
|
|
|
mon.Meter("object_delete").Mark(len(result.Objects))
|
2023-07-19 10:09:53 +01:00
|
|
|
for _, object := range result.Objects {
|
|
|
|
mon.Meter("segment_delete").Mark(int(object.SegmentCount))
|
|
|
|
}
|
2021-06-04 14:21:09 +01:00
|
|
|
|
2020-12-01 23:17:05 +00:00
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2023-08-09 10:19:12 +01:00
|
|
|
// DeletePendingObjectNew deletes a pending object.
|
|
|
|
// TODO DeletePendingObjectNew will replace DeletePendingObject when objects table will be free from pending objects.
|
|
|
|
func (db *DB) DeletePendingObjectNew(ctx context.Context, opts DeletePendingObject) (result DeleteObjectResult, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
if err := opts.Verify(); err != nil {
|
|
|
|
return DeleteObjectResult{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = withRows(db.db.QueryContext(ctx, `
|
|
|
|
WITH deleted_objects AS (
|
|
|
|
DELETE FROM pending_objects
|
|
|
|
WHERE
|
|
|
|
project_id = $1 AND
|
|
|
|
bucket_name = $2 AND
|
|
|
|
object_key = $3 AND
|
|
|
|
stream_id = $4
|
|
|
|
RETURNING
|
|
|
|
stream_id, created_at, expires_at,
|
|
|
|
encrypted_metadata_nonce, encrypted_metadata, encrypted_metadata_encrypted_key,
|
|
|
|
encryption
|
|
|
|
), deleted_segments AS (
|
|
|
|
DELETE FROM segments
|
|
|
|
WHERE segments.stream_id IN (SELECT deleted_objects.stream_id FROM deleted_objects)
|
|
|
|
RETURNING segments.stream_id
|
|
|
|
)
|
|
|
|
SELECT * FROM deleted_objects
|
|
|
|
`, opts.ProjectID, []byte(opts.BucketName), opts.ObjectKey, opts.StreamID))(func(rows tagsql.Rows) error {
|
|
|
|
result.Objects, err = db.scanPendingObjectDeletion(ctx, opts.Location(), rows)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return DeleteObjectResult{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(result.Objects) == 0 {
|
|
|
|
return DeleteObjectResult{}, ErrObjectNotFound.Wrap(Error.New("no rows deleted"))
|
|
|
|
}
|
|
|
|
|
|
|
|
mon.Meter("object_delete").Mark(len(result.Objects))
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2020-11-03 15:51:03 +00:00
|
|
|
// DeleteObjectsAllVersions deletes all versions of multiple objects from the same bucket.
|
|
|
|
func (db *DB) DeleteObjectsAllVersions(ctx context.Context, opts DeleteObjectsAllVersions) (result DeleteObjectResult, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2022-03-16 18:44:21 +00:00
|
|
|
if db.config.ServerSideCopy {
|
|
|
|
return DeleteObjectResult{}, errs.New("method cannot be used when server-side copy is enabled")
|
|
|
|
}
|
|
|
|
|
2020-11-03 15:51:03 +00:00
|
|
|
if len(opts.Locations) == 0 {
|
|
|
|
// nothing to delete, no error
|
|
|
|
return DeleteObjectResult{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := opts.Verify(); err != nil {
|
|
|
|
return DeleteObjectResult{}, err
|
|
|
|
}
|
|
|
|
|
2021-03-11 14:45:00 +00:00
|
|
|
// It is aleady verified that all object locations are in the same bucket
|
|
|
|
projectID := opts.Locations[0].ProjectID
|
|
|
|
bucketName := opts.Locations[0].BucketName
|
2020-11-03 15:51:03 +00:00
|
|
|
|
2021-03-11 14:45:00 +00:00
|
|
|
objectKeys := make([][]byte, len(opts.Locations))
|
|
|
|
for i := range opts.Locations {
|
|
|
|
objectKeys[i] = []byte(opts.Locations[i].ObjectKey)
|
|
|
|
}
|
2020-11-12 11:56:15 +00:00
|
|
|
|
2021-03-11 14:45:00 +00:00
|
|
|
// Sorting the object keys just in case.
|
|
|
|
// TODO: Check if this is really necessary for the SQL query.
|
|
|
|
sort.Slice(objectKeys, func(i, j int) bool {
|
|
|
|
return bytes.Compare(objectKeys[i], objectKeys[j]) < 0
|
|
|
|
})
|
2021-07-28 14:44:22 +01:00
|
|
|
err = withRows(db.db.QueryContext(ctx, `
|
2021-03-11 14:45:00 +00:00
|
|
|
WITH deleted_objects AS (
|
|
|
|
DELETE FROM objects
|
|
|
|
WHERE
|
2020-11-12 11:56:15 +00:00
|
|
|
project_id = $1 AND
|
|
|
|
bucket_name = $2 AND
|
|
|
|
object_key = ANY ($3) AND
|
2020-11-16 13:58:22 +00:00
|
|
|
status = `+committedStatus+`
|
2021-03-11 14:45:00 +00:00
|
|
|
RETURNING
|
2023-07-19 10:09:53 +01:00
|
|
|
project_id, bucket_name, object_key, version, stream_id, created_at, expires_at,
|
|
|
|
status, segment_count, encrypted_metadata_nonce, encrypted_metadata,
|
|
|
|
encrypted_metadata_encrypted_key, total_plain_size, total_encrypted_size,
|
|
|
|
fixed_segment_size, encryption
|
2021-03-11 14:45:00 +00:00
|
|
|
), deleted_segments AS (
|
|
|
|
DELETE FROM segments
|
2022-02-22 14:41:20 +00:00
|
|
|
WHERE segments.stream_id IN (SELECT deleted_objects.stream_id FROM deleted_objects)
|
2023-07-19 10:09:53 +01:00
|
|
|
RETURNING segments.stream_id
|
2021-03-11 14:45:00 +00:00
|
|
|
)
|
|
|
|
SELECT
|
2023-07-19 10:09:53 +01:00
|
|
|
project_id, bucket_name, object_key, version, stream_id, created_at, expires_at,
|
|
|
|
status, segment_count, encrypted_metadata_nonce, encrypted_metadata,
|
|
|
|
encrypted_metadata_encrypted_key, total_plain_size, total_encrypted_size,
|
|
|
|
fixed_segment_size, encryption
|
2021-03-11 14:45:00 +00:00
|
|
|
FROM deleted_objects
|
|
|
|
`, projectID, []byte(bucketName), pgutil.ByteaArray(objectKeys)))(func(rows tagsql.Rows) error {
|
2023-07-19 10:09:53 +01:00
|
|
|
result.Objects, err = db.scanMultipleObjectsDeletion(ctx, rows)
|
2021-03-11 14:45:00 +00:00
|
|
|
return err
|
2020-11-12 11:56:15 +00:00
|
|
|
})
|
2021-03-11 14:45:00 +00:00
|
|
|
|
2020-11-03 15:51:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return DeleteObjectResult{}, err
|
|
|
|
}
|
2021-06-04 14:21:09 +01:00
|
|
|
|
|
|
|
mon.Meter("object_delete").Mark(len(result.Objects))
|
2023-07-19 10:09:53 +01:00
|
|
|
for _, object := range result.Objects {
|
|
|
|
mon.Meter("segment_delete").Mark(int(object.SegmentCount))
|
2022-03-23 13:16:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2023-07-19 10:09:53 +01:00
|
|
|
func (db *DB) scanObjectDeletion(ctx context.Context, location ObjectLocation, rows tagsql.Rows) (objects []Object, err error) {
|
2021-10-21 07:47:45 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2020-10-28 15:28:06 +00:00
|
|
|
|
2020-11-03 10:45:49 +00:00
|
|
|
objects = make([]Object, 0, 10)
|
2021-03-11 14:45:00 +00:00
|
|
|
|
|
|
|
var object Object
|
2020-10-28 15:28:06 +00:00
|
|
|
for rows.Next() {
|
2020-11-03 10:45:49 +00:00
|
|
|
object.ProjectID = location.ProjectID
|
|
|
|
object.BucketName = location.BucketName
|
|
|
|
object.ObjectKey = location.ObjectKey
|
|
|
|
|
|
|
|
err = rows.Scan(&object.Version, &object.StreamID,
|
|
|
|
&object.CreatedAt, &object.ExpiresAt,
|
|
|
|
&object.Status, &object.SegmentCount,
|
2020-11-16 16:46:47 +00:00
|
|
|
&object.EncryptedMetadataNonce, &object.EncryptedMetadata, &object.EncryptedMetadataEncryptedKey,
|
2020-11-24 12:29:16 +00:00
|
|
|
&object.TotalPlainSize, &object.TotalEncryptedSize, &object.FixedSegmentSize,
|
2023-07-19 10:09:53 +01:00
|
|
|
encryptionParameters{&object.Encryption},
|
2022-03-16 18:44:21 +00:00
|
|
|
)
|
2020-10-28 15:28:06 +00:00
|
|
|
if err != nil {
|
2023-07-19 10:09:53 +01:00
|
|
|
return nil, Error.New("unable to delete object: %w", err)
|
2021-03-11 14:45:00 +00:00
|
|
|
}
|
2022-03-23 13:16:46 +00:00
|
|
|
|
2023-07-19 10:09:53 +01:00
|
|
|
objects = append(objects, object)
|
2020-10-28 15:28:06 +00:00
|
|
|
}
|
|
|
|
|
2023-07-19 10:09:53 +01:00
|
|
|
return objects, nil
|
2020-10-28 15:28:06 +00:00
|
|
|
}
|
|
|
|
|
2023-07-19 10:09:53 +01:00
|
|
|
func (db *DB) scanMultipleObjectsDeletion(ctx context.Context, rows tagsql.Rows) (objects []Object, err error) {
|
2021-10-21 07:47:45 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2020-11-03 15:51:03 +00:00
|
|
|
|
|
|
|
objects = make([]Object, 0, 10)
|
2021-03-11 14:45:00 +00:00
|
|
|
|
|
|
|
var object Object
|
2020-11-03 15:51:03 +00:00
|
|
|
for rows.Next() {
|
|
|
|
err = rows.Scan(&object.ProjectID, &object.BucketName,
|
|
|
|
&object.ObjectKey, &object.Version, &object.StreamID,
|
|
|
|
&object.CreatedAt, &object.ExpiresAt,
|
|
|
|
&object.Status, &object.SegmentCount,
|
2020-11-16 16:46:47 +00:00
|
|
|
&object.EncryptedMetadataNonce, &object.EncryptedMetadata, &object.EncryptedMetadataEncryptedKey,
|
2020-11-24 12:29:16 +00:00
|
|
|
&object.TotalPlainSize, &object.TotalEncryptedSize, &object.FixedSegmentSize,
|
2023-07-19 10:09:53 +01:00
|
|
|
encryptionParameters{&object.Encryption})
|
2020-11-03 15:51:03 +00:00
|
|
|
if err != nil {
|
2023-07-19 10:09:53 +01:00
|
|
|
return nil, Error.New("unable to delete object: %w", err)
|
2021-03-11 14:45:00 +00:00
|
|
|
}
|
|
|
|
|
2023-07-19 10:09:53 +01:00
|
|
|
objects = append(objects, object)
|
2020-11-03 15:51:03 +00:00
|
|
|
}
|
|
|
|
|
2020-11-12 11:56:15 +00:00
|
|
|
if len(objects) == 0 {
|
|
|
|
objects = nil
|
|
|
|
}
|
2020-11-04 09:59:56 +00:00
|
|
|
|
2023-07-19 10:09:53 +01:00
|
|
|
return objects, nil
|
2020-10-28 15:28:06 +00:00
|
|
|
}
|
2022-08-31 12:49:30 +01:00
|
|
|
|
2023-08-09 10:19:12 +01:00
|
|
|
func (db *DB) scanPendingObjectDeletion(ctx context.Context, location ObjectLocation, rows tagsql.Rows) (objects []Object, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
objects = make([]Object, 0, 10)
|
|
|
|
|
|
|
|
var object Object
|
|
|
|
for rows.Next() {
|
|
|
|
object.ProjectID = location.ProjectID
|
|
|
|
object.BucketName = location.BucketName
|
|
|
|
object.ObjectKey = location.ObjectKey
|
|
|
|
|
|
|
|
err = rows.Scan(&object.StreamID,
|
|
|
|
&object.CreatedAt, &object.ExpiresAt,
|
|
|
|
&object.EncryptedMetadataNonce, &object.EncryptedMetadata, &object.EncryptedMetadataEncryptedKey,
|
|
|
|
encryptionParameters{&object.Encryption},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.New("unable to delete pending object: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
object.Status = Pending
|
|
|
|
objects = append(objects, object)
|
|
|
|
}
|
|
|
|
return objects, nil
|
|
|
|
}
|
|
|
|
|
2022-08-31 12:49:30 +01:00
|
|
|
// DeleteObjectLastCommitted contains arguments necessary for deleting last committed version of object.
|
|
|
|
type DeleteObjectLastCommitted struct {
|
|
|
|
ObjectLocation
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify delete object last committed fields.
|
|
|
|
func (obj *DeleteObjectLastCommitted) Verify() error {
|
|
|
|
return obj.ObjectLocation.Verify()
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteObjectLastCommitted deletes an object last committed version.
|
|
|
|
func (db *DB) DeleteObjectLastCommitted(
|
|
|
|
ctx context.Context, opts DeleteObjectLastCommitted,
|
|
|
|
) (result DeleteObjectResult, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
if err := opts.Verify(); err != nil {
|
|
|
|
return DeleteObjectResult{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = withRows(
|
2023-07-19 10:09:53 +01:00
|
|
|
db.db.QueryContext(ctx, deleteObjectLastCommitted,
|
|
|
|
opts.ProjectID, []byte(opts.BucketName), opts.ObjectKey),
|
2022-08-31 12:49:30 +01:00
|
|
|
)(func(rows tagsql.Rows) error {
|
2023-07-19 10:09:53 +01:00
|
|
|
result.Objects, err = db.scanObjectDeletion(ctx, opts.ObjectLocation, rows)
|
2022-08-31 12:49:30 +01:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-07-19 10:09:53 +01:00
|
|
|
return DeleteObjectResult{}, err
|
2022-08-31 12:49:30 +01:00
|
|
|
}
|
|
|
|
|
2023-07-19 10:09:53 +01:00
|
|
|
mon.Meter("object_delete").Mark(len(result.Objects))
|
|
|
|
for _, object := range result.Objects {
|
|
|
|
mon.Meter("segment_delete").Mark(int(object.SegmentCount))
|
2022-08-31 12:49:30 +01:00
|
|
|
}
|
|
|
|
|
2023-07-19 10:09:53 +01:00
|
|
|
return result, nil
|
2022-08-31 12:49:30 +01:00
|
|
|
}
|