Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added mango selector to changes feed #1

Merged
merged 1 commit into from
Feb 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 38 additions & 7 deletions src/main/java/org/lightcouch/Changes.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

import org.apache.commons.codec.Charsets;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.lightcouch.ChangesResult.Row;

import com.google.gson.Gson;
Expand Down Expand Up @@ -56,6 +58,14 @@
* JsonObject doc = feed.getDoc();
* // changes.stop(); // stop continuous feed
* }
*
* Selector filter:
* ChangesResult changeResult = dbClient.changes()
* .since(since)
* .limit(10)
* .selector("{\"selector":{\"_deleted\":true}}")
* .getChanges();
*
* </pre>
* @see ChangesResult
* @since 0.0.2
Expand All @@ -64,14 +74,16 @@
public class Changes {

private BufferedReader reader;
private HttpGet httpGet;
private HttpUriRequest httpRequest;
private Row nextRow;
private boolean stop;

private CouchDbClientBase dbc;
private Gson gson;
private URIBuilder uriBuilder;

private String selector;

Changes(CouchDbClientBase dbc) {
this.dbc = dbc;
this.gson = dbc.getGson();
Expand All @@ -85,10 +97,19 @@ public class Changes {
*/
public Changes continuousChanges() {
final URI uri = uriBuilder.query("feed", "continuous").build();
httpGet = new HttpGet(uri);
final InputStream in = dbc.get(httpGet);
final InputStreamReader is = new InputStreamReader(in, Charsets.UTF_8);
setReader(new BufferedReader(is));
if (selector == null) {
final HttpGet get = new HttpGet(uri);
httpRequest = get;
final InputStream in = dbc.get(get);
final InputStreamReader is = new InputStreamReader(in, Charsets.UTF_8);
setReader(new BufferedReader(is));
} else {
final HttpPost post = new HttpPost(uri);
httpRequest = post;
final InputStream in = dbc.post(post, selector);
final InputStreamReader is = new InputStreamReader(in, Charsets.UTF_8);
setReader(new BufferedReader(is));
}
return this;
}

Expand Down Expand Up @@ -121,7 +142,11 @@ public void stop() {
*/
public ChangesResult getChanges() {
final URI uri = uriBuilder.query("feed", "normal").build();
return dbc.get(uri, ChangesResult.class);
if (selector == null) {
return dbc.get(uri, ChangesResult.class);
} else {
return dbc.post(uri, selector, ChangesResult.class);
}
}

// Query Params
Expand Down Expand Up @@ -151,6 +176,12 @@ public Changes filter(String filter) {
return this;
}

public Changes selector(String json) {
uriBuilder.query("filter", "_selector");
this.selector = json;
return this;
}

public Changes includeDocs(boolean includeDocs) {
uriBuilder.query("include_docs", includeDocs);
return this;
Expand Down Expand Up @@ -206,7 +237,7 @@ private void setNextRow(Row nextRow) {
}

private void terminate() {
httpGet.abort();
httpRequest.abort();
CouchDbUtil.close(getReader());
}
}
1 change: 0 additions & 1 deletion src/main/java/org/lightcouch/CouchDbClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ public void shutdown() {
HttpClientUtils.closeQuietly(this.httpClient);
}

@Override
public void close() throws IOException {
shutdown();
}
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/org/lightcouch/CouchDbClientBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,34 @@ HttpResponse post(URI uri, String json) {
return executeRequest(post);
}

/**
* Performs a HTTP POST request.
*
* @return {@link HttpResponse}
*/
InputStream post(HttpPost post, String json) {
setEntity(post, json);
HttpResponse resp = executeRequest(post);
return getStream(resp);
}

/**
* Performs a HTTP POST request.
*
* @return An object of type T
*/
<T> T post(URI uri, String json, Class<T> classType) {
InputStream in = null;
try {
in = getStream(post(uri, json));
return getGson().fromJson(new InputStreamReader(in, "UTF-8"), classType);
} catch (UnsupportedEncodingException e) {
throw new CouchDbException(e);
} finally {
close(in);
}
}

/**
* Performs a HTTP DELETE request.
* @return {@link Response}
Expand Down
47 changes: 47 additions & 0 deletions src/test/java/org/lightcouch/tests/ChangeNotificationsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,28 @@ public void changes_normalFeed() {
assertThat(rows.size(), is(1));
}

@Test
public void changes_normalFeed_selector() {
dbClient.save(new Foo());

ChangesResult changes = dbClient.changes().includeDocs(true).limit(1)
.selector("{\"selector\":{\"_id\": {\"$gt\": null}}}").getChanges();

List<ChangesResult.Row> rows = changes.getResults();

for (Row row : rows) {
List<ChangesResult.Row.Rev> revs = row.getChanges();
String docId = row.getId();
JsonObject doc = row.getDoc();

assertNotNull(revs);
assertNotNull(docId);
assertNotNull(doc);
}

assertThat(rows.size(), is(1));
}

@Test
public void changes_continuousFeed() {
dbClient.save(new Foo());
Expand All @@ -99,4 +121,29 @@ public void changes_continuousFeed() {
changes.stop();
}
}

@Test
public void changes_continuousFeed_selector() {
dbClient.save(new Foo());

CouchDbInfo dbInfo = dbClient.context().info();
String since = dbInfo.getUpdateSeq();

Changes changes = dbClient.changes().includeDocs(true).since(since).heartBeat(1000)
.selector("{\"selector\":{\"_id\": {\"$gt\": null}}}").continuousChanges();

Response response = dbClient.save(new Foo());

while (changes.hasNext()) {
ChangesResult.Row feed = changes.next();
final JsonObject feedObject = feed.getDoc();
final String docId = feed.getId();
System.out.println("next()=" + docId);

assertEquals(response.getId(), docId);
assertNotNull(feedObject);

changes.stop();
}
}
}