dc3e5f422dd65b728f62aed76c9d30e95dee3575 hiram Tue Sep 8 08:47:17 2020 -0700 hash up the chromAlias lookups to avoid numerous MySQL calls refs #24396 diff --git src/hg/lib/hdb.c src/hg/lib/hdb.c index 9d52867..d606637 100644 --- src/hg/lib/hdb.c +++ src/hg/lib/hdb.c @@ -36,30 +36,31 @@ #include "axtInfo.h" #include "ctgPos.h" #include "hubConnect.h" #include "customTrack.h" #include "hgFind.h" #endif /* GBROWSE */ #include "hui.h" #include "trackHub.h" #include "net.h" #include "udc.h" #include "paraFetch.h" #include "regexHelper.h" #include "filePath.h" #include "wikiLink.h" #include "cheapcgi.h" +#include "chromAlias.h" #ifdef LOWELAB #define DEFAULT_PROTEINS "proteins060115" #define DEFAULT_GENOME "Pyrobaculum aerophilum" #else #define DEFAULT_PROTEINS "proteins" #define DEFAULT_GENOME "Human" #endif static struct sqlConnCache *hdbCc = NULL; /* cache for primary database connection */ static struct sqlConnCache *centralCc = NULL; static char *centralDb = NULL; static struct sqlConnCache *cartCc = NULL; /* cache for cart; normally same as centralCc */ @@ -186,55 +187,68 @@ static struct chromInfo *mustGetChromInfo(char *db, char *chrom) /* Get chromInfo for named chrom from primary database or * die trying. */ { struct chromInfo *ci = hGetChromInfo(db, chrom); if (ci == NULL) errAbort("Couldn't find chromosome/scaffold %s in database", chrom); return ci; } char *hgOfficialChromName(char *db, char *name) /* Returns "canonical" name of chromosome or NULL * if not a chromosome. (Case-insensitive search w/sameWord()) */ { +/* aliasHash will be initialized if chromAlias table exists */ +static struct hash *aliasHash = NULL; + if (strlen(name) > HDB_MAX_CHROM_STRING) return NULL; struct chromInfo *ci = NULL; char buf[HDB_MAX_CHROM_STRING]; strncpy(buf, name, HDB_MAX_CHROM_STRING); buf[HDB_MAX_CHROM_STRING-1] = 0; ci = hGetChromInfo(db, buf); if (ci != NULL) return cloneString(ci->chrom); else { - if (hTableExists(db, "chromAlias")) + if (aliasHash || hTableExists(db, "chromAlias")) + { + if (! aliasHash) /* first time, initialize aliasHash */ { + aliasHash = newHash(4); struct sqlConnection *conn = hAllocConn(db); char query[512]; - char *chrom; - sqlSafef(query, sizeof(query), - "select chrom from chromAlias where alias='%s'", name); - chrom = sqlQuickString(conn, query); + sqlSafef(query, sizeof(query), "select * from chromAlias"); + struct sqlResult *sr = sqlGetResult(conn, query); + char **row; + while ((row = sqlNextRow(sr)) != NULL) + { + struct chromAlias *new = chromAliasLoad(row); + hashAdd(aliasHash, new->alias, cloneString(new->chrom)); + } + sqlFreeResult(&sr); hFreeConn(&conn); - if (isNotEmpty(chrom)) // chrom is already a cloneString result - return chrom; } - return NULL; + char *chrom = (char *)hashFindVal(aliasHash, name); + if (isNotEmpty(chrom)) + return cloneString(chrom); + } } +return NULL; } boolean hgIsOfficialChromName(char *db, char *name) /* Determine if name is exact (case-sensitive) match with * a chromosome in the given assembly */ { char *chrom; return ((chrom = hgOfficialChromName(db, name)) != NULL && sameString(name, chrom)); } static boolean minLen = 0; void setMinIndexLengthForTrashCleaner() /* set the minimum index size so trash cleaner will not die