[rygel] media-export: Cache exists information



commit 1bd047f87738b17f8e7c55954ff3644e1249f3f4
Author: Jens Georg <mail jensge org>
Date:   Sat Apr 16 15:50:42 2011 +0300

    media-export: Cache exists information
    
    Exists information is cached for the first run of rygel. This speeds up
    initial start-up (and lowers stress on SQLite)

 .../rygel-media-export-media-cache.vala            |   43 +++++++++++++++++--
 .../rygel-media-export-sql-factory.vala            |    9 ++++-
 2 files changed, 46 insertions(+), 6 deletions(-)
---
diff --git a/src/plugins/media-export/rygel-media-export-media-cache.vala b/src/plugins/media-export/rygel-media-export-media-cache.vala
index 7bbc562..7157cb7 100644
--- a/src/plugins/media-export/rygel-media-export-media-cache.vala
+++ b/src/plugins/media-export/rygel-media-export-media-cache.vala
@@ -37,15 +37,21 @@ internal enum Rygel.MediaExport.ObjectType {
     ITEM
 }
 
+internal struct Rygel.MediaExport.ExistsCacheEntry {
+    int64 mtime;
+    int64 size;
+}
+
 /**
  * Persistent storage of media objects
  *
  *  MediaExportDB is a sqlite3 backed persistent storage of media objects
  */
 public class Rygel.MediaExport.MediaCache : Object {
-    private Database db;
-    private ObjectFactory factory;
-    private SQLFactory sql;
+    private Database                           db;
+    private ObjectFactory                      factory;
+    private SQLFactory                         sql;
+    private HashMap<string, ExistsCacheEntry?> exists_cache;
 
     private static MediaCache instance;
 
@@ -154,14 +160,40 @@ public class Rygel.MediaExport.MediaCache : Object {
         return count;
     }
 
+
+    private void get_exists_cache () throws DatabaseError {
+        this.exists_cache = new HashMap<string, ExistsCacheEntry?> ();
+        this.db.exec (this.sql.make (SQLString.EXISTS_CACHE),
+                      null,
+                      (statement) => {
+                          var entry = ExistsCacheEntry ();
+                          entry.mtime = statement.column_int64 (1);
+                          entry.size = statement.column_int64 (0);
+                          this.exists_cache.set (statement.column_text (2),
+                                                 entry);
+
+                          return true;
+                      });
+    }
+
     public bool exists (File      file,
                         out int64 timestamp,
                         out int64 size) throws DatabaseError {
-        bool exists = false;
-        GLib.Value[] values = { file.get_uri () };
+        var exists = false;
+        var uri = file.get_uri ();
+        GLib.Value[] values = { uri };
         int64 tmp_timestamp = 0;
         int64 tmp_size = 0;
 
+        if (this.exists_cache.has_key (uri)) {
+            var entry = this.exists_cache.get (uri);
+            this.exists_cache.unset (uri);
+            timestamp = entry.mtime;
+            size = entry.size;
+
+            return true;
+        }
+
         this.db.exec (this.sql.make (SQLString.EXISTS),
                       values,
                       (statement) => {
@@ -357,6 +389,7 @@ public class Rygel.MediaExport.MediaCache : Object {
         this.sql = new SQLFactory ();
         this.open_db ("media-export");
         this.factory = new ObjectFactory ();
+        this.get_exists_cache ();
     }
 
     private void open_db (string name) throws Error {
diff --git a/src/plugins/media-export/rygel-media-export-sql-factory.vala b/src/plugins/media-export/rygel-media-export-sql-factory.vala
index 2a551af..95b719d 100644
--- a/src/plugins/media-export/rygel-media-export-sql-factory.vala
+++ b/src/plugins/media-export/rygel-media-export-sql-factory.vala
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010 Jens Georg <mail jensge org>.
+ * Copyright (C) 2010,2011 Jens Georg <mail jensge org>.
  *
  * Author: Jens Georg <mail jensge org>
  *
@@ -64,6 +64,7 @@ internal enum Rygel.MediaExport.SQLString {
     TRIGGER_COMMON,
     INDEX_COMMON,
     SCHEMA,
+    EXISTS_CACHE,
 }
 
 internal class Rygel.MediaExport.SQLFactory : Object {
@@ -234,6 +235,10 @@ internal class Rygel.MediaExport.SQLFactory : Object {
     "CREATE INDEX IF NOT EXISTS idx_uri on Object(uri);";
 
 
+    private const string EXISTS_CACHE_STRING =
+    "SELECT m.size, o.timestamp, o.uri FROM Object o " +
+        "JOIN meta_data m ON o.upnp_id = m.object_fk";
+
     public unowned string make (SQLString query) {
         switch (query) {
             case SQLString.SAVE_METADATA:
@@ -268,6 +273,8 @@ internal class Rygel.MediaExport.SQLFactory : Object {
                 return CREATE_INDICES_STRING;
             case SQLString.SCHEMA:
                 return SCHEMA_STRING;
+            case SQLString.EXISTS_CACHE:
+                return EXISTS_CACHE_STRING;
             case SQLString.TABLE_CLOSURE:
                 return CREATE_CLOSURE_TABLE;
             default:



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]