On 29 Jul 2010, at 04:35, Sven wrote:
I played around with a couple of ways to generate identifiers which do
not look too simple (short integers, such as primary key values), not
to complicated (too long or cryptic) and not too predictable (System
time). My current favorite is to generate a random integer, to convert
it to a alphabetical representation and to prefix it with a primary
key value of a entity I store to the database when creating the
identifier.
You might want to instead use datastore's built in ability to
efficiently create unique long ids and then when sending them to a
client "muddle" them using a reversible hash function so they don't
appear predictable. You will find that using long ids results in
shorter Keys which saves space in the datastore.
You can only guarantee a unique value in the datastore by creating an
Entity with the value in its key. This does not need to be your
"main" entity - just a special UniqueName type. Then you can
start transaction
load existing entity - fail if exists
store new entity
commit transaction
Step 4 will fail if another request created an entity with the same key.
when storing an entity with Twig its can check for existing entities
automatically like:
Transaction txn = datastore.beginTransaction();
datastore.store().instance(myUniqueName).ensureUniqueKey().now();
txn.commit();
The cool thing about .ensureUniqueKey() is that it also works for
storing multiple instances in bulk.
--
You received this message because you are subscribed to the Google Groups "Google
App Engine for Java" group.
To post to this group, send email to google-appengine-j...@googlegroups.com.
To unsubscribe from this group, send email to
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/google-appengine-java?hl=en.