1515import java .util .Collection ;
1616import java .util .concurrent .BlockingQueue ;
1717import java .util .concurrent .TimeUnit ;
18+ import java .util .concurrent .atomic .AtomicInteger ;
1819
1920/**
2021 * Created by tburch on 8/18/17.
@@ -41,8 +42,10 @@ protected boolean handleDirectory(File directory, int depth, Collection<Void> re
4142 boolean process = localDate .isBefore (LocalDate .now ().minusDays (blobAccessTtl .toDays ()));
4243 if (process ) {
4344 log .info ("Processing {} blobs for un-accessed blobs" , directory .getAbsolutePath ());
45+ AtomicInteger fileCount = new AtomicInteger (0 );
4446 Files .newDirectoryStream (directory .toPath ())
4547 .forEach (path -> {
48+ fileCount .incrementAndGet ();
4649 File file = path .toFile ();
4750 if (file .getName ().startsWith (FileSystemJsonBlobManager .BLOB_METADATA_FILE_NAME )) {
4851 return ;
@@ -53,27 +56,26 @@ protected boolean handleDirectory(File directory, int depth, Collection<Void> re
5356 log .warn ("Interrupted while trying to add file to be processed at {}" , file .getAbsolutePath (), e );
5457 }
5558 });
56- }
5759
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 ());
60+ if (fileCount .get () == 0 ) {
61+ log .info ("{} has no files, so it's being deleted" , directory .getAbsolutePath ());
62+ } else if (fileCount .get () == 1 ) {
63+ File [] files = directory .listFiles ();
64+ if (files != null && files [0 ].getName ().startsWith (FileSystemJsonBlobManager .BLOB_METADATA_FILE_NAME )) {
65+ if (directory .delete ()) {
66+ log .info ("{} has only a metadata file, so it's being deleted" , directory .getAbsolutePath ());
67+ }
6368 }
6469 }
65- } else if (files .length == 0 ) {
66- log .info ("{} has no files, so it's being deleted" , directory .getAbsolutePath ());
70+ return false ;
6771 }
68- return false ;
6972 } else {
7073 File [] files = directory .listFiles ();
7174 if (files != null && files .length == 0 ) {
7275 if (directory .delete ()) log .info ("{} has no files, so it's being deleted" , directory .getAbsolutePath ());
7376 return false ;
7477 }
7578 }
76-
7779 return true ;
7880 }
7981
0 commit comments