FilterParam based replication - CBLite android?

134 views
Skip to first unread message

Pankaj Jakhar

unread,
Feb 17, 2014, 1:35:38 AM2/17/14
to mobile-c...@googlegroups.com
How would I be able to use FilterParam based replication because Documentation does not cover this? 

Can anybody share a sample code for this. Because its syntax is a mystery for me.

1. How would I write a filter function on server side which accepts params and works on the basis of params.
2. Where do I need to specify that filter function because in case of CouchDB I was using _design documents to write my filter functions. But now at mobile platform I am using CBLite but on the server side it is CouchDB. How will I exactly write filters?

Jens Alfke

unread,
Feb 17, 2014, 1:49:07 AM2/17/14
to mobile-c...@googlegroups.com
On Feb 16, 2014, at 10:35 PM, Pankaj Jakhar <pankaj...@gmail.com> wrote:

1. How would I write a filter function on server side which accepts params and works on the basis of params.

The same way you would before. Its JS code that goes in a design document on the server.

2. Where do I need to specify that filter function because in case of CouchDB I was using _design documents to write my filter functions. But now at mobile platform I am using CBLite but on the server side it is CouchDB. How will I exactly write filters?

Filters for pull replications live on the server just as before.
Filters for push replications need to be specified on the client, in Java. As shown in the API documentation, call database.setFilter.

—Jens

Pankaj Jakhar

unread,
Feb 17, 2014, 2:50:01 AM2/17/14
to mobile-c...@googlegroups.com
Hi Jens 

Can you please provide me the syntax of the filter function that accepts extra params.
Like this:

fun(doc, params)
{

}


Is it so?



PankaJ Jakhar

There Is No Secret Ingredient. It’s Just You.



--
You received this message because you are subscribed to a topic in the Google Groups "Couchbase Mobile" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mobile-couchbase/JoJeEJyhBuY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mobile-couchba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mobile-couchbase/D301F86C-CAB8-41F7-8A8C-E578AC727444%40mooseyard.com.

For more options, visit https://groups.google.com/groups/opt_out.

Pankaj Jakhar

unread,
Feb 17, 2014, 2:58:29 AM2/17/14
to mobile-c...@googlegroups.com
Currently I am writing CBLite filter function in code as(Just to test it) :

mDatabaseLocal.setFilter("Filter", new ReplicationFilter() {
@Override
public boolean filter(SavedRevision savedRevision, Map<String, Object> map) {
String property = savedRevision.getProperty("text").toString();
String text = map.get("text").toString();
if(property == text)
return true;
return false;
}
});
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("text", "a");
pullReplication.setFilter("filter");

Is it okay to write filter function like this. Because here I am considering second param of filter function as filterparams(map given below).
filter(SavedRevision savedRevision, Map<String, Object> map)

Pankaj Jakhar

unread,
Feb 17, 2014, 4:21:38 AM2/17/14
to mobile-c...@googlegroups.com
Hi 

I somewhere got it that how I should create filter.

Correct me if I am wrong. Here is my filter.
{
   "docFilter": "function(doc, req)  {if(doc.text == req.query.text){return true} else { return false}}"
}

Does req param works exactly the way I have written it - req.query.text, like this?

Jens Alfke

unread,
Feb 17, 2014, 12:16:47 PM2/17/14
to mobile-c...@googlegroups.com
This looks fine (but I'm not an expert on our Java API.)

You can simplify the code, though. Instead of

> if(property == text)
> return true;
> return false;

you can simply write

return (property == text);

—Jens

Pankaj Jakhar

unread,
Feb 17, 2014, 12:24:09 PM2/17/14
to mobile-c...@googlegroups.com

Oaky. Thanks for your help.
Here I tried filtered replication. I was getting the result using HTTP query on curl shell but CBLite android does not return document. Here is the description. 


I am using this method for filtered replicating a document which has "text" field's value "cblite". Here is the code(You can simply replace ).

private void startSync() {

        URL syncUrl;
        try {
            syncUrl = new URL(SYNC_URL);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }

        Replication pullReplication = mDatabaseLocal
                .createPullReplication(syncUrl);

        //Temporarily setting filter.
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("text", "cblite");
        pullReplication.setFilterParams(map);       
        pullReplication.setFilter("docFilter");
        pullReplication.setContinuous(true);

        Replication pushReplication = mDatabaseLocal
                .createPushReplication(syncUrl);
        pushReplication.setContinuous(true);

        pullReplication.start();
        pushReplication.start();

        pullReplication.addChangeListener(this);
        pushReplication.addChangeListener(this);
    }

But nothing is being replicated. If I remove the filter then replication works successfully, replicating all documents.

Also, I have tested my database if the filter is working or not. So running this Curl query, replication was successful.

curl -H 'Content-Type: application/json' -X POST http://localhost:5984/_replicator -d \
'{
  "source":"sync_gateway",
  "target":"http://localhost:5984/testreplication",
  "create_target":true,
  "continuous":true,
  "filter":"grocery-sync/docFilter",
  "query_params":{"text":"cblite"}
}'

and here is the filter

{
   "docFilter": "function(doc, req)  {if(doc.text == req.query.text){return true} else { return false}}"
}

Note: I am using CouchDB on the Server side.

Traun Leyden

unread,
Feb 17, 2014, 1:04:53 PM2/17/14
to mobile-c...@googlegroups.com
The problem is either on the Couchbase Lite side, or in the way that you've defined your filter function.

I think it should be possible to isolate it by doing a pull replication to a different CouchDB database and specify your filter function.  

Possible Outcomes:

1) If the expected documents are pulled, then it would indicate a bug in Couchbase Lite

2) If none of the expected documents are pulled, then it would indicate an issue with your filter function



--
You received this message because you are subscribed to the Google Groups "Couchbase Mobile" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mobile-couchba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mobile-couchbase/731f63be-373d-4aca-b491-441396c8b436%40googlegroups.com.

Pankaj Jakhar

unread,
Feb 17, 2014, 1:09:24 PM2/17/14
to mobile-c...@googlegroups.com
Hi Traun

As I said earlier, I have queried the same database with curl shell and filter is working properly. SO when I am trying to call it using CBLite code then filter does not even return a single document.

Traun Leyden

unread,
Feb 17, 2014, 2:39:58 PM2/17/14
to mobile-c...@googlegroups.com


--
You received this message because you are subscribed to the Google Groups "Couchbase Mobile" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mobile-couchba...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages