0
Fork 0
mirror of https://github.com/project-zot/zot.git synced 2025-01-06 22:40:28 -05:00
zot/pkg/meta/dynamo/iterator.go
LaurentiuNiculae af819e7b76
refactor(repodb): moving common utilities under pkg/meta (#1292)
* refactor(repodb): moving common utilities under pkg/meta

Signed-off-by: Laurentiu Niculae <niculae.laurentiu1@gmail.com>

* refactor(repodb): moved update, version components under pkg/meta

- updated wrapper initialization to recieve a log object in constructor

Signed-off-by: Laurentiu Niculae <niculae.laurentiu1@gmail.com>

* refactor(repodb): moved repodb initialization from controller to pkg/meta/repodb

Signed-off-by: Laurentiu Niculae <niculae.laurentiu1@gmail.com>

---------

Signed-off-by: Laurentiu Niculae <niculae.laurentiu1@gmail.com>
2023-03-28 10:20:09 -07:00

99 lines
2.3 KiB
Go

package dynamo
import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"zotregistry.io/zot/pkg/log"
)
type AttributesIterator interface {
First(ctx context.Context) (types.AttributeValue, error)
Next(ctx context.Context) (types.AttributeValue, error)
}
type BaseAttributesIterator struct {
Client *dynamodb.Client
Table string
Attribute string
itemBuffer []map[string]types.AttributeValue
currentItemIndex int
lastEvaluatedKey map[string]types.AttributeValue
readLimit *int32
log log.Logger
}
func NewBaseDynamoAttributesIterator(client *dynamodb.Client, table, attribute string, maxReadLimit int32,
log log.Logger,
) *BaseAttributesIterator {
var readLimit *int32
if maxReadLimit > 0 {
readLimit = &maxReadLimit
}
return &BaseAttributesIterator{
Client: client,
Table: table,
Attribute: attribute,
itemBuffer: []map[string]types.AttributeValue{},
currentItemIndex: 0,
readLimit: readLimit,
log: log,
}
}
func (dii *BaseAttributesIterator) First(ctx context.Context) (types.AttributeValue, error) {
scanOutput, err := dii.Client.Scan(ctx, &dynamodb.ScanInput{
TableName: aws.String(dii.Table),
Limit: dii.readLimit,
})
if err != nil {
return nil, err
}
if len(scanOutput.Items) == 0 {
return nil, nil
}
dii.itemBuffer = scanOutput.Items
dii.lastEvaluatedKey = scanOutput.LastEvaluatedKey
dii.currentItemIndex = 1
return dii.itemBuffer[0][dii.Attribute], nil
}
func (dii *BaseAttributesIterator) Next(ctx context.Context) (types.AttributeValue, error) {
if len(dii.itemBuffer) <= dii.currentItemIndex {
if dii.lastEvaluatedKey == nil {
return nil, nil
}
scanOutput, err := dii.Client.Scan(ctx, &dynamodb.ScanInput{
TableName: aws.String(dii.Table),
ExclusiveStartKey: dii.lastEvaluatedKey,
})
if err != nil {
return nil, err
}
// all items have been scanned
if len(scanOutput.Items) == 0 {
return nil, nil
}
dii.itemBuffer = scanOutput.Items
dii.lastEvaluatedKey = scanOutput.LastEvaluatedKey
dii.currentItemIndex = 0
}
nextItem := dii.itemBuffer[dii.currentItemIndex][dii.Attribute]
dii.currentItemIndex++
return nextItem, nil
}