Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Replicator Documents and Connector #85

Merged
merged 3 commits into from

2 participants

@dlvenable

I added a new ReplicatorDocument class to represent the documents stored in the _replicator database. I also create a method to get the _replicator database from a CouchDbInstance.

@helun
Owner

Cool!
One thing though, all Ektorp representations of CouchDB documents and responses should be able to handle unknown fields without breaking. This way, existing Ektorp releases will still work even if a new CouchDB release introduces a new field.

Take a look att org.ektorp.ReplicationStatus.setUnknown to see how it is done.

@dlvenable dlvenable Provided support for unknown fields in the ReplicatorDocument and Use…
…rContext. This was added following the pattern set in DbInfo.
a6a5574
@dlvenable

Thanks for the comment.

I made the update, but actually followed the pattern in DbInfo. I think it is more helpful for two reasons: 1. The name getUnknownFields() has "unknown" and is clearer. The getField() method in ReplicationStatus only returns unknown fields, but the name does not make that clear. 2. The pattern in ReplicationStatus only works if you know the name of the unknown field. Providing the map as in DbInfo lets callers get the unknown field names.

If you would like, I could also deprecate ReplicationStatus.getField() and replace it with getUnknownFields().

@helun helun merged commit 6a7f5dc into helun:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 26, 2012
  1. @dlvenable
  2. @dlvenable

    Added getReplicatorConnector to CouchDbInstance as a way for clients …

    dlvenable authored
    …to get the Couch _replicator database.
Commits on May 28, 2012
  1. @dlvenable

    Provided support for unknown fields in the ReplicatorDocument and Use…

    dlvenable authored
    …rContext. This was added following the pattern set in DbInfo.
