2021-02-18 12:54:09 +00:00
|
|
|
// Copyright (C) 2021 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package metabase
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
|
|
|
"storj.io/common/uuid"
|
|
|
|
"storj.io/storj/private/tagsql"
|
|
|
|
)
|
|
|
|
|
2021-03-01 14:29:03 +00:00
|
|
|
const loopIteratorBatchSizeLimit = 2500
|
2021-02-19 13:02:52 +00:00
|
|
|
|
2021-03-01 14:29:03 +00:00
|
|
|
// LoopObjectEntry contains information about object needed by metainfo loop.
|
|
|
|
type LoopObjectEntry struct {
|
2021-03-01 17:01:49 +00:00
|
|
|
ObjectStream // metrics, repair, tally
|
|
|
|
ExpiresAt *time.Time // tally
|
|
|
|
SegmentCount int32 // metrics
|
|
|
|
EncryptedMetadataSize int // tally
|
2021-02-18 12:54:09 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 14:29:03 +00:00
|
|
|
// LoopObjectsIterator iterates over a sequence of LoopObjectEntry items.
|
|
|
|
type LoopObjectsIterator interface {
|
|
|
|
Next(ctx context.Context, item *LoopObjectEntry) bool
|
2021-02-18 12:54:09 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 14:29:03 +00:00
|
|
|
// IterateLoopObjects contains arguments necessary for listing objects in metabase.
|
|
|
|
type IterateLoopObjects struct {
|
2021-02-18 12:54:09 +00:00
|
|
|
BatchSize int
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify verifies get object request fields.
|
2021-03-01 14:29:03 +00:00
|
|
|
func (opts *IterateLoopObjects) Verify() error {
|
2021-02-18 12:54:09 +00:00
|
|
|
if opts.BatchSize < 0 {
|
|
|
|
return ErrInvalidRequest.New("BatchSize is negative")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-01 14:29:03 +00:00
|
|
|
// IterateLoopObjects iterates through all objects in metabase.
|
|
|
|
func (db *DB) IterateLoopObjects(ctx context.Context, opts IterateLoopObjects, fn func(context.Context, LoopObjectsIterator) error) (err error) {
|
2021-02-18 12:54:09 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
if err := opts.Verify(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-01 14:29:03 +00:00
|
|
|
it := &loopIterator{
|
2021-02-18 12:54:09 +00:00
|
|
|
db: db,
|
|
|
|
|
|
|
|
batchSize: opts.BatchSize,
|
|
|
|
|
|
|
|
curIndex: 0,
|
2021-03-01 14:29:03 +00:00
|
|
|
cursor: loopIterateCursor{},
|
2021-02-18 12:54:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ensure batch size is reasonable
|
2021-03-01 14:29:03 +00:00
|
|
|
if it.batchSize <= 0 || it.batchSize > loopIteratorBatchSizeLimit {
|
|
|
|
it.batchSize = loopIteratorBatchSizeLimit
|
2021-02-18 12:54:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
it.curRows, err = it.doNextQuery(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if rowsErr := it.curRows.Err(); rowsErr != nil {
|
|
|
|
err = errs.Combine(err, rowsErr)
|
|
|
|
}
|
|
|
|
err = errs.Combine(err, it.curRows.Close())
|
|
|
|
}()
|
|
|
|
|
|
|
|
return fn(ctx, it)
|
|
|
|
}
|
|
|
|
|
2021-03-01 14:29:03 +00:00
|
|
|
// loopIterator enables iteration of all objects in metabase.
|
|
|
|
type loopIterator struct {
|
2021-02-18 12:54:09 +00:00
|
|
|
db *DB
|
|
|
|
|
|
|
|
batchSize int
|
|
|
|
|
|
|
|
curIndex int
|
|
|
|
curRows tagsql.Rows
|
2021-03-01 14:29:03 +00:00
|
|
|
cursor loopIterateCursor
|
2021-02-18 12:54:09 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 14:29:03 +00:00
|
|
|
type loopIterateCursor struct {
|
2021-02-18 12:54:09 +00:00
|
|
|
ProjectID uuid.UUID
|
|
|
|
BucketName string
|
|
|
|
ObjectKey ObjectKey
|
|
|
|
Version Version
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next returns true if there was another item and copy it in item.
|
2021-03-01 14:29:03 +00:00
|
|
|
func (it *loopIterator) Next(ctx context.Context, item *LoopObjectEntry) bool {
|
2021-02-18 12:54:09 +00:00
|
|
|
next := it.curRows.Next()
|
|
|
|
if !next {
|
|
|
|
if it.curIndex < it.batchSize {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if it.curRows.Err() != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := it.doNextQuery(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if it.curRows.Close() != nil {
|
|
|
|
_ = rows.Close()
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
it.curRows = rows
|
|
|
|
it.curIndex = 0
|
|
|
|
if !it.curRows.Next() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := it.scanItem(item)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
it.curIndex++
|
|
|
|
it.cursor.ProjectID = item.ProjectID
|
|
|
|
it.cursor.BucketName = item.BucketName
|
|
|
|
it.cursor.ObjectKey = item.ObjectKey
|
|
|
|
it.cursor.Version = item.Version
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-03-01 14:29:03 +00:00
|
|
|
func (it *loopIterator) doNextQuery(ctx context.Context) (_ tagsql.Rows, err error) {
|
2021-02-18 12:54:09 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
return it.db.db.Query(ctx, `
|
|
|
|
SELECT
|
|
|
|
project_id, bucket_name,
|
2021-03-01 17:01:49 +00:00
|
|
|
object_key, stream_id, version,
|
|
|
|
expires_at,
|
2021-02-18 12:54:09 +00:00
|
|
|
segment_count,
|
2021-03-01 17:01:49 +00:00
|
|
|
LENGTH(COALESCE(encrypted_metadata,''))
|
2021-02-18 12:54:09 +00:00
|
|
|
FROM objects
|
|
|
|
WHERE (project_id, bucket_name, object_key, version) > ($1, $2, $3, $4)
|
|
|
|
ORDER BY project_id ASC, bucket_name ASC, object_key ASC, version ASC
|
|
|
|
LIMIT $5
|
2021-03-02 11:22:49 +00:00
|
|
|
`, it.cursor.ProjectID, []byte(it.cursor.BucketName),
|
2021-02-18 12:54:09 +00:00
|
|
|
[]byte(it.cursor.ObjectKey), int(it.cursor.Version),
|
|
|
|
it.batchSize,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-03-01 14:29:03 +00:00
|
|
|
// scanItem scans doNextQuery results into LoopObjectEntry.
|
|
|
|
func (it *loopIterator) scanItem(item *LoopObjectEntry) error {
|
2021-02-18 12:54:09 +00:00
|
|
|
return it.curRows.Scan(
|
|
|
|
&item.ProjectID, &item.BucketName,
|
2021-03-01 17:01:49 +00:00
|
|
|
&item.ObjectKey, &item.StreamID, &item.Version,
|
|
|
|
&item.ExpiresAt,
|
2021-02-18 12:54:09 +00:00
|
|
|
&item.SegmentCount,
|
2021-03-01 17:01:49 +00:00
|
|
|
&item.EncryptedMetadataSize,
|
2021-02-18 12:54:09 +00:00
|
|
|
)
|
|
|
|
}
|