Skip to content

Commit 7301e67

Browse files
committed
Use a directory Stream
1 parent 4527035 commit 7301e67

File tree

1 file changed

+31
-23
lines changed

1 file changed

+31
-23
lines changed

src/main/java/com/lowtuna/jsonblob/core/BlobCleanupProducer.java

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import java.io.File;
1111
import java.io.IOException;
12+
import java.nio.file.Files;
1213
import java.nio.file.Path;
1314
import java.time.LocalDate;
1415
import 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

Comments
 (0)