0

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!

Ayaka Nonaka
  • 856
  • 1
  • 10
  • 22

3 Answers3

2

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
  • 100,655
  • 16
  • 128
  • 198
  • 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. – Ayaka Nonaka Jul 01 '10 at 15:01
  • Use MyModel(key=db.Key.from_path('MyModel', id), ...), or stringify your ID (Eg, MyModel(key_name=str(id))). – Nick Johnson Jul 01 '10 at 18:53
0

For Python GAE SDK, you can increase the argument "limit" of the count method: https://developers.google.com/appengine/docs/python/datastore/queryclass#Query_count

Replicated from Google AppEngine: how to count a database's entries beyond 1000?

Community
  • 1
  • 1
Paulo Cheque
  • 2,797
  • 21
  • 22
0

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
  • 854,459
  • 170
  • 1,222
  • 1,395
  • 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 Jul 01 '10 at 18:54