MAPBLOCK to CBLModel

61 views
Skip to first unread message

Ragu Vijaykumar

unread,
Apr 20, 2014, 10:27:24 PM4/20/14
to mobile-c...@googlegroups.com
I've been defining a bunch of MAPBLOCKs for my view and I was wondering whether there was anyway of converting the (NSDictionary* doc) parameter to a CBLModel inside my map function? Ideally, I'd want to convert the doc to a CBLDocument* object, and then use the CBLModelFactory to create the right type of CBLModel for me, or nil, and then I can check if the CBLModel is the right kind of model I want (isKindOfClass). If so, I can then do my map processing using the properties of the model.

The reason I want to do this is so that my map functions aren't littered with strings which don't give me any compile-time safety. Is there a way to properly do this?

Thanks for your help!
Ragu

Jens Alfke

unread,
Apr 21, 2014, 12:07:40 AM4/21/14
to mobile-c...@googlegroups.com

On Apr 20, 2014, at 7:27 PM, Ragu Vijaykumar <ra...@scrxpt.com> wrote:

I've been defining a bunch of MAPBLOCKs for my view and I was wondering whether there was anyway of converting the (NSDictionary* doc) parameter to a CBLModel inside my map function? Ideally, I'd want to convert the doc to a CBLDocument* object, and then use the CBLModelFactory to create the right type of CBLModel for me, or nil, and then I can check if the CBLModel is the right kind of model I want (isKindOfClass). If so, I can then do my map processing using the properties of the model.

I can see the appeal of this, as it makes your map functions look more like the rest of your code and lets you use the higher-level abstractions that the model provides. But I think it’s too heavyweight. Map functions need to be fast or they’ll become a bottleneck in view indexing. (I’m coincidentally doing some optimization work on indexing right now, so this is on my mind.)

Also, I think there’s too much state involved in CBLDocument and CBLModel to be safe to use in a map function. The map function may be run on a background thread (if you use a live query), and in the future we may parallelize it so it runs on several threads at once, if the CPU has enough cores.

—Jens

Ragu Vijaykumar

unread,
Apr 21, 2014, 12:30:43 PM4/21/14
to mobile-c...@googlegroups.com
Oh, I hear your point. Thanks for the insight!

Is there any way to traverse a relationship from a MapBlock? I have a DocumentA that has a relationship to DocumentB, and I want to return all DocumentAs that have a specific field value in DocumentB. Is there anyway to do this with a MapBlock?

Cheers,
Ragu

Jens Alfke

unread,
Apr 21, 2014, 1:22:30 PM4/21/14
to mobile-c...@googlegroups.com

On Apr 21, 2014, at 9:30 AM, Ragu Vijaykumar <ra...@scrxpt.com> wrote:

Is there any way to traverse a relationship from a MapBlock?

No; a map function by design is only allowed to look at the document passed into it. Otherwise if documentB changes but documentA doesn’t, then the index values emitted by documentA would be out of date.

I have a DocumentA that has a relationship to DocumentB, and I want to return all DocumentAs that have a specific field value in DocumentB. Is there anyway to do this with a MapBlock?

You’ll need to do this with two views and a hand-made join:
- View A maps from documentA to documentB, i.e. its map function detects a documentA and emits its documentB ID.
- View B maps the specific field of documentB, i.e. emits that field.

First you query view B to find all documentBs that have that value in them. Then you query view B setting the ‘keys’ property to that array of documentB IDs, and you’ll get rows for all the documentAs.

—Jens
Reply all
Reply to author
Forward
0 new messages