Return-Path: Delivered-To: apmail-couchdb-user-archive@www.apache.org Received: (qmail 64463 invoked from network); 10 Aug 2009 19:30:37 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 10 Aug 2009 19:30:37 -0000 Received: (qmail 71046 invoked by uid 500); 10 Aug 2009 19:30:43 -0000 Delivered-To: apmail-couchdb-user-archive@couchdb.apache.org Received: (qmail 70963 invoked by uid 500); 10 Aug 2009 19:30:43 -0000 Mailing-List: contact user-help@couchdb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: user@couchdb.apache.org Delivered-To: mailing list user@couchdb.apache.org Received: (qmail 70953 invoked by uid 99); 10 Aug 2009 19:30:43 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 10 Aug 2009 19:30:43 +0000 X-ASF-Spam-Status: No, hits=-0.0 required=10.0 tests=SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of jchris@gmail.com designates 209.85.212.177 as permitted sender) Received: from [209.85.212.177] (HELO mail-vw0-f177.google.com) (209.85.212.177) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 10 Aug 2009 19:30:32 +0000 Received: by vws7 with SMTP id 7so2970300vws.29 for ; Mon, 10 Aug 2009 12:30:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:sender:received:in-reply-to :references:date:x-google-sender-auth:message-id:subject:from:to :content-type:content-transfer-encoding; bh=D0VnLSTspUQLj8Qt9F6ZCih2dtgPfuAVhnw6mxlEhzw=; b=D5dyvOLkh8QeavmZ1Yk9xetre+/jzIxio86Y+nBJO5XxuhpQ5oW/E0gksxtGjWza3l hh5OqmZ0A2ZmhttAntrL+Aigajt0I4dNDBCHOgoBr2Y+/bUguMsudZGT4SbzCwVlVC85 l6lMWndRAw0df+YPA6V8bXP5s3RnurFqFx6Og= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:content-type :content-transfer-encoding; b=NibetMb32bWJ2nyPAGn9Jt2Fgz2j5phpKr2R+YXmjuu8UVTNRHTUdL/ov1RwgVDD4g hS5pOyxCRU6TDq73kJpjaWIpZpT9E6tl8DTwPDYKAixci1A6pdIn0cIPTL3LeWry28Pq wInusvhWaMUNjM34tlsSZoz0TAa+1jLD7yV90= MIME-Version: 1.0 Sender: jchris@gmail.com Received: by 10.220.98.198 with SMTP id r6mr5348175vcn.3.1249932608422; Mon, 10 Aug 2009 12:30:08 -0700 (PDT) In-Reply-To: References: Date: Mon, 10 Aug 2009 12:30:08 -0700 X-Google-Sender-Auth: 21221732aea57f48 Message-ID: Subject: Re: pmap and document update example From: Chris Anderson To: user@couchdb.apache.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Virus-Checked: Checked by ClamAV on apache.org On Mon, Aug 10, 2009 at 8:01 AM, Norman Barker wro= te: > Hi, > > I commented on this in a recent thread, a couple of minutes later I > had a working example (compliments to the modular design of couchdb > and the easy set up with an ini file). > > Folloing Joe Armstrong's book on pmap I have > > update_db(JsonObject, Db)-> > =A0 =A0NewDoc =3D couch_doc:from_json_obj(JsonObject), > =A0 =A0DocId =3D couch_util:new_uuid(), > =A0 =A0{ok, _NewRev} =3D couch_db:update_doc(Db, NewDoc#doc{id=3DDocId}, = []). > You should just change this function to accept a list of docs. couch_db:update_docs takes a list. Writing a batch of docs in serial is much faster than doing it in parallel would be (if you could even get it to work). > > pmap(F, L, Db) -> > =A0 =A0S =3D self(), > =A0 =A0%% make_ref() returns a unique reference > =A0 =A0%% we'll match on this later > =A0 =A0Ref =3D erlang:make_ref(), > =A0 =A0Pids =3D map(fun(I) -> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 spawn(fun() -> do_f(S, Ref, F= , I, Db) end) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 end, L), > =A0 =A0%% gather the results > =A0 =A0gather(Pids, Ref). > > do_f(Parent, Ref, F, I, Db) -> > =A0 =A0Parent ! {self(), Ref, (catch F(I, Db))}. > > gather([Pid|T], Ref) -> > =A0 =A0receive > =A0 =A0 =A0 =A0 =A0{Pid, Ref, Ret} -> [Ret|gather(T, Ref)] > =A0 =A0end; > > gather([], _) -> > =A0 =A0[]. > > > and then I invoke this with > > =A0 =A0{FormResp} =3D my_update_query_servers:render_update(Lang, UpFun, > nil, nil, Req, Db), > =A0 =A0case proplists:get_value(<<"results">>, FormResp, nil) of > =A0 =A0 =A0 =A0undefined -> > =A0 =A0 =A0 =A0 =A0 =A0throw({error, no_result}); > =A0 =A0 =A0 =A0ResultList -> > =A0 =A0 =A0 =A0 =A0 =A0Res =3D pmap(fun update_db/2, ResultList, Db) > =A0 =A0end, > > > is there something bad with this? my_update_query_servers returns a > collection of JSON objects and this seems to me to be a parallel > write. =A0Since I gather all the references I should be able to update > the caller with the list of created documents? > > Norman > --=20 Chris Anderson http://jchrisa.net http://couch.io