Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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

  • Save the retrieved data in an array and use the array to navigate to the previous page. (I don't think that this is a good solution as I'll have to store large chunks the data in the memory.)

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.

like image 760
Ivan Stoyanov Avatar asked Jan 12 '16 13:01

Ivan Stoyanov


1 Answers

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) {
    ...
  });
like image 194
Andy Tolbert Avatar answered Sep 30 '22 16:09

Andy Tolbert