简体   繁体   English

检查谷歌应用引擎数据存储区中是否存在实体。

[英]Checking if Entity exists in google app engine datastore.

What is the best/fastest way to check if an Entity exists in a google-app-engine datastore? 检查实体是否存在于Google-app-engine数据存储区中的最佳/最快方法是什么? For now I'm trying to get the entity by key and checking if the get() returns an error. 现在我试图通过键获取实体并检查get()是否返回错误。

I don't know the process of getting an Entity on the datastore. 我不知道在数据存储区上获取实体的过程。 Is there a faster way for doing only this check? 有没有更快的方法只做这个检查?

What you proposed would indeed be the fastest way to know if your entity exists. 你提出的建议确实是了解你的实体是否存在的最快方法。 The only thing slowing you down is the time it takes to fetch and deserialize your entity. 减慢速度的唯一因素是获取和反序列化实体所需的时间。 If your entity is large, this can slow you down. 如果您的实体很大,这可能会让您失望。

IF this action (checking for existence) is a major bottleneck for you and you have large entities, you may want to roll your own system of checking by using two entities - first you would have your existing entity with data, and a second entity that either stores the reference to the real entity, or perhaps an empty entity where the key is just a variation on the original entity key that you can compute. 如果此操作(检查是否存在)是您的主要瓶颈并且您拥有大型实体,则可能需要使用两个实体来推送您自己的检查系统 - 首先您将拥有包含数据的现有实体,以及第二个实体或者存储对真实实体的引用,或者可能是一个空实体,其中键只是您可以计算的原始实体键的变体。 You can check for existence quickly using the 2nd entity, and then fetch the first entity only if the data is necessary. 您可以使用第二个实体快速检查是否存在,然后仅在需要数据时才获取第一个实体。

The better way I think would just be to design your keys such they you know there would not be duplicates, or that your operations are idempotent, so that even if an old entity was overwritten, it wouldn't matter. 我认为更好的方法就是设计你的密钥,你知道它们不会重复,或者你的操作是幂等的,这样即使旧的实体被覆盖,也没关系。

com.google.appengine.api has been deprecated in favor of the App Engine GCS client. com.google.appengine.api已被弃用,转而使用App Engine GCS客户端。

Have you considered using a query? 你考虑过使用查询吗? Guess-and-check is not a scalable way to find out of an entity exists in a data store. 猜测和检查不是一种可扩展的方法来查找数据存储中存在的实体。 A query can be created to retrieve entities from the datastore that meet a specified set of conditions: 可以创建查询以从数据存储中检索满足指定条件集的实体:

https://developers.google.com/appengine/docs/java/datastore/queries https://developers.google.com/appengine/docs/java/datastore/queries

EDIT: 编辑:

What about the key-only query? 那么关键的查询怎么样? Key-only queries run faster than queries that return complete entities. 仅键查询比返回完整实体的查询运行得更快。 To return only the keys, use the Query.setKeysOnly() method. 要仅返回键,请使用Query.setKeysOnly()方法。

new Query("Kind").addFilter(Entity.KEY_RESERVED_PROPERTY, FilterOperator.EQUAL, key).setKeysOnly();

Source: [1]: http://groups.google.com/group/google-appengine-java/browse_thread/thread/b1d1bb69f0635d46/0e2ba938fad3a543?pli=1 资料来源:[1]: http//groups.google.com/group/google-appengine-java/browse_thread/thread/b1d1bb69f0635d46/0e2ba938fad3a543?pli = 1

You could fetch using a List<Key> containing only one Key , that method returns a Map<Key, Entity> which you can check if it contains an actual value or null , for example: 您可以使用仅包含一个KeyList<Key>获取, 该方法返回Map<Key, Entity> ,您可以检查它是否包含实际值或null ,例如:

Entity e = datastoreService.get(Arrays.asList(key)).get(key);

In general though I think it'd be easier to wrap the get() in a try/catch that returns null if the EntityNotFoundException is caught. 一般来说,虽然我认为在try / catch中包装get()更容易,如果捕获到EntityNotFoundException则返回null

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM