My goal is to compute the absolute rank of an entity based on some attribute provided as a string. The basic approach I am using is to issue a query, sort its results in descending order and count all those results which are greater than the attribute value of my particular entity. The query looks something like this
int rank = o.query(Entity.class).order(String.format("-%s",attribute)).
.filter(String.format("%s >", attribute), something).count();
However, something is the part where I am stuck. As far as I understand the concepts of objectify, querying for specific entity members is out of the question. My next step would either be to use an (ugly but fast in terms of dev time) if-construct to identify the particular entity member, or start using reflection (a bit less ugly, but slow in terms of dev time).
Either way, I am left with the feeling that I miss some obvious and/or elegant way to accomplish this task. Any suggestions? Thx.
If I understand this correctly, you want the first entity and you want the count of remaining entities? There are two ways to do this:
limit(1)
on the >= one that will return the first entity. Start it first, but don't materialize the result so that it runs asynchronously in parallel with the second.count()
, run a keys-only query with >=. Keep the first key to do a fetch, and count the rest manually. Keys-only queries cost the same (small ops per count) as count() queries because count() queries are essentially the same thing under the covers.I would probably go with #2. Either way, I hope that your counts are not large because otherwise you will churn through a lot of small datastore ops and your app will be expensive!