New to TouchDB replication

110 views
Skip to first unread message

Paul Okstad

unread,
Oct 4, 2012, 7:24:01 PM10/4/12
to mobile-c...@googlegroups.com
I'm getting really frustrated getting TouchDB replication working from a remote server. I am running CouchDB v1.2 on a local Ubuntu 12.04 VM. I have been trying to figure out how to pull data from the CouchDB server into the local TouchDB in a simple command line app. After replication, my TouchDB server reports no documents being inside the database. Anyone see any obvious problems with my code?

My code is here:

#import <Foundation/Foundation.h>

#import <CouchCocoa/CouchCocoa.h>

#import <CouchCocoa/CouchTouchDBServer.h>

#import <TouchDB/TouchDB.h>


#define DB_NAME @"test"


int main(int argc, const char * argv[])

{

    

    @autoreleasepool {

        

        // insert code here...

        NSLog(@"Main bundle directory: %@", [NSBundle mainBundle].resourcePath);

        // Connect to local Linux VM hosting CouchDB server

        NSURL* testServerURL = [NSURL URLWithString: @"http://172.16.106.145:5984"];

        CouchServer *testServer = [[CouchServer alloc] initWithURL:testServerURL];

        CouchDatabase *testDB = [testServer databaseNamed: DB_NAME];

        // Show all docs from production server

        CouchQuery* allDocs = [testDB getAllDocuments];

        NSLog(@"Test server has %ld docs. Printing all of their ID's:", allDocs.rows.count);

        for (CouchQueryRow* row in allDocs.rows) {

            NSLog(@"test server doc: %@", row.documentID);

        }

        // Create a TouchDB server

        CouchTouchDBServer* tdbserver = [CouchTouchDBServer sharedInstance];

        if (tdbserver.error) {

            NSLog(@"ERROR: %@", [tdbserver.error localizedDescription]);

        }

        // Create database

        CouchDatabase *localDB = [tdbserver databaseNamed:DB_NAME];

        NSError *error = Nil;

        if (![localDB ensureCreated:&error]) {

            NSLog(@"ERROR: %@", [error localizedDescription]);

        }

        // Get some info about this server

        NSLog(@"TouchDB databases: %@", tdbserver.getDatabases);

        

        // Now replicate: pull from test server to touchdb

        NSLog(@"Pulling from test to touchdb");

        CouchReplication *pull = [localDB pullFromDatabaseAtURL:testDB.URL];

        pull.continuous = FALSE;

        RESTOperation *op = [pull start];

        if (op != Nil) {

            NSLog(@"Waiting for operation to finish: %d to replicate", pull.total);

            [op wait];

            NSLog(@"Operation has finished.");

        }

        allDocs = [localDB getAllDocuments];

        NSLog(@"Printing all %ld docs in touchdb", allDocs.rows.count);

        for (CouchQueryRow* row in allDocs.rows) {

            NSLog(@"Test server doc: %@", row.documentID);

        }

        NSLog(@"Finished printing all docs in touchdb");

    }

    return 0;

}



This is the output I get:

2012-10-04 16:14:12.336 couchdb[19772:303] Main bundle directory: /Users/...blah blah blah...

2012-10-04 16:14:12.366 couchdb[19772:303] Test server has 5 docs. Printing all of their ID's:

2012-10-04 16:14:12.371 couchdb[19772:303] test server doc: c6613b51d3689329525e583511000ff3

2012-10-04 16:14:12.371 couchdb[19772:303] test server doc: c6613b51d3689329525e583511001021

2012-10-04 16:14:12.372 couchdb[19772:303] test server doc: c6613b51d3689329525e583511001e88

2012-10-04 16:14:12.372 couchdb[19772:303] test server doc: c6613b51d3689329525e5835110028f7

2012-10-04 16:14:12.372 couchdb[19772:303] test server doc: c6613b51d3689329525e583511003333

2012-10-04 16:14:12.383 couchdb[19772:303] TouchDB databases: (

    "CouchTouchDBDatabase[_replicator]",

    "CouchTouchDBDatabase[test]"

)

2012-10-04 16:14:12.384 couchdb[19772:303] Pulling from prod to test

