google app engine - Load list of items in objectify -
i have question, , hashtag entities. there 1 many relationship between , question entities. using google cloud endpoints , problem begins here. in list method, return 20 question json. each question object in query have check if user liked question , fetch related hashtags belongs question. how can same operation key batch query. otherwise,
ofy().load().type(like.class) .filter("questionref =", questionkey) .filter("accountref =", accountkey).first().now();
for each object.
like entity
@entity @cache public class { @id @getter protected long id; @index @load @apiresourceproperty(ignored = annotationboolean.true) private ref<account> accountref; @index @load @apiresourceproperty(ignored = annotationboolean.true) private ref<question> questionref; @index @getter protected date createdat; like() { } like(key<account> accountkey) { this.accountref = ref.create(accountkey); this.createdat = new date(); } }
hashtag entity
@entity @cache public class hashtag implements model<hashtag> { @id @getter @apiresourceproperty(ignored = annotationboolean.true) private long id; @index @load @apiresourceproperty(ignored = annotationboolean.true) private ref<question> questionref; @index @getter @setter private string text; private hashtag() { } private hashtag(builder builder) { this.questionref = builder.questionref; this.text = builder.text; } }
there several parts question.
first, hashtags: store hashtags in question indexed list property. easy.
second, likes: there couple ways efficiently.
one create entity natural key of "account:question" (use stringified websafe key). way can batch key {user,question} tuples. absent, present. reasonably efficient if you're concerned 20 questions, if @cache
like.
another create separate relation index entity tracks likes of user , load each time. can put 5k items in list property, means you'll need juggle multiple entities when user likes more 5k things. it's easy load them single ancestor query. rie need @parent
ed user.
on separate note - don't call fields thingref
. it's thing
. data in database key. can interchange ref<?>
, key<?>
, , native low-level key
. type information doesn't belong in database names.
Comments
Post a Comment