incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jeremy Wall <jw...@google.com>
Subject Re: Reduce to nothing
Date Thu, 05 Feb 2009 16:26:34 GMT
map
function
On Thu, Feb 5, 2009 at 8:50 AM, Matt Goodall <matt.goodall@gmail.com> wrote:

> 2009/2/5 Jeremy Wall <jwall@google.com>:
> > Can't you just have the map screen out the cancellations? Seems like
> that's
> > the most natural spot for the behaviour you want.
>
> Nope, because the "thing" doesn't know it's been cancelled.
>
> - Matt
>
>
> >
> > On Feb 5, 2009 8:34 AM, "Matt Goodall" <matt.goodall@gmail.com> wrote:
> >
> > Hi,
> >
> > Is it possible to reduce a key to nothing, i.e. completely remove a
> > key from the reduction result.
> >
> > For instance, say you post three documents:
> >
> >   {"_id": "thing1", "type": "thing"}
> >   {"_id": "thing2", "type": "thing"}
> >   {"_id": "...", "type": "cancellation", "cancels": "thing1"}
> >
> > It's trivial to produce a map function that collates the "thing" and
> > "cancellation" documents. However, I can't work out how, or even it
> > it's possible, to reduce the view that so that only "thing2" remains.
> >
> > This seems like it might be a useful thing to be able to do at times.
> > For instance, posting a "cancellation" document effectively removes a
> > "thing" from the database. Nothing needs to touch the "thing",
> > avoiding any chance of conflicts and it doesn't matter how many
> > "cancellation" documents get posted making cancellation an idempotent
> > operation.
> >
> > I tried not returning anything, just in case it worked ;-), but got a
> > JSON encoding error (can't encode undefined, iirc). That would be a
> > simple approach. However, I wondered if the more "normal" approach of
> > allowing a reduce function to emit zero or more (key, value) pairs
> > would be even better? Not sure if that would break the incremental
> > reduce though.
> >
> > Any ideas, or is there another way to achieve this that I'm missing?
> >
> > - Matt
> >
>
map:
function(doc) {
  if (doc.cancels)
    emit(doc.cancels, [canceled]);
  emit(doc._id, []);
}

reduce:
function(key, values, reduce) {
    //if values has canceled then
    return []
    //otherwise
    return values;
}

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message