From 3afae3c87da2ef58612598187ac72153d874db3b Mon Sep 17 00:00:00 2001 From: Pasin Suriyentrakorn Date: Mon, 21 Oct 2024 16:56:40 -0700 Subject: [PATCH] CBL-6350 : Implement GetRevisionHistory API Implemented CBLDocument_GetRevisionHistory for end-to-end testing. --- src/CBLDocument.cc | 19 ++++++++++++ src/CBLDocument_CAPI.cc | 5 +++ src/CBLDocument_Internal.hh | 3 ++ src/CBLPrivate.h | 2 ++ src/exports/CBL_Exports.txt | 1 + src/exports/generated/CBL.def | 1 + src/exports/generated/CBL.exp | 1 + src/exports/generated/CBL.gnu | 1 + src/exports/generated/CBL_Android.gnu | 1 + src/exports/generated/CBL_EE.def | 1 + src/exports/generated/CBL_EE.exp | 1 + src/exports/generated/CBL_EE.gnu | 1 + src/exports/generated/CBL_EE_Android.gnu | 1 + test/DocumentTest.cc | 39 +++++++++++++++++++++++- 14 files changed, 76 insertions(+), 1 deletion(-) diff --git a/src/CBLDocument.cc b/src/CBLDocument.cc index 7c61033f..4b2f398a 100644 --- a/src/CBLDocument.cc +++ b/src/CBLDocument.cc @@ -72,6 +72,25 @@ CBLDatabase* _cbl_nullable CBLDocument::database() const { } +#pragma mark - REVISION HISTORY: + + +alloc_slice CBLDocument::getRevisionHistory() const { + if (!_collection) { + return fleece::nullslice; + } + + auto doc = _collection->getDocument(_docID, false, true); + if (!doc) { + return fleece::nullslice; + } + + auto c4doc = doc->_c4doc.useLocked(); + assert(c4doc); + return c4doc->getRevisionHistory(UINT_MAX, nullptr, 0); +} + + #pragma mark - SAVING: diff --git a/src/CBLDocument_CAPI.cc b/src/CBLDocument_CAPI.cc index c8dcb7cc..e702500e 100644 --- a/src/CBLDocument_CAPI.cc +++ b/src/CBLDocument_CAPI.cc @@ -51,6 +51,11 @@ unsigned CBLDocument_Generation(const CBLDocument* doc) noexcept { return doc->generation(); } +/** Private API */ +FLSliceResult CBLDocument_GetRevisionHistory(const CBLDocument* doc) noexcept { + return FLSliceResult(doc->getRevisionHistory()); +} + FLMutableDict CBLDocument_MutableProperties(CBLDocument* doc) noexcept { return doc->mutableProperties(); } diff --git a/src/CBLDocument_Internal.hh b/src/CBLDocument_Internal.hh index 104ff4a8..d5682f13 100644 --- a/src/CBLDocument_Internal.hh +++ b/src/CBLDocument_Internal.hh @@ -108,6 +108,9 @@ public: auto c4doc = _c4doc.useLocked(); return c4doc ? c4doc->selectedRev().flags : (kRevNew | kRevLeaf); } + + + alloc_slice getRevisionHistory() const; #pragma mark - Properties: diff --git a/src/CBLPrivate.h b/src/CBLPrivate.h index cf08f4c5..87e1f4b1 100644 --- a/src/CBLPrivate.h +++ b/src/CBLPrivate.h @@ -72,6 +72,8 @@ FLMutableArray CBLCollection_GetIndexesInfo(const CBLCollection* collection, FLSliceResult CBLDocument_CanonicalRevisionID(const CBLDocument* doc) CBLAPI; unsigned CBLDocument_Generation(const CBLDocument* doc) CBLAPI; + + FLSliceResult CBLDocument_GetRevisionHistory(const CBLDocument* doc) CBLAPI; FLSlice CBLReplicator_UserAgent(const CBLReplicator* repl) CBLAPI; diff --git a/src/exports/CBL_Exports.txt b/src/exports/CBL_Exports.txt index a24271ec..f81783e3 100644 --- a/src/exports/CBL_Exports.txt +++ b/src/exports/CBL_Exports.txt @@ -239,6 +239,7 @@ CBLDatabase_LastSequence CBLDocument_CanonicalRevisionID CBLDocument_Generation +CBLDocument_GetRevisionHistory CBLError_GetCaptureBacktraces CBLError_SetCaptureBacktraces diff --git a/src/exports/generated/CBL.def b/src/exports/generated/CBL.def index 35a30e2e..4bc4a851 100644 --- a/src/exports/generated/CBL.def +++ b/src/exports/generated/CBL.def @@ -175,6 +175,7 @@ CBLDatabase_DeleteDocumentByID CBLDatabase_LastSequence CBLDocument_CanonicalRevisionID CBLDocument_Generation +CBLDocument_GetRevisionHistory CBLError_GetCaptureBacktraces CBLError_SetCaptureBacktraces CBLQuery_SetListenerCallbackDelay diff --git a/src/exports/generated/CBL.exp b/src/exports/generated/CBL.exp index 408be345..bb7c88b6 100644 --- a/src/exports/generated/CBL.exp +++ b/src/exports/generated/CBL.exp @@ -183,6 +183,7 @@ _CBLDatabase_DeleteDocumentByID _CBLDatabase_LastSequence _CBLDocument_CanonicalRevisionID _CBLDocument_Generation +_CBLDocument_GetRevisionHistory _CBLError_GetCaptureBacktraces _CBLError_SetCaptureBacktraces _CBLQuery_SetListenerCallbackDelay diff --git a/src/exports/generated/CBL.gnu b/src/exports/generated/CBL.gnu index 61fd55b2..82319938 100644 --- a/src/exports/generated/CBL.gnu +++ b/src/exports/generated/CBL.gnu @@ -173,6 +173,7 @@ CBL_C { CBLDatabase_LastSequence; CBLDocument_CanonicalRevisionID; CBLDocument_Generation; + CBLDocument_GetRevisionHistory; CBLError_GetCaptureBacktraces; CBLError_SetCaptureBacktraces; CBLQuery_SetListenerCallbackDelay; diff --git a/src/exports/generated/CBL_Android.gnu b/src/exports/generated/CBL_Android.gnu index 13250d5f..9d60c026 100644 --- a/src/exports/generated/CBL_Android.gnu +++ b/src/exports/generated/CBL_Android.gnu @@ -174,6 +174,7 @@ CBL_C { CBLDatabase_LastSequence; CBLDocument_CanonicalRevisionID; CBLDocument_Generation; + CBLDocument_GetRevisionHistory; CBLError_GetCaptureBacktraces; CBLError_SetCaptureBacktraces; CBLQuery_SetListenerCallbackDelay; diff --git a/src/exports/generated/CBL_EE.def b/src/exports/generated/CBL_EE.def index ad0f0a6b..efdd5493 100644 --- a/src/exports/generated/CBL_EE.def +++ b/src/exports/generated/CBL_EE.def @@ -216,6 +216,7 @@ CBLDatabase_DeleteDocumentByID CBLDatabase_LastSequence CBLDocument_CanonicalRevisionID CBLDocument_Generation +CBLDocument_GetRevisionHistory CBLError_GetCaptureBacktraces CBLError_SetCaptureBacktraces CBLQuery_SetListenerCallbackDelay diff --git a/src/exports/generated/CBL_EE.exp b/src/exports/generated/CBL_EE.exp index b69c90d7..ae6f4f2e 100644 --- a/src/exports/generated/CBL_EE.exp +++ b/src/exports/generated/CBL_EE.exp @@ -224,6 +224,7 @@ _CBLDatabase_DeleteDocumentByID _CBLDatabase_LastSequence _CBLDocument_CanonicalRevisionID _CBLDocument_Generation +_CBLDocument_GetRevisionHistory _CBLError_GetCaptureBacktraces _CBLError_SetCaptureBacktraces _CBLQuery_SetListenerCallbackDelay diff --git a/src/exports/generated/CBL_EE.gnu b/src/exports/generated/CBL_EE.gnu index 911a541b..525dfb15 100644 --- a/src/exports/generated/CBL_EE.gnu +++ b/src/exports/generated/CBL_EE.gnu @@ -214,6 +214,7 @@ CBL_C { CBLDatabase_LastSequence; CBLDocument_CanonicalRevisionID; CBLDocument_Generation; + CBLDocument_GetRevisionHistory; CBLError_GetCaptureBacktraces; CBLError_SetCaptureBacktraces; CBLQuery_SetListenerCallbackDelay; diff --git a/src/exports/generated/CBL_EE_Android.gnu b/src/exports/generated/CBL_EE_Android.gnu index 56cc8d3a..6fa8bb4b 100644 --- a/src/exports/generated/CBL_EE_Android.gnu +++ b/src/exports/generated/CBL_EE_Android.gnu @@ -215,6 +215,7 @@ CBL_C { CBLDatabase_LastSequence; CBLDocument_CanonicalRevisionID; CBLDocument_Generation; + CBLDocument_GetRevisionHistory; CBLError_GetCaptureBacktraces; CBLError_SetCaptureBacktraces; CBLQuery_SetListenerCallbackDelay; diff --git a/test/DocumentTest.cc b/test/DocumentTest.cc index dd72af6d..114f5dae 100644 --- a/test/DocumentTest.cc +++ b/test/DocumentTest.cc @@ -691,6 +691,43 @@ TEST_CASE_METHOD(DocumentTest, "Save Document into Different Collection", "[Docu CBLDocument_Release(doc); } +#pragma mark - Revision History + +/* + https://github.com/couchbaselabs/couchbase-lite-api/blob/master/spec/tests/T0005-Version-Vector.md + 2. TestDocumentRevisionHistory + + Description + Test that the document's timestamp returns value as expected. + + Steps + 1. Create a new document with id = "doc1" + 2. Get document's _revisionIDs and check that the value returned is an empty array. + 3. Save the document into the default collection. + 4. Get document's _revisionIDs and check that the value returned is an array containing a + single revision id which is the revision id of the documnt. + 5. Get the document id = "doc1" from the database. + 6. Get document's _revisionIDs and check that the value returned is an array containing a + single revision id which is the revision id of the documnt. + */ +TEST_CASE_METHOD(DocumentTest, "Revision History", "[Document]") { + CBLDocument* doc = CBLDocument_CreateWithID("foo"_sl); + + alloc_slice revHistory = CBLDocument_GetRevisionHistory(doc); + CHECK(revHistory == nullslice); + + CBLError error; + REQUIRE(CBLCollection_SaveDocument(col, doc, &error)); + revHistory = CBLDocument_GetRevisionHistory(doc); + CHECK(revHistory != nullslice); + CBLDocument_Release(doc); + + doc = CBLCollection_GetMutableDocument(col, "foo"_sl, &error); + revHistory = CBLDocument_GetRevisionHistory(doc); + CHECK(revHistory != nullslice); + CBLDocument_Release(doc); +} + #pragma mark - Delete Document: TEST_CASE_METHOD(DocumentTest, "Delete Non Existing Document", "[Document]") { @@ -1156,4 +1193,4 @@ TEST_CASE_METHOD(DocumentTest, "Collection Change Notifications", "[Document]") createDocument(col, "bar", "greeting", "yo."); CHECK(collectionListenerCalls == 0); CHECK(docListenerCalls == 0); -} \ No newline at end of file +}