Find out the index of an entity given some filters?
Is it possible to find out the index of an entity? For example, my users are creating thousands of Foos. I can locate an arbitrary Foo instance using its key, but I don't know its index compared to all oth开发者_JAVA百科er Foos. I'd like to figure out that "this is Foo 12 out of 20,000". I only have one sort order for Foos. Is something like that possible?
Thanks
Not trivially.
If you know the key of your entity, you could do a keys-only query using your sort order and just iterate over (and count) the returned keys until hit the one you're looking for. You can use query cursors and task chaining to split the job into reasonable chunks.
If your index is just the order of creation, you could alternately de-normalize by storing a single counter entity. Each time you want to create a new Foo, transactionally lock the counter object, increment it, and then write back the new counter value to the counter and your new Foo. This does prevent you from creating multiple Foo entities at once, although if you don't need an ID returned to the user immediately, you could defer entity creation to a task.
精彩评论