You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Larry <co...@lroot.com> on 2009/08/26 23:18:45 UTC

Looking for help/ideas on how to fetch a Rank from a CouchDB based Highscores system

I currently have a a bunch of "high score" documents in couchdb. I'm trying
to find the best way for me to create a view that would allow me to easily
get the rank for a given score. As an example consider that I have 5 high
scores:

1) John, 100 pts
2) Jane, 90 pts
3) Bill, 85 pts
4) Jen, 70 pts
5) Bob, 55 pts

I have a function in my application called getRank( int $score) which would
return the rank (int) for the given score. So for example, using the scores
above:

getRank(30) == 6
getRank(71) == 4
getRank(101) == 1

My document structure for a highscore is:

{
   "_id": "0a68cdbe4a8d130662217f3abf4046bc",
   "_rev": "1-1264504508",
   "score": "000000000000000000000000000000100",
   "player_name": "John"
}

My initial approach was to create a view that does emit([doc._id,doc.score],
null). I Then query the view by setting my start key to the *top* of the
view list and set my end key to the value of the score I'm looking for. I
then count the results that are returned and derive the rank that way. This
is grossly inefficient I know and I'm having a hard time trying to find the
most optimal way to construct this. Any thoughts are ideas would be greatly
appreciated.

Thanks

larry

Re: Looking for help/ideas on how to fetch a Rank from a CouchDB based Highscores system

Posted by Matt Aimonetti <ma...@gmail.com>.
emit(doc.score, doc.player_name)

The doc id is always available. This way you can sort by score, add a skip
or a limit and everything should be fine. (you can even create a reduce
method to get averages etc..)

- Matt


On Wed, Aug 26, 2009 at 2:18 PM, Larry <co...@lroot.com> wrote:

> I currently have a a bunch of "high score" documents in couchdb. I'm trying
> to find the best way for me to create a view that would allow me to easily
> get the rank for a given score. As an example consider that I have 5 high
> scores:
>
> 1) John, 100 pts
> 2) Jane, 90 pts
> 3) Bill, 85 pts
> 4) Jen, 70 pts
> 5) Bob, 55 pts
>
> I have a function in my application called getRank( int $score) which would
> return the rank (int) for the given score. So for example, using the scores
> above:
>
> getRank(30) == 6
> getRank(71) == 4
> getRank(101) == 1
>
> My document structure for a highscore is:
>
> {
>   "_id": "0a68cdbe4a8d130662217f3abf4046bc",
>   "_rev": "1-1264504508",
>   "score": "000000000000000000000000000000100",
>   "player_name": "John"
> }
>
> My initial approach was to create a view that does
> emit([doc._id,doc.score],
> null). I Then query the view by setting my start key to the *top* of the
> view list and set my end key to the value of the score I'm looking for. I
> then count the results that are returned and derive the rank that way. This
> is grossly inefficient I know and I'm having a hard time trying to find the
> most optimal way to construct this. Any thoughts are ideas would be greatly
> appreciated.
>
> Thanks
>
> larry
>

Re: Looking for help/ideas on how to fetch a Rank from a CouchDB based Highscores system

Posted by Jeremy Wall <jw...@google.com>.
Are these rankings relative to other players or hard rankings?

On Wed, Aug 26, 2009 at 4:18 PM, Larry <co...@lroot.com> wrote:

> I currently have a a bunch of "high score" documents in couchdb. I'm trying
> to find the best way for me to create a view that would allow me to easily
> get the rank for a given score. As an example consider that I have 5 high
> scores:
>
> 1) John, 100 pts
> 2) Jane, 90 pts
> 3) Bill, 85 pts
> 4) Jen, 70 pts
> 5) Bob, 55 pts
>
> I have a function in my application called getRank( int $score) which would
> return the rank (int) for the given score. So for example, using the scores
> above:
>
> getRank(30) == 6
> getRank(71) == 4
> getRank(101) == 1
>
> My document structure for a highscore is:
>
> {
>   "_id": "0a68cdbe4a8d130662217f3abf4046bc",
>   "_rev": "1-1264504508",
>   "score": "000000000000000000000000000000100",
>   "player_name": "John"
> }
>
> My initial approach was to create a view that does
> emit([doc._id,doc.score],
> null). I Then query the view by setting my start key to the *top* of the
> view list and set my end key to the value of the score I'm looking for. I
> then count the results that are returned and derive the rank that way. This
> is grossly inefficient I know and I'm having a hard time trying to find the
> most optimal way to construct this. Any thoughts are ideas would be greatly
> appreciated.
>
> Thanks
>
> larry
>

