Skip to content

Commit

Permalink
JCBC-240 Add total number of rows in ViewResponse
Browse files Browse the repository at this point in the history
Change-Id: I82bcb65e48e3d6fde8d5361ee242f89960d38a6f
Reviewed-on: http://review.couchbase.org/25027
Reviewed-by: Michael Nitschinger <michael.nitschinger@couchbase.com>
Tested-by: Michael Nitschinger <michael.nitschinger@couchbase.com>
  • Loading branch information
johnsdouglass authored and Michael Nitschinger committed Feb 20, 2014
1 parent e7b8b77 commit 009833c
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 16 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/couchbase/client/internal/ViewFuture.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ public ViewResponse get(long duration, TimeUnit units)
docMap.get(r.getId())));
}
}
return new ViewResponseWithDocs(rows, viewResp.getErrors());
return new ViewResponseWithDocs(rows, viewResp.getErrors(),
viewResp.getTotalRows());
}

public void set(ViewResponse viewResponse,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,13 @@ protected ViewResponseWithDocs parseResult(String json)
throws ParseException {
final Collection<ViewRow> rows = new LinkedList<ViewRow>();
final Collection<RowError> errors = new LinkedList<RowError>();
long totalViewRows = 0;
if (json != null) {
try {
JSONObject base = new JSONObject(json);
if (base.has("total_rows")) {
totalViewRows = (long)base.getDouble("total_rows");
}
if (base.has("rows")) {
JSONArray ids = base.getJSONArray("rows");
for (int i = 0; i < ids.length(); i++) {
Expand Down Expand Up @@ -82,7 +86,7 @@ protected ViewResponseWithDocs parseResult(String json)
throw new ParseException("Cannot read json: " + json, 0);
}
}
return new ViewResponseWithDocs(rows, errors);
return new ViewResponseWithDocs(rows, errors, totalViewRows);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ protected ViewResponseNoDocs parseResult(String json)
throws ParseException {
final Collection<ViewRow> rows = new LinkedList<ViewRow>();
final Collection<RowError> errors = new LinkedList<RowError>();
long totalViewRows = 0;
if (json != null) {
try {
JSONObject base = new JSONObject(json);
if (base.has("total_rows")) {
totalViewRows = (long)base.getDouble("total_rows");
}
if (base.has("rows")) {
JSONArray ids = base.getJSONArray("rows");
for (int i = 0; i < ids.length(); i++) {
Expand Down Expand Up @@ -83,7 +87,7 @@ protected ViewResponseNoDocs parseResult(String json)
throw new ParseException("Cannot read json: " + json, 0);
}
}
return new ViewResponseNoDocs(rows, errors);
return new ViewResponseNoDocs(rows, errors, totalViewRows);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,23 @@
public abstract class ViewResponse implements Iterable<ViewRow> {
protected final Collection<ViewRow> rows;
protected final Collection<RowError> errors;
protected final long totalRows;

public ViewResponse(final Collection<ViewRow> r,
final Collection<RowError> e) {
final Collection<RowError> e, long t) {
rows = r;
errors = e;
totalRows = t;
}

public Collection<RowError> getErrors() {
return errors;
}

public long getTotalRows() {
return totalRows;
}

@Override
public Iterator<ViewRow> iterator() {
return rows.iterator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
public class ViewResponseNoDocs extends ViewResponse {

public ViewResponseNoDocs(final Collection<ViewRow> rows,
final Collection<RowError> errors) {
super(rows, errors);
final Collection<RowError> errors, long totalViewRows) {
super(rows, errors, totalViewRows);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ public class ViewResponseReduced extends ViewResponse {

public ViewResponseReduced(final Collection<ViewRow> rows,
final Collection<RowError> errors) {
super(rows, errors);
super(rows, errors, -1);
}

public ViewResponseReduced(final Collection<ViewRow> rows,
final Collection<RowError> errors, long totalViewRows) {
super(rows, errors, totalViewRows);
}

@Override
Expand All @@ -53,4 +58,11 @@ public String toString() {
}
return s.toString();
}

@Override
public long getTotalRows() {
throw new IllegalStateException("Total Number of Rows is not available on "
+ "reduced views.");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public class ViewResponseWithDocs extends ViewResponse {
private Map<String, Object> map;

public ViewResponseWithDocs(final Collection<ViewRow> rows,
final Collection<RowError> errors) {
super(rows, errors);
final Collection<RowError> errors, long totalViewRows) {
super(rows, errors, totalViewRows);
map = null;
}

Expand Down
56 changes: 49 additions & 7 deletions src/test/java/com/couchbase/client/ViewTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -741,13 +741,13 @@ public void testViewLoadWithListener() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
client.asyncGetView(DESIGN_DOC_WO_REDUCE, VIEW_NAME_W_REDUCE).addListener(
new HttpCompletionListener() {
@Override
public void onComplete(HttpFuture<?> httpFuture) throws Exception {
if (httpFuture.getStatus().isSuccess()) {
latch.countDown();
@Override
public void onComplete(HttpFuture<?> httpFuture) throws Exception {
if (httpFuture.getStatus().isSuccess()) {
latch.countDown();
}
}
}
});
});
assertTrue(latch.await(1, TimeUnit.MINUTES));
}

Expand Down Expand Up @@ -1003,8 +1003,50 @@ public void testViewWithBinaryDocs() {
}
if(row.getKey().equals("nonjson2")) {
assertEquals(42, row.getDocument());
}
}
}
}

@Test
public void testTotalNumRowsWithDocs() {
Query query = new Query();
query.setReduce(false).setIncludeDocs(true).setStale(Stale.FALSE);

View view = client.getView(DESIGN_DOC_W_REDUCE, VIEW_NAME_W_REDUCE);
ViewResponse response = client.query(view, query);
long totalRows = response.getTotalRows();
assertTrue(ITEMS.size() <= totalRows);

query.setLimit(5);
response = client.query(view, query);
totalRows = response.getTotalRows();
assertTrue(ITEMS.size() <= totalRows);
}

@Test
public void testTotalNumRowsWithoutDocs() {
Query query = new Query();
query.setReduce(false).setIncludeDocs(false).setStale(Stale.FALSE);

View view = client.getView(DESIGN_DOC_W_REDUCE, VIEW_NAME_W_REDUCE);
ViewResponse response = client.query(view, query);
long totalRows = response.getTotalRows();
assertTrue(ITEMS.size() <= totalRows);

query.setLimit(5);
response = client.query(view, query);
totalRows = response.getTotalRows();
assertTrue(ITEMS.size() <= totalRows);
}

@Test(expected = IllegalStateException.class)
public void testTotalNumRowsReduced() {
Query query = new Query();
query.setIncludeDocs(true).setStale(Stale.FALSE);

View view = client.getView(DESIGN_DOC_W_REDUCE, VIEW_NAME_W_REDUCE);
ViewResponse response = client.query(view, query);
response.getTotalRows();
}

/**
Expand Down

0 comments on commit 009833c

Please sign in to comment.