Skip to content

Commit ccba3bb

Browse files
authored
CBL-7234 : Sync ReplicatorConfiguration API with Multipeer Version (#3429)
CBL-7234: * Added ReplicatorConfiguration’s init(collections: [CollectionConfiguration], target: Endpoint) * Added ReplicatorConfiguration’s collectionConfigs property (cannot do collections as it’s already exists). * Added CollectionConfiguration's init(collection: Collection) for creating with a collection object. * Added CollectionConfigurationps fromCollections(_ [Collection]) -> [CollectionConfiguration's] * Updated tests to use the new APIs CBL-7236: * Deprecated ReplicatorConfiguration’s init(target: Endpoint) CBL-7238: * Deprecated ReplicatorConfiguration’s functions for managing collections and their configurations (addCollections, removeCollections, getCollections, getCollectionConfig) CBL-7249: * Deprecated CollectionConfiguration’s init() CBL-7226: * Add info about default collection used in the replication when creating ReplicatorConfiguration with database object * Update and clean up tests * Updated tests to use the new APIs. * Deleted ReplicatorTest+CustomConflict.testCollection() as the purpose of the test is unknown. * Disabled URLEndpointListenerTest’s testEmptyNetworkInterface() as it’s flaky depending on your local network and it doesn’t seem to add much values. * Removed code comment in MultipeerReplicatorTest.m that was accidentally commited
1 parent e3bfade commit ccba3bb

25 files changed

+953
-819
lines changed

Objective-C/CBLCollectionConfiguration.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,19 @@
2020
#import <Foundation/Foundation.h>
2121
#import <CouchbaseLite/CBLReplicatorTypes.h>
2222

23+
@class CBLCollection;
24+
2325
@protocol CBLConflictResolver;
2426

2527
NS_ASSUME_NONNULL_BEGIN
2628
/** The collection configuration that can be configured specifically for the replication. */
2729
@interface CBLCollectionConfiguration : NSObject
2830

31+
/**
32+
The custom conflict resolver function.
33+
If this value is nil, the default conflict resolver will be used. */
34+
@property (nonatomic, readonly, nullable) CBLCollection* collection;
35+
2936
/**
3037
The custom conflict resolver function.
3138
If this value is nil, the default conflict resolver will be used. */
@@ -55,6 +62,35 @@ NS_ASSUME_NONNULL_BEGIN
5562
with the remote endpoint. If not specified, all docs in the collection will be replicated. */
5663
@property (nonatomic, nullable) NSArray<NSString*>* documentIDs;
5764

65+
/**
66+
Initializes a collection configuration with the given collection.
67+
68+
@param collection The collection instance.
69+
*/
70+
- (instancetype) initWithCollection: (CBLCollection*)collection;
71+
72+
/**
73+
Initializes a collection configuration.
74+
75+
@deprecated Use `-initWithCollection:` instead.
76+
*/
77+
- (instancetype) init __deprecated_msg("Use -initWithCollection: instead.");
78+
79+
/**
80+
Creates an array of `CBLCollectionConfiguration` objects from the given collections.
81+
82+
Each collection is wrapped in a `CBLCollectionConfiguration`using default settings
83+
(no filters and no custom conflict resolvers).
84+
85+
This is a convenience method for configuring multiple collections with default configurations.
86+
If custom configurations are needed, construct `CBLCollectionConfiguration` objects
87+
directly instead.
88+
89+
@param collections The collections to replicate.
90+
@return An array of CBLCollectionConfiguration objects for the given collections.
91+
*/
92+
+ (NSArray<CBLCollectionConfiguration*>*) fromCollections: (NSArray<CBLCollection*>*)collections;
93+
5894
@end
5995

6096
NS_ASSUME_NONNULL_END

Objective-C/CBLCollectionConfiguration.m

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,32 @@
1717
// limitations under the License.
1818
//
1919
#import "CBLCollectionConfiguration.h"
20+
#import "CBLCollection+Internal.h"
21+
#import "CBLPrecondition.h"
2022

