Skip to content

Commit

Permalink
Consolidated query functions in CouchbaseClient.
Browse files Browse the repository at this point in the history
Now there is only one query function

Change-Id: I21b0ababa452ff760e3c0b8ddf72a9a8ed3fdc24
Reviewed-on: http://review.couchbase.org/9006
Reviewed-by: Michael Wiederhold <[email protected]>
Tested-by: Michael Wiederhold <[email protected]>
  • Loading branch information
Mike Wiederhold authored and Michael Wiederhold committed Aug 19, 2011
1 parent 4e7831a commit d18560a
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 66 deletions.
67 changes: 16 additions & 51 deletions src/main/java/net/spy/memcached/CouchbaseClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,16 @@ public List<View> getViews(final String designDocumentName) {
}
}

public HttpFuture<ViewResponse> asyncQuery(View view, Query query) {
if (query.willReduce()) {
return asyncQueryAndReduce(view, query);
} else if (query.willIncludeDocs()) {
return asyncQueryAndIncludeDocs(view, query);
} else {
return asyncQueryAndExcludeDocs(view, query);
}
}

/**
* Asynchronously queries a Couchbase view by calling its map function. This
* type of query will return the view result along with all of the documents
Expand All @@ -258,12 +268,9 @@ public List<View> getViews(final String designDocumentName) {
* @param query the type of query to run against the view.
* @return a Future containing the results of the query.
*/
public HttpFuture<ViewResponse> asyncQuery(View view, Query query) {
String queryString = query.toString();
String params = (queryString.length() > 0) ? "&reduce=false"
: "?reduce=false";

String uri = view.getURI() + queryString + params;
private HttpFuture<ViewResponse> asyncQueryAndIncludeDocs(View view,
Query query) {
String uri = view.getURI() + query.toString();
final CountDownLatch couchLatch = new CountDownLatch(1);
final ViewFuture crv = new ViewFuture(couchLatch, 60000);

Expand Down Expand Up @@ -310,14 +317,9 @@ public void gotData(ViewResponse response) {
* @param query the type of query to run against the view.
* @return a Future containing the results of the query.
*/
public HttpFuture<ViewResponse> asyncQueryAndExcludeDocs(View view,
private HttpFuture<ViewResponse> asyncQueryAndExcludeDocs(View view,
Query query) {
String queryString = query.toString();
String params = (queryString.length() > 0) ? "&reduce=false"
: "?reduce=false";
params += "&include_docs=false";

String uri = view.getURI() + queryString + params;
String uri = view.getURI() + query.toString();
final CountDownLatch couchLatch = new CountDownLatch(1);
final HttpFuture<ViewResponse> crv =
new HttpFuture<ViewResponse>(couchLatch, 60000);
Expand Down Expand Up @@ -356,7 +358,7 @@ public void gotData(ViewResponse response) {
* @param query the type of query to run against the view.
* @return a Future containing the results of the query.
*/
public HttpFuture<ViewResponse> asyncQueryAndReduce(final View view,
private HttpFuture<ViewResponse> asyncQueryAndReduce(final View view,
final Query query) {
if (!view.hasReduce()) {
throw new RuntimeException("This view doesn't contain a reduce function");
Expand Down Expand Up @@ -411,43 +413,6 @@ public ViewResponse query(View view, Query query) {
}
}

/**
* Queries a Couchbase view by calling its map function. This type of query
* will return the view result but will not get the documents associated with
* each row of the query.
*
* @param view the view to run the query against.
* @param query the type of query to run against the view.
* @return a ViewResponseNoDocs containing the results of the query.
*/
public ViewResponse queryAndExcludeDocs(View view, Query query) {
try {
return asyncQueryAndExcludeDocs(view, query).get();
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted while accessing the view", e);
} catch (ExecutionException e) {
throw new RuntimeException("Failed to access the view", e);
}
}

/**
* Queries a Couchbase view by calling its map function and then the views
* reduce function.
*
* @param view the view to run the query against.
* @param query the type of query to run against the view.
* @return a Future containing the results of the query.
*/
public ViewResponse queryAndReduce(View view, Query query) {
try {
return asyncQueryAndReduce(view, query).get();
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted while accessing the view", e);
} catch (ExecutionException e) {
throw new RuntimeException("Failed to access the view", e);
}
}

/**
* Adds an operation to the queue where it waits to be sent to Couchbase. This
* function is for internal use only.
Expand Down
10 changes: 0 additions & 10 deletions src/main/java/net/spy/memcached/CouchbaseClientIF.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,7 @@ HttpFuture<View> asyncGetView(final String designDocumentName,
List<View> getViews(final String designDocumentName);

// Query
// Query
HttpFuture<ViewResponse> asyncQuery(View view, Query query);

HttpFuture<ViewResponse> asyncQueryAndExcludeDocs(View view, Query query);

HttpFuture<ViewResponse> asyncQueryAndReduce(View view, Query query);

ViewResponse query(View view, Query query);

ViewResponse queryAndExcludeDocs(View view, Query query);

ViewResponse queryAndReduce(View view, Query query);

}
21 changes: 21 additions & 0 deletions src/main/java/net/spy/memcached/protocol/couch/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,29 @@ public class Query {
private static final String INCLUSIVEEND = "inclusive_end";
private static final String KEY = "key";
private static final String LIMIT = "limit";
private static final String REDUCE = "reduce";
private static final String SKIP = "skip";
private static final String STALE = "stale";
private static final String STARTKEY = "startkey";
private static final String STARTKEYDOCID = "startkey_docid";
private static final String UPDATESEQ = "update_seq";
private boolean includedocs = false;

private Map<String, Object> args;

public Query() {
args = new HashMap<String, Object>();
}

public boolean willReduce() {
return (args.containsKey(REDUCE))
? ((Boolean)args.get(REDUCE)).booleanValue() : false;
}

public boolean willIncludeDocs() {
return includedocs;
}

public Query setDescending(boolean descending) {
args.put(DESCENDING, Boolean.valueOf(descending));
return this;
Expand All @@ -71,6 +82,11 @@ public Query setGroup(boolean group, int grouplevel) {
return this;
}

public Query setIncludeDocs(boolean includedocs) {
this.includedocs = includedocs;
return this;
}

public Query setInclusiveEnd(boolean inclusiveend) {
args.put(INCLUSIVEEND, Boolean.valueOf(inclusiveend));
return this;
Expand All @@ -97,6 +113,11 @@ public Query setRangeStart(String startkey) {
return this;
}

public Query setReduce(boolean reduce) {
args.put(REDUCE, new Boolean(reduce));
return this;
}

public Query setRangeEnd(String endkey) {
args.put(ENDKEY, endkey);
return this;
Expand Down
29 changes: 24 additions & 5 deletions src/test/java/net/spy/memcached/couch/CouchbaseClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ public void testAssertions() {
@Test
public void testQueryWithDocs() throws Exception {
Query query = new Query();
query.setReduce(false);
query.setIncludeDocs(true);
View view = client.getView(DESIGN_DOC_W_REDUCE, VIEW_NAME_W_REDUCE);
HttpFuture<ViewResponse> future = client.asyncQuery(view, query);
ViewResponse response = future.get();
Expand All @@ -189,9 +191,10 @@ public void testQueryWithDocs() throws Exception {
@Test
public void testViewNoDocs() throws Exception {
Query query = new Query();
query.setReduce(false);
View view = client.getView(DESIGN_DOC_W_REDUCE, VIEW_NAME_W_REDUCE);
HttpFuture<ViewResponse> future =
client.asyncQueryAndExcludeDocs(view, query);
client.asyncQuery(view, query);
assert future.getStatus().isSuccess() : future.getStatus();
ViewResponse response = future.get();

Expand All @@ -208,9 +211,10 @@ public void testViewNoDocs() throws Exception {
@Test
public void testReduce() throws Exception {
Query query = new Query();
query.setReduce(true);
View view = client.getView(DESIGN_DOC_W_REDUCE, VIEW_NAME_W_REDUCE);
HttpFuture<ViewResponse> future =
client.asyncQueryAndReduce(view, query);
client.asyncQuery(view, query);
ViewResponse reduce = future.get();

Iterator<ViewRow> itr = reduce.iterator();
Expand All @@ -225,6 +229,7 @@ public void testReduce() throws Exception {
@Test
public void testQuerySetDescending() throws Exception {
Query query = new Query();
query.setReduce(false);
View view = client.getView(DESIGN_DOC_W_REDUCE, VIEW_NAME_W_REDUCE);
HttpFuture<ViewResponse> future =
client.asyncQuery(view, query.setDescending(true));
Expand All @@ -235,6 +240,7 @@ public void testQuerySetDescending() throws Exception {
@Test
public void testQuerySetEndKeyDocID() throws Exception {
Query query = new Query();
query.setReduce(false);
View view = client.getView(DESIGN_DOC_W_REDUCE, VIEW_NAME_W_REDUCE);
HttpFuture<ViewResponse> future =
client.asyncQuery(view, query.setEndkeyDocID("an_id"));
Expand All @@ -245,26 +251,29 @@ public void testQuerySetEndKeyDocID() throws Exception {
@Test
public void testQuerySetGroup() throws Exception {
Query query = new Query();
query.setReduce(true);
View view = client.getView(DESIGN_DOC_W_REDUCE, VIEW_NAME_W_REDUCE);
HttpFuture<ViewResponse> future =
client.asyncQueryAndReduce(view, query.setGroup(true));
client.asyncQuery(view, query.setGroup(true));
ViewResponse response = future.get();
assert response != null : future.getStatus();
}

@Test
public void testQuerySetGroupWithLevel() throws Exception {
Query query = new Query();
query.setReduce(true);
View view = client.getView(DESIGN_DOC_W_REDUCE, VIEW_NAME_W_REDUCE);
HttpFuture<ViewResponse> future =
client.asyncQueryAndReduce(view, query.setGroup(true, 1));
client.asyncQuery(view, query.setGroup(true, 1));
ViewResponse response = future.get();
assert response != null : future.getStatus();
}

@Test
public void testQuerySetInclusiveEnd() throws Exception {
Query query = new Query();
query.setReduce(false);
View view = client.getView(DESIGN_DOC_W_REDUCE, VIEW_NAME_W_REDUCE);
HttpFuture<ViewResponse> future =
client.asyncQuery(view, query.setInclusiveEnd(true));
Expand All @@ -275,6 +284,7 @@ public void testQuerySetInclusiveEnd() throws Exception {
@Test
public void testQuerySetKey() throws Exception {
Query query = new Query();
query.setReduce(false);
View view = client.getView(DESIGN_DOC_W_REDUCE, VIEW_NAME_W_REDUCE);
HttpFuture<ViewResponse> future =
client.asyncQuery(view, query.setKey("a_key"));
Expand All @@ -285,6 +295,7 @@ public void testQuerySetKey() throws Exception {
@Test
public void testQuerySetLimit() throws Exception {
Query query = new Query();
query.setReduce(false);
View view = client.getView(DESIGN_DOC_W_REDUCE, VIEW_NAME_W_REDUCE);
HttpFuture<ViewResponse> future =
client.asyncQuery(view, query.setLimit(10));
Expand All @@ -295,6 +306,7 @@ public void testQuerySetLimit() throws Exception {
@Test
public void testQuerySetRange() throws Exception {
Query query = new Query();
query.setReduce(false);
View view = client.getView(DESIGN_DOC_W_REDUCE, VIEW_NAME_W_REDUCE);
HttpFuture<ViewResponse> future =
client.asyncQuery(view, query.setRange("key0", "key2"));
Expand All @@ -305,6 +317,7 @@ public void testQuerySetRange() throws Exception {
@Test
public void testQuerySetRangeStart() throws Exception {
Query query = new Query();
query.setReduce(false);
View view = client.getView(DESIGN_DOC_W_REDUCE, VIEW_NAME_W_REDUCE);
HttpFuture<ViewResponse> future =
client.asyncQuery(view, query.setRangeStart("start"));
Expand All @@ -315,6 +328,7 @@ public void testQuerySetRangeStart() throws Exception {
@Test
public void testQuerySetRangeEnd() throws Exception {
Query query = new Query();
query.setReduce(false);
View view = client.getView(DESIGN_DOC_W_REDUCE, VIEW_NAME_W_REDUCE);
HttpFuture<ViewResponse> future =
client.asyncQuery(view, query.setRangeEnd("end"));
Expand All @@ -325,6 +339,7 @@ public void testQuerySetRangeEnd() throws Exception {
@Test
public void testQuerySetSkip() throws Exception {
Query query = new Query();
query.setReduce(false);
View view = client.getView(DESIGN_DOC_W_REDUCE, VIEW_NAME_W_REDUCE);
HttpFuture<ViewResponse> future =
client.asyncQuery(view, query.setSkip(0));
Expand All @@ -335,6 +350,7 @@ public void testQuerySetSkip() throws Exception {
@Test
public void testQuerySetStale() throws Exception {
Query query = new Query();
query.setReduce(false);
View view = client.getView(DESIGN_DOC_W_REDUCE, VIEW_NAME_W_REDUCE);
HttpFuture<ViewResponse> future =
client.asyncQuery(view, query.setStale(Stale.OK));
Expand All @@ -345,6 +361,7 @@ public void testQuerySetStale() throws Exception {
@Test
public void testQuerySetStartkeyDocID() throws Exception {
Query query = new Query();
query.setReduce(false);
View view = client.getView(DESIGN_DOC_W_REDUCE, VIEW_NAME_W_REDUCE);
HttpFuture<ViewResponse> future =
client.asyncQuery(view, query.setStartkeyDocID("key0"));
Expand All @@ -355,6 +372,7 @@ public void testQuerySetStartkeyDocID() throws Exception {
@Test
public void testQuerySetUpdateSeq() throws Exception {
Query query = new Query();
query.setReduce(false);
View view = client.getView(DESIGN_DOC_W_REDUCE, VIEW_NAME_W_REDUCE);
HttpFuture<ViewResponse> future =
client.asyncQuery(view, query.setUpdateSeq(true));
Expand All @@ -365,9 +383,10 @@ public void testQuerySetUpdateSeq() throws Exception {
@Test
public void testReduceWhenNoneExists() throws Exception {
Query query = new Query();
query.setReduce(true);
View view = client.getView(DESIGN_DOC_WO_REDUCE, VIEW_NAME_WO_REDUCE);
try {
client.asyncQueryAndReduce(view, query);
client.asyncQuery(view, query);
} catch (RuntimeException e) {
return; // Pass, no reduce exists.
}
Expand Down

0 comments on commit d18560a

Please sign in to comment.