Skip to content

Instantly share code, notes, and snippets.

@stevesun21
Last active November 5, 2018 12:24
Show Gist options
  • Select an option

  • Save stevesun21/df3fa5141bd01a4f83fc to your computer and use it in GitHub Desktop.

Select an option

Save stevesun21/df3fa5141bd01a4f83fc to your computer and use it in GitHub Desktop.

Revisions

  1. stevesun21 revised this gist May 31, 2015. 1 changed file with 19 additions and 4 deletions.
    23 changes: 19 additions & 4 deletions CassandraPaging
    Original file line number Diff line number Diff line change
    @@ -34,17 +34,29 @@ public class CassandraPaging {

    private ResultSet skipRows(Statement statement, int start, int size) {
    ResultSet result = null;
    int page = getPageNumber(start, size);
    int skippingPages = getPageNumber(start, size);
    String savingPageState = null;
    statement.setFetchSize(size);
    for (int i = 0; i < page; i++) {
    boolean isEnd = false;
    for (int i = 0; i < skippingPages; i++) {
    if (null != savingPageState) {
    statement = statement.setPagingState(PagingState.fromString(savingPageState));
    }
    result = session.execute(statement);
    if (null != result.getExecutionInfo().getPagingState()) {
    PagingState pagingState = result.getExecutionInfo().getPagingState();
    if (null != pagingState) {
    savingPageState = result.getExecutionInfo().getPagingState().toString();
    }

    if (result.isFullyFetched() && null == pagingState) {

    //if hit the end more than once, then nothing to return, otherwise, mark the isEnd to 'true'
    if (true == isEnd) {
    return null;
    } else {
    isEnd = true;
    }
    }
    }
    return result;
    }
    @@ -62,6 +74,9 @@ public class CassandraPaging {

    private List<Row> getRows(ResultSet result, int start, int size) {
    List<Row> rows = new ArrayList<>(size);
    if (null == result) {
    return rows;
    }
    int skippingRows = (start - 1) % size;
    int index = 0;
    for (Iterator<Row> iter = result.iterator(); iter.hasNext() && rows.size() < size;) {
    @@ -74,4 +89,4 @@ public class CassandraPaging {
    return rows;
    }

    }
    }
  2. stevesun21 revised this gist May 31, 2015. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions CassandraPaging
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,13 @@
    import com.datastax.driver.core.*;

    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;

    /**
    * This is a helper class for implementing a pagination function based on Cassandra Java Driver (http://datastax.github.io/java-driver/)
    *
    * This solution is that try to skip rows by page state rather than iterator rows one by one.
    * The solution of skipping rows is that use page state rather than iterator rows one by one.
    *
    */
    public class CassandraPaging {
    @@ -72,4 +74,4 @@ public class CassandraPaging {
    return rows;
    }

    }
    }
  3. stevesun21 revised this gist May 31, 2015. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions CassandraPaging
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,11 @@
    import com.datastax.driver.core.*;

    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;

    /**
    * This is a helper class for implementing a pagination function based on Cassandra Java Driver (http://datastax.github.io/java-driver/)
    *
    * This solution utilizes the page state to skip rows rather than iterator rows one by one.
    * This solution is that try to skip rows by page state rather than iterator rows one by one.
    *
    */
    public class CassandraPaging {
  4. stevesun21 revised this gist May 31, 2015. 1 changed file with 25 additions and 15 deletions.
    40 changes: 25 additions & 15 deletions CassandraPaging
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,3 @@
    package com.rewardofclans.base.cassandra;


    import com.datastax.driver.core.*;

    import java.util.ArrayList;
    @@ -24,17 +21,22 @@ public class CassandraPaging {
    /**
    * Retrieve rows for the specified page offset.
    *
    *
    * @param statement
    * @param start which row start with (>1)
    * @param start starting row (>1), inclusive
    * @param size the maximum rows need to retrieve.
    * @return List<Row>
    */
    public List<Row> fetchRowsWithPage(Statement statement, int start, int size) {
    List<Row> rows = new ArrayList<>(size);
    int page = getPageNumber(start, size);
    ResultSet result = skipRows(statement, start, size);
    return getRows(result, start, size);
    }

    private ResultSet skipRows(Statement statement, int start, int size) {
    ResultSet result = null;
    statement.setFetchSize(size);
    int page = getPageNumber(start, size);
    String savingPageState = null;
    statement.setFetchSize(size);
    for (int i = 0; i < page; i++) {
    if (null != savingPageState) {
    statement = statement.setPagingState(PagingState.fromString(savingPageState));
    @@ -43,14 +45,8 @@ public class CassandraPaging {
    if (null != result.getExecutionInfo().getPagingState()) {
    savingPageState = result.getExecutionInfo().getPagingState().toString();
    }
    if (result.isFullyFetched()) {
    return rows; //no more rows need to fetch.
    }
    }
    for (Iterator<Row> iter = result.iterator(); iter.hasNext() && rows.size() < size;) {
    rows.add(iter.next());
    }
    return rows;
    return result;
    }

    private int getPageNumber(int start, int size) {
    @@ -64,4 +60,18 @@ public class CassandraPaging {
    return page;
    }

    }
    private List<Row> getRows(ResultSet result, int start, int size) {
    List<Row> rows = new ArrayList<>(size);
    int skippingRows = (start - 1) % size;
    int index = 0;
    for (Iterator<Row> iter = result.iterator(); iter.hasNext() && rows.size() < size;) {
    Row row = iter.next();
    if (index >= skippingRows) {
    rows.add(row);
    }
    index ++;
    }
    return rows;
    }

    }
  5. stevesun21 revised this gist May 31, 2015. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions CassandraPaging
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,14 @@
    package com.rewardofclans.base.cassandra;


    import com.datastax.driver.core.*;

    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;

    /**
    * This is a helper class for implementing a pagination function for Cassandra Java Driver (http://datastax.github.io/java-driver/)
    * This is a helper class for implementing a pagination function based on Cassandra Java Driver (http://datastax.github.io/java-driver/)
    *
    * This solution utilizes the page state to skip rows rather than iterator rows one by one.
    *
    @@ -61,4 +64,4 @@ public class CassandraPaging {
    return page;
    }

    }
    }
  6. stevesun21 created this gist May 31, 2015.
    64 changes: 64 additions & 0 deletions CassandraPaging
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    import com.datastax.driver.core.*;

    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;

    /**
    * This is a helper class for implementing a pagination function for Cassandra Java Driver (http://datastax.github.io/java-driver/)
    *
    * This solution utilizes the page state to skip rows rather than iterator rows one by one.
    *
    */
    public class CassandraPaging {

    private Session session;

    public CassandraPaging(Session session) {
    this.session = session;
    }

    /**
    * Retrieve rows for the specified page offset.
    *
    * @param statement
    * @param start which row start with (>1)
    * @param size the maximum rows need to retrieve.
    * @return List<Row>
    */
    public List<Row> fetchRowsWithPage(Statement statement, int start, int size) {
    List<Row> rows = new ArrayList<>(size);
    int page = getPageNumber(start, size);
    ResultSet result = null;
    statement.setFetchSize(size);
    String savingPageState = null;
    for (int i = 0; i < page; i++) {
    if (null != savingPageState) {
    statement = statement.setPagingState(PagingState.fromString(savingPageState));
    }
    result = session.execute(statement);
    if (null != result.getExecutionInfo().getPagingState()) {
    savingPageState = result.getExecutionInfo().getPagingState().toString();
    }
    if (result.isFullyFetched()) {
    return rows; //no more rows need to fetch.
    }
    }
    for (Iterator<Row> iter = result.iterator(); iter.hasNext() && rows.size() < size;) {
    rows.add(iter.next());
    }
    return rows;
    }

    private int getPageNumber(int start, int size) {
    if (start < 1) {
    throw new IllegalArgumentException("Starting row need to be larger than 1");
    }
    int page = 1;
    if (start > size) {
    page = (start - 1) / size + 1;
    }
    return page;
    }

    }