satellite/metainfo/metabase: add status option to iteration

Change-Id: Ie9d1a3704d8d08e3421ba2244a1f3be57d15ed8f
This commit is contained in:
Egon Elbre 2020-11-16 15:08:22 +02:00
parent a015f41927
commit 7aba265db0
4 changed files with 101 additions and 8 deletions

View File

@ -30,10 +30,14 @@ func iterateAllVersions(ctx context.Context, db *DB, opts IterateObjects, fn fun
opts: &opts,
batchSize: opts.BatchSize,
curIndex: 0,
status: Committed,
status: opts.Status,
cursor: opts.Cursor,
}
if it.batchSize <= 0 || it.batchSize > batchsizeLimit {
it.batchSize = batchsizeLimit
}
it.curRows, err = it.doNextQuery(ctx)
if err != nil {
return err

View File

@ -31,6 +31,7 @@ type IterateObjects struct {
BatchSize int
Prefix ObjectKey
Cursor IterateCursor
Status ObjectStatus
}
// IterateObjectsAllVersions iterates through all versions of all committed objects.
@ -55,8 +56,8 @@ func (opts *IterateObjects) Verify() error {
return ErrInvalidRequest.New("prefixed listing not implemented yet")
case opts.BatchSize < 0:
return ErrInvalidRequest.New("BatchSize is negative")
case opts.BatchSize == 0 || opts.BatchSize > batchsizeLimit:
opts.BatchSize = batchsizeLimit
case !(opts.Status == Pending || opts.Status == Committed):
return ErrInvalidRequest.New("Status %v is not supported", opts.Status)
}
return nil
}

View File

