Skip to content

Commit

Permalink
Added API for creating persistent replication even if one already exists
Browse files Browse the repository at this point in the history
This allows for multiple replications with different filters, between
the same databases.
  • Loading branch information
snej committed Mar 25, 2013
1 parent ee0e927 commit e425585
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
22 changes: 17 additions & 5 deletions Couch/CouchDatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,28 +146,40 @@ typedef NSString* (^CouchDocumentPathMap)(NSString* documentID);

#pragma mark REPLICATION & SYNCHRONIZATION:

/** Triggers replication from a source database, to this database.
/** Triggers a non-persistent replication from a source database, to this database.
@param sourceURL The URL of the database to replicate from.
@return The CouchReplication object managing the replication. You have a chance to customize its properties (like .continuous and .filter) before it starts. */
- (CouchReplication*) pullFromDatabaseAtURL: (NSURL*)sourceURL;

/** Triggers replication from this database to a target database.
/** Triggers a non-persistent replication from this database to a target database.
@param targetURL The URL of the database to replicate to.
@return The CouchReplication object managing the replication. You have a chance to customize its properties (like .continuous and .filter) before it starts.*/
- (CouchReplication*) pushToDatabaseAtURL: (NSURL*)targetURL;

/** Creates a new persistent replication from a source database, to this database.
Be careful with this -- since persistent replications are recreated on launch, if you call this method every time the app launches you will create more and more redundant replications! It's often more appropriate to call -replicationFromDatabaseAtURL: unless you know for sure that you need multiple replications.
@param sourceURL The URL of the database to replicate from.
@return The CouchPersistentReplication object managing the replication. You have a chance to customize its properties (like .continuous and .filter) before it starts. */
- (CouchPersistentReplication*) createPersistentPullFromDatabaseAtURL: (NSURL*)sourceURL;

/** Creates a new persistent replication from this database, to a target database.
Be careful with this -- since persistent replications are recreated on launch, if you call this method every time the app launches you will create more and more redundant replications! It's often more appropriate to call -replicationToDatabaseAtURL: unless you know for sure that you need multiple replications.
@param targetURL The URL of the database to replicate to.
@return The CouchPersistentReplication object managing the replication. You have a chance to customize its properties (like .continuous and .filter) before it starts. */
- (CouchPersistentReplication*) createPersistentPushToDatabaseAtURL: (NSURL*)targetURL;

/** Configures this database to replicate bidirectionally (sync to and from) a database at the given URL.
@param otherURL The URL of the other database, or nil to indicate no replication.
@param exclusively If YES, any existing replications to or from other URLs will be removed.
@return A two-element NSArray whose values are the CouchPersistentReplications from and to the other URL, respectively. Returns nil if no target URL was given, or on failure. */
- (NSArray*) replicateWithURL: (NSURL*)otherURL exclusively: (BOOL)exclusively;

/** Creates a persistent replication from a database (a pull).
/** Creates or returns a persistent replication from a database (a pull).
Returns an object representing this replication. If a replication from this URL already exists, the configuration is unchanged. */
- (CouchPersistentReplication*) replicationFromDatabaseAtURL: (NSURL*)sourceURL;

/** Creates a persistent replication to a database (a push).
Returns an object representing this replication. If a replication from this URL already exists, the configuration is unchanged. */
/** Creates or returns a persistent replication to a database (a push).
Returns an object representing this replication. If a replication to this URL already exists, the configuration is unchanged. */
- (CouchPersistentReplication*) replicationToDatabaseAtURL: (NSURL*)targetURL;

/** All currently configured persistent replications involving this database, as CouchPersistentReplication objects. */
Expand Down
14 changes: 14 additions & 0 deletions Couch/CouchDatabase.m
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,20 @@ - (CouchPersistentReplication*) replicationToDatabaseAtURL: (NSURL*)targetURL {
return [self.server replicationWithSource: self.relativePath target: targetURL.absoluteString];
}

- (CouchPersistentReplication*) createPersistentPullFromDatabaseAtURL: (NSURL*)sourceURL {

return [CouchPersistentReplication createWithReplicatorDatabase: self.server.replicatorDatabase
source: sourceURL.absoluteString
target: self.relativePath];
}

- (CouchPersistentReplication*) createPersistentPushToDatabaseAtURL: (NSURL*)targetURL {

return [CouchPersistentReplication createWithReplicatorDatabase: self.server.replicatorDatabase
source: self.relativePath
target: targetURL.absoluteString];
}


- (NSArray*) replicateWithURL: (NSURL*)targetURL exclusively: (BOOL)exclusively {
NSMutableArray* repls = nil;
Expand Down
1 change: 1 addition & 0 deletions Couch/CouchInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ typedef void (^OnDatabaseChangeBlock)(CouchDocument*, BOOL externalChange);


@interface CouchServer ()
@property (readonly) CouchDatabase* replicatorDatabase;
@property (readonly) BOOL isEmbeddedServer;
- (CouchPersistentReplication*) replicationWithSource: (NSString*)source
target: (NSString*)target;
Expand Down

0 comments on commit e425585

Please sign in to comment.