asp.netasp.net-mvccassandrapagination

Backward paging in cassandra c# driver


We are tryint to use cassandra to store database. We are not able to page backward/forward in c# datastax driver. Can anyone suggest a methodology to page results in MVC project.


Solution

  • You can use the manual paging feature of the C# driver to page through results from Cassandra. Basically it works like this:

    1. Before querying the first page, call SetAutoPage(false) and SetPageSize(pageSize) on your statement that's going to get the first page.
    2. Select the first page of results from Cassandra. The RowSet that's returned from the query will have a PagingState property. Save that paging state somewhere (for example, in Session storage or a Cookie)
    3. Later, when you want to get the next page, retrieve the PagingState from where you stored it.
    4. Before you query for the next page, call SetAutoPage(false) and SetPagingState(yourPagingStateFromStorage) on the statement to get the next page.
    5. Repeat steps 2-4 for subsequent pages.

    The tricky part is paging backwards. If you're caching the results of your paging in your UI (i.e. not doing a full page refresh every time some clicks a button to go to the next/previous page), this isn't an issue since paging backwards is really just moving backwards through the data you've already queried and have cached (probably in an array in your JavaScript code).

    If you are doing a full page refresh every time someone clicks to page through results, then you'll need to keep the PagingState from all pages around until you're sure they're done paging (i.e. store multiple paging state values in something like Session or a cookie). That way, if someone pages backwards you can just lookup the paging state token for that previous page and use it in your query.

    Note: If you're in the full page refresh situation, you might also be able to pass the paging state around as a querystring parameter. For example, make the link to the next page "/some/page?pagingState=PAGING_STATE_FROM_ROWSET_CURRENTLY_DISPLAYED" and then having backwards paging just do the same thing as the browser's back button.