Skip to content
This repository was archived by the owner on Mar 11, 2022. It is now read-only.

Commit f0ee90e

Browse files
authored
Merge pull request #479 from cloudant/477-replication-selector
Add replication selector support.
2 parents 30f5c4c + df77740 commit f0ee90e

File tree

6 files changed

+58
-10
lines changed

6 files changed

+58
-10
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# UNRELEASED
22
- [NEW] Added database partition metadata fields for partitioned index count/limit.
3+
- [NEW] Added replication selector support.
34

45
# 2.15.0 (2019-02-12)
56
- [NEW] Added option for client to authenticate with IAM token server.

cloudant-client/src/main/java/com/cloudant/client/api/Replicator.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015 IBM Corp. All rights reserved.
2+
* Copyright (c) 2015, 2019 IBM Corp. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
55
* except in compliance with the License. You may obtain a copy of the License at
@@ -14,6 +14,7 @@
1414

1515
package com.cloudant.client.api;
1616

17+
import com.cloudant.client.api.query.Selector;
1718
import com.cloudant.client.org.lightcouch.Replication;
1819
import com.cloudant.client.org.lightcouch.ReplicatorDocument;
1920
import com.cloudant.client.org.lightcouch.Response;
@@ -42,8 +43,9 @@
4243
* .target("target-db")
4344
* .continuous(true)
4445
* .createTarget(true)
45-
* .replicatorDB("replicator-db-name") // optional, defaults to _replicator
46-
* .replicatorDocId("doc-id") // optional, defaults to UUID
46+
* .replicatorDB("replicator-db-name") // optional, defaults to _replicator
47+
* .replicatorDocId("doc-id") // optional, defaults to UUID
48+
* .selector(eq("_id", "Schwarzenegger")) // optional replication selector
4749
* .save(); // trigger replication
4850
*
4951
* // find an existing replicator doc
@@ -239,4 +241,9 @@ public Replicator targetIamApiKey(String iamApiKey) {
239241
return this;
240242
}
241243

244+
public Replicator selector(Selector selector) {
245+
this.replicator = replicator.selector(selector);
246+
return this;
247+
}
248+
242249
}

cloudant-client/src/main/java/com/cloudant/client/api/query/JsonIndex.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public Builder designDocument(String designDocumentId) {
159159
* <p>
160160
* Obtain a selector from an {@link Operation} or {@link Expression}.
161161
* </p>
162-
* @param selector string representation of a JSON object describing criteria used to add
162+
* @param selector {@code Selector} object describing criteria used to add
163163
* documents to index
164164
* @return the builder for chaining
165165
* @see Selector

cloudant-client/src/main/java/com/cloudant/client/org/lightcouch/Replicator.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright (C) 2011 lightcouch.org
3-
* Copyright (c) 2015 IBM Corp. All rights reserved.
3+
* Copyright (c) 2015, 2019 IBM Corp. All rights reserved.
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
66
* except in compliance with the License. You may obtain a copy of the License at
@@ -19,6 +19,7 @@
1919
import static com.cloudant.client.org.lightcouch.internal.CouchDbUtil.close;
2020
import static com.cloudant.client.org.lightcouch.internal.CouchDbUtil.getAsString;
2121

22+
import com.cloudant.client.api.query.Selector;
2223
import com.cloudant.client.internal.DatabaseURIHelper;
2324
import com.cloudant.client.internal.URIBase;
2425
import com.cloudant.client.org.lightcouch.ReplicatorDocument.UserCtx;
@@ -287,4 +288,9 @@ public Replicator targetIamApiKey(String iamApiKey) {
287288
replicatorDoc.setTargetIamApiKey(iamApiKey);
288289
return this;
289290
}
291+
292+
public Replicator selector(Selector selector) {
293+
replicatorDoc.setSelector(selector);
294+
return this;
295+
}
290296
}

