Skip to content
This repository has been archived by the owner on Mar 9, 2022. It is now read-only.

TouchDB Ektorp Replication

mschoch edited this page Jun 14, 2012 · 2 revisions

The following example shows how you can install a filter function with TouchDB-Android and then use the filter when performing a push replication with Ektorp. This filter function will only return true for documents with an even value for the field "foo".

        String filesDir = getFilesDir().getAbsolutePath();
        TDServer tdserver = new TDServer(filesDir);

        // install the filter
        TDDatabase database = tdserver.getDatabaseNamed("ektorp_filter_test");
        database.defineFilter("evenFoo", new TDFilterBlock() {

            @Override
            public boolean filter(TDRevision revision) {
                Integer foo = (Integer)revision.getProperties().get("foo");
                if(foo != null && foo.intValue() % 2 == 0) {
                    return true;
                }
                return false;
            }
        });

        HttpClient httpClient = new TouchDBHttpClient(tdserver);
        CouchDbInstance server = new StdCouchDbInstance(httpClient);

        // create a local database
        CouchDbConnector db = server.createConnector("ektorp_filter_test", true);

        // create 3 objects
        TestObject test1 = new TestObject(1, false, "ektorp-1");
        TestObject test2 = new TestObject(2, false, "ektorp-2");
        TestObject test3 = new TestObject(3, false, "ektorp-3");

        // save these objects in the database
        db.create(test1);
        db.create(test2);
        db.create(test3);

        // push this database to the test replication server
        ReplicationCommand pushCommand = new ReplicationCommand.Builder()
            .source("ektorp_filter_test")
            .target("http://@10.0.2.2:5984" + "/ektorp_filter_test")
            .continuous(false)
            .filter("evenFoo")
            .build();

        ReplicationStatus status = server.replicate(pushCommand);