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

#3370 LimitOffsetPagedList add getTotalCount cache #3373

Merged
merged 3 commits into from
Mar 31, 2024
Merged
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import io.ebeaninternal.api.SpiQuery;

import jakarta.persistence.PersistenceException;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.Future;
import java.util.concurrent.locks.ReentrantLock;
Expand All @@ -20,7 +22,7 @@ public final class LimitOffsetPagedList<T> implements PagedList<T> {
private final int firstRow;
private final int maxRows;

private int foregroundTotalRowCount = -1;
private int totalRowCount = -1;
private Future<Integer> futureRowCount;
private List<T> list;

Expand Down Expand Up @@ -57,7 +59,12 @@ public List<T> getList() {
lock.lock();
try {
if (list == null) {
list = server.findList(query);
if (totalRowCount == 0) {
//already count and no rows
list = Collections.emptyList();
} else {
list = server.findList(query);
}
}
return list;
} finally {
Expand Down Expand Up @@ -85,22 +92,23 @@ public int getTotalPageCount() {

@Override
public int getTotalCount() {
// already fetched?
if (totalRowCount > -1) return totalRowCount;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to move inside the lock ... (or be volatile? but I think just move inside the lock)

lock.lock();
try {
if (futureRowCount != null) {
try {
// background query already initiated so get it with a wait
return futureRowCount.get();
totalRowCount = futureRowCount.get();
return totalRowCount;
} catch (Exception e) {
throw new PersistenceException(e);
}
}
// already fetched?
if (foregroundTotalRowCount > -1) return foregroundTotalRowCount;

// just using foreground thread
foregroundTotalRowCount = server.findCount(query);
return foregroundTotalRowCount;
totalRowCount = server.findCount(query);
return totalRowCount;
} finally {
lock.unlock();
}
Expand Down
Loading