couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul Davis <paul.joseph.da...@gmail.com>
Subject Re: Reducing to unique values...
Date Sat, 28 Feb 2009 01:10:11 GMT
On Fri, Feb 27, 2009 at 8:06 PM, Ben Browning <ben324@gmail.com> wrote:
> Does something as simple as this work?
>

Only if the size of the uniques array is bounded in the size it grows.
Reduce output sizes must not grow faster than approximately log(# keys
reduced).

> map:
> // This assumes "a" is in a doc field called key and "one" is in a
> field called value
> function(doc) {
>  emit(doc.key, doc.value);
> }
>
> reduce:
> function(keys, values, rereduce) {
>  // rereduce and regular reduce are the
>  // same code here since they both just operate
>  // on the values array and return an array
>  var uniques = [];
>  for (var i = 0; i < values.length; i++) {
>    if (uniques.indexOf(values[i]) < 0) {
>      uniques.push(values[i]);
>    }
>  }
> return uniques;
> }
>
> We just construct an array of unique values in the reduce. My simple
> testing shows this returns reduce values like
>
> {"rows":[{"key":"a","value":["two","one"]},{"key":"b","value":["three"]}]}
>
> for map values of
>
> {"total_rows":6,"offset":0,"rows":[
> {"id":"34ab35d58a1540a24cea02aa6b97d2a6","key":"a","value":"one"},
> {"id":"41099d0591233670250ebf146983cade","key":"a","value":"one"},
> {"id":"70bd1f511ede201d93655f896ad4c99f","key":"a","value":"one"},
> {"id":"c65afdfddfb3ea6a33cfd20e67364d11","key":"a","value":"two"},
> {"id":"3b06e1ee7e63cd856aae52513f11bc51","key":"b","value":"three"},
> {"id":"611a47c396492eb7b88190e81836427b","key":"b","value":"three"}
> ]}
>

Mime
View raw message