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

Initial addition of cursored pagination for SQL #2884

Merged
merged 20 commits into from
May 22, 2024

Conversation

andriy-dmytruk
Copy link
Contributor

This is an initial PR to check if this approach could be considered.

  • Create the CursoredPageable type.
  • Modify the DefaultSqlPreparedQuery to support cursored pageable for SQL. Since cursored pagination requires a new query part with parameters, the type was modified with additional query bindings.
  • Modify DefaultFindPageInterceptor to return the correct pageable for further pagination in the cursored case.

Other interceptors will further need to be changed for this to work in all cases, like the FindPageSpecificationInterceptor. Additionally, the cursor could be encoded to base 64 instead of a JSON array. It seems like Pageabe.hasNext() method would also be beneficial. Should that be added to both offset and cursor pagination?

@andriy-dmytruk andriy-dmytruk changed the base branch from 4.8.x to 4.7.x April 15, 2024 15:27
@andriy-dmytruk andriy-dmytruk force-pushed the andriy/cursored-pagination branch from c1c1fe6 to e3896d2 Compare April 15, 2024 15:27
@graemerocher
Copy link
Contributor

Could you look at the Jakarta Data spec https://github.com/jakartaee/data/blob/main/api/src/main/java/jakarta/data/page/CursoredPage.java

And make sure we mirror that implementation as closely as possible

@dstepanov
Copy link
Contributor

Please target 4.8.x with adjusted @since

@andriy-dmytruk andriy-dmytruk changed the base branch from 4.7.x to 4.8.x April 15, 2024 16:32
@andriy-dmytruk andriy-dmytruk force-pushed the andriy/cursored-pagination branch from e3896d2 to 449d0d2 Compare April 15, 2024 16:34
@andriy-dmytruk
Copy link
Contributor Author

Do you know how I can solve this error?

io.micronaut.context.exceptions.BeanInstantiationException: Bean definition [io.micronaut.data.jdbc.config.SchemaGenerator] could not be loaded: Error instantiating bean of type  [io.micronaut.data.jdbc.config.SchemaGenerator]

Message: Unable to create database schema: FATAL: sorry, too many clients already
Path Taken: new SchemaGenerator(List configurations,JdbcSchemaHandler schemaHandler)

@dstepanov
Copy link
Contributor

Looks like DB is out of connections. How do you get it?

@andriy-dmytruk
Copy link
Contributor Author

Looks like DB is out of connections. How do you get it?

The AbstractCursoredPageSpec has a few parametrized tests that perhaps create many connections. But this error appears only when testing with PostgreSQL.

@andriy-dmytruk
Copy link
Contributor Author

And make sure we mirror that implementation as closely as possible

I added similar Page.hasNext() and Page.hasPrevious() methods.

@dstepanov
Copy link
Contributor

Can you please make an API closer to Jakarta Data?
We should have similar API as https://github.com/jakartaee/data/blob/main/api/src/main/java/jakarta/data/page/PageRequest.java
New class Pageable.Cursor methods afterCursor, beforeCursor, Optional<Cursor> cursor();, Mode.

It would be nice to add requestTotal, withoutTotal, withoutTotal with a logic it has - retrieving a total from the DB or not.

@dstepanov
Copy link
Contributor

Can you also add tests for R2dbc and Hibernate?
Not sure how it should behave for other implementations like Mongo, maybe it should throw an error

@andriy-dmytruk andriy-dmytruk force-pushed the andriy/cursored-pagination branch from ff2acd6 to 5146d90 Compare April 17, 2024 18:15
@andriy-dmytruk andriy-dmytruk force-pushed the andriy/cursored-pagination branch from a881e77 to 019dacd Compare April 18, 2024 01:09
@sdelamo sdelamo added the type: enhancement New feature or request label Apr 19, 2024
Copy link
Contributor

@sdelamo sdelamo left a comment

Choose a reason for hiding this comment

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

It misses documentation.


@Override
void init() {
context = ApplicationContext.run(properties)
Copy link
Contributor

Choose a reason for hiding this comment

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

Less important, I think you could move context creation in the abstract class

@AutoCleanup
    @Shared
    ApplicationContext context = ApplicationContext.run(properties)

and then use it in all tests (usually how we do it) so init() method might not be needed.

@dstepanov
Copy link
Contributor

@andriy-dmytruk Let's try to finish this PR, the only thing that is missing is the documentation and maybe the changes you mentioned regarding the DefaultCursoredPage

Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
3 New Bugs (required ≤ 0)
2 New Blocker Issues (required ≤ 0)
7 New Critical Issues (required ≤ 0)

See analysis details on SonarCloud

Catch issues before they fail your Quality Gate with our IDE extension SonarLint

@andriy-dmytruk
Copy link
Contributor Author

@dstepanov and @sdelamo could you take another look?

sdelamo added a commit to micronaut-projects/micronaut-guides that referenced this pull request May 9, 2024
Guide for the Cursor-based pagination support added to Micronaut Data JDBC by this [PR #2884](micronaut-projects/micronaut-data#2884)

We cannot merge this PR until the framework 4.5.0 release which will contain a Micronaut Data release with that feature.

You can generate the guide’s HTML locally with :

```
./gradlew build
 open build/dist/micronaut-data-cursored-page-gradle-java.html
```

To run the sample code locally, publish to maven local Micronaut Data PR and use:

```
    annotationProcessor("io.micronaut.data:micronaut-data-processor:4.8.0-SNAPSHOT")
    implementation("io.micronaut.data:micronaut-data-jdbc:4.8.0-SNAPSHOT")
```
Copy link
Contributor

@sdelamo sdelamo left a comment

Choose a reason for hiding this comment

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

I wrote a guide which we can publish once we have framework release which contains a data release with this feature.

@dstepanov dstepanov merged commit 696cc25 into 4.8.x May 22, 2024
40 of 43 checks passed
@dstepanov dstepanov deleted the andriy/cursored-pagination branch May 22, 2024 09:38
@dstepanov
Copy link
Contributor

@sdelamo It would be nice to show how to propagate the cursor to the FE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement New feature or request
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

5 participants