diff --git a/query.py b/query.py index bfb7b26..12ebdd0 100644 --- a/query.py +++ b/query.py @@ -46,7 +46,7 @@ class _Cache(gobject.GObject): self._array = [] self._dict = {} if jobjects is not None: - self.append_all(jobjects) + self.append(jobjects) logging.debug('_Cache.__init__: connecting signals.') bus = dbus.SessionBus() @@ -59,22 +59,27 @@ class _Cache(gobject.GObject): self._datastore_deleted_handler = \ datastore.connect_to_signal('Deleted', self._datastore_deleted_cb) - def prepend_all(self, jobjects): + def prepend(self, jobjects): for jobject in jobjects[::-1]: self._array.insert(0, jobject) self._dict[jobject.object_id] = jobject - def append_all(self, jobjects): + def append(self, jobjects): for jobject in jobjects: self._array.append(jobject) self._dict[jobject.object_id] = jobject - def remove_all(self, jobjects): + def remove(self, jobjects): for jobject in jobjects: self._array.remove(jobject) del self._dict[jobject.object_id] self._destroy_jobjects(jobjects) + def remove_all(self): + self._destroy_jobjects(self._array) + self._array = [] + self._dict = {} + def __len__(self): return len(self._array) @@ -89,12 +94,10 @@ class _Cache(gobject.GObject): self._datastore_created_handler.remove() self._datastore_updated_handler.remove() self._datastore_deleted_handler.remove() - self._destroy_jobjects(self._array) + self.remove_all() def _invalidate(self): - self._destroy_jobjects(self._array) - self._array = [] - self._dict = {} + self.remove_all() self.emit('modified') def _destroy_jobjects(self, jobjects): @@ -155,7 +158,7 @@ class ResultSet(gobject.GObject): sorting=self._sorting, limit=ResultSet._CACHE_LIMIT, properties=PROPERTIES) - self._cache.append_all(jobjects) + self._cache.append(jobjects) self._offset = 0 return self._total_count @@ -194,8 +197,8 @@ class ResultSet(gobject.GObject): limit=ResultSet._CACHE_LIMIT, properties=PROPERTIES) - self._cache.remove_all(self._cache) - self._cache.append_all(jobjects) + self._cache.remove_all() + self._cache.append(jobjects) self._offset = offset elif remaining_forward_entries < 2 * max_count and \ @@ -208,13 +211,13 @@ class ResultSet(gobject.GObject): offset=last_cached_entry, limit=max_count) # update cache - self._cache.append_all(jobjects) + self._cache.append(jobjects) # apply the cache limit objects_excess = len(self._cache) - ResultSet._CACHE_LIMIT if objects_excess > 0: self._offset += objects_excess - self._cache.remove_all(self._cache[:objects_excess]) + self._cache.remove(self._cache[:objects_excess]) elif remaining_backwards_entries < 2 * max_count and self._offset > 0: @@ -231,12 +234,12 @@ class ResultSet(gobject.GObject): properties=PROPERTIES) # update cache - self._cache.prepend_all(jobjects) + self._cache.prepend(jobjects) # apply the cache limit objects_excess = len(self._cache) - ResultSet._CACHE_LIMIT if objects_excess > 0: - self._cache.remove_all(self._cache[-objects_excess:]) + self._cache.remove(self._cache[-objects_excess:]) else: logging.debug('cache hit and no need to grow the cache')