google-app-enginegoogle-cloud-datastoreentity-groups

Unique short identifier across entity groups in App Engine


I've searched around for answers to this question, but not found anything quite on the money. I'd be really interested to hear people's thoughts. Here goes:

In Google AppEngine, lets say I have a number of User objects, each of which can have a number of Photo objects. The User objects need to be parents of their respective Photo objects.

But I also want to be able to have nice short urls for each Photo. I intended to generate these by Base64 encoding the automatically generated ID property of each Photo, but I realise I cannot do this as the IDs AppEngine generates are not guaranteed to be unique across entity groups (i.e. for entities with different parents). So a Photo which is a child of one User could conceivably have the same ID as a Photo which is the child of a different User.

This leaves me in a pickle. I can either:

  1. Try to come up with my own unique ID generator and use that

  2. Lose the parent->child hierarchy so IDs will be unique (not keen on this at all)

  3. Some super-clever option suggested as an answer to this question

I'm really hoping for option 3.

Any thoughts or ideas on the best way to deal with this would be fantastic.

Thanks in advance.

Edit

Just after posting I had the idea of incorporating a mini URL shortening service into the app. I'd just need a model with no parent and a single 'Key' property which would point to the Photo I wanted to link to. Then I can Base64 encode the Id of this entity and I'm done. What do you think?


Solution

  • Why not just encode the ID of the parent user along with the ID of the relevant photo? You can encode it as two integers - /123/2 or in any other format you wish, such as base64 as you suggest. If you let users pick a unique name of some sort and use that as the key name on the user object, this is also more useful from a UI point of view, since it gives you URLs like /photos/nick/123