Little tweaks to item fetching.
authorPaweł Paprota <ppawel@fastmail.fm>
Sat, 12 Apr 2008 20:55:41 +0000 (12 22:55 +0200)
committerPaweł Paprota <ppawel@fastmail.fm>
Sun, 20 Apr 2008 16:45:48 +0000 (20 18:45 +0200)
straw/Application.py
straw/ItemList.py
straw/ItemManager.py
straw/storage/SQLiteDAO.py
straw/storage/SQLiteStorage.py

index 32ff027..dad4198 100644 (file)
@@ -432,6 +432,7 @@ class ApplicationPresenter(MVP.BasicPresenter):
 
     def quit(self):
         self._feed_list_presenter.store_state()
+        ItemManager.shutdown()
         gtk.main_quit()
 
     def _setup_filechooser_dialog(self, title, action, extra_widget_title):
index 6193de9..04c7026 100644 (file)
@@ -53,6 +53,7 @@ class ItemListModel:
 
         if not items:
             return
+        i = 0
 
         for item in items:
             weight = (pango.WEIGHT_BOLD, pango.WEIGHT_NORMAL)[item.is_read]
@@ -60,6 +61,8 @@ class ItemListModel:
             #colour = ("#0000FF", "#000000")[item.is_read]
             colour = "#000000"
             treeiter = self.store.append()
+            #print self.store[i]
+            
             self.store.set(treeiter,
                             Column.TITLE, item.title,
                             Column.PUB_DATE, item.pub_date,
@@ -69,6 +72,7 @@ class ItemListModel:
                             Column.FOREGROUND, colour)
 
             item.connect("notify", self.item_changed_cb)
+            i += 1
 
     def item_changed_cb(self, item, property):
         row = [row for row in self.store if row[Column.ITEM] is item]
@@ -285,16 +289,26 @@ class ItemListPresenter(MVP.BasicPresenter):
         items = []
         node = nodes[0]
 
+        def populate_items(items):
+            self.view._widget.set_model(None)
+            #import time
+            #s = time.time()
+            self.model.populate(items)
+            #print "populate took %s" % (time.time() - s)
+            #gobject.idle_add(self.model.populate, items)
+            self.view._widget.set_model(self._model.model)
+
         if node.node.type == "F":
             items = ItemManager.get_feed_items(node.node.id)
+            populate_items(items)
         elif node.node.type == "C":
-            items = ItemManager.get_category_items(node.node.id)
+            items = ItemManager.get_category_items(node.node.id, populate_items)
 
-        self.view._widget.set_model(None)
-        self.model.populate(items)
-        self.view._widget.set_model(self._model.model)
+        #self.view._widget.set_model(None)
+        #self.model.populate(items)
+        #self.view._widget.set_model(self._model.model)
 
-        if not len(self.model.model): return
+        #if not len(self.model.model): return
         #if not self.view.select_next_unread_item():
         #    self.view.select_first_item()
 
index 7c50853..918b26c 100644 (file)
@@ -3,11 +3,29 @@ from gobject import GObject, SIGNAL_RUN_FIRST
 from model import Feed, Item, Category
 from storage import DAO, Storage
 import FeedManager
+import Queue
 import gobject
+import threading
 
 _storage_path = None
-
 model_data = None
+calls = Queue.Queue()
+
+class ServiceThread(threading.Thread):
+    def run(self):
+        global calls
+
+        while 1:
+            call = calls.get()
+
+            if not call:
+                break
+
+            result = call[0](call[1][0])
+            gobject.idle_add(call[2], result)
+
+serviceThread = ServiceThread()
+serviceThread.start()
 
 def init():
     fm = _get_instance()
@@ -17,13 +35,22 @@ def setup(storage_path = None):
     global _storage_path
     _storage_path = storage_path
 
+def shutdown():
+    calls.put(None)
+    serviceThread.join()
+
 def get_feed_items(id):
     im = _get_instance()
     return im.get_feed_items(id)
 
-def get_category_items(id):
+def get_category_items(id, callback):
+    global calls
+
     im = _get_instance()
-    return im.get_category_items(id)
+    calls.put_nowait((im.get_category_items, (id,), callback))
+    #print calls.get_nowait()
+    #return im.get_category_items(id)
+    return []
 
 def feed_item_exists(item):
     im = _get_instance()
@@ -54,7 +81,9 @@ class ItemManager(GObject):
 
         in_str = ", ".join([str(feed.id) for feed in FeedManager.get_children_feeds(id)])
 
-        query = "SELECT id, title, feed_id, is_read, link, pub_date, description FROM items WHERE feed_id IN(%s) ORDER BY pub_date DESC" % in_str
+        query = "SELECT id, title, feed_id, is_read, link, pub_date FROM items WHERE feed_id IN(%s) ORDER BY pub_date DESC" % in_str
+        #query = "SELECT id, title, feed_id, is_read, link, pub_date FROM items WHERE feed_id IN(%s) ORDER BY pub_date DESC" % in_str# LIMIT %d OFFSET %d" % (in_str, size, start)
+        #query = "SELECT id, title, feed_id, is_read, link, pub_date FROM items ORDER BY pub_date DESC"# LIMIT %d OFFSET %d" % (in_str, size, start)
 
         items = self.dao.get(Item, query = query)
 
index a4ba852..83d6ba6 100644 (file)
@@ -133,18 +133,31 @@ class DAO(object):
         return result[0]
 
     def get(self, clazz, sql = "", query = None, params = []):
+        #import time
+        #s = time.time()
         if not query:
             result = self.storage.query("SELECT * FROM %s%s" % (clazz.persistent_table, sql), params)
         else:
             result = self.storage.query(query, params)
 
+        fields = clazz.persistent_properties
+
+        if len(result) > 0:
+            fields = filter(lambda x: x in fields, result[0].keys())
+
         entities = []
 
+        #print "query took %s" % (time.time() - s)
+        
+        #s = time.time()
+
         for row in result:
             entity = clazz()
-            [setattr(entity, field, row[field]) for field in clazz.persistent_properties]
+            [setattr(entity, field, row[field]) for field in fields]
             entities.append(entity)
 
+        #print "hydration took %s" % (time.time() - s)
+
         return entities
 
     def get_indexed(self, clazz, index_field, sql = ""):
index c89f263..c82b2c9 100644 (file)
@@ -56,8 +56,8 @@ class SQLiteStorage:
 
         if not self._connections.has_key(key):
             self._connections[key] = sqlite.connect(self._db_path)
-            self._connections[key].execute("PRAGMA cache_size = 20000;")
-            self._connections[key].execute("PRAGMA synchronous = NORMAL;")
+            #self._connections[key].execute("PRAGMA cache_size = 20000;")
+            #self._connections[key].execute("PRAGMA synchronous = NORMAL;")
             self._connections[key].row_factory = sqlite.Row
 
         return self._connections[key]