开发者

Unique short identifier across entity groups in App Engine

开发者 https://www.devze.com 2023-03-15 08:25 出处:网络
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:

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 开发者_Go百科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?


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


If you can get away with the idea number 2 - you are done. as then you got your key - and "URL shortening service" is a single servlet of 3-4 lines and you are done.

But!

I you got to pay a price - no transactions for you.

As AppEngine support transactions only inside an entity group. this actually fires back on your later idea of "URL shortening service" based on another model with a key...

The catch is you will not be able to manage it in the same transaction you manage your "photos of the user" so you can end up with wrong URLs.

If you must have transaction - build a url out of the parent keys. if not - use direct unique key with no parent->child hierarchy.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号