Repository navigation
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hi Jeff, hi everyone 👋
I first came across Objectify while working on my bachelor's thesis, where I migrated our app from JDO to Objectify. It has been a real pleasure to work with ever since, so thank you for building and maintaining it!
One pattern that kept coming up in our codebase is a hand-written "create" method: check whether the entity already exists, throw if it does, and only then save it. That costs an extra read, and unless it's wrapped in a transaction there's a race between the check and the save. The datastore already supports exactly these semantics through its
insertmutation (DatastoreWriter.add()in the Java client), so it felt natural for Objectify to expose them too. This PR does that and addresses 521.What this adds
A new top-level command, alongside
save():If an entity with the same key already exists, the result throws a
DatastoreExceptionwith reasonALREADY_EXISTS. The check happens atomically on the server as part of the commit: one round trip, no extra read, no transaction required. We could also wrap that in a custom exception, but that would also touch the commit path for transactions.How it works
Inserter/InserterImpl: the new command, mirroringSaver/SaverImpl.WriteEngine.insert()reuses the existing save pipeline (POJO translation, ID generation, session updates). The only difference is that it callsadd()instead ofput()on the datastore.AsyncDatastoreReaderWriter.add()is implemented for the plain datastore, the caching layer (keys are emptied from memcache exactly like forput()), and transactions.save(): batches are split into chunks of 500 and are only atomic inside a transaction. Inside a transaction,ALREADY_EXISTSsurfaces on commit. All of this is documented in the Javadoc.Room to grow
In a second commit I replaced the internal boolean in
WriteEnginewith a smallWriteModeenum that holds the datastore operation. Supporting the datastore'supdatemutation (fail if the entity does not exist) would only take one more enum value plus a thinofy().update()command, so anUpdaterwould be easy to add. I kept it out of this PR to keep the change focused, but I'm happy to follow up if you're interested.Of course I'm open to any feedback on naming or API shape, and if you'd prefer a different design, just let me know. Thanks for taking a look! 🙂
Closes 521