google app engine - Objectify list consistency in one entity -
i try make chat solution on app engine android app. decided instead of save messages send topic in separated entity chatmessage or this, can save them in list of strings inside topic entity, this:
@entity public class topic { @id public string id; public list<long> users = new arraylist<long>(2); public long lastchangetime; public linkedlist<string> messages = new linkedlist<string>(); }
i came because storing topic id every message more data message string itself. :s don't know is, can list strong consistent?
how add new message topic:
// 2. topic if exists or create new if not topic topic = ofy().load().key(key.create(topic.class, topicid)).now(); if (topic == null) { topic = new topic(senderid, recipientid); } // 3. add message // method adds new string topic , update // last change time topic.addmessage(senderid, recipientid, send.message); // 4. save topic & new message ofy().save().entity(topic).now();
so if 2 users send message @ same time, can happens first user load topic, add message, in same time second user loaded topic (without first user's message) , add own new message. first save topic first. can second override previous save of first user? or happens?
if can happen, how can avoid this, bearing in mind it's high write rate entity need more write 1/sec!
thanks, , best regards.
what don't know is, can list strong consistent?
consistency determined entity groups , queries, not properties.
so if 2 users send message @ same time, can happens first user load topic, add message, in same time second user loaded topic (without first user's message) , add own new message. first save topic first. can second override previous save of first user? or happens?
you need inside transaction. if concurrentmodificationexception
thrown inside transaction (your example scenario) objectify retry you.
but, avoid contention, need change data model. have message
class , topic
, this:
@entity public class topic { @id string id; list<long> users = new arraylist<long>(2); long lastchangetime; }
and message referencing 1 or more topics (i'm making assumptions here):
@entity public class message { @id long id; long lastchangetime; @index ref<topic> topic; }
the @index
annotation on topic
allow query message
s topic
. change ref<topic>
list
of same if messages can in multiple topics.
Comments
Post a Comment