Skip to content

Commit 5703e22

Browse files
authored
DOC-7519 -- Pending Doc Ids not documented (#408)
https://issues.couchbase.com/browse/DOC-7519 merged prior commits ... DOC-7519 -- JAVA Snippets https://issues.couchbase.com/browse/DOC-7519 Pending Doc Ids not documented DOC-7519 -- iOS Snippets https://issues.couchbase.com/browse/DOC-7519 Pending Doc Ids not documented DOC-7519 -- C# Snippets https://issues.couchbase.com/browse/DOC-7519 Pending Doc Ids not documented DOC-7519 -- Pending Doc Ids not documented https://issues.couchbase.com/browse/DOC-7519 DOC-7519 -- iOS Snippets (objc) Simplify iteration look at ln 875 https://issues.couchbase.com/browse/DOC-7519 Revert "DOC-7519 -- iOS Snippets (objc)" This reverts commit d9ee906. DOC-7519 -- iOS Snippets (objc) https://issues.couchbase.com/browse/DOC-7519 Simplify iteration look at ln 875 DOC-7519 - Apply changes to Java snippets https://issues.couchbase.com/browse/DOC-7519
1 parent c10b09e commit 5703e22

File tree

11 files changed

+353
-19
lines changed

11 files changed

+353
-19
lines changed

modules/ROOT/pages/_partials/commons/common-sgw-replication.adoc

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ include::{snippet}[tag=replication-reset-checkpoint,indent=0]
236236
== Monitoring
237237

238238
In this section::
239-
<<lbl-repl-chng>> | <<lbl-repl-status>> | <<lbl-repl-evnts>>
239+
<<lbl-repl-chng>> | <<lbl-repl-status>> | <<lbl-repl-evnts>> | <<lbl-repl-pend>>
240240

241241
You can monitor a replication’s status by using a combination of <<lbl-repl-chng>> and the `replication.status.activity` property -- see; {url-api-enum-replicator-activity}.
242242
This enables you to know, for example, when the replication is actively transferring data and when it has stopped.
@@ -319,6 +319,35 @@ include::{snippet}[tag=remove-document-replication-listener,indent=0]
319319
When access to a document is removed on Sync Gateway (see: Sync Gateway's xref:sync-gateway::sync-function-api.adoc[Sync Function]), the document replication listener sends a notification with the `AccessRemoved` flag set to `true` and subsequently purges the document from the database.
320320
--
321321

322+
[#lbl-repl-pend]
323+
=== Documents Pending Push
324+
325+
TIP: {url-api-method-replicator-isDocumentPending} is quicker and more efficient.
326+
Use it in preference to returning a list of pending document IDs, where possible.
327+
328+
You can check whether documents are waiting to be pushed in any forthcoming sync by using either of the following API methods:
329+
330+
* Use the {url-api-method-replicator-getPendingDocumentIds} method, which returns a list of document IDs that have local changes, but which have not yet been pushed to the server.
331+
+
332+
This can be very useful in tracking the progress of a push sync, enabling the app to provide a visual indicator to the end user on its status, or decide when it is safe to exit.
333+
334+
* Use the {url-api-method-replicator-isDocumentPending} method to quickly check whether an individual document is pending a push.
335+
336+
[#ex-pending]
337+
.Use Pending Document ID API
338+
====
339+
340+
[source, {source-language}]
341+
----
342+
include::{snippet}[tags=replication-pendingdocuments, indent=0]
343+
344+
----
345+
<.> {url-api-method-replicator-getPendingDocumentIds} returns a list of the document IDs for all documents waiting to be pushed.
346+
This is a snapshot and may have changed by the time the response is received and processed.
347+
348+
<.> {url-api-method-replicator-isDocumentPending} returns `true` if the document is waiting to be pushed, and `false` otherwise.
349+
350+
====
322351

323352
[#lbl-repl-stop]
324353
== Stop Sync

modules/android/examples/java-android/app/src/main/java/com/couchbase/code_snippets/Examples.java

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -894,10 +894,91 @@ public void testReplicationStatus() throws URISyntaxException {
894894
if (change.getStatus().getActivityLevel() == Replicator.ActivityLevel.STOPPED) {
895895
Log.i(TAG, "Replication stopped");
896896
}
897-
});
898-
// end::replication-status[]
897+
});
898+
// end::replication-status[]
899+
}
900+
901+
// BEGIN PendingDocuments BM -- 19/Feb/21 --
902+
import android.support.annotation.NonNull;
903+
import android.util.Log;
904+
905+
import java.net.URI;
906+
import java.net.URISyntaxException;
907+
import java.util.Iterator;
908+
import java.util.Set;
909+
910+
import com.couchbase.lite.CouchbaseLiteException;
911+
import com.couchbase.lite.Database;
912+
import com.couchbase.lite.Endpoint;
913+
import com.couchbase.lite.Replicator;
914+
import com.couchbase.lite.ReplicatorConfiguration;
915+
import com.couchbase.lite.URLEndpoint;
916+
917+
class PendingDocsExample {
918+
private static final String TAG = "SCRATCH";
919+
920+
private Database database;
921+
private Replicator replicator;
922+
923+
// BEGIN PendingDocuments IB -- 11/Feb/21 --
924+
public void testReplicationPendingDocs() throws URISyntaxException, CouchbaseLiteException {
925+
// tag::replication-pendingdocuments[]
926+
// ... include other code as required
927+
//
928+
final Endpoint endpoint =
929+
new URLEndpoint(new URI("ws://localhost:4984/db"));
930+
931+
final ReplicatorConfiguration config =
932+
new ReplicatorConfiguration(database, endpoint)
933+
.setReplicatorType(ReplicatorConfiguration.ReplicatorType.PUSH);
934+
// tag::replication-push-pendingdocumentids[]
935+
936+
replicator = new Replicator(config);
937+
final Set<String> pendingDocs =
938+
replicator.getPendingDocumentIds(); // <.>
939+
940+
// end::replication-push-pendingdocumentids[]
941+
942+
replicator.addChangeListener(change -> {
943+
onStatusChanged(pendingDocs, change.getStatus()); });
944+
945+
replicator.start();
946+
947+
// ... include other code as required
948+
// end::replication-pendingdocuments[]
949+
}
950+
//
951+
// tag::replication-pendingdocuments[]
952+
//
953+
private void onStatusChanged(
954+
@NonNull final Set<String> pendingDocs,
955+
@NonNull final Replicator.Status status) {
956+
// ... sample onStatusChanged function
957+
//
958+
Log.i(TAG,
959+
"Replicator activity level is " + status.getActivityLevel().toString());
960+
961+
// iterate and report-on previously
962+
// retrieved pending docids 'list'
963+
for (Iterator<String> itr = pendingDocs.iterator(); itr.hasNext(); ) {
964+
final String docId = itr.next();
965+
try {
966+
// tag::replication-push-isdocumentpending[]
967+
if (!replicator.isDocumentPending(docId)) { continue; } // <.>
968+
// end::replication-push-isdocumentpending[]
969+
970+
itr.remove();
971+
Log.i(TAG, "Doc ID " + docId + " has been pushed");
972+
}
973+
catch (CouchbaseLiteException e) {
974+
Log.w(TAG, "isDocumentPending failed", e); }
975+
}
976+
}
977+
// end::replication-pendingdocuments[]
978+
// END PendingDocuments BM -- 19/Feb/21 --
899979
}
900980

981+
901982
public void testHandlingNetworkErrors() throws URISyntaxException {
902983
Endpoint endpoint = new URLEndpoint(new URI("ws://localhost:4984/db"));
903984
ReplicatorConfiguration config = new ReplicatorConfiguration(database, endpoint);

modules/android/pages/_partials/_attributes-module.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@
217217

218218
:url-api-method-replicator-getPendingDocumentIds: {url-api-properties-replicator-abs}getPendingDocumentIds--[Replicator.getPendingDocumentIds()]
219219

220-
:url-api-method-replicator-isDocumentPending: {url-api-properties-replicator-abs}isDocumentPending--[Replicator.isDocumentPending()]
220+
:url-api-method-replicator-isDocumentPending: {url-api-properties-replicator-abs}isDocumentPending-java.lang.String-[Replicator.isDocumentPending()]
221221

222222
:url-api-method-replicator-start: {url-api-properties-replicator-abs}start-boolean-[start()]
223223

modules/csharp/examples/code_snippets/Program.cs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,55 @@ private static void SetupReplicatorListener()
10741074
// end::replication-status[]
10751075
}
10761076

1077+
1078+
1079+
1080+
// BEGIN PendingDocuments IB -- 11/Feb/21 --
1081+
private static void ReplicatorPendingDocuments()
1082+
{
1083+
// tag::replication-pendingdocuments[]
1084+
var url = new Uri("ws://localhost:4984/mydatabase");
1085+
var target = new URLEndpoint(url);
1086+
var database = new Database("myDB");
1087+
var config = new ReplicatorConfiguration(database, target);
1088+
config.ReplicatorType = ReplicatorType.Push;
1089+
1090+
// tag::replication-push-pendingdocumentids[]
1091+
var replicator = new Replicator(config);
1092+
1093+
var mydocids =
1094+
new HashSet <string> (replicator.GetPendingDocumentIDs()); // <.>
1095+
1096+
// end::replication-push-pendingdocumentids[]
1097+
1098+
if (mydocids.Count > 0)
1099+
{
1100+
Console.WriteLine($"There are {mydocids.Count} documents pending");
1101+
replicator.AddChangeListener((sender, change) =>
1102+
{
1103+
Console.WriteLine($"Replicator activity level is " +
1104+
change.Status.Activity.ToString());
1105+
// iterate and report-on previously
1106+
// retrieved pending docids 'list'
1107+
foreach (var thisId in mydocids)
1108+
// tag::replication-push-isdocumentpending[]
1109+
if (!replicator.IsDocumentPending(thisId)) // <.>
1110+
{
1111+
Console.WriteLine($"Doc ID {thisId} now pushed");
1112+
};
1113+
// end::replication-push-isdocumentpending[]
1114+
});
1115+
1116+
replicator.Start();
1117+
}
1118+
// end::replication-pendingdocuments[]
1119+
}
1120+
1121+
1122+
// END PendingDocuments IB -- 11/Feb/21 --
1123+
1124+
1125+
10771126
private static void ReplicatorDocumentEvent()
10781127
{
10791128
var replicator = _Replicator;
@@ -1572,4 +1621,3 @@ public Document Resolve(Conflict conflict)
15721621
}
15731622
}
15741623
// end::merge-conflict-resolver[]
1575-
}

