Return-Path: Delivered-To: apmail-couchdb-dev-archive@www.apache.org Received: (qmail 16938 invoked from network); 19 Apr 2010 17:20:48 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 19 Apr 2010 17:20:48 -0000 Received: (qmail 51471 invoked by uid 500); 19 Apr 2010 17:20:47 -0000 Delivered-To: apmail-couchdb-dev-archive@couchdb.apache.org Received: (qmail 51398 invoked by uid 500); 19 Apr 2010 17:20:47 -0000 Mailing-List: contact dev-help@couchdb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@couchdb.apache.org Delivered-To: mailing list dev@couchdb.apache.org Received: (qmail 51390 invoked by uid 99); 19 Apr 2010 17:20:47 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 19 Apr 2010 17:20:47 +0000 X-ASF-Spam-Status: No, hits=0.1 required=10.0 tests=AWL,FREEMAIL_FROM,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,SPF_PASS,T_TO_NO_BRKTS_FREEMAIL X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of nrstott@gmail.com designates 209.85.221.172 as permitted sender) Received: from [209.85.221.172] (HELO mail-qy0-f172.google.com) (209.85.221.172) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 19 Apr 2010 17:20:42 +0000 Received: by qyk2 with SMTP id 2so5538743qyk.0 for ; Mon, 19 Apr 2010 10:20:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:received:message-id:subject:from:to:content-type; bh=9ILA22VqTNcvIX166Rx72JZz1mTe5ztY/EsOpbtz+GQ=; b=Nw7MkeZ9JAUAMsqpVNVW36evCpUxvUVFKgVWJR1LoFdmu5/D5sWysK7nn7xC3QQhdO KoAxtm5v9oap4u66ELjxqfbgJjnJGEf7z3F5iLp1ByJU86QP9ALamdldywmUCuF6iPWh vUmad0sevfagQVuSA/fGMAxv9Jp2mn9D78Bmw= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; b=d28W9/6IjH3BD0dilfhI1xbzyQJds0/C0Xn9KmuDSlHt4UuJ0KMIyCVVj2zP/uh839 CQDGIKJDverxu6WYYvZaDH85JkBx82XYZSdNYwNXcq/bkCnVgGnJSTrweuWL6msjdinb /VM3ljJ4wPrvR1D1yvmchdt1kRxuAAAl0ftD4= MIME-Version: 1.0 Received: by 10.229.251.8 with HTTP; Mon, 19 Apr 2010 10:20:15 -0700 (PDT) In-Reply-To: <4EDCD199-A285-4510-B8F0-E6030BA9553D@apache.org> References: <4EDCD199-A285-4510-B8F0-E6030BA9553D@apache.org> Date: Mon, 19 Apr 2010 13:20:15 -0400 Received: by 10.229.88.193 with SMTP id b1mr358555qcm.27.1271697616163; Mon, 19 Apr 2010 10:20:16 -0700 (PDT) Message-ID: Subject: Re: Idea for View & Bulk Insert combination for ad-hoc database changes From: Nathan Stott To: dev@couchdb.apache.org Content-Type: multipart/alternative; boundary=0016363b797aacaad304849a2ccf --0016363b797aacaad304849a2ccf Content-Type: text/plain; charset=ISO-8859-1 That would be a very cool feature to have in couch! How hard do you think it would be to write a patch that accomplished this the correct way? On Mon, Apr 19, 2010 at 12:45 PM, J Chris Anderson wrote: > > On Apr 18, 2010, at 12:01 PM, Michael Genereux wrote: > > > Reposting this from the user list. Figured it belongs in the dev list. > > > > Sorry to leave you hanging for so long. (Vacation weekend!) > > There are 2 reasons CouchDB doesn't have this feature. The important one is > that it could easily give new users the wrong idea. Coming from a RDBMS > background you might expect something like this to be transactional. In > Couch, it wouldn't be. > > Generally in Couch we've pushed work to the database client. When the > client is running on the same box as CouchDB, there would be 0 savings in > processing overhead to run these transforms on the server, vs using a > application script. > > The second reason is the amount of code it would require. If someone wants > to write it, there's a decent chance it'd be accepted as a patch (if it were > done right.) > > Here's how I think it could be implemented (actually a more generic feature > that can be used for lots of things.) > > A design document could have a ddoc.changes function, which subscribes to > the changes feed of the database it runs in. The changes function could then > do anything it wanted, like send an email anytime a doc is saved that has a > doc.send_me_as_email = true field. > > Your use case can be accomplished by having a function that watches to see > if any of the docs have a price that hasn't yet been updated, and change the > price. So it could load the document, change the price, and set a flag on > the document that says doc.price_changed = true. It would ignore any > documents that already had price_changed. > > This is almost exactly how you would accomplish this at the application > level. The only reason to pull such an operation into the design document is > that CouchDB would take care of keeping it running, and that there would be > a standard way to author a changes listener. > > I'm not sure the status of it (node.js is a moving target) but Mikeal has > something much like this already on github: > > http://github.com/mikeal/node.couch.js/ > > I'd suggest understanding how this works (and why it's like it is) before > thinking about extending CouchDB. The non-transactional nature of CouchDB > means you need to understand the _changes feed before you can think about > doing anything "complete" like updating all docs that match a given pattern. > > Chris > > > > I was doing some ad-hoc UPDATEs and DELETEs on a SQL database the > > other day and it crossed my mind, how could I do the same on CouchDB? > > I don't want to write an application to do something so simple. It > > seems to me that I should be able to produce a Javascript views, both > > named and temporary, that in turn produce results that get imported > > into the CouchDB Bulk Document API as a single transaction. > > > > Here's the update example for a 10% increase in prices: > > function( doc ) { > > doc.price = doc.price * 1.1; > > emit( null, doc ) > > } > > > > Here's the delete example for old records: > > function( doc ) { > > if( doc.year == 2009 ) { > > doc._deleted = true; > > emit( null, doc ) > > } > > } > > > > Also, much like INSERT ... SELECT notation, this could be used to copy > > records. No need for the non-http compliant COPY method that does the > > same on a single record level. Changes can be made to the copy on the > > fly. Very efficient since the duplication and the update occur in one > > transaction. > > > > Example to duplicate all 'foo' widgets to a set of 'bar' widgets: > > function( doc ) { > > if( doc.widget_type == 'foo' ) { > > reset( doc ); // helper function that does: > > // delete( doc._id ); delete( doc._rev ); > > // and any other special vars in future > > doc.widget_type = 'bar' > > emit( null, doc ) > > } > > } > > > > I like not having to learn another command, so I reused emit even > > though bulk api won't use the first field. If this feature existed, > > this is how I would have expected to use it knowing that the key > > parameter has no value in this use of a view. > > > > I would love some feedback on this. > > > > Michael > > --0016363b797aacaad304849a2ccf--