Reputation: 19655
In the Google Cloud Datastore API, Entities are immutable. I appreciate the value of immutability, but this means that every time I set a property, I must again build -- i.e., copy -- the entire Entity, which is very inefficient. It makes the simple act of setting N property values into O(N^2).
The alternative is to save property values in my own entity data structure, then copy this to the API's Entity right before saving, and again from the API's Entity to my own right after querying or getting it.
Or else I could use the Builder to store mutable state and only build an Entity right before saving; when I get
or query an entity, I would take the Builder from that Entity.
Am I misunderstanding something? Is there a way to do this efficiently?
(I am using Java.)
Upvotes: 0
Views: 400
Reputation: 2207
The API offers the below methods to copy an Entity/FullEntity -
Entity.newBuilder(Entity copyFrom) FullEntity.newBuilder(FullEntity copyFrom)
Use the above to make changes to an existing entity. After calling one of the above methods, you can set/update one or more properties, then build the entity and persist.
Typically, Java based applications, like you said, would have model classes (e.g. Customer, User). You work on these objects to change the state and copy the object's state to the persistence API objects (Entity/FullEntity) and save. The opposite is done when reading data.
You might also want to look at Catatumbo, an easy to use, JPA like persistence framework that takes care of all the plumbing needed to read/write the data.
Disclosure: I'm the author of Catatumbo.
Upvotes: 1