Question

I'm pretty useless when it comes to queries, I'm wondering what's the correct structure for this problem.

Clients are sent data including the key of the object, they use the key to tell the server what was the most recent object they downloaded.

I want to get all objects since that point, the objects have an automatic date attribute.

Additionally, I want to be able to give the 15 (or so) most recent objects to new users who may request using a specific 'new user' key or something similar.

Using the Python2.7 runtime, never used GQL before,

Any help is greatly appreciated.

The Model Class is this:

class Message(db.Model):
    user = db.StringProperty()
    content = db.TextProperty()
    colour = db.StringProperty()
    room = db.StringProperty()
    date = db.DateTimeProperty(auto_now_add=True)
Was it helpful?

Solution

If it is a db.Key object or a string representation of a key using the db (as opposed to the ndb) API:

last_message = Message.get(lastkey)

If you have the key in another representation, such as the key name:

last_message = Message.get_by_key_name(lastkey)

If you have the key as the numeric ID of the object:

last_message = Message.get_by_id(int(lastkey))

Then, you can get the messages since that last message as follows:

messages_since_last_message = Message.all().filter('date >', last_message.date).order('date')
#OR GQL:
messages_since_last_message = Message.gql("WHERE date > :1 ORDER BY date ASC", last_message.date)

You should maybe use the >= comparator only because there may be multiple messages that arrive at the same exact time, and then filter out all messages that are in the list up to and including the last key you are looking for (this actually depends on your use case and how closely message could be written). Additionally, with the High Replication datastore, there is eventual consistency, so your query is not guaranteed to accurately reflect the datastore unless you use ancestor queries, in which case you limit your entity group to ~1 write per second, which again, depending on your use case, could be a non-issue. The Entity Group here reflects the parent model and all of its children ancestors. The group of ancestors resides on a single entity group.

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