Is PagedQuery class a Singleton?

3 views
Skip to first unread message

Ratnadeep Rakshit

unread,
Jan 22, 2011, 3:49:41 PM1/22/11
to he3-appengine-lib
Hi, I am a java programmer and trying to port your python code to
Objectify(GAE/Java). As I have very limited knowledge of python,
pardon me if my question sounds stupid.

In the function _get_memcache_key(self), you return return namespace +
'_PagedQuery-persistence_' + str(self.id)
. The id can only be consistent when the class PagedQuery is reused in
the future calls by other users aswell. So, do I use this class as a
Singleton? If not, then every time the PagedQuery object is created, a
new ID could be generated and then the same memcache entry could not
be fetched. Please give me some hints.

Also, in the comments I saw you mention : "Note that when retrieving a
page for a second time, the internal cursors
are checked for changes. If changes exist, the cursors corresponding
to all subsequent pages are cleared from the cache." Could you please
elaborate how we can know that the cursor changed?

Thanks

Ben from He3

unread,
Jan 23, 2011, 10:56:52 PM1/23/11
to he3-appengine-lib
Hi Ratnadeep,

Sorry for the delay in replying - I had to look at the code again to
provide sensible answers.

Q1. Is PagedQuery a singleton?

PagedQuery is not a Singleton in the traditional sense. 'self.id' is a
property:

paging.py line 507: id = property(fget=_get_query_id, doc='unique id
of this query')

this effectively makes the instance method _get_query_id() the getter
of this property. As you can see from the code, _get_query_id()
returns an existing id if it has already been generated for that
PagedQuery instance, and if not, calls _generate_query_id(). The query
id in question is defined by a hash of the pickled (serialized)
db.Query or db.GqlQuery object:

paging.py line 386: return str(hash(pickle.dumps(self._query,2)))

2 query instances of the same kind, filters, order, etc, always
serialized the same. Therefore the hash is the same. Therefore the id
is the same between separate PagedQuery instances of the same query.
Therefore, two separate PagedQuery objects of the exact same query
will find the same memcache cache.

I am not sure about GAE Java works, but in GAE Python a singleton
wouldn't work since there is no guarantee any particular instance will
receive any particular HTTP request. I hope that answers that
question.

Q2. How do we know the cursor has changed for the purposes of clearing
the cache:

Good catch. Looking at the code I don't think this implemented.
It should go in _update_cursors_with_results()
I will need to follow up with that and fix it when I have my he3-
appengine-lib development environment set up again.

I hope these answers help. Let me know if you have any more questions.
Sorry for the delay again.
Reply all
Reply to author
Forward
0 new messages