Skip to content

Commit

Permalink
Updated iterators to track number of entries retrieved, potentially r…
Browse files Browse the repository at this point in the history
…educing total reads by one.
  • Loading branch information
shane committed Dec 22, 2011
1 parent 071717a commit 2bbcfde
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
Expand Up @@ -124,6 +124,10 @@ public IndexedSlicesQuery<K, N, V> setRowCount(int rowCount) {
return this;
}

public int getRowCount() {
return indexClause.getCount();
}

@Override
public QueryResult<OrderedRows<K, N, V>> execute() {

Expand Down
Expand Up @@ -5,6 +5,7 @@
import me.prettyprint.hector.api.query.SliceQuery;

/**
* Iterates over the column slice, refreshing until all qualifing columns are retrieved.
*
* @author thrykol
*/
Expand All @@ -16,6 +17,7 @@ public class ColumnSliceIterator<K, N, V> implements Iterator {
private ColumnSliceFinish<N> finish;
private boolean reversed;
private int count = 100;
private int columns = 0;

/**
* Constructor
Expand All @@ -28,6 +30,7 @@ public class ColumnSliceIterator<K, N, V> implements Iterator {
public ColumnSliceIterator(SliceQuery<K, N, V> query, N start, final N finish, boolean reversed) {
this(query, start, new ColumnSliceFinish<N>() {

@Override
public N function() {
return finish;
}
Expand All @@ -51,12 +54,14 @@ public ColumnSliceIterator(SliceQuery<K, N, V> query, N start, ColumnSliceFinish
this.query.setRange(this.start, this.finish.function(), this.reversed, count);
}

@Override
public boolean hasNext() {
if (iterator == null) {
iterator = query.execute().get().getColumns().iterator();
} else if (!iterator.hasNext()) {
} else if (!iterator.hasNext() && columns == count) { // only need to do another query if maximum columns were retrieved
query.setRange(start, finish.function(), reversed, count);
iterator = query.execute().get().getColumns().iterator();
columns = 0;

// First element is start which was the last element on the previous query result - skip it
if (iterator.hasNext()) {
Expand All @@ -67,13 +72,16 @@ public boolean hasNext() {
return iterator.hasNext();
}

@Override
public HColumn<N, V> next() {
HColumn<N, V> column = iterator.next();
start = column.getName();
columns++;

return column;
}

@Override
public void remove() {
iterator.remove();
}
Expand Down
Expand Up @@ -5,6 +5,7 @@
import me.prettyprint.hector.api.beans.Row;

/**
* Iterates over the index slices, automatically refreshing the query until all matching rows are returned.
*
* @author thrykol
*/
Expand All @@ -13,6 +14,7 @@ public class IndexedSlicesIterator<K, N, V> implements Iterator {
private IndexedSlicesQuery<K, N, V> query;
private K startKey;
private Iterator<Row<K, N, V>> iterator;
private int rows = 0;

public IndexedSlicesIterator(IndexedSlicesQuery<K, N, V> query, K startKey) {
this.query = query;
Expand All @@ -21,14 +23,16 @@ public IndexedSlicesIterator(IndexedSlicesQuery<K, N, V> query, K startKey) {
this.query.setStartKey(startKey);
}

@Override
public boolean hasNext() {
if (iterator == null) {
// First time through
iterator = query.execute().get().getList().iterator();
} else if (!iterator.hasNext()) {
} else if (!iterator.hasNext() && rows == query.getRowCount()) { // only need to do another query if maximum rows were retrieved
query.setStartKey(startKey);
iterator = query.execute().get().getList().iterator();

rows = 0;

if (iterator.hasNext()) {
// First element is startKey which was the last element on the previous query result - skip it
iterator.next();
Expand All @@ -38,13 +42,16 @@ public boolean hasNext() {
return iterator.hasNext();
}

@Override
public Row<K, N, V> next() {
Row<K, N, V> row = iterator.next();
startKey = row.getKey();
rows++;

return row;
}

@Override
public void remove() {
iterator.remove();
}
Expand Down

0 comments on commit 2bbcfde

Please sign in to comment.