Re: Looking for help/ideas on how to fetch a Rank from a CouchDB based Highscores system

Posted by Paul Davis <pa...@gmail.com>.
On Wed, Aug 26, 2009 at 6:59 PM, Paul Davis<pa...@gmail.com> 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

Re: Looking for help/ideas on how to fetch a Rank from a CouchDB based Highscores system

Posted by Paul Davis <pa...@gmail.com>.
> 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

Re: Looking for help/ideas on how to fetch a Rank from a CouchDB based Highscores system

Posted by Paul Davis <pa...@gmail.com>.
On Wed, Aug 26, 2009 at 5:18 PM, Larry<co...@lroot.com> wrote:
> I currently have a a bunch of "high score" documents in couchdb. I'm trying
> to find the best way for me to create a view that would allow me to easily
> get the rank for a given score. As an example consider that I have 5 high
> scores:
>
> 1) John, 100 pts
> 2) Jane, 90 pts
> 3) Bill, 85 pts
> 4) Jen, 70 pts
> 5) Bob, 55 pts
>
> I have a function in my application called getRank( int $score) which would
> return the rank (int) for the given score. So for example, using the scores
> above:
>
> getRank(30) == 6
> getRank(71) == 4
> getRank(101) == 1
>
> My document structure for a highscore is:
>
> {
>   "_id": "0a68cdbe4a8d130662217f3abf4046bc",
>   "_rev": "1-1264504508",
>   "score": "000000000000000000000000000000100",
>   "player_name": "John"
> }
>
> My initial approach was to create a view that does emit([doc._id,doc.score],
> null). I Then query the view by setting my start key to the *top* of the
> view list and set my end key to the value of the score I'm looking for. I
> then count the results that are returned and derive the rank that way. This
> is grossly inefficient I know and I'm having a hard time trying to find the
> most optimal way to construct this. Any thoughts are ideas would be greatly
> appreciated.
>
> Thanks
>
> larry
>

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

RE: Looking for help/ideas on how to fetch a Rank from a CouchDB based Highscores system

Posted by Kevin Ferguson <ke...@meebo-inc.com>.
If I understand you right, I would try this:

map:
emit(doc.score, 1)

reduce: return sum(v);

When you query on a particular key, you get the number of docs with that exact score.

When you query on a range, you get the number of docs with scores in that range.  So to find the rank of e.g. score 25, you could query startkey=26&endkey=99999999, which will return the number of docs that have 26 or higher.

hth

Kevin
________________________________________
From: larryroot@gmail.com [larryroot@gmail.com] On Behalf Of Larry [contact@lroot.com]
Sent: Wednesday, August 26, 2009 2:18 PM
To: user@couchdb.apache.org
Subject: Looking for help/ideas on how to fetch a Rank from a CouchDB based     Highscores system

I currently have a a bunch of "high score" documents in couchdb. I'm trying
to find the best way for me to create a view that would allow me to easily
get the rank for a given score. As an example consider that I have 5 high
scores:

1) John, 100 pts
2) Jane, 90 pts
3) Bill, 85 pts
4) Jen, 70 pts
5) Bob, 55 pts

I have a function in my application called getRank( int $score) which would
return the rank (int) for the given score. So for example, using the scores
above:

getRank(30) == 6
getRank(71) == 4
getRank(101) == 1

My document structure for a highscore is:

{
   "_id": "0a68cdbe4a8d130662217f3abf4046bc",
   "_rev": "1-1264504508",
   "score": "000000000000000000000000000000100",
   "player_name": "John"
}

My initial approach was to create a view that does emit([doc._id,doc.score],
null). I Then query the view by setting my start key to the *top* of the
view list and set my end key to the value of the score I'm looking for. I
then count the results that are returned and derive the rank that way. This
is grossly inefficient I know and I'm having a hard time trying to find the
most optimal way to construct this. Any thoughts are ideas would be greatly
appreciated.

Thanks

larry