Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactor KeyIterator constructor to builder pattern

and mark the constructors as deprecated and leave it for back for backward compatability
  • Loading branch information...
commit b21dcb77a833297a3836c7eac680c2c7f215aafe 1 parent 65e33cf
@skaruppiah skaruppiah authored
View
55 core/src/main/java/me/prettyprint/cassandra/service/KeyIterator.java
@@ -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);
+ }
}
View
35 core/src/main/java/me/prettyprint/cassandra/service/StringKeyIterator.java
@@ -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);
+ }
}
View
16 core/src/test/java/me/prettyprint/cassandra/service/KeyIteratorTest.java
@@ -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);
+ }
+
}
Please sign in to comment.
Something went wrong with that request. Please try again.