satellite/metabase: simplify main commit object query

Simplifying over complicated SQL query for committing object while
upload.

Change-Id: I99b918192c292288e6b9397d0f9ccf9b81838f4f
This commit is contained in:
Michal Niewrzal 2023-09-27 14:29:48 +02:00 committed by Storj Robot
parent b28439be24
commit 4737e912f0

View File

@ -766,46 +766,42 @@ func (db *DB) CommitObject(ctx context.Context, opts CommitObject) (object Objec
bucket_name = $2 AND
object_key = $3 AND
stream_id = $5
RETURNING
project_id, bucket_name, object_key, $4::INT4 as version, stream_id,
`+committedStatus+` as status, expires_at, $6::INT4 as segment_count, $7::INT8 as total_plain_size, $8::INT8 as total_encrypted_size, $9::INT4 as fixed_segment_size,
NULL::timestamp as zombie_deletion_deadline,
CASE
WHEN encryption = 0 AND $10 <> 0 THEN $10
WHEN encryption = 0 AND $10 = 0 THEN NULL
ELSE encryption
END as
encryption,
encrypted_metadata_nonce, encrypted_metadata, encrypted_metadata_encrypted_key
), object_to_commit AS (
SELECT
project_id, bucket_name, object_key, version, stream_id,
status, expires_at, segment_count, total_plain_size, total_encrypted_size, fixed_segment_size,
zombie_deletion_deadline, encryption,
CASE
WHEN $14::BOOL = true THEN $11
ELSE delete_pending_object.encrypted_metadata_nonce
END as
encrypted_metadata_nonce,
CASE
WHEN $14::BOOL = true THEN $12
ELSE delete_pending_object.encrypted_metadata
END as
encrypted_metadata,
CASE
WHEN $14::BOOL = true THEN $13
ELSE delete_pending_object.encrypted_metadata_encrypted_key
END as
encrypted_metadata_encrypted_key
FROM delete_pending_object
RETURNING expires_at, encryption, encrypted_metadata_nonce, encrypted_metadata, encrypted_metadata_encrypted_key
)
INSERT INTO objects (
project_id, bucket_name, object_key, version, stream_id,
status, expires_at, segment_count, total_plain_size, total_encrypted_size, fixed_segment_size, zombie_deletion_deadline,
status, segment_count, total_plain_size, total_encrypted_size,
fixed_segment_size, zombie_deletion_deadline, expires_at,
encryption,
encrypted_metadata_nonce, encrypted_metadata, encrypted_metadata_encrypted_key
)
SELECT * FROM object_to_commit
SELECT
$1 as project_id, $2 as bucket_name, $3 as object_key, $4::INT4 as version, $5 as stream_id,
`+committedStatus+` as status, $6::INT4 as segment_count, $7::INT8 as total_plain_size, $8::INT8 as total_encrypted_size,
$9::INT4 as fixed_segment_size, NULL::timestamp as zombie_deletion_deadline, expires_at,
-- TODO should we allow to override existing encryption parameters or return error if don't match with opts?
CASE
WHEN encryption = 0 AND $10 <> 0 THEN $10
WHEN encryption = 0 AND $10 = 0 THEN NULL
ELSE encryption
END as
encryption,
CASE
WHEN $14::BOOL = true THEN $11
ELSE encrypted_metadata_nonce
END as
encrypted_metadata_nonce,
CASE
WHEN $14::BOOL = true THEN $12
ELSE encrypted_metadata
END as
encrypted_metadata,
CASE
WHEN $14::BOOL = true THEN $13
ELSE encrypted_metadata_encrypted_key
END as
encrypted_metadata_encrypted_key
FROM delete_pending_object
-- we don't want ON CONFLICT clause to update existign object
-- as this way we may miss removing old object segments
RETURNING