Question

I'm building a Threaded Messaging System that will be hosted on Google AppEngine

I've modeled it after the technique described by Brett Slatkin in Building Scalable, Complex Apps on App Engine

class Message(db.Model):
  sender = db.StringProperty()
  body = db.TextProperty()

class MessageIndex(db.Model):
  receivers = db.StringListProperty()

The issue I'm having to determining the most efficient way to track the message state for a User. For example is a message read, archived, deleted for a particular user.

Here are the solution I have come up with so far.

I'm using Datastore+'s StructuredProperty to add a state to the message MessageIndex

class Message(model.Model):
  sender = model.StringProperty()
  body = model.TextProperty()

class _DisplayState(model.Model):
  user_key = model.KeyProperty()
  state = model.IntegerProperty(default=0) # 0-unread, 1-read, 2-archived

class MessageIndex(model.Model):
  receivers = model.StructuredProperty(_DisplayState, repeated=True)

This solution, while simple, negates the benefit of the MessageIndex. Additionally since the the MessageIndex is in the same entity group as the message datastore writes will be limited.

Full Source Code

What would be the most efficient way to accomplish this? Would adding an additional entity group be a better solution?

class MessageState(model.Model):
  user_key = model.KeyProperty()
  message_key = model.KeyPropery()
  message_state = model.IntegerProperty(default=0) # 0-unread, 1-read, 2-archived
Was it helpful?

Solution

For the easiest querying - split your 'receivers' list into four different lists - 'unread', 'read', 'archived', 'deleted' and shuffle the receiver record between the lists as appropriate.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top