Ivan Stoyanov
Ivan Stoyanov

Reputation: 5482

How to navigate to previous page using Cassandra manual paging

I am using the Nodejs Cassandra driver and I want to be able to retrieve the previous and next pages. So far the documentation shows the retrieval of the next page, which is saving the pageState from the previous page and passing it as a parameter. Sadly there is no info on how to navigate to the previous page.

As I see it there are two options:

Both methods does not seem to be an elegant solution to me, but if I have to choose I'll use the first one.

Is there any way to do this out of the box using the Nodejs Cassandra driver?

Another thing is that in the documentation the manual paging is used by calling the eachRow function. If I understand it correctly it gives you every row as soon as it is red from the database. The problem is that this is implemented in my API and I am returning the data for the current page in the HTTP response. So in order for me to do that I'll have to push each row to a custom array and then return the array when the data for the current page is retrieved. Is there a way to use execute with the manual paging as the above seems redundant?

Thanks


EDIT:

This is my data model:

CREATE TABLE store_customer_report (
    store_id uuid,
    segment_id uuid,
    report_time timestamp,
    sharder int,
    customer_email text,
    count int static,
    first_name text,
    last_name text,
    PRIMARY KEY ((store_id, segment_id, report_time, sharder), customer_email)
) WITH CLUSTERING ORDER BY (customer_email ASC)

I am displaying the data in a grid, so that the user can navigate trough it. As I write this I thought of a way to do this without needing the previous functionality, but nevertheless I think that this is a valid case and it will be great if there is an elegant solution to it.

Upvotes: 6

Views: 1665

Answers (2)

Andy Tolbert
Andy Tolbert

Reputation: 11638

Sadly there is no info on how to navigate to the previous page.

That is correct, when you make a query and there are more rows, Cassandra returns a paging state to fetch the next set of rows, but not the previous ones. While what the paging state represents is abstracted away, it is generally a pointer to where to continue reading the next set of data, there really isn't a concept of reading the previous set of data (because you just read it).

Save each pageState and page as a key-value pair and use the pageState for the page that you want to navigate to.

This is the strategy i'd recommend too, of course to get the paging state you actually have to make the queries.

Is there any way to do this out of the box using the Nodejs Cassandra driver?

Not that I am aware of unfortunately, if you want to go back to previous pages you are going to need to track the state.

Is there a way to use execute with the manual paging as the above seems redundant?

Yep, you can provide pageState in the options parameter with execute as well and it will be regarded, i.e.:

  client.execute('SELECT * FROM table', [], {pageState: pageState}, function (err, result) {
    ...
  });

Upvotes: 4

Chris Lohfink
Chris Lohfink

Reputation: 16410

There is an option to be "more" manual, this was common before paging features. You can store the last partition/clustering keys of the current page you are sending back in response (possibly encrypted depending on what it is, most likely generate the partition key and only send/receive the clustering keys to avoid security issues). Then when doing your "next page" response you just have to start your CQL query from there. To go back a page, change ORDER BY clause of clustering key and walk backwards. If you provide your schema it would be easier to give examples. Thats all the pageState really does anyway.

Upvotes: 3

Related Questions