incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Chris Anderson <jch...@apache.org>
Subject Re: Best practice for date dependend maps
Date Fri, 07 Aug 2009 16:51:45 GMT
On Fri, Aug 7, 2009 at 2:15 AM, Mario Müller<mario.mueller.mac@me.com> wrote:
> Hi folks,
>
> I'm new to couchdb (so please don't hurt me ;)). I want to build a view of
> valid documents. Valid, in this case, means that those documents have a
> "valid_from" and a "valid_until" date and "now" must be within this range.

This is a bit tough when edge cases are taken into account.
Essentially you want to query this view to find all events that have
started, but not yet ended. The naive approach is to emit "start" and
"end" times, and then do an intersection. However, with long events
and lots of them, you end up dealing with a lot of data. eg: all
events that started before now, regardless of if they've ended, and
vice versa. If you want to put a bound on that query, you have to know
the maximum duration that events can have in your system.

A simpler approach is to chose a time-granularity for your system. If
you'll never display a window of less than 24 hrs, then you can have
each event emit a "beacon" for every day that it is ongoing. Or pick a
smaller or larger granularity depending on your app. You'll end up
with more data in your view but the queries become greatly simplified.

Something like:

var time = new Date(doc.start)
var end = new Date(doc.end);
while (time < end) {
  emit(time, foo);
  time = time + 24*3600;
}

>
> I've written a map script to do the work for me, but I do not know if this
> is a good way for couchdb...? The dates are in mysql datetime format (for
> historical reason)
>
> function(doc) {
>   var convert = {
>       "do": function(timestamp) {
>           var regex = /^([0-9]{2,4})-([0-1][0-9])-([0-3][0-9])
> (?:([0-2][0-9]):([0-5][0-9]):([0-5][0-9]))?$/;
>           var parts = timestamp.replace(regex, "$1 $2 $3 $4 $5 $6").split('
> ');
>           return new Date(parts[0], parts[1] - 1, parts[2], parts[3],
> parts[4], parts[5]).getTime();
>       },
>   };
>   var iT = new Date().getTime();
>   var iS = convert.do(doc.valid_from);
>   var iEnd = convert.do(doc.valid_unitl);
>   if (iS < iT < iEnd) {
>       emit(doc._id, null);
>   }
> }
>
> I'll be thankful for any kind of comment.
>
> Thanks!
> Mario
> ------------------------------------
> Mario Mueller
> Ellerstraße 130
> 40227 Duesseldorf, Germany
>
> mail. mario.mueller.mac@me.com
> phon. 0049 176 83016418
> icq.     436092688
> xing.  http://tinyurl.com/MarioMueller
> blog.  http://tinyurl.com/MariosBlog
>
> Attached to this mail you may find a PGP Key file (PGP.sig).
> If you are not using PGP anyway you can ignore it.
>
>



-- 
Chris Anderson
http://jchrisa.net
http://couch.io

Mime
View raw message