You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Thomas Van de Velde <th...@gmail.com> on 2011/10/03 02:52:14 UTC
Emiting a Doc from Erlang View
Hi,
How can I emit the document as part of an Erlang view? Emit(Something,*Doc*)
doesn't work.
Is there a good sample app or set of unit tests to look though? I looked at
the test that ships with Futon but it doesn't show this.
Thanks!
Thomas
Re: Emiting a Doc from Erlang View
Posted by Damian Dobroczyński <qo...@gmail.com>.
W dniu 04.10.2011 15:34, Thomas Van de Velde pisze:
> Damian,
>
> You can simply do this to emit a document from a view:
>
> %% Map Function
> fun({Doc}) ->
> ItemId = couch_util:get_value(<<"_id">>, Doc),
> Emit(ItemId,{Doc})
> end.
>
Yep, I've just learned (after to hours of struggling...). CouchDb
encodes JSON in Erlang as a list of two-element tuples packed into a
tuple ;)
> On Tue, Oct 4, 2011 at 1:58 AM, Paul Davis <pa...@gmail.com>wrote:
>
>> 2011/10/4 Damian Dobroczyński <qo...@gmail.com>:
>>> W dniu 03.10.2011 02:52, Thomas Van de Velde pisze:
>>>> Hi,
>>>>
>>>> How can I emit the document as part of an Erlang view?
>> Emit(Something,*Doc*)
>>>> doesn't work.
>>>>
>>>> Is there a good sample app or set of unit tests to look though? I looked
>> at
>>>> the test that ships with Futon but it doesn't show this.
>>>>
>>>> Thanks!
>>>>
>>>> Thomas
>>>>
>>>
>>> Still, nobody's answered this question ... :D
>>> So, is it possible at all?
>>>
>>> -- D.
>>>
>>>
>>
>> Of course. Pretty sure answered in a separate thread related to this.
>>
>> Answer: Same as in JavaScript. Just emit it.
>>
>
Re: Emiting a Doc from Erlang View
Posted by Thomas Van de Velde <th...@gmail.com>.
Damian,
You can simply do this to emit a document from a view:
%% Map Function
fun({Doc}) ->
ItemId = couch_util:get_value(<<"_id">>, Doc),
Emit(ItemId,{Doc})
end.
On Tue, Oct 4, 2011 at 1:58 AM, Paul Davis <pa...@gmail.com>wrote:
> 2011/10/4 Damian Dobroczyński <qo...@gmail.com>:
> > W dniu 03.10.2011 02:52, Thomas Van de Velde pisze:
> >> Hi,
> >>
> >> How can I emit the document as part of an Erlang view?
> Emit(Something,*Doc*)
> >> doesn't work.
> >>
> >> Is there a good sample app or set of unit tests to look though? I looked
> at
> >> the test that ships with Futon but it doesn't show this.
> >>
> >> Thanks!
> >>
> >> Thomas
> >>
> >
> > Still, nobody's answered this question ... :D
> > So, is it possible at all?
> >
> > -- D.
> >
> >
>
> Of course. Pretty sure answered in a separate thread related to this.
>
> Answer: Same as in JavaScript. Just emit it.
>
Re: Emiting a Doc from Erlang View
Posted by Paul Davis <pa...@gmail.com>.
2011/10/4 Damian Dobroczyński <qo...@gmail.com>:
> W dniu 03.10.2011 02:52, Thomas Van de Velde pisze:
>> Hi,
>>
>> How can I emit the document as part of an Erlang view? Emit(Something,*Doc*)
>> doesn't work.
>>
>> Is there a good sample app or set of unit tests to look though? I looked at
>> the test that ships with Futon but it doesn't show this.
>>
>> Thanks!
>>
>> Thomas
>>
>
> Still, nobody's answered this question ... :D
> So, is it possible at all?
>
> -- D.
>
>
Of course. Pretty sure answered in a separate thread related to this.
Answer: Same as in JavaScript. Just emit it.
Re: Emiting a Doc from Erlang View
Posted by Damian Dobroczyński <qo...@gmail.com>.
W dniu 03.10.2011 02:52, Thomas Van de Velde pisze:
> Hi,
>
> How can I emit the document as part of an Erlang view? Emit(Something,*Doc*)
> doesn't work.
>
> Is there a good sample app or set of unit tests to look though? I looked at
> the test that ships with Futon but it doesn't show this.
>
> Thanks!
>
> Thomas
>
Still, nobody's answered this question ... :D
So, is it possible at all?
-- D.
Re: Emiting a Doc from Erlang View
Posted by Jason Smith <jh...@iriscouch.com>.
On Mon, Oct 3, 2011 at 11:50 PM, Jens Alfke <je...@couchbase.com> wrote:
>
> On Oct 3, 2011, at 4:53 AM, Thomas Van de Velde wrote:
>
> I thought include_docs only works when you have a reduce function defined as well.
>
> Nope, it always works. But if you’re going to use it all the time, it’s faster to just emit the document into the view, as that saves a b-tree lookup per row every time the view is fetched.
include_docs does not always work. However, IMO Jens gives good
advice. For 99% of developers, 99% of the time, it works as expected.
include_docs just more syntactic sugar where CouchDB does the same
thing a client would do: no more, no less.
The well-known syntactic sugar is _update functions. They are not
magic, they are the same fetch/modify/store cycle we all know. Since
they are very low-latency, they rarely get 409 conflicts, but it does
happen!
include_docs is the same. There is a low-latency lookup by doc ID but
it is not magic either. If you emit {_id:"doc_id"} then CouchDB will
fetch the latest revision (like GET /db/doc_id). If you are very
unlucky, or if there is a high update frequency, you could get a
different revision from that which caused the view row.
If you emit {_id:"doc_id", _rev:"rev-token"} then it will fetch
exactly the revision specified (like GET /db/doc_id?rev=rev-token),
which might be missing if it was compacted away.
In both cases, the document might be deleted during the interim as
well. None of these concerns apply if you emit the doc directly into
the view row. Instead, you get more disk usage, so it is a trade-off.
--
Iris Couch
Re: Emiting a Doc from Erlang View
Posted by Jens Alfke <je...@couchbase.com>.
On Oct 3, 2011, at 4:53 AM, Thomas Van de Velde wrote:
I thought include_docs only works when you have a reduce function defined as well.
Nope, it always works. But if you’re going to use it all the time, it’s faster to just emit the document into the view, as that saves a b-tree lookup per row every time the view is fetched.
—Jens
Re: Emiting a Doc from Erlang View
Posted by Thomas Van de Velde <th...@gmail.com>.
I thought include_docs only works when you have a reduce function defined as
well.
On Sun, Oct 2, 2011 at 8:28 PM, Keith Gable <zi...@ignition-project.com>wrote:
> I wouldn't emit a document in a view because you can always use
> include_docs
> to get the doc.
> On Oct 2, 2011 7:52 PM, "Thomas Van de Velde" <th...@gmail.com>
> wrote:
> > Hi,
> >
> > How can I emit the document as part of an Erlang view?
> Emit(Something,*Doc*)
> > doesn't work.
> >
> > Is there a good sample app or set of unit tests to look though? I looked
> at
> > the test that ships with Futon but it doesn't show this.
> >
> > Thanks!
> >
> > Thomas
>
Re: Emiting a Doc from Erlang View
Posted by Keith Gable <zi...@ignition-project.com>.
I wouldn't emit a document in a view because you can always use include_docs
to get the doc.
On Oct 2, 2011 7:52 PM, "Thomas Van de Velde" <th...@gmail.com> wrote:
> Hi,
>
> How can I emit the document as part of an Erlang view?
Emit(Something,*Doc*)
> doesn't work.
>
> Is there a good sample app or set of unit tests to look though? I looked
at
> the test that ships with Futon but it doesn't show this.
>
> Thanks!
>
> Thomas