Skip to content

Commit

Permalink
#3370 LimitOffsetPagedList add getTotalCount cache (#3373)
Browse files Browse the repository at this point in the history
* #3370 LimitOffsetPagedList add getTotalCount cache

* Move totalRowCount check to inside the lock

* No change, format only on LimitOffsetPagedList

---------

Co-authored-by: Rob Bygrave <robin.bygrave@gmail.com>
  • Loading branch information
spinachomes and rbygrave authored Mar 31, 2024
1 parent e4e7805 commit e0eda4f
Showing 1 changed file with 16 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import io.ebean.PagedList;
import io.ebeaninternal.api.SpiEbeanServer;
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 +21,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 +58,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 @@ -87,20 +93,21 @@ public int getTotalPageCount() {
public int getTotalCount() {
lock.lock();
try {
if (totalRowCount > -1) {
return totalRowCount;
}
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

0 comments on commit e0eda4f

Please sign in to comment.