@ -22,6 +22,7 @@ func TestIterateObjects(t *testing.T) {
ProjectID: uuid.UUID{1},
BucketName: "",
Recursive: true,
Status: metabase.Committed,
},
ErrClass: &metabase.ErrInvalidRequest,
ErrText: "BucketName missing",
@ -35,6 +36,7 @@ func TestIterateObjects(t *testing.T) {
ProjectID: uuid.UUID{},
BucketName: "sj://mybucket",
Recursive: true,
Status: metabase.Committed,
},
ErrClass: &metabase.ErrInvalidRequest,
ErrText: "ProjectID missing",
@ -49,14 +51,29 @@ func TestIterateObjects(t *testing.T) {
BucketName: "mybucket",
BatchSize: -1,
Recursive: true,
Status: metabase.Committed,
},
ErrClass: &metabase.ErrInvalidRequest,
ErrText: "BatchSize is negative",
}.Check(ctx, t, db)
Verify{}.Check(ctx, t, db)
})
t.Run("Status is invalid", func(t *testing.T) {
defer DeleteAll{}.Check(ctx, t, db)
IterateObjects{
Opts: metabase.IterateObjects{
ProjectID: uuid.UUID{1},
BucketName: "test",
Recursive: true,
Status: 255,
},
ErrClass: &metabase.ErrInvalidRequest,
ErrText: "Status 255 is not supported",
}.Check(ctx, t, db)
Verify{}.Check(ctx, t, db)
})
t.Run("List empty bucket", func(t *testing.T) {
t.Run("empty bucket", func(t *testing.T) {
defer DeleteAll{}.Check(ctx, t, db)
objects := createObjects(ctx, t, db, 2, uuid.UUID{1}, "mybucket")
IterateObjects{
@ -65,13 +82,79 @@ func TestIterateObjects(t *testing.T) {
BucketName: "myemptybucket",
BatchSize: 10,
Recursive: true,
Status: metabase.Committed,
},
Result: nil,
}.Check(ctx, t, db)
Verify{Objects: objects}.Check(ctx, t, db)
})
t.Run("List less objects than limit", func(t *testing.T) {
t.Run("based on status", func(t *testing.T) {
defer DeleteAll{}.Check(ctx, t, db)
now := time.Now()
pending := randObjectStream()
committed := randObjectStream()
committed.ProjectID = pending.ProjectID
committed.BucketName = pending.BucketName
projectID := pending.ProjectID
bucketName := pending.BucketName
BeginObjectExactVersion{
Opts: metabase.BeginObjectExactVersion{
ObjectStream: pending,
Encryption: defaultTestEncryption,
},
Version: 1,
}.Check(ctx, t, db)
BeginObjectExactVersion{
Opts: metabase.BeginObjectExactVersion{
ObjectStream: committed,
Encryption: defaultTestEncryption,
},
Version: 1,
}.Check(ctx, t, db)
CommitObject{
Opts: metabase.CommitObject{
ObjectStream: committed,
},
}.Check(ctx, t, db)
IterateObjects{
Opts: metabase.IterateObjects{
ProjectID: projectID,
BucketName: bucketName,
Recursive: true,
Status: metabase.Committed,
},
Result: []metabase.ObjectEntry{{
ObjectStream: committed,
CreatedAt: now,
Status: metabase.Committed,
Encryption: defaultTestEncryption,
}},
}.Check(ctx, t, db)
IterateObjects{
Opts: metabase.IterateObjects{
ProjectID: projectID,
BucketName: bucketName,
Recursive: true,
Status: metabase.Pending,
},
Result: []metabase.ObjectEntry{{
ObjectStream: pending,
CreatedAt: now,
Status: metabase.Pending,
Encryption: defaultTestEncryption,
}},
}.Check(ctx, t, db)
})
t.Run("less objects than limit", func(t *testing.T) {
defer DeleteAll{}.Check(ctx, t, db)
numberOfObjects := 3
limit := 10
@ -86,13 +169,14 @@ func TestIterateObjects(t *testing.T) {
BucketName: "mybucket",
Recursive: true,
BatchSize: limit,
Status: metabase.Committed,
},
Result: expected,
}.Check(ctx, t, db)
Verify{Objects: objects}.Check(ctx, t, db)
})
t.Run("List more objects than limit", func(t *testing.T) {
t.Run("more objects than limit", func(t *testing.T) {
defer DeleteAll{}.Check(ctx, t, db)
numberOfObjects := 10
limit := 3
@ -107,13 +191,14 @@ func TestIterateObjects(t *testing.T) {
BucketName: "mybucket",
Recursive: true,
BatchSize: limit,
Status: metabase.Committed,
},
Result: expected,
}.Check(ctx, t, db)
Verify{Objects: objects}.Check(ctx, t, db)
})
t.Run("List objects in one bucket in project with 2 buckets", func(t *testing.T) {
t.Run("objects in one bucket in project with 2 buckets", func(t *testing.T) {
defer DeleteAll{}.Check(ctx, t, db)
numberOfObjectsPerBucket := 5
batchSize := 10
@ -129,13 +214,14 @@ func TestIterateObjects(t *testing.T) {
BucketName: "bucket-a",
Recursive: true,
BatchSize: batchSize,
Status: metabase.Committed,
},
Result: expected,
}.Check(ctx, t, db)
Verify{Objects: append(objectsBucketA, objectsBucketB...)}.Check(ctx, t, db)
})
t.Run("List objects in one bucket with same bucketName in another project", func(t *testing.T) {
t.Run("objects in one bucket with same bucketName in another project", func(t *testing.T) {
defer DeleteAll{}.Check(ctx, t, db)
numberOfObjectsPerBucket := 5
batchSize := 10
@ -151,6 +237,7 @@ func TestIterateObjects(t *testing.T) {
BucketName: "mybucket",
Recursive: true,
BatchSize: batchSize,
Status: metabase.Committed,
},
Result: expected,
}.Check(ctx, t, db)

View File

@ -924,6 +924,7 @@ func (endpoint *Endpoint) ListObjects(ctx context.Context, req *pb.ObjectListReq
Cursor: metabase.IterateCursor{Key: metabase.ObjectKey(req.EncryptedCursor)},
Recursive: req.Recursive,
BatchSize: limit + 1,
Status: metabase.Committed,
}, func(ctx context.Context, it metabase.ObjectsIterator) error {
entry := metabase.ObjectEntry{}
for len(resp.Items) < limit && it.Next(ctx, &entry) {