Reputation: 525
I want to check if cert entity exist in the database using keys-only queries. So far I'm doing:
Iterable<Key<LikeMW>> liked = ofy().load().type(LikeMW.class).filter("likedObject", postKey).filter("user", userKey).keys();
post.setLiked(liked.iterator().hasNext());
So I have 2 questions:
1 - If I use ".first().now()" after ".keys()", does it switch from "keys-only" or it'll still be a "keys-only" query?
2 - Is there a better way to check if cert entity exist using "keys-only" queries and filter?
Thank you guys!
UPDATING
@Entity
public class LikeMW {
@Id
private Long id;
@JsonIgnore
@Index
@Load
private Ref<UserMW> user;
@JsonIgnore
@Index
private Key likedObject;
...
}
And one of possible liked objects...
@Entity
public class PostMW{
@Id
private Long id;
@JsonIgnore
@Load
private Ref<UserMW> owner;
@JsonIgnore
@Load
private Ref<MediaMW> media;
...
}
Upvotes: 0
Views: 694
Reputation: 13556
The only way to authoritatively look up whether an entity exists is to load it by key. You can certainly do a keys-only query, but it will be eventually consistent and will not guarantee that you do not create duplicates.
Given what you are trying to do, you will almost certainly be better off parenting LikeMW
with the user and using the stringified likedObject as the string id. That way you can do a strongly consistent lookup and use transactions.
Upvotes: 1