diff --git a/src/main/java/sirius/biz/storage/layer2/L3Uplink.java b/src/main/java/sirius/biz/storage/layer2/L3Uplink.java index bab447e80..a26fed5a5 100644 --- a/src/main/java/sirius/biz/storage/layer2/L3Uplink.java +++ b/src/main/java/sirius/biz/storage/layer2/L3Uplink.java @@ -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; /** @@ -245,11 +246,15 @@ public Page queryPage(VirtualFile parent, WebContext webContext) { List children = new ArrayList<>(); BasePageHelper 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); @@ -265,6 +270,21 @@ public Page queryPage(VirtualFile parent, WebContext webContext) { return result; } + private int determineTotalChildElements(Directory directory, + Page result, + BasePageHelper 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 result, @@ -285,7 +305,7 @@ private void queryChildBlobs(VirtualFile parent, List children, BasePageHelper blobPageHelper) { Page 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()))