metabase: CommitObject return metabase.Object

An immediate purpose is to be used in test cases for deleting objects

Change-Id: I481d3be5800e55da231ecb628ef7eb3ea011d196
This commit is contained in:
Kaloyan Raev 2020-11-02 19:25:58 +02:00
parent 7e9ac2ec0a
commit f7aa5b4e39
3 changed files with 44 additions and 24 deletions

View File

@ -455,11 +455,11 @@ type CommitObject struct {
type SegmentProof struct{} type SegmentProof struct{}
// CommitObject adds a pending object to the database. // CommitObject adds a pending object to the database.
func (db *DB) CommitObject(ctx context.Context, opts CommitObject) (err error) { func (db *DB) CommitObject(ctx context.Context, opts CommitObject) (object Object, err error) {
defer mon.Task()(&ctx)(&err) defer mon.Task()(&ctx)(&err)
if err := opts.ObjectStream.Verify(); err != nil { if err := opts.ObjectStream.Verify(); err != nil {
return err return Object{}, err
} }
// TODO: deduplicate basic checks. // TODO: deduplicate basic checks.
@ -471,12 +471,12 @@ func (db *DB) CommitObject(ctx context.Context, opts CommitObject) (err error) {
} }
} }
func (db *DB) commitObjectWithoutProofs(ctx context.Context, opts CommitObject) (err error) { func (db *DB) commitObjectWithoutProofs(ctx context.Context, opts CommitObject) (object Object, err error) {
defer mon.Task()(&ctx)(&err) defer mon.Task()(&ctx)(&err)
tx, err := db.db.BeginTx(ctx, nil) tx, err := db.db.BeginTx(ctx, nil)
if err != nil { if err != nil {
return Error.New("failed BeginTx: %w", err) return Object{}, Error.New("failed BeginTx: %w", err)
} }
committed := false committed := false
defer func() { defer func() {
@ -487,7 +487,7 @@ func (db *DB) commitObjectWithoutProofs(ctx context.Context, opts CommitObject)
// TODO: fetch info from segments // TODO: fetch info from segments
result, err := tx.ExecContext(ctx, ` err = tx.QueryRow(ctx, `
UPDATE objects SET UPDATE objects SET
status = 1, -- committed status = 1, -- committed
segment_count = 0, -- TODO segment_count = 0, -- TODO
@ -504,29 +504,45 @@ func (db *DB) commitObjectWithoutProofs(ctx context.Context, opts CommitObject)
object_key = $3 AND object_key = $3 AND
version = $4 AND version = $4 AND
stream_id = $5 AND stream_id = $5 AND
status = 0; status = 0
RETURNING
stream_id,
created_at, expires_at,
encryption;
`, opts.ProjectID, opts.BucketName, []byte(opts.ObjectKey), opts.Version, opts.StreamID, `, opts.ProjectID, opts.BucketName, []byte(opts.ObjectKey), opts.Version, opts.StreamID,
opts.EncryptedMetadataNonce, opts.EncryptedMetadata) opts.EncryptedMetadataNonce, opts.EncryptedMetadata).
Scan(
&object.StreamID,
&object.CreatedAt, &object.ExpiresAt,
encryptionParameters{&object.Encryption},
)
if err != nil { if err != nil {
return Error.New("failed to update object: %w", err) if errors.Is(err, sql.ErrNoRows) {
} return Object{}, storj.ErrObjectNotFound.Wrap(Error.New("object with specified version and pending status is missing"))
rowsAffected, err := result.RowsAffected() }
if err != nil { return Object{}, Error.New("failed to update object: %w", err)
return Error.New("failed to get rows affected: %w", err)
}
if rowsAffected == 0 {
return Error.New("object with specified version and pending status is missing")
} }
object.ProjectID = opts.ProjectID
object.BucketName = opts.BucketName
object.ObjectKey = opts.ObjectKey
object.Version = opts.Version
object.Status = Committed
object.SegmentCount = 0 // TODO
object.EncryptedMetadataNonce = opts.EncryptedMetadataNonce
object.EncryptedMetadata = opts.EncryptedMetadata
object.TotalEncryptedSize = 0 // TODO
object.FixedSegmentSize = 0 // TODO
// TODO: delete segments // TODO: delete segments
err = tx.Commit() err = tx.Commit()
committed = true committed = true
return Error.Wrap(err) return object, Error.Wrap(err)
} }
func (db *DB) commitObjectWithProofs(ctx context.Context, opts CommitObject) (err error) { func (db *DB) commitObjectWithProofs(ctx context.Context, opts CommitObject) (object Object, err error) {
defer mon.Task()(&ctx)(&err) defer mon.Task()(&ctx)(&err)
return Error.New("unimplemented") return Object{}, Error.New("unimplemented")
} }

View File

@ -1387,8 +1387,8 @@ func TestCommitObject(t *testing.T) {
StreamID: obj.StreamID, StreamID: obj.StreamID,
}, },
}, },
ErrClass: &metabase.Error, ErrClass: &storj.ErrObjectNotFound,
ErrText: "object with specified version and pending status is missing", // TODO: this error message could be better ErrText: "metabase: object with specified version and pending status is missing", // TODO: this error message could be better
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) Verify{}.Check(ctx, t, db)
}) })
@ -1439,8 +1439,8 @@ func TestCommitObject(t *testing.T) {
StreamID: obj.StreamID, StreamID: obj.StreamID,
}, },
}, },
ErrClass: &metabase.Error, ErrClass: &storj.ErrObjectNotFound,
ErrText: "object with specified version and pending status is missing", // TODO: this error message could be better ErrText: "metabase: object with specified version and pending status is missing", // TODO: this error message could be better
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{ Verify{

View File

@ -50,9 +50,13 @@ type CommitObject struct {
ErrText string ErrText string
} }
func (step CommitObject) Check(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { func (step CommitObject) Check(ctx *testcontext.Context, t *testing.T, db *metabase.DB) metabase.Object {
err := db.CommitObject(ctx, step.Opts) object, err := db.CommitObject(ctx, step.Opts)
checkError(t, err, step.ErrClass, step.ErrText) checkError(t, err, step.ErrClass, step.ErrText)
if err == nil {
require.Equal(t, step.Opts.ObjectStream, object.ObjectStream)
}
return object
} }
type BeginSegment struct { type BeginSegment struct {