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/07 17:49:49 UTC

[Erlang] Returning JSON from a List

Hi,

I made great progress converting Javascript shows to Erlang. I need some
help with lists in Erlang. I have a JSON document that has pointers to other
documents (with include_docs=true). Within the list, I construct a new JSON
document that aggregates information I pulled from the documents that were
referenced. Below you'll find the Javascript implementation. I am having
trouble reproducing this in Erlang.

This is what I have in Javascript:

function(head, req) {

start({
"headers" : {
"Content-Type" : "text/plain"
}
});

var page = [];

while( row = getRow()) {
var module = row.value.module;
var desc = row.doc.desc;
var title = row.doc.title;
 var module = {
"module" : module,
"desc" : desc,
"title" : title
};
page.push(module);
}
send(JSON.stringify(page));

}

I am starting from what's in the unit test:

%% Page generator (from Futon unit test)
fun(Head, {Req}) ->
Send(<<"head">>),
Fun = fun({Row}, _) ->
Val = couch_util:get_value(<<"value">>, Row, -1),
Send(list_to_binary(integer_to_list(Val))),
{ok, nil}
end,
{ok, _} = FoldRows(Fun, nil),
<<"tail">>
end.

In a show I was able to construct a new JSON document and then return it
with:

{[{<<"code">>, 200}, {<<"headers">>, {[]}}, {<<"json">>, MyDoc}]}

How can I do something similar where I iterate through the rows, take some
data from each row and then send the result as a single JSON document?

Thank you!

Thomas

Re: [Erlang] Returning JSON from a List

Posted by Thomas Van de Velde <th...@gmail.com>.
The point of shows and lists?

OK. So I spent a lot of time learning Erlang. Now that I got shows to work,
I am noticing that the performance is still many times slower than getting a
document straight from Couch and processing it in Java. Javascript shows are
unusable when you're expecting to scale to significant numbers. Here's what
I get:
Javascript: 60 tx/sec
Erlang: 300 tx/sec
No show: 1000 tx/sec
No show + Java middle tier: 800 tx/sec

I wish I knew about this before.

Thomas
On Oct 8, 2011 8:13 AM, "Thomas Van de Velde" <th...@gmail.com> wrote:

> So is this possible? I found no way to access the data sent from within
> the Fun = fun({Row}, _) -> function outside that function block. FoldRows
> seems to return whatever was sent within the rows iterator directly to the
> client. How can I capture that output so it can be modified to construct my
> own JSON object? Thanks!
>
> On Fri, Oct 7, 2011 at 10:49 AM, Thomas Van de Velde <thomasevdv@gmail.com
> > wrote:
>
>> Hi,
>>
>> I made great progress converting Javascript shows to Erlang. I need some
>> help with lists in Erlang. I have a JSON document that has pointers to other
>> documents (with include_docs=true). Within the list, I construct a new JSON
>> document that aggregates information I pulled from the documents that were
>> referenced. Below you'll find the Javascript implementation. I am having
>> trouble reproducing this in Erlang.
>>
>> This is what I have in Javascript:
>>
>> function(head, req) {
>>
>> start({
>> "headers" : {
>>  "Content-Type" : "text/plain"
>> }
>> });
>>
>> var page = [];
>>
>> while( row = getRow()) {
>> var module = row.value.module;
>>  var desc = row.doc.desc;
>> var title = row.doc.title;
>>  var module = {
>> "module" : module,
>> "desc" : desc,
>>  "title" : title
>> };
>> page.push(module);
>>  }
>> send(JSON.stringify(page));
>>
>> }
>>
>> I am starting from what's in the unit test:
>>
>> %% Page generator (from Futon unit test)
>> fun(Head, {Req}) ->
>> Send(<<"head">>),
>>  Fun = fun({Row}, _) ->
>> Val = couch_util:get_value(<<"value">>, Row, -1),
>>  Send(list_to_binary(integer_to_list(Val))),
>> {ok, nil}
>> end,
>>  {ok, _} = FoldRows(Fun, nil),
>> <<"tail">>
>> end.
>>
>> In a show I was able to construct a new JSON document and then return it
>> with:
>>
>> {[{<<"code">>, 200}, {<<"headers">>, {[]}}, {<<"json">>, MyDoc}]}
>>
>> How can I do something similar where I iterate through the rows, take some
>> data from each row and then send the result as a single JSON document?
>>
>> Thank you!
>>
>>  Thomas
>>
>
>

Re: [Erlang] Returning JSON from a List

Posted by Thomas Van de Velde <th...@gmail.com>.
So is this possible? I found no way to access the data sent from within the Fun
= fun({Row}, _) -> function outside that function block. FoldRows seems to
return whatever was sent within the rows iterator directly to the client.
How can I capture that output so it can be modified to construct my own JSON
object? Thanks!

On Fri, Oct 7, 2011 at 10:49 AM, Thomas Van de Velde
<th...@gmail.com>wrote:

> Hi,
>
> I made great progress converting Javascript shows to Erlang. I need some
> help with lists in Erlang. I have a JSON document that has pointers to other
> documents (with include_docs=true). Within the list, I construct a new JSON
> document that aggregates information I pulled from the documents that were
> referenced. Below you'll find the Javascript implementation. I am having
> trouble reproducing this in Erlang.
>
> This is what I have in Javascript:
>
> function(head, req) {
>
> start({
> "headers" : {
>  "Content-Type" : "text/plain"
> }
> });
>
> var page = [];
>
> while( row = getRow()) {
> var module = row.value.module;
>  var desc = row.doc.desc;
> var title = row.doc.title;
>  var module = {
> "module" : module,
> "desc" : desc,
>  "title" : title
> };
> page.push(module);
>  }
> send(JSON.stringify(page));
>
> }
>
> I am starting from what's in the unit test:
>
> %% Page generator (from Futon unit test)
> fun(Head, {Req}) ->
> Send(<<"head">>),
>  Fun = fun({Row}, _) ->
> Val = couch_util:get_value(<<"value">>, Row, -1),
>  Send(list_to_binary(integer_to_list(Val))),
> {ok, nil}
> end,
>  {ok, _} = FoldRows(Fun, nil),
> <<"tail">>
> end.
>
> In a show I was able to construct a new JSON document and then return it
> with:
>
> {[{<<"code">>, 200}, {<<"headers">>, {[]}}, {<<"json">>, MyDoc}]}
>
> How can I do something similar where I iterate through the rows, take some
> data from each row and then send the result as a single JSON document?
>
> Thank you!
>
> Thomas
>