简体   繁体   English

如何更改 Google Cloud Datastore API 中的实体?

[英]How do I mutate Entities in the Google Cloud Datastore API?

In the Google Cloud Datastore API, Entities are immutable.在 Google Cloud Datastore API 中,实体是不可变的。 I appreciate the value of immutability, but this means that every time I set a property, I must again build -- ie, copy -- the entire Entity, which is very inefficient.我欣赏不变性的价值,但这意味着每次设置属性时,我都必须再次构建——即复制——整个实体,这是非常低效的。 It makes the simple act of setting N property values into O(N^2).它使将 N 个属性值设置为 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.另一种方法是将属性值保存在我自己的实体数据结构中,然后在保存之前将其复制到 API 的实体,并在查询或获取它之后再次从 API 的实体复制到我自己的实体。

Or else I could use the Builder to store mutable state and only build an Entity right before saving;或者我可以使用 Builder 来存储可变状态,并且只在保存之前构建一个实体; when I get or query an entity, I would take the Builder from that Entity.当我get或查询一个实体时,我会从该实体中获取 Builder。

Am I misunderstanding something?我误解了什么吗? Is there a way to do this efficiently?有没有办法有效地做到这一点?

(I am using Java.) (我正在使用 Java。)

The API offers the below methods to copy an Entity/FullEntity - API提供了以下方法来复制 Entity/FullEntity -

Entity.newBuilder(Entity copyFrom) FullEntity.newBuilder(FullEntity copyFrom) 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 (eg Customer, User).通常,如您所说,基于 Java 的应用程序将具有模型类(例如客户、用户)。 You work on these objects to change the state and copy the object's state to the persistence API objects (Entity/FullEntity) and save.您处理这些对象以更改状态并将对象的状态复制到持久性 API 对象 (Entity/FullEntity) 并保存。 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.您可能还想看看Catatumbo ,它是一个易于使用的、类似于 JPA 的持久性框架,它负责读/写数据所需的所有管道。

Disclosure: I'm the author of Catatumbo.披露:我是 Catatumbo 的作者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Google Cloud Datastore Node.js读取交易中的所有种类的实体 - How do I read all entities of a kind in a transaction with google cloud datastore nodejs 我如何修改在 google 数据存储区中创建的实体 - How do i modify entities created in google datastore 如何在 Google Cloud Platform 的 DataStore 中存储值? - How do I store values in DataStore in Google Cloud Platform? 用于安排数据存储实体导出的 Google Cloud 函数 - Google Cloud Function to schedule exports for datastore entities 我们如何使用Google Cloud Java API进行数据存储不等式和“IN”过滤? - How do we do Datastore inequality and “IN” filters using the Google Cloud Java API? 如何使用Google Datastore MultiQueryBuilder加载所有类型的实体? - How do I use Google Datastore MultiQueryBuilder to load all Entities of a Kind? 为什么使用 Google Cloud Datastore API Explorer 会收到 503 Service Unavailable 错误? - Why do I get 503 Service Unavailable errors using the Google Cloud Datastore API explorer? Google Cloud Datastore的PHP API - PHP API for Google Cloud Datastore 如何从本地Google App引擎数据存储中删除所有实体? - How do I delete all entities from my local Google App-engine datastore? 我们如何在Google Cloud API中解析并生成App Engine URL安全的数据存储区密钥? - How do we parse and generate App Engine URL-Safe Datastore Keys in the Google Cloud API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM