views:

101

answers:

2

I understand the sharded counter, here: http://code.google.com/appengine/articles/sharding_counters.html The problem is that a simple counter will not work in my application. I am sorting my entities by a particular variable so I am returned not so much a count, but more of a rank. My current method is:

SELECT COUNT(this) FROM Entity.class WHERE value <= ?

Result + 1 is then the rank of the parameter in relation to the value variable in the persistent Entity objects. The limitation of this is the highest rank being returned is 1001 because count() can give a maximum of 1000. The reason I cannot store the rank on the Entity object is that the ranks are updated very often, and re-setting this rank variable would be much too costly.

Any ideas on the best way to accomplish this?

A: 

You might want to consider adapting something like the google-appengine-ranklist project to Java. It builds a tree of 'ranker' nodes, such that the nth ranked item can be found in O(log n) time, and updates are likewise O(log n).

Nick Johnson
A: 

I would create a class with a static variable to hold the current count value. E.g., you could create something like the following:

public class Counter {
    private static int counter = -1;

    public static synchronized int getNextValue() {
    if (counter == -1) {                
            counter = // Insert code that retrieves the highest value from Google datastore
    }
    counter++;
    return counter;
    }
}
Andrew Dyster