Couchbase Lite 2.x: implementing view-like behaviour

Hello!

I’m migrating an Android app from CBL1.4 to 2.1. I’ve converted the older map/reduce stuff to the new query syntax and it seems to be working well so far. I do have one case where I need to do some fairly expensive calculations on each document, and I’d like to avoid doing this on-the-fly with the query model if possible. Previously this was handled with the old map/reduce view behaviour, and I was hoping to get some guidance on manually implementing something similar with CBL2.1.

At present I’m looking at:

  • On first load, create a “view” document to contain the aggregate information (if it does not already exist)
  • On first load, calculate info from any documents that do not already appear in the view and update the view accordingly.
  • On save/add document, calculate info and add it to the view. Save both documents (added doc and view doc) in a batch. This includes docs added via replication.
  • Docs being aggregated are read-only, so should not need to handle edits or deletions.
  • I’m also considering not having a single view document, but multiple. Could have a 1-1 relationship between the docs with raw info, and a “view” version of the doc that contains the computed values.
  • I do not want to replicate the view doc/s. That info can be recalculated from the source docs.

Rough example of aggregated info:

{ "sourceDocId": "doc-123",
   "sumOfMultipleProperties": 1234,
   "conversionOfDateStringToTicks": 1543557458079,
   "encodedVersionOfMultipleProperties": "fieldA-10th char of fieldB-conditional selection from fieldC" }

My main concerns at the moment are finding a good way of finding any documents that are missing from the view, making sure that I can handle exceptions etc. in such a way as the view will be eventually consistent with raw documents, and finding good times to trigger the “update view” operation to ensure that consistency.

Appreciate any help you can provide. Also happy to get “you’re doing it wrong!” responses with suggestions for alternatives. :slight_smile:

Regards,
Dave