Repository navigation
JavaDoc not clear as it could be, e.g. Datastore.add vs. Datastore.put #1339
Description
Activity
- addedapi: datastoreIssues related to the Datastore API.Issues related to the Datastore API.
on Oct 27, 2016 Datastore.putperforms a Datastoreupsert(Update or Insert) operation. I believe this is documented in our javadoc.Lol I'm not British but I would say you're being "dodgy". put is clear. Upsert is clear. What does "add" do? You didn't confirm or deny my assumption in the original question. "Add" isn't really documented.
I have no idea what you mean with "clobber". I thought add behavior was straightforward, it adds an entity, in other words it performs a Datastore insert.
More details on datastore operations can be found at https://cloud.google.com/datastore/docs/concepts/entities.
A well "clobber" is an old school computer science term. Very technical. It means overwrite existing data. So... will "add" ever overwrite existing data? If not is there a way to know which ones were already present? (let's assume I'm adding 100 entities and half might already in the datastore). Will it throw an exception? Lots of potential questions. I'm just an API user trying to swim. I actually need this functionality of "add" that would add only those things not already present... leave existing datastore items alone. I'm testing. But I just wish I didn't have to.
@unitydynamics, add() is equivalent to insert. If you try to insert an entity and the same key already exists, you will get an exception stating that fact. Your existing entity with the same key will be unaffected.
I get that now... I just wish that the "add" documentation had at least a brief statement saying that existing entities were not overwritten and explained what happens if it does already exist. Moreover I wish the documentation defined the behavior for multiple entities being inserted at the same time, some of which are not in the data store yet and some of which already are. If you don't document the basic behaviors (preferably in JavaDoc) then you'll get clueless people like me asking questions like this. Let's close this issue. Thank you for your time and hard work on this API.
- changed the title
[-]Datastore.add vs. Datastore.put[/-][+]JavaDoc not clear as it could be, e.g. Datastore.add vs. Datastore.put[/+]on Oct 27, 2016 @unitydynamics Here are some more details:
add(Entity entity)- if
entity.key()does not existentityis inserted - if
entity.key()already exists the method throws aDatastoreException exsuch thatex.reason() == "ALREADY_EXISTS"and the entity is not inserted
- if
add(Entity... entities)- this is syntactic on top of a batch operation (i.e. is not transactional)- if none of entities' keys exist, all
entitiesare inserted - if any of
entities' keys already exists the method throws aDatastoreException exsuch thatex.reason() == "ALREADY_EXISTS". All entities inentitieswhose key did not exist are inserted.
- if none of entities' keys exist, all
You might find strange that
add(Entity... entities)throws an exception while still inserting non-existing entities. You might also be wondering which entities have been inserted, and which already existed. Unfortunately, the service does not provide us with that information (it just returns anALREADY_EXISTSerror) so there's nothing more that we can do with the information we have.BTW, you are totally right when saying that this must be added to our javadoc. Please keep this issue open until I add more docs.
Wow. Above and beyond! Good clarifications all around, updated Datastore interface, added some good tests. I like that you can get whatever behavior you want using transactions. Thanks Marco!
16 remaining items
- added a commit that references this issue
on Mar 23, 2026 - added 6 commits that reference this issue
on Apr 29, 2026
What's the difference between Datastore.add and Datastore.put?
Am I right in assuming that "add" will not clobber existing entities?
Maybe a quick update to the javadoc to clarify--would be much appreciated!!!