deleteDir recursion fix

This commit is contained in:
Niklas 2021-08-27 22:03:19 +02:00
parent 1d5dbfaa1c
commit 8296b21883
2 changed files with 28 additions and 2 deletions

View File

@ -32,12 +32,33 @@ func listObjectsBatch(c context.Context, k dataloader.Keys) []*dataloader.Result
return results return results
} }
// listObjectsRecursiveBatch just like listObjectsBatch but with recursive set to true
func listObjectsRecursiveBatch(c context.Context, k dataloader.Keys) []*dataloader.Result {
log.Debug("listObjectsRecursiveBatch: ", k.Keys())
var results []*dataloader.Result
s3Client, ok := c.Value("s3Client").(*minio.Client)
if !ok {
return handleLoaderError(k, fmt.Errorf("Failed to get s3Client from context"))
}
for _, v := range k {
results = append(results, &dataloader.Result{
Data: listObjects(s3Client, bucketName, v.String(), true),
Error: nil,
})
}
return results
}
// listObjects helper func for listObjectsBatch // listObjects helper func for listObjectsBatch
func listObjects(s3Client *minio.Client, bukitName, path string, recursive bool) []minio.ObjectInfo { func listObjects(s3Client *minio.Client, bukitName, path string, recursive bool) []minio.ObjectInfo {
log.Debug("S3 call 'ListObjects': ", path) log.Debug("S3 call 'ListObjects': ", path)
objectCh := s3Client.ListObjects(context.Background(), bukitName, minio.ListObjectsOptions{ objectCh := s3Client.ListObjects(context.Background(), bukitName, minio.ListObjectsOptions{
Prefix: path, Prefix: path,
Recursive: false, Recursive: recursive,
}) })
result := make([]minio.ObjectInfo, 0) result := make([]minio.ObjectInfo, 0)
@ -210,6 +231,11 @@ func createDataloader(config AppConfig) map[string]*dataloader.Loader {
dataloader.WithCache(newCache(config.CacheTTL, config.CacheCleanup)), dataloader.WithCache(newCache(config.CacheTTL, config.CacheCleanup)),
) )
loaderMap["listObjectsRecursive"] = dataloader.NewBatchedLoader(
listObjectsRecursiveBatch,
dataloader.WithCache(newCache(config.CacheTTL, config.CacheCleanup)),
)
loaderMap["getDirs"] = dataloader.NewBatchedLoader( loaderMap["getDirs"] = dataloader.NewBatchedLoader(
getDirsBatch, getDirsBatch,
dataloader.WithCache(newCache(config.CacheTTL, config.CacheCleanup)), dataloader.WithCache(newCache(config.CacheTTL, config.CacheCleanup)),

View File

@ -152,7 +152,7 @@ func deleteDirectory(ctx context.Context, path string) error {
} }
// Get all files inside the directory // Get all files inside the directory
thunk := loader["listObjects"].Load(ctx, dataloader.StringKey(path)) thunk := loader["listObjectsRecursive"].Load(ctx, dataloader.StringKey(path))
result, err := thunk() result, err := thunk()