2012-10-04 16:14:12.385 couchdb[19772:303] Waiting for operation to finish: 0 to replicate

2012-10-04 16:14:12.389 couchdb[19772:303] Operation has finished.

2012-10-04 16:14:12.392 couchdb[19772:303] Printing all 0 docs in touchdb

2012-10-04 16:14:12.397 couchdb[19772:303] Finished printing all docs in touchdb

Jens Alfke

unread,
Oct 4, 2012, 8:10:43 PM10/4/12
to mobile-c...@googlegroups.com

On Oct 4, 2012, at 4:24 PM, Paul Okstad <pok...@gmail.com> wrote:

> RESTOperation *op = [pull start];
> if (op != Nil) {
> NSLog(@"Waiting for operation to finish: %d to replicate", pull.total);
> [op wait];
> NSLog(@"Operation has finished.");
> }

Replication is asynchronous. The [op wait] call will return as soon as the replication request has been received by TouchDB, but the actual replication won’t have started yet. You’ll need to observe the ‘running’ property of the replication, then run the runloop until you observe that it’s changed to NO.

—Jens

PS: A minor Obj-C nit: use “nil” not “Nil”. The latter is of type Class, not id; it’s only supposed to be used for comparing class objects. I have barely ever seen it used.

Paul Okstad

unread,
Oct 5, 2012, 8:08:50 PM10/5/12
to mobile-c...@googlegroups.com
Thank you Jens! Got it working and attached my working code for others to see. BTW Jens, you probably don't remember me but I spoke to you at the first CouchConf in San Francisco. I asked you about a more native stripped down solution for iOS instead of the Erlang based mobile solution. At the time it was just a twinkle in your eye, but it's great to see that you guys have committed to TouchDB and I'm sure that this will become an extremely popular and important mobile framework as it matures. Great job!

#import <Foundation/Foundation.h>

#import <CouchCocoa/CouchCocoa.h>

#import <CouchCocoa/CouchTouchDBServer.h>

#import <TouchDB/TouchDB.h>


#define DB_NAME @"testdb"


int main(int argc, const char * argv[])

{

    

    @autoreleasepool {

        

        // insert code here...

        NSLog(@"Main bundle directory: %@", [NSBundle mainBundle].resourcePath);

        // Connect to local Linux VM hosting CouchDB server

        NSURL* testServerURL = [NSURL URLWithString: @"http://172.16.106.145:5984"];

        CouchServer *testServer = [[CouchServer alloc] initWithURL:testServerURL];

        CouchDatabase *testDB = [testServer databaseNamed: DB_NAME];

        // Show all docs from production server

        CouchQuery* allDocs = [testDB getAllDocuments];

        NSLog(@"Test server has %ld docs. Printing all of their ID's:", allDocs.rows.count);

        for (CouchQueryRow* row in allDocs.rows) {

            NSLog(@"test server doc: %@", row.documentID);

        }

        // Create a TouchDB server

        CouchTouchDBServer* tdbserver = [CouchTouchDBServer sharedInstance];

        if (tdbserver.error) {

            NSLog(@"ERROR: %@", [tdbserver.error localizedDescription]);

        }

        // Create touchdb database

        CouchDatabase *localDB = [tdbserver databaseNamed:DB_NAME];

        NSError *error = nil;

        if (![localDB ensureCreated:&error]) {

            NSLog(@"ERROR: %@", [error localizedDescription]);

        }

        // Now replicate: pull from test server to touchdb

        NSLog(@"Pulling from test server to touchdb");

        CouchReplication *pull = [localDB pullFromDatabaseAtURL:testDB.URL];

        pull.continuous = FALSE;

        RESTOperation *op = [pull start];

        [op wait];

        // start an explicit run loop to turn this async call to a sync call

        NSTimeInterval timeout = 0.1;

        while(timeout < 15.0 && pull.running){

            NSLog(@"Waiting for touchdb to hurry the hell up!");

            timeout += 0.1;

            [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];

        }

        NSLog(@"Replication has finished.");

        allDocs = [localDB getAllDocuments];

        NSLog(@"Printing all %ld docs in touchdb database", allDocs.rows.count);

        for (CouchQueryRow* row in allDocs.rows) {

            NSLog(@"Test server doc: %@", row.documentID);

        }

        NSLog(@"Finished printing all docs in touchdb");

    }

    return 0;

}


