Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SIRI-989: File Storage – Fix Duplicate Listing of Files and incorrect Total Count for Pagination #2020

Merged
merged 2 commits into from
Jul 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions src/main/java/sirius/biz/storage/layer2/L3Uplink.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;

/**
Expand Down Expand Up @@ -245,11 +246,15 @@ public Page<VirtualFile> queryPage(VirtualFile parent, WebContext webContext) {
List<VirtualFile> children = new ArrayList<>();

BasePageHelper<? extends Blob, ?, ?, ?> blobPageHelper = directory.queryChildBlobsAsPage(webContext);
blobPageHelper.withTotalCount();
if (!blobPageHelper.hasFacetFilters()) {

if (blobPageHelper.hasFacetFilters()) {
blobPageHelper.withTotalCount();
} else {
// We only query for directories if there are no filters (facets) are active,
// as we know that we cannot satisfy them anyway...
queryChildDirectories(parent, directory, result, limit, children);
// We need to calculate total count manually as the page's base query does not include directories.
result.withTotalItems(determineTotalChildElements(directory, result, blobPageHelper));
}

queryChildBlobs(parent, result, limit, children, blobPageHelper);
Expand All @@ -265,6 +270,21 @@ public Page<VirtualFile> queryPage(VirtualFile parent, WebContext webContext) {
return result;
}

private int determineTotalChildElements(Directory directory,
Page<VirtualFile> result,
BasePageHelper<? extends Blob, ?, ?, ?> blobPageHelper) {
AtomicInteger counter = new AtomicInteger();

directory.listChildDirectories(result.getQuery(), Integer.MAX_VALUE, _ -> {
counter.incrementAndGet();
return true;
});

counter.addAndGet((int) blobPageHelper.getBaseQuery().count());

return counter.get();
}

private void queryChildDirectories(VirtualFile parent,
Directory directory,
Page<VirtualFile> result,
Expand All @@ -285,7 +305,7 @@ private void queryChildBlobs(VirtualFile parent,
List<VirtualFile> children,
BasePageHelper<? extends Blob, ?, ?, ?> blobPageHelper) {
Page<? extends Blob> blobPage =
blobPageHelper.withStart(limit.getItemsToSkip()).withPageSize(limit.getMaxItems()).asPage();
blobPageHelper.withStart(limit.getItemsToSkip() + 1).withPageSize(limit.getMaxItems()).asPage();
blobPage.getItems()
.stream()
.filter(blob -> Strings.isFilled(blob.getFilename()))
Expand Down