cloudant-client/src/main/java/com/cloudant/client/org/lightcouch/ReplicatorDocument.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright (C) 2011 lightcouch.org
3-
* Copyright (c) 2015 IBM Corp. All rights reserved.
3+
* Copyright (c) 2015, 2019 IBM Corp. All rights reserved.
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
66
* except in compliance with the License. You may obtain a copy of the License at
@@ -15,6 +15,8 @@
1515

1616
package com.cloudant.client.org.lightcouch;
1717

18+
import com.cloudant.client.api.query.Selector;
19+
import com.cloudant.client.internal.query.Helpers;
1820
import com.google.gson.JsonElement;
1921
import com.google.gson.JsonObject;
2022
import com.google.gson.JsonPrimitive;
@@ -67,6 +69,7 @@ public class ReplicatorDocument extends Document {
6769
private UserCtx userCtx;
6870
@SerializedName("since_seq")
6971
private Integer sinceSeq;
72+
private JsonElement selector;
7073

7174
public String getSource() {
7275
return getEndpointUrl(source);
@@ -289,6 +292,14 @@ public void setTargetIamApiKey(String iamApiKey) {
289292
target = getDestinationIamJson(this.getTarget(), iamApiKey);
290293
}
291294

295+
public String getSelector() {
296+
return selector.getAsString();
297+
}
298+
299+
public void setSelector(Selector selector) {
300+
this.selector = Helpers.getJsonObjectFromSelector(selector);
301+
}
302+
292303
public static class UserCtx {
293304
private String name;
294305
private String[] roles;

cloudant-client/src/test/java/com/cloudant/tests/ReplicatorMockTest.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2018 IBM Corp. All rights reserved.
2+
* Copyright © 2018, 2019 IBM Corp. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
55
* except in compliance with the License. You may obtain a copy of the License at
@@ -14,6 +14,7 @@
1414

1515
package com.cloudant.tests;
1616

17+
import static com.cloudant.client.api.query.Expression.eq;
1718
import static com.cloudant.tests.HttpTest.takeN;
1819
import static com.cloudant.tests.util.MockWebServerResources.IAM_API_KEY;
1920
import static com.cloudant.tests.util.MockWebServerResources.IAM_API_KEY_2;
@@ -24,15 +25,13 @@
2425

2526
import com.cloudant.client.api.CloudantClient;
2627
import com.cloudant.client.api.model.ReplicatorDocument;
28+
import com.cloudant.tests.base.TestWithMockedServer;
2729
import com.cloudant.tests.util.Utils;
2830

29-
import com.cloudant.tests.base.TestWithMockedServer;
3031
import org.junit.jupiter.api.Test;
3132

3233
import okhttp3.mockwebserver.RecordedRequest;
3334

34-
import java.nio.charset.Charset;
35-
3635
/**
3736
* Created by samsmith on 08/03/2018.
3837
*/
@@ -232,4 +231,28 @@ public void createAndSaveReplicatorDocumentWithSourceAndTargetIamAuth() throws E
232231
assertThat("The replication document should contain the target IAM API key",
233232
body, containsString(authJson + IAM_API_KEY_2));
234233
}
234+
235+
@Test
236+
public void createAndSaveReplicatorDocumentWithSimpleSelector() throws Exception {
237+
server.enqueue(JSON_OK);
238+
239+
CloudantClient c = CloudantClientHelper.newMockWebServerClientBuilder(server)
240+
.build();
241+
242+
c.replicator()
243+
.replicatorDocId(replicatorDocId)
244+
.source(sourceDbUrl)
245+
.target(targetDbUrl)
246+
.selector(eq("_id", "Schwarzenegger"))
247+
.save();
248+
249+
RecordedRequest[] requests = takeN(server, 1);
250+
251+
assertEquals(requests[0].getPath(), "/_replicator/" + replicatorDocId);
252+
253+
String body = requests[0].getBody().readUtf8();
254+
255+
assertThat("The replication document should contain the selector", body,
256+
containsString(",\"selector\":{\"_id\":{\"$eq\":\"Schwarzenegger\"}},"));
257+
}
235258
}

0 commit comments

Comments
 (0)