Objectify - 仅使用键测试对象是否存在?
Objectify - test if object exists using only the key?
我想测试数据存储中是否存在对象。我知道它的关键。我现在通过加载整个对象来执行此操作:
public boolean doesObjectExist(String knownFooId) {
Key<Foo> key = Key.create(Foo.class, knownFooId);
Foo foo = ofy().load().key(key).now();
if (foo != null) {
// yes it exists.
return true;
}
return false;
}
这必须花费 1 次数据存储读取操作。有没有一种方法可以做到这一点而不必加载整个对象,这可能更便宜?换句话说,一种只需要 1 "small" 操作的方式?
谢谢
没有比这更便宜的方法了。
即使您只执行仅键查询,查询也是 1 次读取操作 + 1 次读取每个键的小操作。 (https://cloud.google.com/appengine/pricing#costs-for-datastore-calls)
继续进行按键获取,这只是 1 次读取。
您可以尝试获取密钥,据我了解这只是一个小操作。
// You can query for just keys, which will return Key objects much more efficiently than fetching whole objects
Iterable<Key<F>> allKeys = ofy().load().type(Foo.class).filter("id", knownFooId).keys();
应该可以。另请查看 objectfy 文档:https://github.com/objectify/objectify/wiki/Queries
public boolean doesObjectExist(String knownFooId) {
Key<Foo> fooKey = Key.create(Foo.class, knownFooId);
Key<Foo> datastoreKey = ofy().load().type(Foo.class).filterKey(fooKey).keys().first().now();
return datastoreKey.equals(fooKey);
}
QueryKeys keys()
Switches to a keys-only query. Keys-only responses are billed as "minor datastore operations" which are faster and free compared to fetching whole entities.
我想测试数据存储中是否存在对象。我知道它的关键。我现在通过加载整个对象来执行此操作:
public boolean doesObjectExist(String knownFooId) {
Key<Foo> key = Key.create(Foo.class, knownFooId);
Foo foo = ofy().load().key(key).now();
if (foo != null) {
// yes it exists.
return true;
}
return false;
}
这必须花费 1 次数据存储读取操作。有没有一种方法可以做到这一点而不必加载整个对象,这可能更便宜?换句话说,一种只需要 1 "small" 操作的方式?
谢谢
没有比这更便宜的方法了。
即使您只执行仅键查询,查询也是 1 次读取操作 + 1 次读取每个键的小操作。 (https://cloud.google.com/appengine/pricing#costs-for-datastore-calls)
继续进行按键获取,这只是 1 次读取。
您可以尝试获取密钥,据我了解这只是一个小操作。
// You can query for just keys, which will return Key objects much more efficiently than fetching whole objects
Iterable<Key<F>> allKeys = ofy().load().type(Foo.class).filter("id", knownFooId).keys();
应该可以。另请查看 objectfy 文档:https://github.com/objectify/objectify/wiki/Queries
public boolean doesObjectExist(String knownFooId) {
Key<Foo> fooKey = Key.create(Foo.class, knownFooId);
Key<Foo> datastoreKey = ofy().load().type(Foo.class).filterKey(fooKey).keys().first().now();
return datastoreKey.equals(fooKey);
}
QueryKeys keys()
Switches to a keys-only query. Keys-only responses are billed as "minor datastore operations" which are faster and free compared to fetching whole entities.