From ff630a61449cd5ad63372f2d46b610c629e88fb4 Mon Sep 17 00:00:00 2001 From: Vlad Velicu Date: Wed, 23 Oct 2024 15:19:37 +0100 Subject: [PATCH 1/6] getRevHist impl --- Objective-C/CBLDocument.h | 3 +++ Objective-C/CBLDocument.mm | 11 +++++++++++ Swift/Document.swift | 7 +++++++ 3 files changed, 21 insertions(+) diff --git a/Objective-C/CBLDocument.h b/Objective-C/CBLDocument.h index d1fb3e5a7..48208a184 100644 --- a/Objective-C/CBLDocument.h +++ b/Objective-C/CBLDocument.h @@ -58,6 +58,9 @@ NS_ASSUME_NONNULL_BEGIN /** Return document data as JSON String. */ - (NSString*) toJSON; +/** Internally used for testing purpose. */ +- (nullable NSString*) _getRevisionHistory; + @end NS_ASSUME_NONNULL_END diff --git a/Objective-C/CBLDocument.mm b/Objective-C/CBLDocument.mm index 105b70073..dc6db31d7 100644 --- a/Objective-C/CBLDocument.mm +++ b/Objective-C/CBLDocument.mm @@ -29,6 +29,7 @@ #import "CBLFleece.hh" #import "MRoot.hh" #import "CBLErrorMessage.h" +#import using namespace fleece; @@ -199,6 +200,16 @@ - (void) replaceC4Doc: (CBLC4Document*)c4doc { } } +- (nullable NSString*) _getRevisionHistory { + CBL_LOCK(self) { + if (!_c4Doc) { + return nil; + } else { + return sliceResult2string(c4doc_getRevisionHistory(_c4Doc.rawDoc, UINT_MAX, nil, 0)); + } + } +} + #pragma mark - Fleece Encoding - (FLSliceResult) encodeWithRevFlags: (C4RevisionFlags*)outRevFlags error:(NSError**)outError { diff --git a/Swift/Document.swift b/Swift/Document.swift index 649c14dc6..48fa38295 100644 --- a/Swift/Document.swift +++ b/Swift/Document.swift @@ -45,6 +45,13 @@ public class Document : DictionaryProtocol, Equatable, Hashable, Sequence { /// The collection that the document belongs to. internal(set) public var collection: Collection? + // MARK: Unsupported - Internal use for testing + + /// Internally used for testing purpose. + public func _getRevisionHistory() -> String? { + return impl._getRevisionHistory() + } + // MARK: Edit /// Returns a mutable copy of the document. From 83b3b5a35eecb95ace9da0850aaa9ba860ea2a02 Mon Sep 17 00:00:00 2001 From: Vlad Velicu Date: Wed, 23 Oct 2024 15:19:43 +0100 Subject: [PATCH 2/6] tests --- Objective-C/Tests/DocumentTest.m | 34 ++++++++++++++++++++++++++++++++ Swift/Tests/DocumentTest.swift | 27 +++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/Objective-C/Tests/DocumentTest.m b/Objective-C/Tests/DocumentTest.m index 2d92a4bf2..355eae474 100644 --- a/Objective-C/Tests/DocumentTest.m +++ b/Objective-C/Tests/DocumentTest.m @@ -2262,6 +2262,40 @@ - (void) testDocumentResaveInAnotherCollection { }]; } +#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. + */ +- (void) testDocumentRevisionHistory { + NSError* err; + CBLCollection* defaultCollection = [self.db defaultCollection: &err]; + AssertNil(err); + + CBLMutableDocument* doc = [[CBLMutableDocument alloc] initWithID: @"doc1"]; + Assert(doc); + AssertNil(doc._getRevisionHistory); + + Assert([defaultCollection saveDocument:doc error: &err]); + Assert(doc._getRevisionHistory); + + CBLDocument* remoteDoc = [defaultCollection documentWithID: @"doc1" error: &err]; + Assert(remoteDoc._getRevisionHistory); +} + #pragma clang diagnostic pop @end diff --git a/Swift/Tests/DocumentTest.swift b/Swift/Tests/DocumentTest.swift index d0a757a88..6c09ec35e 100644 --- a/Swift/Tests/DocumentTest.swift +++ b/Swift/Tests/DocumentTest.swift @@ -1855,4 +1855,31 @@ class DocumentTest: CBLTestCase { } } } + + // MARK: toJSONTimestamp & 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. + func testDocumentRevisionHistory() throws { + let doc = MutableDocument(id: "doc1") + assert(doc._getRevisionHistory() == nil) + + try defaultCollection!.save(document: doc) + assert(doc._getRevisionHistory() != nil) + + let remoteDoc = try defaultCollection!.document(id: "doc1")!; + assert(doc._getRevisionHistory() != nil) + } } From 793bbdc167d7598907bf2ca1cbcfe9fc3411f33e Mon Sep 17 00:00:00 2001 From: Vlad Velicu Date: Wed, 23 Oct 2024 15:19:58 +0100 Subject: [PATCH 3/6] LiteCore 3.2.1-12 --- vendor/couchbase-lite-core | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vendor/couchbase-lite-core b/vendor/couchbase-lite-core index da8b267c0..fb6e666c1 160000 --- a/vendor/couchbase-lite-core +++ b/vendor/couchbase-lite-core @@ -1 +1 @@ -Subproject commit da8b267c0ef7ae36a814dc3f5af984f112dc2151 +Subproject commit fb6e666c1adebd873872540c7e0ffe0cc01320a3 From d418d8bf7e3ad95ec592ea902a86c53be986a8c6 Mon Sep 17 00:00:00 2001 From: Vlad Velicu Date: Wed, 23 Oct 2024 15:20:05 +0100 Subject: [PATCH 4/6] fix spacing --- Objective-C/CBLMutableDocument.mm | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Objective-C/CBLMutableDocument.mm b/Objective-C/CBLMutableDocument.mm index ae569e033..402d643ca 100644 --- a/Objective-C/CBLMutableDocument.mm +++ b/Objective-C/CBLMutableDocument.mm @@ -100,9 +100,9 @@ - (instancetype) initWithID: (nullable NSString*)documentID return self; } -/* internal */ - (instancetype) initAsCopyWithDocument: (CBLDocument*)doc - dict: (nullable CBLDictionary*)dict -{ +#pragma mark - Internal +- (instancetype) initAsCopyWithDocument: (CBLDocument*)doc + dict: (nullable CBLDictionary*)dict { self = [self initWithCollection: doc.collection documentID: doc.id c4Doc: doc.c4Doc]; @@ -111,7 +111,6 @@ - (instancetype) initWithID: (nullable NSString*)documentID _dict = [dict mutableCopy]; } return self; - } #pragma mark - Edit From 1a3d2db3254f2f04d3b31f5e18e189635a4dba40 Mon Sep 17 00:00:00 2001 From: Vlad Velicu Date: Wed, 23 Oct 2024 17:40:21 +0100 Subject: [PATCH 5/6] get new c4doc using kDocGetAll content lvl --- Objective-C/CBLDocument.mm | 14 +++++++++----- Objective-C/Tests/DocumentTest.m | 4 ++-- Swift/Tests/DocumentTest.swift | 2 +- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/Objective-C/CBLDocument.mm b/Objective-C/CBLDocument.mm index dc6db31d7..fa6e7c01c 100644 --- a/Objective-C/CBLDocument.mm +++ b/Objective-C/CBLDocument.mm @@ -201,12 +201,16 @@ - (void) replaceC4Doc: (CBLC4Document*)c4doc { } - (nullable NSString*) _getRevisionHistory { + if (!_collection) { + return nil; + } + CBL_LOCK(self) { - if (!_c4Doc) { - return nil; - } else { - return sliceResult2string(c4doc_getRevisionHistory(_c4Doc.rawDoc, UINT_MAX, nil, 0)); - } + C4Error err; + C4Document* doc = c4coll_getDoc(_collection.c4col, _c4Doc.docID, true, kDocGetAll, &err); + NSString* revHistory = doc ? sliceResult2string(c4doc_getRevisionHistory(doc, UINT_MAX, nil, 0)) : nil; + c4doc_release(doc); + return revHistory; } } diff --git a/Objective-C/Tests/DocumentTest.m b/Objective-C/Tests/DocumentTest.m index 355eae474..956c9e1ac 100644 --- a/Objective-C/Tests/DocumentTest.m +++ b/Objective-C/Tests/DocumentTest.m @@ -2292,8 +2292,8 @@ - (void) testDocumentRevisionHistory { Assert([defaultCollection saveDocument:doc error: &err]); Assert(doc._getRevisionHistory); - CBLDocument* remoteDoc = [defaultCollection documentWithID: @"doc1" error: &err]; - Assert(remoteDoc._getRevisionHistory); + doc = [[defaultCollection documentWithID: @"doc1" error: &err] toMutable]; + Assert(doc._getRevisionHistory); } #pragma clang diagnostic pop diff --git a/Swift/Tests/DocumentTest.swift b/Swift/Tests/DocumentTest.swift index 6c09ec35e..a461b8100 100644 --- a/Swift/Tests/DocumentTest.swift +++ b/Swift/Tests/DocumentTest.swift @@ -1879,7 +1879,7 @@ class DocumentTest: CBLTestCase { try defaultCollection!.save(document: doc) assert(doc._getRevisionHistory() != nil) - let remoteDoc = try defaultCollection!.document(id: "doc1")!; + let remoteDoc = try defaultCollection!.document(id: "doc1")!.toMutable(); assert(doc._getRevisionHistory() != nil) } } From 91a04aec4e04a299b374fb28b05ca0b389a8155b Mon Sep 17 00:00:00 2001 From: Vlad Velicu Date: Wed, 23 Oct 2024 17:48:43 +0100 Subject: [PATCH 6/6] add docID to CBL c4doc --- Objective-C/Internal/CBLC4Document.h | 5 ++++- Objective-C/Internal/CBLC4Document.mm | 6 +++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/Objective-C/Internal/CBLC4Document.h b/Objective-C/Internal/CBLC4Document.h index 574f9c827..7608a869e 100644 --- a/Objective-C/Internal/CBLC4Document.h +++ b/Objective-C/Internal/CBLC4Document.h @@ -2,7 +2,7 @@ // CBLC4Document.h // CouchbaseLite // -// Copyright (c) 2017 Couchbase, Inc All rights reserved. +// Copyright (c) 2024 Couchbase, Inc All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -32,6 +32,9 @@ NS_ASSUME_NONNULL_BEGIN /** Sequence of the selected revision. */ @property (readonly, nonatomic) C4SequenceNumber sequence; +/** Document ID of the selected revision. */ +@property (readonly, nonatomic) C4String docID; + /** Revision ID of the selected revision. */ @property (readonly, nonatomic) C4String revID; diff --git a/Objective-C/Internal/CBLC4Document.mm b/Objective-C/Internal/CBLC4Document.mm index 477e9af3d..3d505c498 100644 --- a/Objective-C/Internal/CBLC4Document.mm +++ b/Objective-C/Internal/CBLC4Document.mm @@ -2,7 +2,7 @@ // CBLC4Document.mm // CouchbaseLite // -// Copyright (c) 2017 Couchbase, Inc All rights reserved. +// Copyright (c) 2024 Couchbase, Inc All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -49,6 +49,10 @@ - (C4SequenceNumber) sequence { return _rawDoc->selectedRev.sequence; } +- (C4String) docID { + return _rawDoc->docID; +} + - (C4String) revID { return _rawDoc->selectedRev.revID; }