src/hg/lib/hdb.c 1.429

1.429 2010/05/18 22:36:36 kent
Adding hGetTableForTrack. Refactoring things so that this and hGetTrackForTable share the same settings hash.
Index: src/hg/lib/hdb.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/hg/lib/hdb.c,v
retrieving revision 1.428
retrieving revision 1.429
diff -b -B -U 4 -r1.428 -r1.429
--- src/hg/lib/hdb.c	18 May 2010 20:05:31 -0000	1.428
+++ src/hg/lib/hdb.c	18 May 2010 22:36:36 -0000	1.429
@@ -3749,11 +3749,14 @@
 hFreeConn(&conn);
 return ret;
 }
 
-static struct hash *makeDbTableToTrackHash(char *db)
-/* Create a hash based on trackDb tables in given database that 
- * will give you a track name given a table name as a key. */
+static struct hash *makeTrackSettingsHash(char *db)
+/* Create  a hash of hashes with all track settings for database.
+ * The returned hash is keyed by track.   The contained hashes
+ * are keyed by tags and contain generic text values, corresponding
+ * to the trackDb.ra settings for that track. Generally you want to
+ * call the version that caches results below instead. */
 {
 struct hash *hash = hashNew(0);
 struct slName *trackTable, *trackTableList = hTrackDbList();
 struct sqlConnection *conn = hAllocConn(db);
@@ -3766,14 +3769,11 @@
 	struct sqlResult *sr = sqlGetResult(conn, query);
 	char **row;
 	while ((row = sqlNextRow(sr)) != NULL)
 	    {
-	    struct hash *settings = raFromString(row[0]);
+	    struct hash *settings = trackDbSettingsFromString(row[0]);
 	    char *track = hashMustFindVal(settings, "track");
-	    char *table = hashFindVal(settings, "table");
-	    if (table == NULL)
-	         table = track;
-	    hashAdd(hash, table, track);
+	    hashAdd(hash, track, settings);
 	    }
 	sqlFreeResult(&sr);
 	}
     }
@@ -3781,8 +3781,49 @@
 hFreeConn(&conn);
 return hash;
 }
 
+struct hash *hTdbGetTrackSettingsHash(char *db)
+/* Get a hash of hashes with all track settings for database.
+ * The returned hash is keyed by track.   The contained hashes
+ * are keyed by tags and contain generic text values, corresponding
+ * to the trackDb.ra settings for that track.  The result returned
+ * is cached, and should not be altered. */
+{
+static struct hash *dbHash = NULL;
+if (dbHash == NULL)
+    dbHash = hashNew(0);
+struct hash *hoh = hashFindVal(dbHash, db);
+if (hoh == NULL)
+    {
+    hoh = makeTrackSettingsHash(db);
+    hashAdd(dbHash, db, hoh);
+    }
+return hoh;
+}
+
+static struct hash *makeDbTableToTrackHash(char *db)
+/* Create a hash based on trackDb tables in given database that 
+ * will give you a track name given a table name as a key. */
+{
+struct hash *hoh = hTdbGetTrackSettingsHash(db);
+struct hash *tableToTrackHash = hashNew(0);
+struct sqlConnection *conn = hAllocConn(db);
+struct hashCookie cookie = hashFirst(hoh);
+struct hashEl *hel;
+while ((hel = hashNext(&cookie)) != NULL)
+    {
+    struct hash *settings = hel->val;
+    char *track = hashMustFindVal(settings, "track");
+    char *table = hashFindVal(settings, "table");
+    if (table == NULL)
+	 table = track;
+    hashAdd(tableToTrackHash, table, track);
+    }
+hFreeConn(&conn);
+return tableToTrackHash;
+}
+
 char *hGetTrackForTable(char *db, char *table)
 /* Given a table name, get first track associated with it. */
 {
 static struct hash *dbHash = NULL;
@@ -3796,8 +3837,21 @@
     }
 return hashFindVal(tableToTrackHash, table);
 }
 
+char *hGetTableForTrack(char *db, char *track)
+/* Given a track name, get table associated with it. */
+{
+struct hash *hoh = hTdbGetTrackSettingsHash(db);
+struct hash *settings = hashFindVal(hoh, track);
+if (settings == NULL)
+    errAbort("Couldn't find settigns for track %s in hGetTableForTrack", track);
+char *table = hashFindVal(settings, "table");
+if (table == NULL)
+    table = track;
+return table;
+}
+
 static struct dbDb *hGetIndexedDbsMaybeClade(char *theDb)
 /* Get list of active databases, in theDb's clade if theDb is not NULL.
  * Dispose of this with dbDbFreeList. */
 {