diff options
| author | Chris Lu <chris.lu@gmail.com> | 2020-12-12 13:25:19 -0800 |
|---|---|---|
| committer | Chris Lu <chris.lu@gmail.com> | 2020-12-12 13:25:19 -0800 |
| commit | f930c713fcb3f75b39459decb7bcd739e0d3e27b (patch) | |
| tree | a5f5380fe0a14cd122fff043cc724f9f23fc2941 | |
| parent | 316d1b4e691f5218042a3b2ccbe8d234ef182171 (diff) | |
| download | seaweedfs-f930c713fcb3f75b39459decb7bcd739e0d3e27b.tar.xz seaweedfs-f930c713fcb3f75b39459decb7bcd739e0d3e27b.zip | |
more efficient recursion
| -rw-r--r-- | weed/s3api/s3api_objects_list_handlers.go | 55 |
1 files changed, 40 insertions, 15 deletions
diff --git a/weed/s3api/s3api_objects_list_handlers.go b/weed/s3api/s3api_objects_list_handlers.go index 5804f0d44..eea06a08a 100644 --- a/weed/s3api/s3api_objects_list_handlers.go +++ b/weed/s3api/s3api_objects_list_handlers.go @@ -263,14 +263,10 @@ func (s3a *S3ApiServer) doListFilerEntries(client filer_pb.SeaweedFilerClient, d } } else { var isEmpty bool - if isEmpty, err = s3a.isDirectoryAllEmpty(client, dir+"/"+entry.Name); err != nil { + if isEmpty, err = s3a.isDirectoryAllEmpty(client, dir, entry.Name); err != nil { return } - if isEmpty { - if err = doDeleteEntry(client, dir, entry.Name, true, true); err != nil { - return - } - } else { + if !isEmpty { eachEntryFn(dir, entry) counter++ } @@ -311,14 +307,43 @@ func getListObjectsV1Args(values url.Values) (prefix, marker, delimiter string, return } -func (s3a *S3ApiServer) isDirectoryAllEmpty(filerClient filer_pb.SeaweedFilerClient, dir string) (isEmpty bool, err error) { - // println("+ isDirectoryAllEmpty", dir) - subCounter, _, _, subErr := s3a.doListFilerEntries(filerClient, dir, "", 32, "", "/", func(dir string, entry *filer_pb.Entry) { - }) - if subErr != nil { - err = fmt.Errorf("isDirectoryAllEmpty: %v", subErr) +func (s3a *S3ApiServer) isDirectoryAllEmpty(filerClient filer_pb.SeaweedFilerClient, parentDir, name string) (isEmpty bool, err error) { + // println("+ isDirectoryAllEmpty", dir, name) + var fileCounter int + var subDirs []string + currentDir := parentDir+"/"+name + err = filer_pb.SeaweedList(filerClient, currentDir, "", func(entry *filer_pb.Entry, isLast bool) error { + if entry.IsDirectory { + subDirs = append(subDirs, entry.Name) + } else { + println("existing file", currentDir, entry.Name) + fileCounter++ + } + return nil + }, "",false, 32) + + if err != nil { + return false, err } - isEmpty = subCounter <= 0 - // println("- isDirectoryAllEmpty", dir, isEmpty) - return + + if fileCounter > 0 { + return false, nil + } + + for _, subDir := range subDirs { + isSubEmpty, subErr := s3a.isDirectoryAllEmpty(filerClient, currentDir, subDir) + if subErr != nil { + return false, subErr + } + if !isSubEmpty { + return false, nil + } + } + + println("deleting empty", currentDir) + if err = doDeleteEntry(filerClient, parentDir, name, true, true); err != nil { + return + } + + return true, nil } |
