Sitelet https://github.com/googleapis/google-cloud-java/issues/1339
Skip to content

JavaDoc not clear as it could be, e.g. Datastore.add vs. Datastore.put #1339

Description

@unitydynamics

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!!!

Activity

  1. mziccard commented on Oct 27, 2016

    @mziccard
    Contributor

    Datastore.put performs a Datastore upsert (Update or Insert) operation. I believe this is documented in our javadoc.

  2. unitydynamics commented on Oct 27, 2016

    @unitydynamics
    Author

    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.

  3. mziccard commented on Oct 27, 2016

    @mziccard
    Contributor

    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.

  4. unitydynamics commented on Oct 27, 2016

    @unitydynamics
    Author

    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.

  5. sai-pullabhotla commented on Oct 27, 2016

    @sai-pullabhotla

    @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.

  6. unitydynamics commented on Oct 27, 2016

    @unitydynamics
    Author

    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.

  7. 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
  8. mziccard commented on Oct 27, 2016

    @mziccard
    Contributor

    @unitydynamics Here are some more details:

    1. add(Entity entity)
      • if entity.key() does not exist entity is inserted
      • if entity.key() already exists the method throws a DatastoreException ex such that ex.reason() == "ALREADY_EXISTS" and the entity is not inserted
    2. add(Entity... entities) - this is syntactic on top of a batch operation (i.e. is not transactional)
      • if none of entities' keys exist, all entities are inserted
      • if any of entities' keys already exists the method throws a DatastoreException ex such that ex.reason() == "ALREADY_EXISTS". All entities in entities whose key did not exist are inserted.

    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 an ALREADY_EXISTS error) 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.

  9. lesv commented on Oct 27, 2016

    @lesv
    Contributor
  10. unitydynamics commented on Oct 31, 2016

    @unitydynamics
    Author

    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!

  11. 16 remaining items

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

Metadata

Metadata

Assignees

Labels

api: datastoreIssues related to the Datastore API.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions