Skip to content

Commit 95bb30d

Browse files
committed
Optimise audit task
1 parent 1d5c77e commit 95bb30d

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

src/main/java/me/lucko/bytebin/content/storage/AuditTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private void run0() throws Exception {
6161
String backendId = backend.getBackendId();
6262

6363
LOGGER.info("[AUDIT] Listing content for backend {}", backendId);
64-
List<String> keys = backend.list().map(Content::getKey).toList();
64+
List<String> keys = backend.listKeys().toList();
6565
LOGGER.info("[AUDIT] Found {} entries for backend {}", keys.size(), backendId);
6666

6767
List<String> keysToDelete = keys.stream()

src/main/java/me/lucko/bytebin/content/storage/LocalDiskBackend.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ public void save(Content c) throws IOException {
9797
}
9898
}
9999

100+
@Override
101+
public Stream<String> listKeys() throws Exception {
102+
return Files.list(this.contentPath).map(path -> path.getFileName().toString());
103+
}
104+
100105
@Override
101106
public Stream<Content> list() throws IOException {
102107
return Files.list(this.contentPath)

src/main/java/me/lucko/bytebin/content/storage/S3Backend.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import software.amazon.awssdk.services.s3.model.ListObjectsV2Request;
4242
import software.amazon.awssdk.services.s3.model.NoSuchKeyException;
4343
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
44+
import software.amazon.awssdk.services.s3.model.S3Object;
4445
import software.amazon.awssdk.services.s3.paginators.ListObjectsV2Iterable;
4546

4647
import java.util.Date;
@@ -111,6 +112,15 @@ public void delete(String key) throws Exception {
111112
);
112113
}
113114

115+
@Override
116+
public Stream<String> listKeys() throws Exception {
117+
ListObjectsV2Iterable iter = this.client.listObjectsV2Paginator(ListObjectsV2Request.builder()
118+
.bucket(this.bucketName)
119+
.build()
120+
);
121+
return iter.stream().flatMap(resp -> resp.contents().stream().map(S3Object::key));
122+
}
123+
114124
@Override
115125
public Stream<Content> list() throws Exception {
116126
ListObjectsV2Iterable iter = this.client.listObjectsV2Paginator(ListObjectsV2Request.builder()

src/main/java/me/lucko/bytebin/content/storage/StorageBackend.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ public interface StorageBackend {
6666
*/
6767
void delete(String key) throws Exception;
6868

69+
/**
70+
* Lists the keys for all content stored in the backend.
71+
*
72+
* @return a list of keys
73+
* @throws Exception catch all
74+
*/
75+
Stream<String> listKeys() throws Exception;
76+
6977
/**
7078
* Lists metadata about all the content stored in the backend. (doesn't load the actual data).
7179
* Used primarily if the index needs to be rebuilt.

0 commit comments

Comments
 (0)