On Mon, Feb 22, 2010 at 12:54 PM, Jeff Schnitzer <[email protected]>wrote:
> I hate to be the dork that argues with the referees, but I'm not fond > of this advice... > No problem, I'm happy to discuss. > > * long-id keys can be user-defined just as easily as string-name > keys, so you can enforce uniqueness constraints either way if you have > some sort of natural key. > > user-defined long-id keys are not quite as easily used. You either need to commit to not letting the datastore generate ids for that kind or you need to reserve a batch of ids using the DatastoreService.allocateIds method. Otherwise you run the risk of a silent collision. There is no such risk with user-defined string keys. > * Readable urls are a high level construct, and it's easy enough to > use pretty names mapped to ids using the memcache. If you ever want > to be able to rename people, you probably shouldn't use their name as > a key! > > Valid point about renaming, but going back to the example I provided, the datastore does not distinguish between inserts and updates. The only way you can guarantee that an entity was inserted, and therefore the only way you can guarantee the uniqueness of the name, is to use a user-defined key. If you're mapping id to name it will be possible to create two entities with the same name. It's of course up to you to decide how important this is to defend against, but without the ability to provide your own id you wouldn't get to make this choice, and without the ability to provide your own string id you wouldn't be able to add some application-specific meaning to this choice. I'd say that the use case of string name vs numeric id is... is your > id a number or is it a string? Boring answer but seems to sum it up > pretty well. > > Jeff > > On Mon, Feb 22, 2010 at 10:50 AM, Max Ross (Google) > <[email protected] <maxr%[email protected]>> wrote: > > 1) Readable-urls. http://yourapp.appspot.com/users/max versus > > http://yourapp.appspot.com/users/192398411324987 > > > > 2) The datastore doesn't support uniqueness constraints so a user-defined > > key is the only way you can do a true createIfNonExistent() method: > > > > beginTxn(); > > Entity e; > > try { > > e = lookupByKeyName(keyName); > > } cath (EntityNotFoundException enfe) { > > e = createNewEntity(keyName); > > } > > commitTxn(); > > > > The final line throws ConcurrentModificationException if someone else > > created an entity with the same keyName in between the lookup and the > > commit. > > > > Max > > On Sun, Feb 21, 2010 at 5:08 PM, DutrowLLC <[email protected]> wrote: > >> > >> Hi, > >> > >> What is the intended purpose of keyName when creating a new Key or new > >> Entity in the low-level API? What are the proper use cases for > >> keyName? > >> > >> Thanks! > >> > >> Chris > >> > >> -- > >> 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 > >> [email protected]. > >> To unsubscribe from this group, send email to > >> [email protected]<google-appengine-java%[email protected]> > . > >> For more options, visit this group at > >> http://groups.google.com/group/google-appengine-java?hl=en. > >> > > > > -- > > 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 > [email protected]. > > To unsubscribe from this group, send email to > > [email protected]<google-appengine-java%[email protected]> > . > > For more options, visit this group at > > http://groups.google.com/group/google-appengine-java?hl=en. > > > > -- > 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 > [email protected]. > To unsubscribe from this group, send email to > [email protected]<google-appengine-java%[email protected]> > . > For more options, visit this group at > http://groups.google.com/group/google-appengine-java?hl=en. > > -- 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 [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/google-appengine-java?hl=en.