This page is out of date. Refresh to see the latest.
View
7 org.ektorp/src/main/java/org/ektorp/CouchDbInstance.java
@@ -35,6 +35,13 @@
* @return
*/
CouchDbConnector createConnector(String path, boolean createIfNotExists);
+
+ /**
+ * Returns the Couch _replicator database
+ * @return CouchDbConnector a connector to the replicator database
+ */
+ CouchDbConnector getReplicatorConnector();
+
/**
* Convenience method for accessing the underlying HttpClient.
* Preferably used wrapped in a org.ektorp.http.RestTemplate.
View
218 org.ektorp/src/main/java/org/ektorp/ReplicatorDocument.java
@@ -0,0 +1,218 @@
+package org.ektorp;
+
+import org.codehaus.jackson.annotate.JsonAnySetter;
+import org.codehaus.jackson.annotate.JsonProperty;
+import org.codehaus.jackson.map.annotate.JsonSerialize;
+import org.ektorp.support.CouchDbDocument;
+
+import java.util.Calendar;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Represents Couch documents used by the Couch _replicator database.
+ */
+@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
+public class ReplicatorDocument extends CouchDbDocument
+{
+ @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
+ public static class UserContext
+ {
+ private String name;
+ private Collection<String> roles;
+
+ private Map<String, Object> unknownFields;
+
+ public String getName()
+ {
+ return name;
+ }
+
+ public void setName(String name)
+ {
+ this.name = name;
+ }
+
+ public Collection<String> getRoles()
+ {
+ return roles;
+ }
+
+ public void setRoles(Collection<String> roles)
+ {
+ this.roles = roles;
+ }
+
+ @JsonAnySetter
+ public void setUnknown(String key, Object value) {
+ unknownFields().put(key, value);
+ }
+
+ public Map<String, Object> getUnknownFields() {
+ return unknownFields();
+ }
+
+ private Map<String, Object> unknownFields() {
+ if (unknownFields == null) {
+ unknownFields = new HashMap<String, Object>();
+ }
+ return unknownFields;
+ }
+ }
+
+ private String source;
+ private String target;
+ private Boolean createTarget;
+ private Boolean continuous;
+ private Collection<String> documentIds;
+ private String filter;
+ private Object queryParameters;
+ private UserContext userContext;
+
+ private String replicationId;
+ private String replicationState;
+ private Calendar replicationStateTime;
+
+ private Map<String, Object> unknownFields;
+
+ public String getSource()
+ {
+ return source;
+ }
+
+ public void setSource(String source)
+ {
+ this.source = source;
+ }
+
+ public String getTarget()
+ {
+ return target;
+ }
+
+ public void setTarget(String target)
+ {
+ this.target = target;
+ }
+
+ @JsonProperty("create_target")
+ public Boolean getCreateTarget()
+ {
+ return createTarget;
+ }
+
+ @JsonProperty("create_target")
+ public void setCreateTarget(Boolean createTarget)
+ {
+ this.createTarget = createTarget;
+ }
+
+ public Boolean getContinuous()
+ {
+ return continuous;
+ }
+
+ public void setContinuous(Boolean continuous)
+ {
+ this.continuous = continuous;
+ }
+
+ @JsonProperty("doc_ids")
+ public Collection<String> getDocumentIds()
+ {
+ return documentIds;
+ }
+
+ @JsonProperty("doc_ids")
+ public void setDocumentIds(Collection<String> documentIds)
+ {
+ this.documentIds = documentIds;
+ }
+
+ public String getFilter()
+ {
+ return filter;
+ }
+
+ public void setFilter(String filter)
+ {
+ this.filter = filter;
+ }
+
+ @JsonProperty("query_params")
+ public Object getQueryParameters()
+ {
+ return queryParameters;
+ }
+
+ @JsonProperty("query_params")
+ public void setQueryParameters(Object queryParameters)
+ {
+ this.queryParameters = queryParameters;
+ }
+
+ @JsonProperty("user_ctx")
+ public UserContext getUserContext()
+ {
+ return userContext;
+ }
+
+ @JsonProperty("user_ctx")
+ public void setUserContext(UserContext userContext)
+ {
+ this.userContext = userContext;
+ }
+
+ @JsonProperty("_replication_id")
+ public String getReplicationId()
+ {
+ return replicationId;
+ }
+
+ @JsonProperty("_replication_id")
+ public void setReplicationId(String replicationId)
+ {
+ this.replicationId = replicationId;
+ }
+
+ @JsonProperty("_replication_state")
+ public String getReplicationState()
+ {
+ return replicationState;
+ }
+
+ @JsonProperty("_replication_state")
+ public void setReplicationState(String replicationState)
+ {
+ this.replicationState = replicationState;
+ }
+
+ @JsonProperty("_replication_state_time")
+ public Calendar getReplicationStateTime()
+ {
+ return replicationStateTime;
+ }
+
+ @JsonProperty("_replication_state_time")
+ public void setReplicationStateTime(Calendar replicationStateTime)
+ {
+ this.replicationStateTime = replicationStateTime;
+ }
+
+ @JsonAnySetter
+ public void setUnknown(String key, Object value) {
+ unknownFields().put(key, value);
+ }
+
+ public Map<String, Object> getUnknownFields() {
+ return unknownFields();
+ }
+
+ private Map<String, Object> unknownFields() {
+ if (unknownFields == null) {
+ unknownFields = new HashMap<String, Object>();
+ }
+ return unknownFields;
+ }
+}
View
8 org.ektorp/src/main/java/org/ektorp/impl/StdCouchDbInstance.java
@@ -106,7 +106,13 @@ public CouchDbConnector createConnector(String path,
return db;
}
- @Override
+ @Override
+ public CouchDbConnector getReplicatorConnector()
+ {
+ return createConnector("_replicator", false);
+ }
+
+ @Override
public <T> T getConfiguration(final Class<T> c) {
return getConfiguration(c, null, null);
}
View
53 org.ektorp/src/test/java/org/ektorp/ReplicatorDocumentTest.java
@@ -0,0 +1,53 @@
+package org.ektorp;
+
+import org.codehaus.jackson.map.ObjectMapper;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
+
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsNull.notNullValue;
+import static org.junit.Assert.assertThat;
+
+public class ReplicatorDocumentTest
+{
+ private ObjectMapper objectMapper;
+
+ @Before
+ public void setUp()
+ {
+ objectMapper = new ObjectMapper();
+ }
+
+ @Test
+ public void getUnknownFields_should_return_an_empty_map_if_there_were_no_fields_which_were_not_mapped() throws IOException
+ {
+ Map<String, Object> jsonMap = new HashMap<String, Object>();
+ jsonMap.put("_id", UUID.randomUUID().toString());
+
+ String jsonString = objectMapper.writeValueAsString(jsonMap);
+
+ ReplicatorDocument document = objectMapper.readValue(jsonString, ReplicatorDocument.class);
+ assertThat(document.getUnknownFields(), notNullValue());
+ assertThat(document.getUnknownFields().isEmpty(), is(true));
+ }
+
+ @Test
+ public void getUnknownFields_should_return_fields_which_were_not_mapped() throws IOException
+ {
+ Map<String, Object> jsonMap = new HashMap<String, Object>();
+ String unknownField = UUID.randomUUID().toString();
+ jsonMap.put(unknownField, UUID.randomUUID().toString());
+
+ String jsonString = objectMapper.writeValueAsString(jsonMap);
+
+ ReplicatorDocument document = objectMapper.readValue(jsonString, ReplicatorDocument.class);
+ assertThat(document.getUnknownFields(), notNullValue());
+ assertThat(document.getUnknownFields().containsKey(unknownField), is(true));
+ assertThat(document.getUnknownFields().get(unknownField), is(jsonMap.get(unknownField)));
+ }
+}
View
53 org.ektorp/src/test/java/org/ektorp/ReplicatorDocument_UserContextTest.java
@@ -0,0 +1,53 @@
+package org.ektorp;
+
+import org.codehaus.jackson.map.ObjectMapper;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
+
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsNull.notNullValue;
+import static org.junit.Assert.assertThat;
+
+public class ReplicatorDocument_UserContextTest
+{
+ private ObjectMapper objectMapper;
+
+ @Before
+ public void setUp()
+ {
+ objectMapper = new ObjectMapper();
+ }
+
+ @Test
+ public void getUnknownFields_should_return_an_empty_map_if_there_were_no_fields_which_were_not_mapped() throws IOException
+ {
+ Map<String, Object> jsonMap = new HashMap<String, Object>();
+ jsonMap.put("name", UUID.randomUUID().toString());
+
+ String jsonString = objectMapper.writeValueAsString(jsonMap);
+
+ ReplicatorDocument.UserContext document = objectMapper.readValue(jsonString, ReplicatorDocument.UserContext.class);
+ assertThat(document.getUnknownFields(), notNullValue());
+ assertThat(document.getUnknownFields().isEmpty(), is(true));
+ }
+
+ @Test
+ public void getUnknownFields_should_return_fields_which_were_not_mapped() throws IOException
+ {
+ Map<String, Object> jsonMap = new HashMap<String, Object>();
+ String unknownField = UUID.randomUUID().toString();
+ jsonMap.put(unknownField, UUID.randomUUID().toString());
+
+ String jsonString = objectMapper.writeValueAsString(jsonMap);
+
+ ReplicatorDocument.UserContext document = objectMapper.readValue(jsonString, ReplicatorDocument.UserContext.class);
+ assertThat(document.getUnknownFields(), notNullValue());
+ assertThat(document.getUnknownFields().containsKey(unknownField), is(true));
+ assertThat(document.getUnknownFields().get(unknownField), is(jsonMap.get(unknownField)));
+ }
+}
Something went wrong with that request. Please try again.