views:

71

answers:

2

I want to be able to give each instance of a particular object a unique number id in the order that they are created, so I was thinking of getting the number of the particular entity already in the datastore and add 1 to get the new number.

I know I can do something like

query = Object.all()
count = query.count()

but that has some limitations.

Does anybody know a better way to find the number of particular entities or even a better way to give objects a unique sequential number id?

Thanks!

A: 

Usual answer to many questions on non-relational DBs: denormalize wisely, by keeping a model with the sole purpose of counting and a single entity -- in the factory function building entities of other model, transactionally increment said counter and use the value in the building. If this proves to be a bottleneck, consider sharded counters or other parallelization techniques for counters -- again, just as usual for App Engine (the fact that you're using that counter as a UID does not really affect this choice).

Alex Martelli
This is the best approach - iff it's not possible to use App Engine's built in IDs, because this approach will have serious scalability limitations.
Nick Johnson
+1  A: 

Why do your IDs need to be sequential? The App Engine datastore generates integer IDs for your entities already; they're not guaranteed to be sequential, but they are guaranteed to be unique, and they tend to be small.

The ID generation strategy for App Engine is not 'perfect' - entirely sequential - because doing so in a distributed system is impractical, as it introduces a single bottleneck (the service that hands out IDs). Any system you build will suffer from the same issue, unless you want only a low rate of ID issuance (eg, 1 per second or less).

Nick Johnson
I guess order doesn't really matter if it's going to clog things up. I know that I can assign my own String id when I create a new object, but is there a way for me to set my own Integer key? I tried Object(key_name = "key") and a few other things, but they all failed. What I am trying to do now is to get a uid that a user has from another application and set it as the key in my datastore.
ayanonagon
Use MyModel(key=db.Key.from_path('MyModel', id), ...), or stringify your ID (Eg, MyModel(key_name=str(id))).
Nick Johnson