[Xfce4-commits] <postler:master> Message queries and indexing should be asynchronous

Christian Dywan noreply at xfce.org
Fri May 27 20:32:06 CEST 2011


Updating branch refs/heads/master
         to 0ebe7a4d493ee7b863138ae4360a0f0d34b15ba8 (commit)
       from 72659e85c0c69ab877a017f1d521d3a6b8f6cc52 (commit)

commit 0ebe7a4d493ee7b863138ae4360a0f0d34b15ba8
Author: Christian Dywan <christian at twotoasts.de>
Date:   Wed May 25 18:34:38 2011 +0200

    Message queries and indexing should be asynchronous

 postler/postler-client.vala  |   10 +++++-----
 postler/postler-folders.vala |    5 +++--
 postler/postler-index.vala   |   24 ++++++++++++++++++++++--
 postler/postler-service.vala |   40 +++++++++++++++++++++++++++-------------
 4 files changed, 57 insertions(+), 22 deletions(-)

diff --git a/postler/postler-client.vala b/postler/postler-client.vala
index af356f4..ef026f0 100644
--- a/postler/postler-client.vala
+++ b/postler/postler-client.vala
@@ -13,9 +13,9 @@ namespace Postler {
 
     [DBus (name = "org.elementary.Postler")]
     interface PostlerClient : Object {
-        public abstract int64 unread_messages (string uri) throws IOError;
+        public async abstract int64 unread_messages (string uri) throws IOError;
         public async abstract GLib.HashTable<string,Variant> get_message (string uri) throws IOError;
-        public abstract string[] get_messages (string uri) throws IOError;
+        public async abstract string[] get_messages (string uri) throws IOError;
         public signal void progress (string account, string text, double fraction);
         public abstract void receive (string account) throws IOError;
         public signal void received (string account, string error_message);
@@ -52,8 +52,8 @@ namespace Postler {
             } catch (GLib.Error error) { }
         }
 
-        public int64 unread_messages (string uri) throws GLib.Error {
-            return client.unread_messages (uri);
+        public async int64 unread_messages (string uri) throws GLib.Error {
+            return yield client.unread_messages (uri);
         }
 
         public async void get_message (Message message, Gtk.TreeRowReference row) throws GLib.Error {
@@ -65,7 +65,7 @@ namespace Postler {
 
         public async GLib.List<Message> get_messages (string uri) throws GLib.Error {
             var messages = new GLib.List<Message> ();
-            string[] uris = client.get_messages (uri);
+            string[] uris = yield client.get_messages (uri);
             foreach (string message_uri in uris)
                 messages.append (new Message.from_uri (message_uri));
             return messages;
diff --git a/postler/postler-folders.vala b/postler/postler-folders.vala
index 6d5175b..c737e85 100644
--- a/postler/postler-folders.vala
+++ b/postler/postler-folders.vala
@@ -210,9 +210,10 @@ public class Postler.Folders : Gtk.TreeView {
         assert_not_reached ();
     }
 
-    void unread_count_update (Gtk.TreeIter iter, File msg_dir, string label) {
+    async void unread_count_update (Gtk.TreeIter iter, File msg_dir, string label) {
         try {
-            int64 unread = new Postler.Client ().unread_messages (msg_dir.get_uri () + "%");
+            var client = new Client ();
+            int64 unread = yield client.unread_messages (msg_dir.get_uri () + "%");
             string escaped = GLib.Markup.escape_text (label);
             if (unread == 0)
                 store.set (iter, Columns.DISPLAY_NAME, "%s".printf (escaped));
diff --git a/postler/postler-index.vala b/postler/postler-index.vala
index cc97241..a611ed3 100644
--- a/postler/postler-index.vala
+++ b/postler/postler-index.vala
@@ -15,6 +15,7 @@ namespace Postler {
         static Sqlite.Statement? statement_insert = null;
         static Sqlite.Statement? statement_get = null;
         static Sqlite.Statement? statement_list = null;
+        static Sqlite.Statement? statement_count = null;
         static Sqlite.Statement? statement_unread = null;
 
         bool init (GLib.Cancellable? cancellable = null) throws GLib.Error {
@@ -46,9 +47,8 @@ namespace Postler {
 
         public void insert_received_message (Message message) throws GLib.Error {
             if (statement_insert == null) {
-                /* TODO: REPLACE or IGNORE? */
                 if (database.prepare_v2 ("""
-                    INSERT OR REPLACE INTO messages
+                    INSERT OR IGNORE INTO messages
                     (uri, subject, sender, recipients, unread, flagged, priority, date)
                     VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)
                     """,
@@ -82,6 +82,26 @@ namespace Postler {
 #endif
         }
 
+        public int64 count_messages (string uri) throws GLib.Error {
+            if (statement_count == null) {
+                if (database.prepare_v2 ("""
+                    SELECT COUNT (*) FROM messages WHERE uri LIKE ?1
+                    """,
+                    -1, out statement_count) != Sqlite.OK)
+                    throw new GLib.FileError.FAILED (_("Failed to open database: %s"), database.errmsg ());
+            }
+            bool success =
+                statement_count.bind_text (1, uri, -1) == Sqlite.OK
+             && statement_count.step () == Sqlite.ROW;
+            if (!success) {
+                statement_count.reset ();
+                throw new GLib.FileError.FAILED (_("Failed to open database: %s"), database.errmsg ());
+            }
+            int64 count = statement_count.column_int64 (0);
+            statement_count.reset ();
+            return count;
+        }
+
         public int64 unread_messages (string folder) throws GLib.Error {
             if (statement_unread == null) {
                 if (database.prepare_v2 ("""
diff --git a/postler/postler-service.vala b/postler/postler-service.vala
index 0efc401..5ae5c0a 100644
--- a/postler/postler-service.vala
+++ b/postler/postler-service.vala
@@ -175,7 +175,24 @@ namespace Postler {
             return true;
         }
 
-        void setup_index (Index index, Accounts accounts) {
+        async void index_folder (File folder) throws GLib.Error {
+            var enumerator = yield folder.enumerate_children_async (
+                FILE_ATTRIBUTE_STANDARD_NAME, 0, Priority.DEFAULT);
+            while (true) {
+                var files = yield enumerator.next_files_async (10, Priority.DEFAULT);
+                if (files == null)
+                    break;
+                foreach (var info in files) {
+                    var file = folder.resolve_relative_path (info.get_name ());
+                    if (index.count_messages (file.get_uri ()) < 1) {
+                        var message = new Message.from_file (file);
+                        index.insert_received_message (message);
+                    }
+                }
+            }
+        }
+
+        async void setup_index (Index index, Accounts accounts) {
             stdout.printf (_("Updating index...\n"));
             foreach (var account_info in accounts.get_infos ()) {
                 if (account_info.type == AccountType.SEARCH)
@@ -186,18 +203,15 @@ namespace Postler {
                     monitor.changed.connect ((monitor, file, other, event) => {            
                         /* TODO */
                     });
-                    var enumerator = folder.enumerate_children (
-                        FILE_ATTRIBUTE_STANDARD_NAME, 0, null);
-                    FileInfo info;
-                    while ((info = enumerator.next_file (null)) != null) {
-                        var cur = folder.resolve_relative_path (info.get_name () + "/new");
-                        var file_enumerator = cur.enumerate_children (
-                            FILE_ATTRIBUTE_STANDARD_NAME, 0, null);
-                        FileInfo file_info;
-                        while ((file_info = file_enumerator.next_file (null)) != null) {
-                            var file = cur.resolve_relative_path (file_info.get_name ());
-                            var message = new Message.from_file (file);
-                            index.insert_received_message (message);
+                    var enumerator = yield folder.enumerate_children_async (
+                        FILE_ATTRIBUTE_STANDARD_NAME, 0, Priority.DEFAULT);
+                    while (true) {
+                        var files = yield enumerator.next_files_async (10, Priority.DEFAULT);
+                        if (files == null)
+                            break;
+                        foreach (var info in files) {
+                            yield index_folder (folder.resolve_relative_path (info.get_name () + "/new"));
+                            yield index_folder (folder.resolve_relative_path (info.get_name () + "/cur"));
                         }
                     }
                     stdout.printf ("    ✓ %s\n", account_info.name);



More information about the Xfce4-commits mailing list