Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
Issue hector-client#419. Ability to specify start and end key
Browse files Browse the repository at this point in the history
in KeyIterator
  • Loading branch information
patricioe committed Feb 13, 2012
1 parent dbc083b commit 6ab4faa
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 6 deletions.
Expand Up @@ -24,7 +24,7 @@
public class KeyIterator<K> implements Iterable<K> {
private static StringSerializer stringSerializer = new StringSerializer();

private int maxRowCount = 500;
private static int MAX_ROW_COUNT_DEFAULT = 500;
private int maxColumnCount = 2; // we only need this to tell if there are any columns in the row (to test for tombstones)

private Iterator<Row<K, String, String>> rowsIterator = null;
Expand All @@ -33,6 +33,8 @@ public class KeyIterator<K> implements Iterable<K> {

private K nextValue = null;
private K lastReadValue = null;
private K endKey;
private boolean firstRun = true;

private Iterator<K> keyIterator = new Iterator<K>() {
@Override
Expand Down Expand Up @@ -66,30 +68,46 @@ private void findNext(boolean fromRunQuery) {
}
}
if (!rowsIterator.hasNext() && nextValue == null) {
runQuery(lastReadValue);
runQuery(lastReadValue, endKey);
}
}

public KeyIterator(Keyspace keyspace, String columnFamily, AbstractSerializer<K> serializer) {
this(keyspace, columnFamily, serializer, null, null, MAX_ROW_COUNT_DEFAULT);
}

public KeyIterator(Keyspace keyspace, String columnFamily, AbstractSerializer<K> serializer, int maxRowCount) {
this(keyspace, columnFamily, serializer, null, null, maxRowCount);
}

public KeyIterator(Keyspace keyspace, String columnFamily, AbstractSerializer<K> serializer, K start, K end) {
this(keyspace, columnFamily, serializer, start, end, MAX_ROW_COUNT_DEFAULT);
}

public KeyIterator(Keyspace keyspace, String columnFamily, AbstractSerializer<K> serializer, K start, K end, int maxRowCount) {
query = HFactory
.createRangeSlicesQuery(keyspace, serializer, stringSerializer, stringSerializer)
.setColumnFamily(columnFamily)
.setRange(null, null, false, maxColumnCount)
.setRowCount(maxRowCount);

runQuery(null);
endKey = end;
runQuery(start, end);
}

private void runQuery(K start) {
query.setKeys(start, null);
private void runQuery(K start, K end) {
query.setKeys(start, end);

rowsIterator = null;
QueryResult<OrderedRows<K, String, String>> result = query.execute();
OrderedRows<K, String, String> rows = (result != null) ? result.get() : null;
rowsIterator = (rows != null) ? rows.iterator() : null;

// we'll skip this first one, since it is the same as the last one from previous time we executed
if (start != null && rowsIterator != null) rowsIterator.next();
if (!firstRun && rowsIterator != null)
rowsIterator.next();

firstRun = false;

if (!rowsIterator.hasNext()) {
nextValue = null; // all done. our iterator's hasNext() will now return false;
Expand Down
@@ -0,0 +1,65 @@
package me.prettyprint.cassandra.service;

import static me.prettyprint.hector.api.factory.HFactory.createColumn;
import static me.prettyprint.hector.api.factory.HFactory.createKeyspace;
import static me.prettyprint.hector.api.factory.HFactory.createMutator;
import static me.prettyprint.hector.api.factory.HFactory.getOrCreateCluster;
import static org.junit.Assert.assertEquals;
import me.prettyprint.cassandra.BaseEmbededServerSetupTest;
import me.prettyprint.cassandra.serializers.IntegerSerializer;
import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.mutation.Mutator;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class KeyIteratorTest extends BaseEmbededServerSetupTest {

private static final StringSerializer se = new StringSerializer();
private static final IntegerSerializer is = IntegerSerializer.get();

private static final String CF = "Standard1";

private Cluster cluster;
private Keyspace keyspace;

@Before
public void setupCase() {
cluster = getOrCreateCluster("Test Cluster", "127.0.0.1:9170");
keyspace = createKeyspace("Keyspace1", cluster);
}

@After
public void teardownCase() {
keyspace = null;
cluster = null;
}

@Test
public void testIterator() {
// Insert 100 rows
Mutator<String> m = createMutator(keyspace, se);
for (int i = 1; i <= 9; i++) {
m.addInsertion("k" + i, CF, createColumn(new Integer(i), new Integer(i), is, is));
}
m.execute();

assertKeys(5, "k5", null);
assertKeys(9, null, null);
assertKeys(7, null, "k7");
}

private void assertKeys(int expected, String start, String end) {
Iterable<String> it = new KeyIterator<String>(keyspace, CF, se, start, end);

int tot = 0;
for (String key : it)
tot++;

assertEquals(expected, tot);
}

}

0 comments on commit 6ab4faa

Please sign in to comment.