Search code examples
google-app-engineobjectify

Objectify - test if object exists using only the key?


I want to test if an object exists in the datastore. I know its key. I am doing this right now by loading the entire object:

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;
}

That must cost 1 read operation from the datastore. Is there a way to do it without having to load the entire object, that might be cheaper? In other words, a way that it would only cost 1 "small" operation?

Thanks


Solution

  • There's no way to do it cheaper.

    Even if you just do a keys only query, the query is 1 Read operation + 1 Small operation per key fetched. (https://cloud.google.com/appengine/pricing#costs-for-datastore-calls)

    Keep doing a get by key, which is just 1 Read.