s3browser-backend/internal/loader/batch.go

135 lines
2.9 KiB
Go
Raw Permalink Normal View History

2021-10-14 17:00:11 +00:00
package loader
import (
"context"
"fmt"
2021-11-23 19:12:24 +00:00
"git.kapelle.org/niklas/s3browser/internal/s3"
"git.kapelle.org/niklas/s3browser/internal/types"
2021-10-14 17:00:11 +00:00
"github.com/graph-gophers/dataloader"
log "github.com/sirupsen/logrus"
)
// listObjectsBatch batch func for calling s3.ListObjects()
func listObjectsBatch(c context.Context, k dataloader.Keys) []*dataloader.Result {
var results []*dataloader.Result
2021-11-23 19:12:24 +00:00
s3Client, ok := c.Value("s3Client").(s3.S3Service)
2021-10-14 17:00:11 +00:00
if !ok {
return handleLoaderError(k, fmt.Errorf("Failed to get s3Client from context"))
}
for _, v := range k {
id := v.Raw().(types.ID)
2021-11-23 19:12:24 +00:00
objects, err := s3Client.ListObjects(c, id)
if err != nil {
results = append(results, &dataloader.Result{
Data: nil,
Error: err,
})
} else {
results = append(results, &dataloader.Result{
Data: objects,
Error: nil,
})
}
2021-10-14 17:00:11 +00:00
}
return results
}
// listObjectsRecursiveBatch just like listObjectsBatch but with recursive set to true
func listObjectsRecursiveBatch(c context.Context, k dataloader.Keys) []*dataloader.Result {
var results []*dataloader.Result
2021-11-23 19:12:24 +00:00
s3Client, ok := c.Value("s3Client").(s3.S3Service)
2021-10-14 17:00:11 +00:00
if !ok {
return handleLoaderError(k, fmt.Errorf("Failed to get s3Client from context"))
}
for _, v := range k {
id := v.Raw().(types.ID)
2021-11-23 19:12:24 +00:00
objects, err := s3Client.ListObjectsRecursive(c, id)
if err != nil {
results = append(results, &dataloader.Result{
Data: nil,
Error: err,
})
} else {
results = append(results, &dataloader.Result{
Data: objects,
Error: nil,
})
}
2021-10-14 17:00:11 +00:00
}
return results
}
func listBucketsBatch(c context.Context, k dataloader.Keys) []*dataloader.Result {
var results []*dataloader.Result
2021-11-23 19:12:24 +00:00
s3Client, ok := c.Value("s3Client").(s3.S3Service)
2021-10-14 17:00:11 +00:00
if !ok {
return handleLoaderError(k, fmt.Errorf("Failed to get s3Client from context"))
}
2021-11-04 18:41:50 +00:00
log.Debug("S3 'ListBuckets'")
2021-10-14 17:00:11 +00:00
buckets, err := s3Client.ListBuckets(c)
if err != nil {
return handleLoaderError(k, err)
}
result := &dataloader.Result{
Data: buckets,
Error: nil,
}
for range k {
results = append(results, result)
}
return results
}
func statObjectBatch(ctx context.Context, k dataloader.Keys) []*dataloader.Result {
log.Debug("statObjectBatch")
var results []*dataloader.Result
2021-11-23 19:12:24 +00:00
s3Client, ok := ctx.Value("s3Client").(s3.S3Service)
2021-10-14 17:00:11 +00:00
if !ok {
return handleLoaderError(k, fmt.Errorf("Failed to get s3Client from context"))
}
for _, v := range k {
id := v.Raw().(types.ID)
2021-11-04 18:41:50 +00:00
log.Debug("S3 'StatObject': ", id)
2021-11-23 19:12:24 +00:00
stat, err := s3Client.StatObject(ctx, id)
2021-10-14 17:00:11 +00:00
results = append(results, &dataloader.Result{
Data: stat,
Error: err,
})
}
return results
}
// handleLoaderError helper func when the whole batch failed
func handleLoaderError(k dataloader.Keys, err error) []*dataloader.Result {
log.Error(err.Error())
var results []*dataloader.Result
for range k {
results = append(results, &dataloader.Result{
Data: nil,
Error: err,
})
}
return results
}