modules/csharp/pages/_partials/_attributes-module.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,9 @@
227227

228228
:url-api-method-replicator-rmv-change-listener: {url-api-properties-replicator}RemoveChangeListener_Couchbase_Lite_ListenerToken_[RemoveChangeListener(ListenerToken)]
229229

230-
:url-api-method-replicator-getPendingDocumentIds: {url-api-properties-replicator}GetPendingDocumentIDs[Replicator.GetPendingDocumentIds()]
230+
:url-api-method-replicator-getPendingDocumentIds: {url-api-properties-replicator}GetPendingDocumentIDs[Replicator.GetPendingDocumentIDs()]
231231

232-
:url-api-method-replicator-isDocumentPending: {url-api-properties-replicator}IsDocumentPending--[Replicator.IsDocumentPending()]
232+
:url-api-method-replicator-isDocumentPending: {url-api-properties-replicator}IsDocumentPending_System_String_[Replicator.IsDocumentPending()]
233233

234234
:url-api-method-replicator-start: {url-api-properties-replicator}Start[Start()]
235235

modules/java/examples/code_snippets/Examples.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,6 +1389,89 @@ public void testReplicationStatus() throws URISyntaxException {
13891389
// end::replication-status[]
13901390
}
13911391

1392+
// BEGIN PendingDocuments BM -- 19/Feb/21 --
1393+
import android.support.annotation.NonNull;
1394+
import android.util.Log;
1395+
1396+
import java.net.URI;
1397+
import java.net.URISyntaxException;
1398+
import java.util.Iterator;
1399+
import java.util.Set;
1400+
1401+
import com.couchbase.lite.CouchbaseLiteException;
1402+
import com.couchbase.lite.Database;
1403+
import com.couchbase.lite.Endpoint;
1404+
import com.couchbase.lite.Replicator;
1405+
import com.couchbase.lite.ReplicatorConfiguration;
1406+
import com.couchbase.lite.URLEndpoint;
1407+
1408+
class PendingDocsExample {
1409+
private static final String TAG = "SCRATCH";
1410+
1411+
private Database database;
1412+
private Replicator replicator;
1413+
1414+
// BEGIN PendingDocuments IB -- 11/Feb/21 --
1415+
public void testReplicationPendingDocs() throws URISyntaxException, CouchbaseLiteException {
1416+
// tag::replication-pendingdocuments[]
1417+
// ... include other code as required
1418+
//
1419+
final Endpoint endpoint =
1420+
new URLEndpoint(new URI("ws://localhost:4984/db"));
1421+
1422+
final ReplicatorConfiguration config =
1423+
new ReplicatorConfiguration(database, endpoint)
1424+
.setReplicatorType(ReplicatorConfiguration.ReplicatorType.PUSH);
1425+
// tag::replication-push-pendingdocumentids[]
1426+
1427+
replicator = new Replicator(config);
1428+
final Set<String> pendingDocs =
1429+
replicator.getPendingDocumentIds(); // <.>
1430+
1431+
// end::replication-push-pendingdocumentids[]
1432+
1433+
replicator.addChangeListener(change -> {
1434+
onStatusChanged(pendingDocs, change.getStatus()); });
1435+
1436+
replicator.start();
1437+
1438+
// ... include other code as required
1439+
// end::replication-pendingdocuments[]
1440+
}
1441+
//
1442+
// tag::replication-pendingdocuments[]
1443+
//
1444+
private void onStatusChanged(
1445+
@NonNull final Set<String> pendingDocs,
1446+
@NonNull final Replicator.Status status) {
1447+
// ... sample onStatusChanged function
1448+
//
1449+
Log.i(TAG,
1450+
"Replicator activity level is " + status.getActivityLevel().toString());
1451+
1452+
// iterate and report-on previously
1453+
// retrieved pending docids 'list'
1454+
for (Iterator<String> itr = pendingDocs.iterator(); itr.hasNext(); ) {
1455+
final String docId = itr.next();
1456+
try {
1457+
// tag::replication-push-isdocumentpending[]
1458+
if (!replicator.isDocumentPending(docId)) { continue; } // <.>
1459+
// end::replication-push-isdocumentpending[]
1460+
1461+
itr.remove();
1462+
Log.i(TAG, "Doc ID " + docId + " has been pushed");
1463+
}
1464+
catch (CouchbaseLiteException e) {
1465+
Log.w(TAG, "isDocumentPending failed", e); }
1466+
}
1467+
}
1468+
// end::replication-pendingdocuments[]
1469+
// END PendingDocuments BM -- 19/Feb/21 --
1470+
}
1471+
1472+
1473+
1474+
13921475
public void testHandlingNetworkErrors() throws URISyntaxException {
13931476
Endpoint endpoint = new URLEndpoint(new URI("ws://localhost:4984/db"));
13941477
ReplicatorConfiguration config = new ReplicatorConfiguration(database, endpoint);

modules/java/pages/_partials/_attributes-module.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,9 @@
213213

214214
:url-api-method-replicator-stop: {url-api-properties-replicator-abs}stop--[stop()]
215215

216-
:url-api-method-replicator-getPendingDocumentIds: {url-api-properties-replicator-abs}getPendingDocumentIds--[Replicator.getPendingDocuments()]
216+
:url-api-method-replicator-getPendingDocumentIds: {url-api-properties-replicator-abs}getPendingDocumentIds--[Replicator.getPendingDocumentIds()]
217217

218-
:url-api-method-replicator-isDocumentPending: {url-api-properties-replicator-abs}isDocumentPending--[Replicator.isDocumentPending]
218+
:url-api-method-replicator-isDocumentPending: {url-api-properties-replicator-abs}isDocumentPending-java.lang.String-[Replicator.isDocumentPending]
219219

220220
:url-api-property-replicator-status: {url-api-properties-replicator-abs}getStatus--[replicator.getStatus]
221221

modules/objc/examples/code_snippets/SampleCodeTest.m

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,62 @@ - (void) dontTestReplicatorStatus {
836836
// end::replication-status[]
837837
}
838838

839+
840+
// BEGIN PendingDocuments IB -- 11/Feb/21 --
841+
// public void testReplicationPendingDocs() throws URISyntaxException {
842+
// tag::replication-pendingdocuments[]
843+
844+
CBLDatabase *database = self.db;
845+
NSURL *url = [NSURL URLWithString:@"ws://localhost:4984/db"];
846+
CBLURLEndpoint *target =
847+
[[CBLURLEndpoint alloc] initWithURL: url];
848+
CBLReplicatorConfiguration *config =
849+
[[CBLReplicatorConfiguration alloc]
850+
initWithDatabase:database
851+
target:target];
852+
853+
config.replicatorType = kCBLReplicatorTypePush;
854+
855+
// tag::replication-push-pendingdocumentids[]
856+
CBLReplicator *replicator =
857+
[[CBLReplicator alloc] initWithConfig:config];
858+
859+
// Get list of pending doc IDs
860+
NSError* err = nil;
861+
NSSet *mydocids =
862+
[NSSet setWithSet:[replicator pendingDocumentIDs:&err]]; // <.>
863+
864+
// end::replication-push-pendingdocumentids[]
865+
866+
if ([mydocids count] > 0) {
867+
868+
NSLog(@"There are %lu documents pending", (unsigned long)[mydocids count]);
869+
870+
[replicator addChangeListener:^(CBLReplicatorChange *change) {
871+
872+
NSLog(@"Replicator activity level is %u", change.status.activity);
873+
// iterate and report-on the pending doc IDs in 'mydocids'
874+
for (thisid in mydocids) {
875+
876+
// tag::replication-push-isdocumentpending[]
877+
NSError* err = nil;
878+
if (![replicator isDocumentPending: thisid error: &err]) { // <.>
879+
NSLog(@"Doc ID %@ now pushed", thisid);
880+
}
881+
// end::replication-push-isdocumentpending[]
882+
}
883+
884+
}];
885+
[replicator start];
886+
887+
};
888+
889+
// end::replication-pendingdocuments[]
890+
}
891+
// END PendingDocuments IB -- 11/Feb/21 --
892+
893+
894+
839895
- (void) dontTestReplicatorDocumentEvent {
840896
CBLDatabase *database = self.db;
841897
NSURL *url = [NSURL URLWithString:@"ws://localhost:4984/db"];

0 commit comments

Comments
 (0)