couchdbcloudanttouchdb

Couchdb - is it possible to "disreplicate" a replicated document


I am designing a system that will use CouchDB and TouchDB\CloudantSync to cache the database on the users' smartphone using replication. Now Let's say I have 1000s of documents, each has a 100Kb attachment, and I want to free some space on the smartphone by removing a document.

I want that after I remove some document, I could replicate it again from the server. This is different from deletion, which will give the document a new revision and avoid replicating it from the server again (because the deleted document on the smartphone is a child of the undeleted one).

I could obviously make redundant updates on the server documents, but that's inefficient...

Is there a way to "unsync" the document?


Solution

    1. Create a continuous\polling replication from the server to the smartphone - filtered to prevent too much space usage on the smartphone.
    2. Whenever you want to free some space, remove some document's id from the last filtered replication, delete it from the smartphone (use compaction for a real cleanup), and keep its id in some unsynced_documents list.
    3. Whenever you want to resync a document, read it from the server and create it on the smartphone as a whole new document (ignore the revision). You can add a field resynced: true to the document's json. Don't forget to update the replication filter and the unsycned_documents list.
    4. When a "resynced" document changes on the server, it will be replicated to the smartphone, which already has a document with the same id (created on the smartphone). This will create a conflict. Resolve the conflict by choosing the server's revision (by deleting the revision with resynced: true).

    I'm talking about the case where a one-way (Server -> User) replication is required. i.e., the users only have read-permissions. If you give the users write permission, you should find a way to differ between an intentional deletion of a document and "unsyncing".