On Wed, Aug 26, 2009 at 6:59 PM, Paul Davis<[email protected]> wrote:
>> First, change your scores to a numeric instead of a string to make math 
>> easier.
>>
>> map:
>> function(doc) {
>>    emit(doc.score, doc.interesting_info);
>> }
>>
>> getRank(score):
>>    SCORE = user_score+min_score_increment
>>    results = 
>> GET("http://127.0.0.1:5984/score_db/_design/high_scores/_view/find_range?startkey=$(SCORE)")
>>    return results.offset
>>
>> HTH,
>> Paul Davis
>>
>
> Whoops! Totally forgot that we wanted this descending.
>
> getRank(score):
>    results = 
> GET("http://127.0.0.1:5984/score_db/_design/high_scores/_view/find_rank?startkey=$(score)&descending=true")
>    return results.total_rows - results.offset + 1
>
> Pretty sure that math is right. Also, I didn't double check that
> offsets are calculated in reverse from the beginning. Ie, for
> ?descending=true&limit=0, offset==total_rows I don't think we do the
> math inside CouchDB, but I'm too distracted to look. It'd be a simple
> test.
>
> HTH,
> Paul Davis
>

Double doh! If you're not on trunk, or 0.10 when it gets released then
you'll have a special case when the input score is so low that no
results are returned. In that case offset is never calculated, but
that's simple enough to account for with:

return result.total_rows - (results.offset || 0) + 1

HTH,
Paul Davis

Reply via email to