From 6fc5bed6b71b52660d7967ad7acd9b9d999b1c4d Mon Sep 17 00:00:00 2001 From: jayahariv <10448770+jayahariv@users.noreply.github.com> Date: Wed, 8 Jun 2022 02:13:09 -0700 Subject: [PATCH 1/4] Fix: update LC APIs * make changes to adopt the new listener config --- Objective-C/CBLDocument.mm | 1 - Objective-C/CBLRemoteDatabase.mm | 2 +- Objective-C/CBLReplicator.mm | 4 ++-- Objective-C/Internal/CBLDocument+Internal.h | 2 +- Objective-C/Internal/Replicator/CBLWebSocket.mm | 1 + Objective-C/Tests/RemoteDatabaseTest.m | 4 ++++ 6 files changed, 9 insertions(+), 5 deletions(-) diff --git a/Objective-C/CBLDocument.mm b/Objective-C/CBLDocument.mm index 33c84f4ad..3b7f45639 100644 --- a/Objective-C/CBLDocument.mm +++ b/Objective-C/CBLDocument.mm @@ -135,7 +135,6 @@ - (instancetype) initWithDocumentID: (NSString*)documentID revisionID: (NSString*)revisionID body: (FLSliceResult)body { NSParameterAssert(documentID != nil); - NSParameterAssert(revisionID != nil); self = [self init]; if (self) { _id = documentID; diff --git a/Objective-C/CBLRemoteDatabase.mm b/Objective-C/CBLRemoteDatabase.mm index be1213306..51aa2c037 100644 --- a/Objective-C/CBLRemoteDatabase.mm +++ b/Objective-C/CBLRemoteDatabase.mm @@ -70,7 +70,7 @@ - (instancetype) initWithURL: (NSURL*)url authenticator: (CBLAuthenticator*)auth } - (void) dealloc { - c4client_free(_client); + c4client_release(_client); _client = nil; } diff --git a/Objective-C/CBLReplicator.mm b/Objective-C/CBLReplicator.mm index 2a904a9c4..ca53bc0ca 100644 --- a/Objective-C/CBLReplicator.mm +++ b/Objective-C/CBLReplicator.mm @@ -686,7 +686,7 @@ - (void) logErrorOnDocument: (CBLReplicatedDocument*)doc pushing: (BOOL)pushing #pragma mark - PUSH/PULL FILTER: -static bool pushFilter(C4String collectionName, +static bool pushFilter(C4CollectionSpec collectionSpec, C4String docID, C4String revID, C4RevisionFlags flags, FLDict flbody, void *context) { auto replicator = (__bridge CBLReplicator*)context; @@ -694,7 +694,7 @@ static bool pushFilter(C4String collectionName, body: flbody pushing: true]; } -static bool pullFilter(C4String collectionName, +static bool pullFilter(C4CollectionSpec collectionSpec, C4String docID, C4String revID, C4RevisionFlags flags, FLDict flbody, void *context) { auto replicator = (__bridge CBLReplicator*)context; diff --git a/Objective-C/Internal/CBLDocument+Internal.h b/Objective-C/Internal/CBLDocument+Internal.h index d9bb93f09..227dbf293 100644 --- a/Objective-C/Internal/CBLDocument+Internal.h +++ b/Objective-C/Internal/CBLDocument+Internal.h @@ -106,7 +106,7 @@ NS_ASSUME_NONNULL_BEGIN Used to create a CBLDocument without database and c4doc Will retain the passed in `body`(FLSliceResult) */ - (instancetype) initWithDocumentID: (NSString*)documentID - revisionID: (NSString*)revisionID + revisionID: (nullable NSString*)revisionID body: (FLSliceResult)body; - (BOOL) selectConflictingRevision; diff --git a/Objective-C/Internal/Replicator/CBLWebSocket.mm b/Objective-C/Internal/Replicator/CBLWebSocket.mm index d54910ee5..27e065d7e 100644 --- a/Objective-C/Internal/Replicator/CBLWebSocket.mm +++ b/Objective-C/Internal/Replicator/CBLWebSocket.mm @@ -28,6 +28,7 @@ #import "c4Socket.h" #import "MYURLUtils.h" #import "fleece/Fleece.hh" +#import "fleece/Expert.hh" // for AllocedDict #import #import #import diff --git a/Objective-C/Tests/RemoteDatabaseTest.m b/Objective-C/Tests/RemoteDatabaseTest.m index 6d2f66122..a4a6a3727 100644 --- a/Objective-C/Tests/RemoteDatabaseTest.m +++ b/Objective-C/Tests/RemoteDatabaseTest.m @@ -84,6 +84,7 @@ - (void) testConnectedClient { // start the listener Config* config = [[Config alloc] initWithDatabase: self.otherDB]; config.disableTLS = YES; + config.allowConnectedClient = YES; [self listen: config errorCode: 0 errorDomain: nil]; // start the connected client @@ -126,6 +127,7 @@ - (void) testSaveDocument { // start the listener Config* config = [[Config alloc] initWithDatabase: self.otherDB]; config.disableTLS = YES; + config.allowConnectedClient = YES; [self listen: config errorCode: 0 errorDomain: nil]; // start the connected client @@ -159,6 +161,7 @@ - (void) testDeleteDocument { // start the listener Config* config = [[Config alloc] initWithDatabase: self.otherDB]; config.disableTLS = YES; + config.allowConnectedClient = YES; [self listen: config errorCode: 0 errorDomain: nil]; // start the connected client @@ -198,6 +201,7 @@ - (void) testSaveUpdatedDocument { __block CBLDocument* doc = nil; Config* config = [[Config alloc] initWithDatabase: self.otherDB]; config.disableTLS = YES; + config.allowConnectedClient = YES; [self listen: config errorCode: 0 errorDomain: nil]; [self startConnectedClient: _listener.localEndpoint.url]; XCTestExpectation* eGet = [self expectationWithDescription: @"get document exp"]; From d0ea0975b8c87e17c95e10c1e6ceb0ba639243ee Mon Sep 17 00:00:00 2001 From: jayahariv <10448770+jayahariv@users.noreply.github.com> Date: Fri, 14 Oct 2022 04:33:46 -0700 Subject: [PATCH 2/4] fix the build to pass pull request validation --- Objective-C/Tests/RemoteDatabaseTest.m | 55 +++++++++++--------------- Scripts/pull_request_build.sh | 5 ++- 2 files changed, 26 insertions(+), 34 deletions(-) diff --git a/Objective-C/Tests/RemoteDatabaseTest.m b/Objective-C/Tests/RemoteDatabaseTest.m index a4a6a3727..ef40f9c09 100644 --- a/Objective-C/Tests/RemoteDatabaseTest.m +++ b/Objective-C/Tests/RemoteDatabaseTest.m @@ -70,9 +70,22 @@ - (void) tearDown { [super tearDown]; } +- (void) start { + // start the listener + Config* config = [[Config alloc] initWithDatabase: self.otherDB]; + config.disableTLS = YES; + config.allowConnectedClient = YES; + [self listen: config errorCode: 0 errorDomain: nil]; + + // start the connected client + [self startConnectedClient: _listener.localEndpoint.url]; +} + #pragma mark - Tests - (void) testConnectedClient { + [self start]; + XCTestExpectation* e = [self expectationWithDescription: @"expectation"]; // create a doc in server (listener) @@ -81,15 +94,6 @@ - (void) testConnectedClient { [doc1 setString: @"someString" forKey: @"someKeyString"]; Assert([self.otherDB saveDocument: doc1 error: &err], @"Fail to save db1 %@", err); - // start the listener - Config* config = [[Config alloc] initWithDatabase: self.otherDB]; - config.disableTLS = YES; - config.allowConnectedClient = YES; - [self listen: config errorCode: 0 errorDomain: nil]; - - // start the connected client - [self startConnectedClient: _listener.localEndpoint.url]; - // get the document with ID [_client documentWithID: @"doc-1" completion: ^(CBLDocument* doc, NSError* error) { // empty error @@ -122,16 +126,9 @@ - (void) testConnectedClientUnknownHostname { } - (void) testSaveDocument { - XCTestExpectation* e = [self expectationWithDescription: @"save document exp"]; + [self start]; - // start the listener - Config* config = [[Config alloc] initWithDatabase: self.otherDB]; - config.disableTLS = YES; - config.allowConnectedClient = YES; - [self listen: config errorCode: 0 errorDomain: nil]; - - // start the connected client - [self startConnectedClient: _listener.localEndpoint.url]; + XCTestExpectation* e = [self expectationWithDescription: @"save document exp"]; CBLMutableDocument* doc1 = [self createDocument: @"doc-1"]; [doc1 setString: @"someString" forKey: @"someKeyString"]; @@ -150,6 +147,8 @@ - (void) testSaveDocument { } - (void) testDeleteDocument { + [self start]; + XCTestExpectation* e = [self expectationWithDescription: @"delete document exp"]; // create a doc in server (listener) @@ -158,15 +157,6 @@ - (void) testDeleteDocument { [doc1 setString: @"someString" forKey: @"someKeyString"]; Assert([self.otherDB saveDocument: doc1 error: &err], @"Fail to save db1 %@", err); - // start the listener - Config* config = [[Config alloc] initWithDatabase: self.otherDB]; - config.disableTLS = YES; - config.allowConnectedClient = YES; - [self listen: config errorCode: 0 errorDomain: nil]; - - // start the connected client - [self startConnectedClient: _listener.localEndpoint.url]; - XCTestExpectation* getDocExp = [self expectationWithDescription: @"get doc exp"]; __block CBLDocument* doc = nil; [_client documentWithID: @"doc-1" completion:^(CBLDocument* d, NSError* error) { @@ -176,6 +166,10 @@ - (void) testDeleteDocument { [self waitForExpectations: @[getDocExp] timeout: timeout]; + Assert(doc, @"document fetch failed!"); + if (!doc) + return; + [_client deleteDocument: doc completion:^(CBLDocument* d, NSError *error) { AssertNil(error); // make sure no error [e fulfill]; @@ -187,6 +181,8 @@ - (void) testDeleteDocument { } - (void) testSaveUpdatedDocument { + [self start]; + // --- // CREATE A DOC & Save to remote-db & GET IT BACK // --- @@ -199,11 +195,6 @@ - (void) testSaveUpdatedDocument { // start the listener & get it to 'doc' __block CBLDocument* doc = nil; - Config* config = [[Config alloc] initWithDatabase: self.otherDB]; - config.disableTLS = YES; - config.allowConnectedClient = YES; - [self listen: config errorCode: 0 errorDomain: nil]; - [self startConnectedClient: _listener.localEndpoint.url]; XCTestExpectation* eGet = [self expectationWithDescription: @"get document exp"]; [_client documentWithID: @"doc-1" completion: ^(CBLDocument* d, NSError* error) { AssertNil(error); diff --git a/Scripts/pull_request_build.sh b/Scripts/pull_request_build.sh index 59a67a164..f267b4781 100755 --- a/Scripts/pull_request_build.sh +++ b/Scripts/pull_request_build.sh @@ -2,8 +2,9 @@ cd couchbase-lite-ios -SCHEMES=("CBL_EE_ObjC" "CBL_EE_Swift" "CBL_ObjC" "CBL_Swift") +TEST_SIMULATOR=$(xcrun simctl list devicetypes | grep \.iPhone- | tail -1 | sed "s/ (com.apple.*//g") +SCHEMES=("CBL_EE_ObjC" "CBL_EE_Swift") for SCHEME in "${SCHEMES[@]}" do - xcodebuild test -project CouchbaseLite.xcodeproj -scheme "$SCHEME" -sdk iphonesimulator -destination "platform=iOS Simulator,name=iPhone SE (2nd generation)" + xcodebuild test -project CouchbaseLite.xcodeproj -scheme "$SCHEME" -sdk iphonesimulator -destination "platform=iOS Simulator,name=$TEST_SIMULATOR" done From 8a53db13c8ed1127da44de2069c2bc5e55010809 Mon Sep 17 00:00:00 2001 From: jayahariv <10448770+jayahariv@users.noreply.github.com> Date: Fri, 14 Oct 2022 04:43:21 -0700 Subject: [PATCH 3/4] enable debug level log to check for test failure --- Objective-C/Tests/RemoteDatabaseTest.m | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Objective-C/Tests/RemoteDatabaseTest.m b/Objective-C/Tests/RemoteDatabaseTest.m index ef40f9c09..e894357ea 100644 --- a/Objective-C/Tests/RemoteDatabaseTest.m +++ b/Objective-C/Tests/RemoteDatabaseTest.m @@ -59,6 +59,8 @@ - (void) validateDocument: (CBLDocument*)expDoc - (void) setUp { [super setUp]; timeout = 20.0; + + CBLDatabase.log.console.level = kCBLLogLevelDebug; } - (void) tearDown { From 78c9d219f9550f2321f6b9d08e835cf6b9cd932b Mon Sep 17 00:00:00 2001 From: jayahariv <10448770+jayahariv@users.noreply.github.com> Date: Fri, 14 Oct 2022 05:38:46 -0700 Subject: [PATCH 4/4] cleanup: separate out remote doc * separate out remote doc * CBL-2993 --- CouchbaseLite.xcodeproj/project.pbxproj | 20 ++++++++ Objective-C/CBLDocument.mm | 18 ++----- Objective-C/CBLMutableDocument.mm | 2 +- Objective-C/Internal/CBLDocument+Internal.h | 6 +-- Objective-C/Internal/CBLRemoteDocument.h | 40 +++++++++++++++ Objective-C/Internal/CBLRemoteDocument.mm | 56 +++++++++++++++++++++ 6 files changed, 123 insertions(+), 19 deletions(-) create mode 100644 Objective-C/Internal/CBLRemoteDocument.h create mode 100644 Objective-C/Internal/CBLRemoteDocument.mm diff --git a/CouchbaseLite.xcodeproj/project.pbxproj b/CouchbaseLite.xcodeproj/project.pbxproj index 634c53cba..c0a43c754 100644 --- a/CouchbaseLite.xcodeproj/project.pbxproj +++ b/CouchbaseLite.xcodeproj/project.pbxproj @@ -246,6 +246,14 @@ 69002EBE234E695600776107 /* CBLErrorMessage.m in Sources */ = {isa = PBXBuildFile; fileRef = 69002EB8234E693F00776107 /* CBLErrorMessage.m */; }; 69002EBF234E695600776107 /* CBLErrorMessage.h in Headers */ = {isa = PBXBuildFile; fileRef = 69002EA9234E693F00776107 /* CBLErrorMessage.h */; }; 69002EC0234E695600776107 /* CBLErrorMessage.m in Sources */ = {isa = PBXBuildFile; fileRef = 69002EB8234E693F00776107 /* CBLErrorMessage.m */; }; + 6981337D28EB0BAC00BFEA7B /* CBLRemoteDocument.h in Headers */ = {isa = PBXBuildFile; fileRef = 6981337028EB0B9F00BFEA7B /* CBLRemoteDocument.h */; }; + 6981337E28EB0BAC00BFEA7B /* CBLRemoteDocument.h in Headers */ = {isa = PBXBuildFile; fileRef = 6981337028EB0B9F00BFEA7B /* CBLRemoteDocument.h */; }; + 6981337F28EB0BAD00BFEA7B /* CBLRemoteDocument.h in Headers */ = {isa = PBXBuildFile; fileRef = 6981337028EB0B9F00BFEA7B /* CBLRemoteDocument.h */; }; + 6981338028EB0BAD00BFEA7B /* CBLRemoteDocument.h in Headers */ = {isa = PBXBuildFile; fileRef = 6981337028EB0B9F00BFEA7B /* CBLRemoteDocument.h */; }; + 6981338128EB0BB300BFEA7B /* CBLRemoteDocument.mm in Sources */ = {isa = PBXBuildFile; fileRef = 6981337128EB0B9F00BFEA7B /* CBLRemoteDocument.mm */; }; + 6981338228EB0BB400BFEA7B /* CBLRemoteDocument.mm in Sources */ = {isa = PBXBuildFile; fileRef = 6981337128EB0B9F00BFEA7B /* CBLRemoteDocument.mm */; }; + 6981338328EB0BB500BFEA7B /* CBLRemoteDocument.mm in Sources */ = {isa = PBXBuildFile; fileRef = 6981337128EB0B9F00BFEA7B /* CBLRemoteDocument.mm */; }; + 6981338428EB0BB500BFEA7B /* CBLRemoteDocument.mm in Sources */ = {isa = PBXBuildFile; fileRef = 6981337128EB0B9F00BFEA7B /* CBLRemoteDocument.mm */; }; 69845B0723354D0A00CC16BB /* DateTimeQueryFunctionTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A8DD7D921C9876E00741C47 /* DateTimeQueryFunctionTest.swift */; }; 72A879F01E2DD51C008466FF /* CBLBlob.mm in Sources */ = {isa = PBXBuildFile; fileRef = 72A879EF1E2DD51C008466FF /* CBLBlob.mm */; }; 72A87A051E2E0E70008466FF /* CBLBlobStream.h in Headers */ = {isa = PBXBuildFile; fileRef = 72A87A031E2E0E70008466FF /* CBLBlobStream.h */; }; @@ -2003,6 +2011,8 @@ 27F961981ED8D9440060F804 /* CBLReachability.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CBLReachability.m; sourceTree = ""; }; 69002EA9234E693F00776107 /* CBLErrorMessage.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CBLErrorMessage.h; sourceTree = ""; }; 69002EB8234E693F00776107 /* CBLErrorMessage.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CBLErrorMessage.m; sourceTree = ""; }; + 6981337028EB0B9F00BFEA7B /* CBLRemoteDocument.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CBLRemoteDocument.h; sourceTree = ""; }; + 6981337128EB0B9F00BFEA7B /* CBLRemoteDocument.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = CBLRemoteDocument.mm; sourceTree = ""; }; 6992582A22DFE9A100E0D1D2 /* build_xcframework.sh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; path = build_xcframework.sh; sourceTree = ""; }; 72A879EF1E2DD51C008466FF /* CBLBlob.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = CBLBlob.mm; sourceTree = ""; }; 72A879FE1E2DD536008466FF /* CBLBlob.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CBLBlob.h; sourceTree = ""; }; @@ -3688,6 +3698,8 @@ 27D721981F8E97F400AA4458 /* CBLFleece.mm */, 27D721B81F904B2500AA4458 /* CBLNewDictionary.h */, 27D721B91F904B2500AA4458 /* CBLNewDictionary.mm */, + 6981337028EB0B9F00BFEA7B /* CBLRemoteDocument.h */, + 6981337128EB0B9F00BFEA7B /* CBLRemoteDocument.mm */, ); name = Document; sourceTree = ""; @@ -3887,6 +3899,7 @@ 9374A89F201FC49800BA0D9E /* MYBackgroundMonitor.h in Headers */, 2753AFF61EC39CA200C12E98 /* CBLHTTPLogic.h in Headers */, 93B5036F1E64B0A0002C4680 /* CBLMisc.h in Headers */, + 6981337E28EB0BAC00BFEA7B /* CBLRemoteDocument.h in Headers */, 9308F3F71E64B21D00F53EE4 /* CollectionUtils.h in Headers */, 930B367724AAAB3F000DF2B3 /* CBLDatabase+Debug.h in Headers */, 93EC42E01FB386BE00D54BB4 /* CBLQueryExpression+Internal.h in Headers */, @@ -4033,6 +4046,7 @@ 932CC559207D9ED2000B4B78 /* CouchbaseLite.h in Headers */, 9343EF94207D611600F19A89 /* CBLQueryFullTextFunction.h in Headers */, 9343EF96207D611600F19A89 /* CBLQueryExpression.h in Headers */, + 6981337F28EB0BAD00BFEA7B /* CBLRemoteDocument.h in Headers */, 9343EF97207D611600F19A89 /* CBLQueryMeta.h in Headers */, 9343EF98207D611600F19A89 /* CBLIndexBuilder.h in Headers */, 939260AC20A0F5EA00E5748C /* CBLMessagingError.h in Headers */, @@ -4204,6 +4218,7 @@ isa = PBXHeadersBuildPhase; buildActionMask = 2147483647; files = ( + 6981338028EB0BAD00BFEA7B /* CBLRemoteDocument.h in Headers */, 1AAB2784227793DE0037A880 /* CBLConflict.h in Headers */, 9343F0B5207D61AB00F19A89 /* CBLQueryResultArray.h in Headers */, 93292D9F22BD448400A0862A /* CBLReplicatorConfiguration+Swift.h in Headers */, @@ -4430,6 +4445,7 @@ 2753AFF51EC39CA200C12E98 /* CBLHTTPLogic.h in Headers */, 939B1B5F2009C0F200FAA3CB /* CBLQueryVariableExpression+Internal.h in Headers */, 93B72063205CA6650069F5FC /* CBLException.h in Headers */, + 6981337D28EB0BAC00BFEA7B /* CBLRemoteDocument.h in Headers */, 93B41D641F0580E700A7F114 /* CBLQueryJoin.h in Headers */, 1A3471A626736E660042C6BA /* CBLQuery+N1QL.h in Headers */, 9388CC3221C18671005CA66D /* CBLLog+Internal.h in Headers */, @@ -5378,6 +5394,7 @@ 93E18737211122EA001D52B9 /* MYURLUtils.m in Sources */, 1A416030227D0AD40061A567 /* Conflict.swift in Sources */, 93C18E831FB638E80029B567 /* CBLDatabaseConfiguration.m in Sources */, + 6981338228EB0BB400BFEA7B /* CBLRemoteDocument.mm in Sources */, 9381961C1EC113860032CC51 /* CBLFragment.m in Sources */, 93EC42E91FB3930E00D54BB4 /* CBLQueryArrayExpression.m in Sources */, 93B41D7E1F05B3A800A7F114 /* Join.swift in Sources */, @@ -5632,6 +5649,7 @@ 9343EF5E207D611600F19A89 /* CBLVersion.m in Sources */, 9369A6A0207DBAA9009B5B83 /* CBLDatabase+Encryption.mm in Sources */, 9343EF5F207D611600F19A89 /* CBLDatabaseConfiguration.m in Sources */, + 6981338328EB0BB500BFEA7B /* CBLRemoteDocument.mm in Sources */, 9343EF60207D611600F19A89 /* CBLQueryFullTextFunction.m in Sources */, 9343EF61207D611600F19A89 /* CBLQueryArrayFunction.m in Sources */, 931713D522C182F500F1B5BF /* CBLQueryFunction+Prediction.m in Sources */, @@ -5826,6 +5844,7 @@ 9343F067207D61AB00F19A89 /* ResultSet.swift in Sources */, 9343F068207D61AB00F19A89 /* CBLReplicator+Backgrounding.m in Sources */, 9343F069207D61AB00F19A89 /* CBLChangeListenerToken.m in Sources */, + 6981338428EB0BB500BFEA7B /* CBLRemoteDocument.mm in Sources */, 9343F06A207D61AB00F19A89 /* MYAnonymousIdentity.m in Sources */, 9343F06B207D61AB00F19A89 /* CBLIndex.m in Sources */, 9343F06C207D61AB00F19A89 /* Limit.swift in Sources */, @@ -6162,6 +6181,7 @@ 93DBD0132004BCE00017CA83 /* CBLURLEndpoint.m in Sources */, 27D7219B1F8E97F400AA4458 /* CBLFleece.mm in Sources */, 932EA56A2061FF7E00EDB667 /* CBLVersion.m in Sources */, + 6981338128EB0BB300BFEA7B /* CBLRemoteDocument.mm in Sources */, 93C18E821FB638E80029B567 /* CBLDatabaseConfiguration.m in Sources */, 9384D8421FC405D200FE89D8 /* CBLQueryFullTextFunction.m in Sources */, 9384D80B1FC3F75700FE89D8 /* CBLQueryArrayFunction.m in Sources */, diff --git a/Objective-C/CBLDocument.mm b/Objective-C/CBLDocument.mm index 3b7f45639..27af637f8 100644 --- a/Objective-C/CBLDocument.mm +++ b/Objective-C/CBLDocument.mm @@ -28,6 +28,7 @@ #import "MRoot.hh" #import "CBLErrorMessage.h" #import "CBLData.h" +#import "CBLRemoteDocument.h" using namespace fleece; @@ -38,7 +39,7 @@ @implementation CBLDocument } @synthesize database=_database, id=_id, c4Doc=_c4Doc, fleeceData=_fleeceData; -@synthesize remoteDocBody=_remoteDocBody, isRemoteDoc=_isRemoteDoc; +@synthesize remoteDoc=_remoteDoc, isRemoteDoc=_isRemoteDoc; - (instancetype) initWithDatabase: (CBLDatabase*)database documentID: (NSString*)documentID @@ -139,15 +140,9 @@ - (instancetype) initWithDocumentID: (NSString*)documentID if (self) { _id = documentID; - // keeps a retained copy of body for CBLDocument & releases in dealloc - _remoteDocBody = FLSliceResult_Retain(body); + _remoteDoc = [[CBLRemoteDocument alloc] initWithBody: body]; + _fleeceData = _remoteDoc.data; - FLDict dict = kFLEmptyDict; - if (body.buf) { - FLValue docBodyVal = FLValue_FromData(slice(_remoteDocBody), kFLTrusted); - dict = FLValue_AsDict(docBodyVal); - } - _fleeceData = dict; _revID = revisionID; _isRemoteDoc = true; [self updateDictionary]; @@ -155,11 +150,6 @@ - (instancetype) initWithDocumentID: (NSString*)documentID return self; } -- (void) dealloc { - if (_remoteDocBody) - FLSliceResult_Release(_remoteDocBody); // releases the retained copy -} - #pragma mark - Public - (NSString*) description { diff --git a/Objective-C/CBLMutableDocument.mm b/Objective-C/CBLMutableDocument.mm index 1cc108754..d3abdc85b 100644 --- a/Objective-C/CBLMutableDocument.mm +++ b/Objective-C/CBLMutableDocument.mm @@ -107,7 +107,7 @@ - (instancetype) initWithID: (nullable NSString*)documentID // This is used by ConnectedClient API // Used to convert a CBLDocument to CBLMutableDocument - (instancetype) initAsCopyOfRemoteDB: (CBLDocument*)doc { - return [self initWithDocumentID: doc.id revisionID: doc.revisionID body: doc.remoteDocBody]; + return [self initWithDocumentID: doc.id revisionID: doc.revisionID body: doc.remoteDoc.body]; } #pragma mark - Edit diff --git a/Objective-C/Internal/CBLDocument+Internal.h b/Objective-C/Internal/CBLDocument+Internal.h index 227dbf293..ac82a1dff 100644 --- a/Objective-C/Internal/CBLDocument+Internal.h +++ b/Objective-C/Internal/CBLDocument+Internal.h @@ -31,7 +31,7 @@ #import "CBLDictionary.h" #import "CBLFragment.h" #import "fleece/Fleece.h" - +#import "CBLRemoteDocument.h" NS_ASSUME_NONNULL_BEGIN @@ -70,9 +70,7 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic, readonly, nullable) FLDict fleeceData; -// Used by ConnectedClient API -// if the document is remoteDoc, this contains the document body -@property (nonatomic, readonly) FLSliceResult remoteDocBody; +@property (nonatomic, nullable, readonly) CBLRemoteDocument* remoteDoc; // Document is from remote-DB or a regular-DB @property (nonatomic, readonly) BOOL isRemoteDoc; diff --git a/Objective-C/Internal/CBLRemoteDocument.h b/Objective-C/Internal/CBLRemoteDocument.h new file mode 100644 index 000000000..a06c29e4e --- /dev/null +++ b/Objective-C/Internal/CBLRemoteDocument.h @@ -0,0 +1,40 @@ +// +// CBLRemoteDocument.h +// CouchbaseLite +// +// Copyright (c) 2022 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. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +/** Interface used by the remote database */ +@interface CBLRemoteDocument : NSObject + +/** if the document is remoteDoc, this contains the document body */ +@property (nonatomic, readonly) FLSliceResult body; +@property (nonatomic, readonly) FLDict data; + +/** + This constructor is used by ConnectedClient APIs. + Used to create a CBLDocument without database and c4doc + Will retain the passed in `body`(FLSliceResult) */ +- (instancetype) initWithBody: (FLSliceResult)body; + + +@end + +NS_ASSUME_NONNULL_END diff --git a/Objective-C/Internal/CBLRemoteDocument.mm b/Objective-C/Internal/CBLRemoteDocument.mm new file mode 100644 index 000000000..6de46321d --- /dev/null +++ b/Objective-C/Internal/CBLRemoteDocument.mm @@ -0,0 +1,56 @@ +// +// CBLRemoteDocument.m +// CouchbaseLite +// +// Copyright (c) 2022 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. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#import "CBLRemoteDocument.h" +#import "CBLFleece.hh" +#include "fleece/FLExpert.h" + +using namespace fleece; + +@implementation CBLRemoteDocument { + FLDict _dict; +} + +@synthesize body=_body; + +- (instancetype) initWithBody:(FLSliceResult)body { + self = [self init]; + if (self) { + // keeps a retained copy of body for CBLDocument & releases in dealloc + _body = FLSliceResult_Retain(body); + + _dict = kFLEmptyDict; + if (body.buf) { + FLValue docBodyVal = FLValue_FromData(slice(_body), kFLTrusted); + _dict = FLValue_AsDict(docBodyVal); + } + } + return self; +} + +- (FLDict) data { + return _dict; +} + +- (void) dealloc { + if (_body) + FLSliceResult_Release(_body); // releases the retained copy +} + +@end