Revision: 2262
          http://gtkpod.svn.sourceforge.net/gtkpod/?rev=2262&view=rev
Author:   teuf
Date:     2009-02-28 10:39:39 +0000 (Sat, 28 Feb 2009)

Log Message:
-----------
Replace linear look-up of songs with local hashtable.

Modified Paths:
--------------
    libgpod/trunk/ChangeLog
    libgpod/trunk/src/db-artwork-parser.c

Modified: libgpod/trunk/ChangeLog
===================================================================
--- libgpod/trunk/ChangeLog     2009-02-25 13:19:01 UTC (rev 2261)
+++ libgpod/trunk/ChangeLog     2009-02-28 10:39:39 UTC (rev 2262)
@@ -1,3 +1,8 @@
+2009-02-28  Javier Kohen <[email protected]>
+
+       * src/db-artwork-parser: replace linear look-up of songs with
+       local hashtable.
+
 2009-02-09  Christophe Fergeau  <[email protected]>
 
        * src/itdb_itunesdb.c: fix crash when generating the album list

Modified: libgpod/trunk/src/db-artwork-parser.c
===================================================================
--- libgpod/trunk/src/db-artwork-parser.c       2009-02-25 13:19:01 UTC (rev 
2261)
+++ libgpod/trunk/src/db-artwork-parser.c       2009-02-28 10:39:39 UTC (rev 
2262)
@@ -43,23 +43,7 @@
 
 typedef int (*ParseListItem)(DBParseContext *ctx, GError *error);
 
-static Itdb_Track *
-get_song_by_dbid (Itdb_iTunesDB *db, guint64 id)
-{
-       GList *it;
 
-       for (it = db->tracks; it != NULL; it = it->next) {
-               Itdb_Track *song;
-
-               song = (Itdb_Track*)it->data;
-               if (song->dbid == id) {
-                       return song;
-               }
-       }
-       return NULL;
-}
-
-
 static int
 parse_mhif (DBParseContext *ctx, GError *error)
 {
@@ -483,6 +467,30 @@
 }
 
 
+/* Compares the two guint64 values being pointed to and returns TRUE if
+ * they are equal. It can be passed to g_hash_table_new() as the
+ *  key_equal_func parameter, when using pointers to guint64 as keys
+ * in a GHashTable.
+ */
+static gboolean
+guint64_equal (gconstpointer v1, gconstpointer v2)
+{
+    guint64 i1 = *(const guint64*)v1;
+    guint64 i2 = *(const guint64*)v2;
+    return i1 == i2;
+}
+
+/* Converts a pointer to a guint64 to a hash value. It can be passed to
+ * g_hash_table_new() as the hash_func parameter, when using pointers
+ * to guint64 values as keys in a GHashTable.
+ */
+static guint
+guint64_hash(gconstpointer v)
+{
+    guint64 i = *(const guint64*)v;
+    return i ^ (i >> 32);
+}
+
 /* Apple introduced a new way to associate artwork. The former way
  * used the dbid to link each artwork (mhii) back to the track. The
  * new way uses the mhii id to link from each track to the mhii. Above
@@ -491,6 +499,7 @@
 mhfd_associate_itunesdb_artwork (DBParseContext *ctx)
 {
     GHashTable *mhii_id_hash;
+    GHashTable *song_dbid_hash;
     Itdb_iTunesDB *itdb;
     GList *gl;
 
@@ -498,6 +507,15 @@
     itdb = db_get_itunesdb (ctx->db);
     g_return_val_if_fail (itdb, -1);
 
+    /* make a hash linking the dbid with the songs for faster
+       lookup */
+    song_dbid_hash = g_hash_table_new (guint64_hash, guint64_equal);
+
+    for (gl = itdb->tracks; gl != NULL; gl = gl->next) {
+       Itdb_Track *song = (Itdb_Track*)gl->data;
+       g_hash_table_insert (song_dbid_hash, &song->dbid, song);
+    }
+
     /* make a hash linking the mhii with the artwork for faster
        lookup */
     mhii_id_hash = g_hash_table_new_full (g_direct_hash,
@@ -515,7 +533,7 @@
 
        /* add Artwork to track indicated by the dbid for backward
           compatibility */
-       track = get_song_by_dbid (itdb, artwork->dbid);
+       track = g_hash_table_lookup (song_dbid_hash, &artwork->dbid);
        if (track == NULL)
        {
            gchar *strval = g_strdup_printf("%" G_GINT64_FORMAT, artwork->dbid);
@@ -576,6 +594,7 @@
        }
     }
     g_hash_table_destroy (mhii_id_hash);
+    g_hash_table_destroy (song_dbid_hash);
     /* The actual ItdbArtwork data was freed through the GHashTable
        value_destroy_func */
     g_list_free (*ctx->artwork);


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
gtkpod-cvs2 mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gtkpod-cvs2

Reply via email to