99
1010import java .io .File ;
1111import java .io .IOException ;
12+ import java .nio .file .Files ;
1213import java .nio .file .Path ;
1314import java .time .LocalDate ;
1415import java .util .Collection ;
@@ -34,39 +35,46 @@ public BlobCleanupProducer(Path dataDirectoryPath, Duration blobAccessTtl, Block
3435
3536 @ Override
3637 protected boolean handleDirectory (File directory , int depth , Collection <Void > results ) throws IOException {
37- File [] files = directory .listFiles ();
38-
39- if (files != null && files .length == 0 ) {
40- if (directory .delete ()) log .info ("{} has no files, so it's being deleted" , directory .getAbsolutePath ());
41- return false ;
42- }
43-
44- if (files .length == 1 ) {
45- if (files [0 ].getName ().startsWith (FileSystemJsonBlobManager .BLOB_METADATA_FILE_NAME )) {
46- if (directory .delete ()) log .info ("{} has only a metadata file, so it's being deleted" , directory .getAbsolutePath ());
47- return false ;
48- }
49- }
50-
51- boolean process = true ;
5238 if (isDataDir (directory .getAbsolutePath ())) {
5339 String [] dateParts = directory .getAbsolutePath ().replace (dataDirectoryPath .toFile ().getAbsolutePath (), "" ).split ("/" , 4 );
5440 LocalDate localDate = LocalDate .of (Integer .parseInt (dateParts [1 ]), Integer .parseInt (dateParts [2 ]), Integer .parseInt (dateParts [3 ]));
55- process = localDate .isBefore (LocalDate .now ().minusDays (blobAccessTtl .toDays ()));
41+ boolean process = localDate .isBefore (LocalDate .now ().minusDays (blobAccessTtl .toDays ()));
5642 if (process ) {
5743 log .info ("Processing {} blobs for un-accessed blobs" , directory .getAbsolutePath ());
58- for (File file : files ) {
59- try {
60- filesToProcess .put (file );
61- } catch (InterruptedException e ) {
62- log .warn ("Interrupted while trying to add file to be processed at {}" , file .getAbsolutePath (), e );
44+ Files .newDirectoryStream (directory .toPath ())
45+ .forEach (path -> {
46+ File file = path .toFile ();
47+ if (file .getName ().startsWith (FileSystemJsonBlobManager .BLOB_METADATA_FILE_NAME )) {
48+ return ;
49+ }
50+ try {
51+ filesToProcess .put (file );
52+ } catch (InterruptedException e ) {
53+ log .warn ("Interrupted while trying to add file to be processed at {}" , file .getAbsolutePath (), e );
54+ }
55+ });
56+ }
57+
58+ File [] files = directory .listFiles ();
59+ if (files != null && files .length == 1 ) {
60+ if (files [0 ].getName ().startsWith (FileSystemJsonBlobManager .BLOB_METADATA_FILE_NAME )) {
61+ if (directory .delete ()) {
62+ log .info ("{} has only a metadata file, so it's being deleted" , directory .getAbsolutePath ());
6363 }
6464 }
65- process = false ;
65+ } else if (files .length == 0 ) {
66+ log .info ("{} has no files, so it's being deleted" , directory .getAbsolutePath ());
67+ }
68+ return false ;
69+ } else {
70+ File [] files = directory .listFiles ();
71+ if (files != null && files .length == 0 ) {
72+ if (directory .delete ()) log .info ("{} has no files, so it's being deleted" , directory .getAbsolutePath ());
73+ return false ;
6674 }
6775 }
6876
69- return process ;
77+ return true ;
7078 }
7179
7280 private boolean isDataDir (String path ) {
0 commit comments