Skip to content

Add insert semantic - #532

Open
JanR99 wants to merge 4 commits into
objectify:masterfrom
JanR99:add-insert-semantic
Open

JanR99 wants to merge 4 commits into
objectify:masterfrom
JanR99:add-insert-semantic

Conversation

@JanR99

@JanR99 JanR99 commented Oct 30, 2026

Copy link
Copy Markdown

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 insert mutation (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():

ofy().insert().entity(thing).now();
ofy().insert().entities(a, b, c).now();

If an entity with the same key already exists, the result throws a DatastoreException with reason ALREADY_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, mirroring Saver / SaverImpl.
  • WriteEngine.insert() reuses the existing save pipeline (POJO translation, ID generation, session updates). The only difference is that it calls add() instead of put() on the datastore.
  • AsyncDatastoreReaderWriter.add() is implemented for the plain datastore, the caching layer (keys are emptied from memcache exactly like for put()), and transactions.
  • Semantics deliberately match save(): batches are split into chunks of 500 and are only atomic inside a transaction. Inside a transaction, ALREADY_EXISTS surfaces on commit. All of this is documented in the Javadoc.

Room to grow

In a second commit I replaced the internal boolean in WriteEngine with a small WriteMode enum that holds the datastore operation. Supporting the datastore's update mutation (fail if the entity does not exist) would only take one more enum value plus a thin ofy().update() command, so an Updater would 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Objectify should support "add" (create/insert) functionality

1 participant