views:

63

answers:

2

Using an alphabet like "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ" I'd like to generate 2 to 4 letter codes to identify unique datastore entries. I have a python function capable of doing this when passed an list indicating the letter positions of the last code [7,17,11] -> "7GA". the next code can be made by incrementing that right most element by one and carrying one up when the alphabet length is exceeded.

This method has the advantage of keeping codes short, sequential, consistent, easy to communicate, and looking how I want them to.

I'm wondering though if this would work on app engine since the function must hold onto or be passed the last identifier to enforce uniqueness which may not play well with the non-continuous nature of Google's infrastructure. Alternate ways to make this happen or reasoned arguments against it are welcome.

+2  A: 

This would be difficult to use as-is on app engine because many copies of your application could be running at once. Each copy would need access to the "last identifier" and be able to update it atomically. This would likely require too much overhead, unless you only need to generate new IDs in this fashion rather infrequently.

Why not use GAE's built-in numeric IDs? They are guaranteed to be unique and are also easy to communicate. They are also generally sequential and increasing, though this is not guaranteed.

David Underhill
+5  A: 

If you're attached to having the codes be sequential, you'll need to have a single counter object that is transactionally locked and incremented each time a new entity is created. The argument against this is that you're defeating one of the major advantages of App Engine: concurrency. Unless your application has a specific need for sequential IDs, it's a bad idea.

If you let App Engine auto-assign IDs they will be non-sequential, but you can convert the integer to and from base 36 when displaying it to the user. Here's a python function to convert an integer to and from arbitrary bases.

Drew Sears
I see the value in using the standard ids and just converting/encoding them for urls.
Regis Frey