Pull command + filtered replication TouchDB-Android

47 views
Skip to first unread message

Emil Einarsson

unread,
May 14, 2013, 6:09:13 AM5/14/13
to mobile-c...@googlegroups.com
Hi!

I am trying to do an filtered replication with TouchDB-Android and everything seems fine in the beginning and everything looks fine until I start deleting stuff on the serverside. If some documents get deleted server side, those documents doesn't get deleted locally. So my question is if there is some way to make this work or if this is intended? I tried doing my sync without a filter function and then all the documents got deleted correctly on the mobile as it should. 

If it doesn't work is there some other way to design the sync so I get the desired functionality? That is:
The database consists of a lot of jobs where each job is 1 document. In each document is a key called worker which the value of the worker decides who gets to do the job. These jobs should be synced to the workers mobile and only the jobs the specific worker should be doing should be synced with his phone.

On another side note if I turn of the filter function the data traffic becomes extremely larger, why is that? 2 documents getting synced with filter function was 6,9kB and without it was 97,0kB. Does it have something to do with revisions? Does it sync all the revisions to the mobile device or something?

A last question also maybe a bit of topic:
How does the sync work on a package lvl? What is it that is really being sent during a sync in different scenarios (update, delete, add, new)?

Sorry for many questions at once =)

Jens Alfke

unread,
May 14, 2013, 3:13:55 PM5/14/13
to mobile-c...@googlegroups.com

On May 14, 2013, at 3:09 AM, Emil Einarsson <emil.ein...@gmail.com> wrote:

I am trying to do an filtered replication with TouchDB-Android and everything seems fine in the beginning and everything looks fine until I start deleting stuff on the serverside. If some documents get deleted server side, those documents doesn't get deleted locally. So my question is if there is some way to make this work or if this is intended? I tried doing my sync without a filter function and then all the documents got deleted correctly on the mobile as it should. 

The filter function probably isn’t passing deleted documents. By default, when you delete a document all of its properties are removed and replaced by “_deleted”:true. If the filter function is looking for something like a worker ID, it won’t see that in any deletions.

You’ll probably want to fix this by changing the way the app deletes these docs. Instead of going through the usual delete call, it should PUT a new revision that adds a “_deleted”:true property. This is equivalent to a deletion but lets you customize what properties remain in the deleted revision. (If you want, you can get rid of any unnecessary properties when you do this; for instance, you might be able to leave just the worker_id and _deleted properties if that’s all your filter needs.)

—Jens

Traun Leyden

unread,
May 14, 2013, 3:56:25 PM5/14/13
to mobile-c...@googlegroups.com
This seems like a FAQ so I added a new section to the guide:


Deleting documents with Filtered Replications

Deleting documents can be tricky in the context of filtered replications. For example, let's assume you have a document that has aworker_id field, and you setup a filtered replication to only pull documents when the worker_id equals a certain value.

When one of these documents is deleted, it will not get sync'd in the pull replication! Since the filter function will look for a document with a specific worker_id, and the deleted document won't contain any worker_id, it will fail the filter function and therefore not be sync'd.

This can be fixed by deleting documents in a different way. Since a document is considered deleted as long as it has the special_deleted field, it is possible to delete the document while still retaining the worker_id field. Instead of using the DELETE verb, you will instead want to use the PUT verb. You will definitely need to set the _deleted field, in order for the document to be considered deleted. You can then either retain the field(s) that you need for filtered replication, like the worker_id field, or you can retain all of the fields in the original document.




--
You received this message because you are subscribed to the Google Groups "Mobile Couchbase" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mobile-couchba...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages