Skip to content

Commit 8cbbecc

Browse files
authored
CBL-7981: Add correlation ID API (#473)
1 parent c10668d commit 8cbbecc

6 files changed

Lines changed: 52 additions & 0 deletions

File tree

common/main/cpp/com_couchbase_lite_internal_core_impl_NativeC4Replicator.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ JNIEXPORT jobject
8181
JNICALL Java_com_couchbase_lite_internal_core_impl_NativeC4Replicator_getStatus
8282
(JNIEnv * , jclass, jlong);
8383

84+
/*
85+
* Class: com_couchbase_lite_internal_core_impl_NativeC4Replicator
86+
* Method: getCorrelationId
87+
* Signature: (J)Ljava/lang/String;
88+
*/
89+
JNIEXPORT jstring JNICALL
90+
Java_com_couchbase_lite_internal_core_impl_NativeC4Replicator_getCorrelationId(
91+
JNIEnv *, jclass, jlong);
92+
8493
/*
8594
* Class: com_couchbase_lite_internal_core_impl_NativeC4Replicator
8695
* Method: getPendingDocIds

common/main/cpp/native_c4replicator.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "native_glue.hh"
2222
#include "native_c4replutils.hh"
2323
#include "socket_factory.h"
24+
#include "fleece/FLSlice.h"
2425
#include "com_couchbase_lite_internal_core_impl_NativeC4Replicator.h"
2526

2627
using namespace litecore;
@@ -604,6 +605,19 @@ Java_com_couchbase_lite_internal_core_impl_NativeC4Replicator_getStatus(
604605
return toJavaReplStatus(env, status);
605606
}
606607

608+
JNIEXPORT jstring JNICALL
609+
Java_com_couchbase_lite_internal_core_impl_NativeC4Replicator_getCorrelationId(
610+
JNIEnv *env,
611+
jclass ignored,
612+
jlong repl) {
613+
auto result = c4repl_getCorrelationID((C4Replicator *)repl);
614+
auto retVal = result
615+
? UTF8ToJstring(env, (const char *)result.buf, result.size)
616+
: nullptr;
617+
FLSliceResult_Release(result);
618+
return retVal;
619+
}
620+
607621
/*
608622
* Class: com_couchbase_lite_internal_core_impl_NativeC4Replicator
609623
* Method: getPendingDocIDs

common/main/java/com/couchbase/lite/AbstractReplicator.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,22 @@ public void stop() {
238238
@NonNull
239239
public ReplicatorConfiguration getConfig() { return new ReplicatorConfiguration(config); }
240240

241+
/**
242+
* Gets the correlation ID of this replication so that it can be analyzed along
243+
* with the logs from the remote side. If not started, or unavailable, returns
244+
* null
245+
*
246+
* @return The correlation ID of the replicator
247+
*/
248+
@Nullable
249+
public String getCorrelationId() {
250+
try { return getOrCreateC4Replicator().getCorrelationId(); }
251+
catch (LiteCoreException e) {
252+
Log.e(LOG_DOMAIN, "Failed getting correlation ID", e);
253+
return null;
254+
}
255+
}
256+
241257
/**
242258
* The replicator's current status: its activity level and progress.
243259
*

common/main/java/com/couchbase/lite/internal/core/C4Replicator.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ long nCreateWithSocket(
234234
void nSetOptions(long replPeer, @Nullable byte[] options);
235235
@NonNull
236236
C4ReplicatorStatus nGetStatus(long replPeer);
237+
@Nullable
238+
String nGetCorrelationId(long replPeer);
237239
@NonNull
238240
FLSliceResult nGetPendingDocIds(long replPeer, @NonNull String scope, @NonNull String collection)
239241
throws LiteCoreException;
@@ -713,6 +715,9 @@ public void setOptions(@Nullable byte[] options) {
713715
@Nullable
714716
public C4ReplicatorStatus getStatus() { return withPeerOrNull(impl::nGetStatus); }
715717

718+
@Nullable
719+
public String getCorrelationId() { return withPeerOrNull(impl::nGetCorrelationId); }
720+
716721
public boolean isDocumentPending(@NonNull String docId, @NonNull String scope, @NonNull String collection)
717722
throws LiteCoreException {
718723
return withPeerOrDefault(false, replPeer -> impl.nIsDocumentPending(replPeer, docId, scope, collection));

common/main/java/com/couchbase/lite/internal/core/impl/NativeC4Replicator.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ public long nCreateWithSocket(
118118
public C4ReplicatorStatus nGetStatus(long peer) { return getStatus(peer); }
119119

120120
// thread safe
121+
@Nullable
122+
@Override
123+
public String nGetCorrelationId(long peer) { return getCorrelationId(peer); }
124+
121125
@NonNull
122126
@Override
123127
public FLSliceResult nGetPendingDocIds(long peer, @NonNull String scope, @NonNull String collection)
@@ -230,6 +234,9 @@ private static native long createWithSocket(
230234
@NonNull
231235
private static native C4ReplicatorStatus getStatus(long peer);
232236

237+
@Nullable
238+
private static native String getCorrelationId(long peer);
239+
233240
/**
234241
* Returns a list of string ids for pending documents.
235242
* thread-safe

common/test/java/com/couchbase/lite/mock/ReplicatorMocks.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ open class MockNativeReplicator : C4Replicator.NativeImpl {
8686
override fun nStart(peer: Long, restart: Boolean) = Unit
8787
override fun nStop(peer: Long) = Unit
8888
override fun nSetOptions(peer: Long, options: ByteArray?) = Unit
89+
override fun nGetCorrelationId(peer: Long): String? = null
8990
override fun nGetPendingDocIds(peer: Long, scope: String, collection: String) = FLSliceResult.createTestSlice()
9091
override fun nIsDocumentPending(peer: Long, id: String, scope: String, collection: String) = true
9192
override fun nSetProgressLevel(peer: Long, progressLevel: Int) = Unit

0 commit comments

Comments
 (0)