2123
@implementation CBLCollectionConfiguration
2224

25+
@synthesize collection=_collection;
2326
@synthesize documentIDs=_documentIDs, channels=_channels;
2427
@synthesize pushFilter=_pushFilter, pullFilter=_pullFilter;
2528
@synthesize conflictResolver=_conflictResolver;
2629

30+
- (instancetype) init {
31+
return [super init];
32+
}
33+
34+
- (instancetype) initWithCollection: (CBLCollection*)collection {
35+
self = [super init];
36+
if (self) {
37+
_collection = collection;
38+
}
39+
return self;
40+
}
41+
2742
- (instancetype) initWithConfig: (CBLCollectionConfiguration*)config {
2843
self = [super init];
2944
if (self) {
45+
_collection = config.collection;
3046
_documentIDs = config.documentIDs;
3147
_channels = config.channels;
3248
_pushFilter = config.pushFilter;
@@ -36,6 +52,15 @@ - (instancetype) initWithConfig: (CBLCollectionConfiguration*)config {
3652
return self;
3753
}
3854

55+
+ (NSArray<CBLCollectionConfiguration*>*) fromCollections: (NSArray<CBLCollection*>*)collections {
56+
[CBLPrecondition assertArrayNotEmpty: collections name: @"collections"];
57+
NSMutableArray* configs = [NSMutableArray arrayWithCapacity: collections.count];
58+
for (CBLCollection* collection in collections) {
59+
[configs addObject: [[CBLCollectionConfiguration alloc] initWithCollection: collection]];
60+
}
61+
return configs;
62+
}
63+
3964
- (NSDictionary*) effectiveOptions {
4065
NSMutableDictionary* options = [NSMutableDictionary dictionary];
4166
options[@kC4ReplicatorOptionChannels] = self.channels;

Objective-C/CBLReplicator.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,8 @@ - (bool) _setupC4Replicator: (C4Error*)outErr {
280280
std::vector<C4ReplicationCollection> cols;
281281
std::vector<alloc_slice> optionDicts;
282282

283-
for (CBLCollection* col in _config.collectionConfigs) {
284-
CBLCollectionConfiguration* colConfig = _config.collectionConfigs[col];
283+
for (CBLCollection* col in _config.collectionConfigMap) {
284+
CBLCollectionConfiguration* colConfig = _config.collectionConfigMap[col];
285285

286286
alloc_slice dict = [self encodedOptions: colConfig.effectiveOptions];
287287
optionDicts.push_back(dict);

Objective-C/CBLReplicatorConfiguration.h

Lines changed: 64 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,22 @@ NS_ASSUME_NONNULL_BEGIN
3535
/** Replicator Configuration */
3636
@interface CBLReplicatorConfiguration: NSObject
3737

38-
/** The local database to replicate with the target endpoint. */
39-
38+
/**
39+
The local database to replicate with the target endpoint.
40+
*/
4041
@property (nonatomic, readonly) CBLDatabase* database
41-
__deprecated_msg(" Use config.collections instead");
42+
__deprecated_msg("Use collectionConfigs instead");
4243

4344
/**
4445
The replication endpoint to replicate with.
4546
*/
4647
@property (nonatomic, readonly) id<CBLEndpoint> target;
4748

49+
/**
50+
The collection configurations used for the replication. Each configuration specifies a collection and its configuration.
51+
*/
52+
@property (nonatomic, readonly) NSArray<CBLCollectionConfiguration*>* collectionConfigs;
53+
4854
/**
4955
Replication type indicating the direction of the replication. The default value is
5056
.pushAndPull which is bidrectional.
@@ -94,46 +100,37 @@ __deprecated_msg(" Use config.collections instead");
94100
@property (nonatomic) BOOL acceptParentDomainCookies;
95101

96102
/**
97-
Channels filter when using init(database:target:) to configure the default collection
98-
for the replication.
103+
A set of Sync Gateway channel names to pull from for the default collection. Ignored for push replication.
99104
100105
@Note: Channels are not supported in Peer-to-Peer and Database-to-Database replication.
101106
*/
102107
@property (nonatomic, nullable) NSArray<NSString*>* channels
103-
__deprecated_msg(" Use [... initWithTarget:] and [config addCollection: config:]" \
104-
" with a CBLCollectionConfiguration object instead");
108+
__deprecated_msg("Use -initWithCollections:target: instead.");
105109

106110
/**
107-
documentIDs filter when using init(database:target:) to configure the default collection
108-
for the replication.
111+
A set of document IDs to filter by for the default collection. If given, only documents with these IDs will be pushed and/or pulled.
109112
*/
110113
@property (nonatomic, nullable) NSArray<NSString*>* documentIDs
111-
__deprecated_msg(" Use [... initWithTarget:] and [config addCollection: config:]" \
112-
" with a CBLCollectionConfiguration object instead");
114+
__deprecated_msg("Use -initWithCollections:target: instead.");
113115

114116
/**
115-
Push filter when using init(database:target:) to configure the default collection
116-
for the replication.
117+
A push filter for the default collection. Only documents for which the filter returns true are replicated.
117118
*/
118119
@property (nonatomic, nullable) CBLReplicationFilter pushFilter
119-
__deprecated_msg(" Use [... initWithTarget:] and [config addCollection: config:]" \
120-
" with a CBLCollectionConfiguration object instead");
120+
__deprecated_msg("Use -initWithCollections:target: instead.");
121121

122122
/**
123-
Pull filter when using init(database:target:) to configure the default collection
124-
for the replication.
123+
A pull filter for the default collection. Only documents for which the closure returns true are replicated.
125124
*/
126125
@property (nonatomic, nullable) CBLReplicationFilter pullFilter
127-
__deprecated_msg(" Use [... initWithTarget:] and [config addCollection: config:]" \
128-
" with a CBLCollectionConfiguration object instead");
126+
__deprecated_msg("Use -initWithCollections:target: instead.");
129127

130128
/**
131-
Conflict resolver when using init(database:target:) to configure the default collection
132-
for the replication.
129+
The custom conflict resolver for the default collection. If this value is not set, or set to nil,
130+
the default conflict resolver will be applied.
133131
*/
134132
@property (nonatomic, nullable) id<CBLConflictResolver> conflictResolver
135-
__deprecated_msg(" Use [... initWithTarget:] and [config addCollection: config:]" \
136-
" with a CBLCollectionConfiguration object instead");
133+
__deprecated_msg("Use -initWithCollections:target: instead.");
137134

138135
#if TARGET_OS_IPHONE
139136
/**
@@ -200,26 +197,35 @@ __deprecated_msg(" Use [... initWithTarget:] and [config addCollection: config:]
200197
*/
201198
@property (nonatomic) BOOL enableAutoPurge;
202199

203-
/** The collections used for the replication. */
204-
@property (nonatomic, readonly) NSArray<CBLCollection*>* collections;
200+
/**
201+
The collections used for the replication.
202+
*/
203+
@property (nonatomic, readonly) NSArray<CBLCollection*>* collections
204+
__deprecated_msg("Use collectionConfigs instead.");
205205

206206
/** Not available */
207207
- (instancetype) init NS_UNAVAILABLE;
208208

209209
/**
210-
Initializes a CBLReplicatorConfiguration with the local database and
211-
the target endpoint.
210+
Initializes a CBLReplicatorConfiguration with the given database and the target's endpoint.
211+
212+
When using this initializer, the default collection of the given database will be automatically
213+
included in the configuration.
214+
215+
If you do not intend to replicate the default collection, use -initWithCollections:target: instead to
216+
explicity specifiy the intended collections.
212217
213218
@param database The database.
214219
@param target The target endpoint.
215220
@return The CBLReplicatorConfiguration object.
216221
*/
217222
- (instancetype) initWithDatabase: (CBLDatabase*)database
218223
target: (id <CBLEndpoint>)target
219-
__deprecated_msg("Use [... initWithTarget:] instead.");
224+
__deprecated_msg("Use -initWithCollections:target: instead.");
220225

221226
/**
222-
Create a ReplicatorConfiguration object with the target’s endpoint.
227+
Initializes a CBLReplicatorConfiguration with the target’s endpoint.
228+
223229
After the ReplicatorConfiguration object is created, use addCollection(_ collection:, config:)
224230
or addCollections(_ collections:, config:) to specify and configure the collections used for
225231
replicating with the target. If there are no collections specified, the replicator will fail
@@ -228,7 +234,19 @@ __deprecated_msg("Use [... initWithTarget:] instead.");
228234
@param target The target endpoint.
229235
@return The CBLReplicatorConfiguration object.
230236
*/
231-
- (instancetype) initWithTarget: (id <CBLEndpoint>)target;
237+
- (instancetype) initWithTarget: (id <CBLEndpoint>)target
238+
__deprecated_msg("Use -initWithCollections:target: instead.");
239+
240+
/**
241+
Initializes a CBLReplicatorConfiguration with the specified collection configurations and target's endpoint.
242+
243+
Each `CBLCollectionConfiguration` in the `collections` array must be initialized using `-initWithCollection:`.
244+
245+
@param collections An array of collection configurations to replicate.
246+
@param target The target endpoint.
247+
*/
248+
- (instancetype) initWithCollections: (NSArray<CBLCollectionConfiguration*>*)collections
249+
target: (id <CBLEndpoint>)target;
232250

233251
/**
234252
Initializes a CBLReplicatorConfiguration with the configuration object.
@@ -245,9 +263,11 @@ __deprecated_msg("Use [... initWithTarget:] instead.");
245263
If a null configuration is specified, a default empty configuration will be applied.
246264
247265
@param collection The collection to be added.
248-
@param config Configuration for the collection, if nil, default config */
266+
@param config Configuration for the collection, if nil, default config
267+
*/
249268
- (void) addCollection: (CBLCollection*)collection
250-
config: (nullable CBLCollectionConfiguration*)config;
269+
config: (nullable CBLCollectionConfiguration*)config
270+
__deprecated_msg("Use -initWithCollections:target: instead.");
251271

252272
/**
253273
Add multiple collections used for the replication with an optional shared collection configuration.
@@ -258,23 +278,29 @@ __deprecated_msg("Use [... initWithTarget:] instead.");
258278
If a null configuration is specified, a default empty configuration will be applied.
259279
260280
@param collections The collections to be added.
261-
@param config Respective configuration for the collections, if nil, default config */
281+
@param config Respective configuration for the collections, if nil, default config
282+
*/
262283
- (void) addCollections: (NSArray*)collections
263-
config: (nullable CBLCollectionConfiguration*)config;
284+
config: (nullable CBLCollectionConfiguration*)config
285+
__deprecated_msg("Use -initWithCollections:target: instead.");
264286

265287
/**
266288
Remove the collection. If the collection doesn’t exist, this operation will be no ops.
267289
268-
@param collection The collection to be removed. */
269-
- (void) removeCollection: (CBLCollection*)collection;
290+
@param collection The collection to be removed.
291+
*/
292+
- (void) removeCollection: (CBLCollection*)collection
293+
__deprecated_msg("Use -initWithCollections:target: instead.");
270294

271295
/**
272296
Get a copy of the collection’s config. If the config needs to be changed for the collection, the
273297
collection will need to be re-added with the updated config.
274298
275299
@param collection The collection whose config is needed.
276-
@return The collection configuration, or nil if config doesn't exist */
277-
- (nullable CBLCollectionConfiguration*) collectionConfig: (CBLCollection*)collection;
300+
@return The collection configuration, or nil if config doesn't exist
301+
*/
302+
- (nullable CBLCollectionConfiguration*) collectionConfig: (CBLCollection*)collection
303+
__deprecated_msg("Use collectionConfigs instead.");
278304

279305
@end
280306

0 commit comments

Comments
 (0)