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{}
// 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)
if err := opts.ObjectStream.Verify(); err != nil {
return err
return Object{}, err
}
// 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)
tx, err := db.db.BeginTx(ctx, nil)
if err != nil {
return Error.New("failed BeginTx: %w", err)
return Object{}, Error.New("failed BeginTx: %w", err)
}
committed := false
defer func() {
@ -487,7 +487,7 @@ func (db *DB) commitObjectWithoutProofs(ctx context.Context, opts CommitObject)
// TODO: fetch info from segments
result, err := tx.ExecContext(ctx, `
err = tx.QueryRow(ctx, `
UPDATE objects SET
status = 1, -- committed
segment_count = 0, -- TODO
@ -504,29 +504,45 @@ func (db *DB) commitObjectWithoutProofs(ctx context.Context, opts CommitObject)
object_key = $3 AND
version = $4 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.EncryptedMetadataNonce, opts.EncryptedMetadata)
opts.EncryptedMetadataNonce, opts.EncryptedMetadata).
Scan(
&object.StreamID,
&object.CreatedAt, &object.ExpiresAt,
encryptionParameters{&object.Encryption},
)
if err != nil {
return Error.New("failed to update object: %w", err)
}
rowsAffected, err := result.RowsAffected()
if err != nil {
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")
if errors.Is(err, sql.ErrNoRows) {
return Object{}, storj.ErrObjectNotFound.Wrap(Error.New("object with specified version and pending status is missing"))
}
return Object{}, Error.New("failed to update object: %w", err)
}
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
err = tx.Commit()
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)
return Error.New("unimplemented")
return Object{}, Error.New("unimplemented")
}

View File

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

View File

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