Permalink
Browse files

Merge pull request #573 from mastersenthilkumar/master

Add other constructors for StringKeyIterator
  • Loading branch information...
2 parents 5a47350 + b21dcb7 commit 684cda8d149f434211d6abb96ce3bfb0844b0e2c @zznate zznate committed Dec 27, 2012
@@ -72,18 +72,25 @@ private void findNext(boolean fromRunQuery) {
}
}
+ @Deprecated
public KeyIterator(Keyspace keyspace, String columnFamily, Serializer<K> serializer) {
this(keyspace, columnFamily, serializer, null, null, MAX_ROW_COUNT_DEFAULT);
}
+ @Deprecated
public KeyIterator(Keyspace keyspace, String columnFamily, Serializer<K> serializer, int maxRowCount) {
this(keyspace, columnFamily, serializer, null, null, maxRowCount);
}
-
+
+ @Deprecated
public KeyIterator(Keyspace keyspace, String columnFamily, Serializer<K> serializer, K start, K end) {
this(keyspace, columnFamily, serializer, start, end, MAX_ROW_COUNT_DEFAULT);
}
+ @Deprecated
+ /*
+ * When pulling deprecated methods out, do not remove this but change it to private constructor
+ */
public KeyIterator(Keyspace keyspace, String columnFamily, Serializer<K> serializer, K start, K end, int maxRowCount) {
query = HFactory
.createRangeSlicesQuery(keyspace, serializer, stringSerializer, stringSerializer)
@@ -109,7 +116,7 @@ private void runQuery(K start, K end) {
firstRun = false;
- if (!rowsIterator.hasNext()) {
+ if (rowsIterator != null && !rowsIterator.hasNext()) {
nextValue = null; // all done. our iterator's hasNext() will now return false;
} else {
findNext(true);
@@ -120,5 +127,49 @@ private void runQuery(K start, K end) {
public Iterator<K> iterator() {
return keyIterator;
}
+
+ public static class Builder<K> {
+
+ //required
+ private Keyspace keyspace;
+ private String columnFamily;
+ private Serializer<K> serializer;
+
+ //optional
+ private K start;
+ private K end;
+ private Integer maxRowCount;
+
+ public Builder(Keyspace keyspace, String columnFamily, Serializer<K> serializer) {
+ this.keyspace = keyspace;
+ this.columnFamily = columnFamily;
+ this.serializer = serializer;
+ }
+
+ public Builder<K> start(K start) {
+ this.start = start;
+ return this;
+ }
+
+ public Builder<K> end(K end) {
+ this.end = end;
+ return this;
+ }
+
+ public Builder<K> maxRowCount(int maxRowCount) {
+ this.maxRowCount = maxRowCount;
+ return this;
+ }
+
+ public KeyIterator<K> build() {
+ return new KeyIterator<K>(this);
+ }
+
+ }
+
+ protected KeyIterator(Builder<K> builder) {
+ this(builder.keyspace, builder.columnFamily, builder.serializer, builder.start, builder.end,
+ builder.maxRowCount == null? MAX_ROW_COUNT_DEFAULT : builder.maxRowCount);
+ }
}
@@ -13,8 +13,43 @@
*/
public class StringKeyIterator extends KeyIterator<String> {
+ @Deprecated
public StringKeyIterator(Keyspace keyspace, String columnFamily) {
super(keyspace, columnFamily, new StringSerializer());
}
+ public static class Builder extends KeyIterator.Builder<String> {
+
+ public Builder(Keyspace keyspace, String columnFamily) {
+ super(keyspace, columnFamily, new StringSerializer());
+ }
+
+ @Override
+ public Builder start(String start) {
+ super.start(start);
+ return this;
+ }
+
+ @Override
+ public Builder end(String end) {
+ super.end(end);
+ return this;
+ }
+
+ @Override
+ public Builder maxRowCount(int maxRowCount) {
+ super.maxRowCount(maxRowCount);
+ return this;
+ }
+
+ @Override
+ public StringKeyIterator build() {
+ return new StringKeyIterator(this);
+ }
+
+ }
+
+ private StringKeyIterator(Builder builder) {
+ super(builder);
+ }
}
@@ -50,10 +50,14 @@ public void testIterator() {
assertKeys(5, "k5", null);
assertKeys(9, null, null);
assertKeys(7, null, "k7");
+
+ assertStringKeys(5, "k5", null);
+ assertStringKeys(9, null, null);
+ assertStringKeys(7, null, "k7");
}
private void assertKeys(int expected, String start, String end) {
- Iterable<String> it = new KeyIterator<String>(keyspace, CF, se, start, end);
+ Iterable<String> it = new KeyIterator.Builder<String>(keyspace, CF, se).start(start).end(end).build();
int tot = 0;
for (String key : it)
@@ -62,4 +66,14 @@ private void assertKeys(int expected, String start, String end) {
assertEquals(expected, tot);
}
+ private void assertStringKeys(int expected, String start, String end) {
+ StringKeyIterator sk = new StringKeyIterator.Builder(keyspace, CF).start(start).end(end).build();
+
+ int tot = 0;
+ for (String key : sk)
+ tot++;
+
+ assertEquals(expected, tot);
+ }
+
}

0 comments on commit 684cda8

Please sign in to comment.