Return-Path: Delivered-To: apmail-incubator-couchdb-user-archive@locus.apache.org Received: (qmail 98007 invoked from network); 17 Oct 2008 19:12:51 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 17 Oct 2008 19:12:51 -0000 Received: (qmail 44510 invoked by uid 500); 17 Oct 2008 19:12:51 -0000 Delivered-To: apmail-incubator-couchdb-user-archive@incubator.apache.org Received: (qmail 44369 invoked by uid 500); 17 Oct 2008 19:12:51 -0000 Mailing-List: contact couchdb-user-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: couchdb-user@incubator.apache.org Delivered-To: mailing list couchdb-user@incubator.apache.org Received: (qmail 44358 invoked by uid 99); 17 Oct 2008 19:12:50 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 17 Oct 2008 12:12:50 -0700 X-ASF-Spam-Status: No, hits=-0.0 required=10.0 tests=SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of kowsik@gmail.com designates 74.125.92.145 as permitted sender) Received: from [74.125.92.145] (HELO qw-out-1920.google.com) (74.125.92.145) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 17 Oct 2008 19:11:44 +0000 Received: by qw-out-1920.google.com with SMTP id 4so289058qwk.54 for ; Fri, 17 Oct 2008 12:12:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:mime-version:content-type:content-transfer-encoding :content-disposition; bh=/utLc3+Zqz6o44Gmrt0HsShja9w+PcqtkynnN7FNdQk=; b=nekQHq8yZ0IHykHSu1JIbDuJT90cuzOgGHooRr0zOLfva6MEibWsnS/mcSTO8kZGKQ Ls4FG7hzmt7RP6kNNQMXmyR86NghDVskwTXDF2RFNacek55gmmivGyig8ccW11VcYXJk 3EausHpGb/fpxLbNGF4qLe2IFOmIgwdIuH/EE= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:mime-version:content-type :content-transfer-encoding:content-disposition; b=BCS7h5nB+/ECHYUG2ICgcHDjbaDXu50o7UYNpcpIRnyuChU3P8vP91dxkwRQKsUQHd +ue4M4UtY65mQdLavM4EmSeBrh6u8fxuhx87b8BtZ44bCujWzcRuJhtZZ1Zf1+k1+uS/ AFvdmRHHT2l8MrcF69oMHDzF4y/m3yXAma2E0= Received: by 10.214.11.18 with SMTP id 18mr5780007qak.82.1224270741878; Fri, 17 Oct 2008 12:12:21 -0700 (PDT) Received: by 10.214.12.19 with HTTP; Fri, 17 Oct 2008 12:12:21 -0700 (PDT) Message-ID: <7db9abd30810171212x2de0d43fk4a3542fa7a7a8091@mail.gmail.com> Date: Fri, 17 Oct 2008 12:12:21 -0700 From: kowsik To: couchdb-user@incubator.apache.org Subject: Note to self... MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Content-Disposition: inline X-Virus-Checked: Checked by ClamAV on apache.org This might be obvious to most of you, but wasn't for me. Just thought I'll share. I have a bunch of design views that come in pairs: count_of_blah and values_for_blah where the only difference is as follows: count_of_blah: map => emit(doc.key, 1); reduce => return sum(values); values_for_blah: map => emit(doc.key, doc.value); reduce => return values; There was just a little too much repetition in the code (not to mention the extra pass over the documents to generate the indices) since for the most part the differences were in the emit and reduce functions. Well, until I figured out that the reduce function gets invoked with __all__ of the keys and values, which means I can build both types of indexes in one go like so: map => function(doc) { emit([ "count", doc.key ], doc.value); emit([ "list", doc.key ], doc.value); } reduce => function(keys, values) { if (keys) { var element = keys[0]; // keys is an array of __all__ emit's that share the same key (but different doc._id) var emit_key = element[0]; // each element is an array of the actual emitted key + the doc._id if (emit_key[0] == "count") { return values.length; } } return values; } Now to get the count of some "foo", I just have to invoke: /_view/blah/count_of_blah?group=true&key=["count","foo"] And to get all the values for 'foo': /_view/blah/count_of_blah?group=true&key=["list","foo"] Anyways, saved myself from a lot of repetition and I now have half the number of design docs, which hopefully also means that the indexing takes lesser time. Is there an alternate, better of doing this? K.