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

Commit 107c28d

Browse files
authored
Type of sinceSeq can be also a String besides an Integer (#527)
1 parent 2c53b88 commit 107c28d

9 files changed

Lines changed: 133 additions & 19 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Unreleased
22
- [DEPRECATED] This library is now deprecated and will be EOL on Dec 31 2021.
3+
- [FIXED] Type of `sinceSeq` can be also a `String` besides an `Integer`.
34

45
# 2.19.2 (2021-04-07)
56
- [NEW] Add migration guide to the newly supported cloudant-java-sdk (coordinates: com.ibm.cloud:cloudant).

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

Lines changed: 15 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, 2021 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
@@ -17,6 +17,7 @@
1717
import com.cloudant.client.org.lightcouch.ReplicationResult;
1818
import com.cloudant.client.org.lightcouch.ReplicationResult.ReplicationHistory;
1919
import com.cloudant.client.org.lightcouch.Replicator;
20+
import com.google.gson.JsonParser;
2021

2122
import java.util.Map;
2223

@@ -172,13 +173,24 @@ public Replication createTarget(Boolean createTarget) {
172173
}
173174

174175
/**
175-
* Starts a replication since an update sequence.
176+
* Create or modify a replication since an update sequence using a replication document.
176177
*
177178
* @param sinceSeq sequence number
178179
* @return this Replication instance to set more options or trigger the replication
179180
*/
180181
public Replication sinceSeq(Integer sinceSeq) {
181-
this.replication = replication.sinceSeq(sinceSeq);
182+
this.replication = replication.sinceSeq(new JsonParser().parse(sinceSeq.toString()));
183+
return this;
184+
}
185+
186+
/**
187+
* Create or modify a replication since an update sequence using a replication document.
188+
*
189+
* @param sinceSeq sequence string
190+
* @return this Replication instance to set more options or trigger the replication
191+
*/
192+
public Replication sinceSeq(String sinceSeq) {
193+
this.replication = replication.sinceSeq(new JsonParser().parse(sinceSeq));
182194
return this;
183195
}
184196

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2019 IBM Corp. All rights reserved.
2+
* Copyright (c) 2015, 2021 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
@@ -18,6 +18,7 @@
1818
import com.cloudant.client.org.lightcouch.Replication;
1919
import com.cloudant.client.org.lightcouch.ReplicatorDocument;
2020
import com.cloudant.client.org.lightcouch.Response;
21+
import com.google.gson.JsonParser;
2122

2223
import java.util.ArrayList;
2324
import java.util.List;
@@ -221,8 +222,25 @@ public Replicator userCtxRoles(String... userCtxRoles) {
221222
return this;
222223
}
223224

225+
/**
226+
* Create a transient replication since an update sequence.
227+
*
228+
* @param sinceSeq sequence number
229+
* @return this Replicator instance to set more options or trigger the replication
230+
*/
224231
public Replicator sinceSeq(Integer sinceSeq) {
225-
this.replicator = replicator.sinceSeq(sinceSeq);
232+
this.replicator = replicator.sinceSeq(new JsonParser().parse(sinceSeq.toString()));
233+
return this;
234+
}
235+
236+
/**
237+
* Create a transient replication since an update sequence.
238+
*
239+
* @param sinceSeq sequence string
240+
* @return this Replicator instance to set more options or trigger the replication
241+
*/
242+
public Replicator sinceSeq(String sinceSeq) {
243+
this.replicator = replicator.sinceSeq(new JsonParser().parse(sinceSeq));
226244
return this;
227245
}
228246

cloudant-client/src/main/java/com/cloudant/client/api/model/ReplicatorDocument.java

Lines changed: 13 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, 2021 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
@@ -16,7 +16,9 @@
1616

1717
import com.cloudant.client.org.lightcouch.Attachment;
1818
import com.cloudant.client.org.lightcouch.Replicator;
19+
import com.google.gson.JsonElement;
1920
import com.google.gson.JsonObject;
21+
import com.google.gson.JsonParser;
2022

2123
import java.util.HashMap;
2224
import java.util.Map;
@@ -228,11 +230,19 @@ public void setRetriesPerRequest(Integer retriesPerRequest) {
228230
}
229231

230232
public Integer getSinceSeq() {
231-
return replicatorDocument.getSinceSeq();
233+
return replicatorDocument.getSinceSeq().getAsInt();
234+
}
235+
236+
public String getSinceSeqString() {
237+
return replicatorDocument.getSinceSeq().getAsString();
232238
}
233239

234240
public void setSinceSeq(Integer sinceSeq) {
235-
replicatorDocument.setSinceSeq(sinceSeq);
241+
replicatorDocument.setSinceSeq(new JsonParser().parse(sinceSeq.toString()));
242+
}
243+
244+
public void setSinceSeq(String sinceSeq) {
245+
replicatorDocument.setSinceSeq(new JsonParser().parse(sinceSeq));
236246
}
237247

238248
public void setSourceIamApiKey(String iamApiKey) {

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

Lines changed: 4 additions & 3 deletions
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, 2021 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

2121
import com.cloudant.client.internal.DatabaseURIHelper;
22+
import com.google.gson.JsonElement;
2223
import com.google.gson.JsonObject;
2324

2425
import java.io.InputStream;
@@ -77,7 +78,7 @@ public class Replication {
7778
private String[] docIds;
7879
private String proxy;
7980
private Boolean createTarget;
80-
private Integer sinceSeq;
81+
private JsonElement sinceSeq;
8182

8283
// IAM
8384
private String sourceIamApiKey;
@@ -177,7 +178,7 @@ public Replication createTarget(Boolean createTarget) {
177178
/**
178179
* Starts a replication since an update sequence.
179180
*/
180-
public Replication sinceSeq(Integer sinceSeq) {
181+
public Replication sinceSeq(JsonElement sinceSeq) {
181182
this.sinceSeq = sinceSeq;
182183
return this;
183184
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright (C) 2011 lightcouch.org
3-
* Copyright (c) 2015, 2019 IBM Corp. All rights reserved.
3+
* Copyright (c) 2015, 2021 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
@@ -274,7 +274,7 @@ public Replicator userCtxRoles(String... userCtxRoles) {
274274
return this;
275275
}
276276

277-
public Replicator sinceSeq(Integer sinceSeq) {
277+
public Replicator sinceSeq(JsonElement sinceSeq) {
278278
replicatorDoc.setSinceSeq(sinceSeq);
279279
return this;
280280
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright (C) 2011 lightcouch.org
3-
* Copyright (c) 2015, 2019 IBM Corp. All rights reserved.
3+
* Copyright (c) 2015, 2021 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
@@ -68,7 +68,7 @@ public class ReplicatorDocument extends Document {
6868
@SerializedName("user_ctx")
6969
private UserCtx userCtx;
7070
@SerializedName("since_seq")
71-
private Integer sinceSeq;
71+
private JsonElement sinceSeq;
7272
private JsonElement selector;
7373

7474
public String getSource() {
@@ -268,11 +268,11 @@ public void setRetriesPerRequest(Integer retriesPerRequest) {
268268
this.retriesPerRequest = retriesPerRequest;
269269
}
270270

271-
public Integer getSinceSeq() {
271+
public JsonElement getSinceSeq() {
272272
return sinceSeq;
273273
}
274274

275-
public void setSinceSeq(Integer sinceSeq) {
275+
public void setSinceSeq(JsonElement sinceSeq) {
276276
this.sinceSeq = sinceSeq;
277277
}
278278

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright (C) 2011 lightcouch.org
3-
* Copyright © 2015, 2019 IBM Corp. All rights reserved.
3+
* Copyright © 2015, 2021 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
@@ -106,4 +106,39 @@ public void replication_conflict() throws Exception {
106106
//a conflict
107107
assertConflictsNotZero(db2);
108108
}
109+
110+
@Test
111+
public void replication_since_seq() throws Exception {
112+
String seq = db1.changes().getChanges().getResults().get(2).getSeq();
113+
ReplicationResult result = db1Resource.appendReplicationAuth(account.replication()
114+
.createTarget(true)
115+
.source(db1URI)
116+
.target(db2URI)
117+
.sinceSeq(seq)
118+
)
119+
.trigger();
120+
121+
assertTrue(result.isOk(), "The replication should complete ok");
122+
123+
List<ReplicationHistory> histories = result.getHistories();
124+
assertThat(histories.size(), not(0));
125+
}
126+
127+
@Test
128+
public void replication_last_seq() throws Exception {
129+
String lastSeq = db1Resource.get().changes().getChanges().getLastSeq();
130+
131+
ReplicationResult result = db1Resource.appendReplicationAuth(account.replication()
132+
.createTarget(true)
133+
.source(db1URI)
134+
.target(db2URI)
135+
.sinceSeq(lastSeq)
136+
)
137+
.trigger();
138+
139+
assertTrue(result.isOk(), "The replication should complete ok");
140+
141+
List<ReplicationHistory> histories = result.getHistories();
142+
assertThat(histories.size(), not(0));
143+
}
109144
}

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2015, 2019 IBM Corp. All rights reserved.
2+
* Copyright © 2015, 2021 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
@@ -131,4 +131,41 @@ public void replication_conflict() throws Exception {
131131
//a conflict
132132
assertConflictsNotZero(db2);
133133
}
134+
135+
@Test
136+
public void replicator_since_seq() throws Exception {
137+
String seq = db1.changes().getChanges().getResults().get(2).getSeq();
138+
Response response = db1Resource.appendReplicatorAuth(account.replicator()
139+
.replicatorDocId(repDocId)
140+
.createTarget(true)
141+
.source(db1URI)
142+
.target(db2URI)
143+
.sinceSeq(seq)
144+
)
145+
.save();
146+
147+
// find and remove replicator doc
148+
String state = Utils.waitForReplicatorToComplete(account, response.getId());
149+
assertTrue("completed".equalsIgnoreCase(state), "The replicator " +
150+
"should reach completed state");
151+
}
152+
153+
@Test
154+
public void replicator_last_seq() throws Exception {
155+
String lastSeq = db1Resource.get().changes().getChanges().getLastSeq();
156+
157+
Response response = db1Resource.appendReplicatorAuth(account.replicator()
158+
.replicatorDocId(repDocId)
159+
.createTarget(true)
160+
.source(db1URI)
161+
.target(db2URI)
162+
.sinceSeq(lastSeq)
163+
)
164+
.save();
165+
166+
// find and remove replicator doc
167+
String state = Utils.waitForReplicatorToComplete(account, response.getId());
168+
assertTrue("completed".equalsIgnoreCase(state), "The replicator " +
169+
"should reach completed state");
170+
}
134171
}

0 commit comments

Comments
 (0)