2012-10-05 16:58:22.755 couchdb[23762:303] Main bundle directory: /Users/.../blah blah blah

2012-10-05 16:58:22.795 couchdb[23762:303] Test server has 5 docs. Printing all of their ID's:

2012-10-05 16:58:22.804 couchdb[23762:303] test server doc: c6613b51d3689329525e583511000ff3

2012-10-05 16:58:22.805 couchdb[23762:303] test server doc: c6613b51d3689329525e583511001021

2012-10-05 16:58:22.805 couchdb[23762:303] test server doc: c6613b51d3689329525e583511001e88

2012-10-05 16:58:22.805 couchdb[23762:303] test server doc: c6613b51d3689329525e5835110028f7

2012-10-05 16:58:22.806 couchdb[23762:303] test server doc: c6613b51d3689329525e583511003333

2012-10-05 16:58:22.851 couchdb[23762:303] Pulling from test server to touchdb

2012-10-05 16:58:22.855 couchdb[23762:303] Waiting for touchdb to hurry the hell up!

2012-10-05 16:58:22.959 couchdb[23762:303] Waiting for touchdb to hurry the hell up!

2012-10-05 16:58:23.060 couchdb[23762:303] Waiting for touchdb to hurry the hell up!

2012-10-05 16:58:23.162 couchdb[23762:303] Waiting for touchdb to hurry the hell up!

2012-10-05 16:58:23.264 couchdb[23762:303] Waiting for touchdb to hurry the hell up!

2012-10-05 16:58:23.365 couchdb[23762:303] Waiting for touchdb to hurry the hell up!

2012-10-05 16:58:23.466 couchdb[23762:303] Waiting for touchdb to hurry the hell up!

2012-10-05 16:58:23.568 couchdb[23762:303] Waiting for touchdb to hurry the hell up!

2012-10-05 16:58:23.670 couchdb[23762:303] Waiting for touchdb to hurry the hell up!

2012-10-05 16:58:23.771 couchdb[23762:303] Waiting for touchdb to hurry the hell up!

2012-10-05 16:58:23.873 couchdb[23762:303] Waiting for touchdb to hurry the hell up!

2012-10-05 16:58:23.974 couchdb[23762:303] Replication has finished.

2012-10-05 16:58:23.977 couchdb[23762:303] Printing all 5 docs in touchdb database

2012-10-05 16:58:23.980 couchdb[23762:303] Test server doc: c6613b51d3689329525e583511000ff3

2012-10-05 16:58:23.980 couchdb[23762:303] Test server doc: c6613b51d3689329525e583511001021

2012-10-05 16:58:23.981 couchdb[23762:303] Test server doc: c6613b51d3689329525e583511001e88

2012-10-05 16:58:23.981 couchdb[23762:303] Test server doc: c6613b51d3689329525e5835110028f7

2012-10-05 16:58:23.982 couchdb[23762:303] Test server doc: c6613b51d3689329525e583511003333

2012-10-05 16:58:23.983 couchdb[23762:303] Finished printing all docs in touchdb

Jens Alfke

unread,
Oct 6, 2012, 3:13:52 PM10/6/12
to mobile-c...@googlegroups.com
On Oct 5, 2012, at 5:08 PM, Paul Okstad <pok...@gmail.com> wrote:

Thank you Jens! Got it working and attached my working code for others to see.

Cool — I’ve added it to the Snippets page on the CouchCocoa wiki.

BTW Jens, you probably don't remember me but I spoke to you at the first CouchConf in San Francisco. I asked you about a more native stripped down solution for iOS instead of the Erlang based mobile solution. At the time it was just a twinkle in your eye, but it's great to see that you guys have committed to TouchDB and I'm sure that this will become an extremely popular and important mobile framework as it matures. Great job!

Thanks! I remember several people asking for such a thing at CouchConf and at other talks I gave, and it was great to get a chance to put it into practice a few months later :)

—Jens

Reply all
Reply to author
Forward
0 new messages