You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Patrick Barnes <mr...@gmail.com> on 2010/02/19 06:30:39 UTC

Suspected view generator bug...

I was tearing my hair out because this view:

map: function(doc) {
	if (doc.doc_type=='group') {
		doc.path.pop();
		emit(doc.path, doc.display_name);
	}
}

Was supposed to take one element off the end of the path array and using 
the array as a view key. However, when I triggered view creation it 
would remove TWO elements!

Strangely, if I opened the view function from within futon, added some 
whitespace and re-ran the function it worked fine.

There are two similar views that both use the doc.path.pop() call.
I'm fairly confident that behind the scenes, the couchdb (trunk) view 
generator is
a) sharing a single copy of each document between all the map functions.
b) permitting map functions to modify that copy, meaning that subsequent 
map functions might get a CORRUPTED version of the document.

Verdict?

For now, I've rewritten my map functions to clone the array to a local 
var before I call path.pop().

-Patrick Barnes

Re: Suspected view generator bug...

Posted by Paul Davis <pa...@gmail.com>.
Patrick,

You are exactly right. Previously we would freeze the document object
to prevent functions from modifying it, but that function was broken
in Spidermonkey so it was disabled. In the future if that's ever fixed
we'll reenable it.

HTH,
Paul Davis

On Fri, Feb 19, 2010 at 12:30 AM, Patrick Barnes <mr...@gmail.com> wrote:
> I was tearing my hair out because this view:
>
> map: function(doc) {
>        if (doc.doc_type=='group') {
>                doc.path.pop();
>                emit(doc.path, doc.display_name);
>        }
> }
>
> Was supposed to take one element off the end of the path array and using the
> array as a view key. However, when I triggered view creation it would remove
> TWO elements!
>
> Strangely, if I opened the view function from within futon, added some
> whitespace and re-ran the function it worked fine.
>
> There are two similar views that both use the doc.path.pop() call.
> I'm fairly confident that behind the scenes, the couchdb (trunk) view
> generator is
> a) sharing a single copy of each document between all the map functions.
> b) permitting map functions to modify that copy, meaning that subsequent map
> functions might get a CORRUPTED version of the document.
>
> Verdict?
>
> For now, I've rewritten my map functions to clone the array to a local var
> before I call path.pop().
>
> -Patrick Barnes
>