开发者

How to create unique key names in google app engine

I'm having trouble figuring out how to create unique key names for my online election app. Here is a typical set of relevant entities:

  • 1 Election
  • 5 Candidates
  • 100,000 Voters
  • up to 100,000 Votes

I need a unique key name for each Voter entity for the reasons expressed here. An email address would be unique for a voter in a particular election but not necessarily across multiple elections (since the same voter could be part of multiple elections).

If the Election was the parent entity of the voter entities, then using the voter email in combination with the election parent would be sufficient for a unique key. Since I need to use transactions in updating Voter entities (to make sure that a voter cannot vote more than once) I think I don't want to use an entity group because only one Voter could be updated at a time according开发者_如何学JAVA to the docs.

Another solution could be to create a unique key for each voter of the form:

E<election ID>_<voter email>

e.g.,

E1001_john@example.com

Is that a reasonable way to create a unique key name for each Voter entity?


Double-check your assumptions. As it is, there is a situation that could hamper the ID's uniqueness:

What if two voters use the same e-mail address? I know that my parents only have a single one between them. As it is, your ID would clash.

There are bunches of way to minimize collisions. If worst comes to worst, you could hash some of the voter's information. Shoot, you could even hash the exact time at which you're creating the ID.

(Hm... I guess I kind of like hashing things...)

Simplest solution? Do what people in databases do for IDs: a number. Every time you add a new voter, increment the number. The first voter you add will have an ID of 1, the second will have an id of 2, and so on. If you need to, you can convert the id to a string.

But, don't assume that data is unique. Two people can have the same name, or street address, or e-mail address, or telephone number, etc.

Hope that gives you some ideas!


That should be unique. I would probably use a GUID myself, and tie it back to a data repository with a unique constraint on the voting session and voter, but that's just a personal preference.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