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

CursorPaginationService.executeQuery could also return the entities.size() before cutting the list to fit the page size #6

Open
diegolodares opened this issue Feb 11, 2025 · 0 comments

Comments

@diegolodares
Copy link

Hi! I'm using your amazing project to manage calls from an external program to a database, and have found out that it is desirable for this "frontend" to receive the total number of elements I have in my database after each pagination-slice call. Up to now I was doing something like this:

 var queryResult = modelPaginationService.executeQuery(pageRequest, filter, ...);
 var numElements = myDataBase.countNumElements(filter);

But I'm experiencing inconsistencies when the actual number of elements in the database changes between the two lines above (due to treacherous insertions taking place at this delicate moment...).

By going through your code, I've seen that I could overcome this situation if I got both results from a single database query, i.e., something akin to

file CursorPaginationService.java, l. 149
List<T> entities;
try {
    entities = mongoOperations.find(query, tClass);
}
catch (Exception e) {
    // Wrap db exception
    throw new CursorPaginationException("Error executing query", e);
}
int entitiesSizeFromContinuationTokenToEnd = entities.size();

boolean hasNext = entities.size() > pageRequest.getSize();
String continuationToken = null;

....

ReturnPair p;
p.first = new CursorPaginationSlice<>(toReturn, pageRequest.getSize(), continuationToken);
p.second = entitiesSizeFromContinuationTokenToEnd;
return p;

I know that "entitiesSizeFromContinuationTokenToEnd" and the "numElements" I had in the first code snippet are not exactly the same thing, but that is not really a problem, the external program could work just fine using this number, the only important thing here is for the returned (CursorPaginationSlice, "size") pair to remain always 100% coherent.

Would you consider adding this useful return value to CursorPaginationService.executeQuery?

Thak you very much for your attention, regards,

Diego

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant