86962915d4b469fe2f1de16fe485c86173a35eb0
angie
  Fri Jun 14 09:07:09 2013 -0700
Fixing code review bugs -- Tim noticed I was not using lm when I meant to, and pointed out a contradiction in the way I was using nextChunkStart. refs #11034
diff --git src/hg/lib/annoStreamDb.c src/hg/lib/annoStreamDb.c
index 890b65c..9aca5b2 100644
--- src/hg/lib/annoStreamDb.c
+++ src/hg/lib/annoStreamDb.c
@@ -152,38 +152,38 @@
 else if (self->notSorted)
     dyStringPrintf(query, " order by %s,%s", self->chromField, self->startField);
 if (self->maxOutRows > 0)
     dyStringPrintf(query, " limit %d", self->maxOutRows);
 struct sqlResult *sr = sqlGetResult(self->conn, query->string);
 dyStringFree(&query);
 self->sr = sr;
 }
 
 static void rowBufInit(struct rowBuf *rowBuf, int size)
 /* Clean up rowBuf and give it a new lm and buffer[size]. */
 {
 resetRowBuf(rowBuf);
 rowBuf->lm = lmInit(0);
 rowBuf->size = size;
-AllocArray(rowBuf->buf, size);
+lmAllocArray(rowBuf->lm, rowBuf->buf, size);
 }
 
 static char **lmCloneRow(struct lm *lm, char **row, int colCount)
 /* Use lm to allocate an array of strings and its contents copied from row. */
 {
 char **cloneRow = NULL;
-AllocArray(cloneRow, colCount);
+lmAllocArray(lm, cloneRow, colCount);
 int i;
 for (i = 0;  i < colCount;  i++)
     cloneRow[i] = lmCloneString(lm, row[i]);
 return cloneRow;
 }
 
 static void updateNextChunkState(struct annoStreamDb *self, int queryMaxItems)
 /* If the just-fetched interval list was limited to ASD_CHUNK_SIZE, set doNextChunk
  * and trim the last row(s) so that when we query the next chunk, we don't get
  * repeat rows due to querying a start coord that was already returned. */
 {
 struct rowBuf *rowBuf = &self->rowBuf;
 if (queryMaxItems == ASD_CHUNK_SIZE && rowBuf->size == ASD_CHUNK_SIZE)
     {
     self->doNextChunk = TRUE;
@@ -266,30 +266,32 @@
     {
     uint start = sSelf->regionStart;
     if (minChrom)
 	{
 	if (differentString(minChrom, sSelf->chrom))
 	    errAbort("annoStreamDb %s: nextRow minChrom='%s' but region chrom='%s'",
 		     sSelf->name, minChrom, sSelf->chrom);
 	if (start < minEnd)
 	    start = minEnd;
 	}
     if (self->doNextChunk && start < self->nextChunkStart)
 	start = self->nextChunkStart;
     dyStringPrintf(query, "where %s = '%s' and ", self->chromField, sSelf->chrom);
     if (self->hasBin)
 	hAddBinToQuery(start, sSelf->regionEnd, query);
+    if (self->doNextChunk)
+	dyStringPrintf(query, "%s >= %u and ", self->startField, self->nextChunkStart);
     dyStringPrintf(query, "%s < %u and %s > %u limit %d", self->startField, sSelf->regionEnd,
 		   self->endField, start, queryMaxItems);
     bufferRowsFromSqlQuery(self, query->string, queryMaxItems);
     }
 else
     {
     // Genome-wide query: break it into chrom-by-chrom queries.
     if (self->queryChrom == NULL)
 	self->queryChrom = self->chromList;
     else if (!self->doNextChunk)
 	self->queryChrom = self->queryChrom->next;
     if (minChrom != NULL)
 	{
 	// Skip chroms that precede minChrom
 	while (self->queryChrom != NULL && strcmp(self->queryChrom->name, minChrom) < 0)
@@ -298,38 +300,39 @@
 	    self->doNextChunk = FALSE;
 	    }
 	}
     if (self->queryChrom == NULL)
 	self->eof = TRUE;
     else
 	{
 	char *chrom = self->queryChrom->name;
 	int start = 0;
 	if (minChrom != NULL && sameString(chrom, minChrom))
 	    start = minEnd;
 	if (self->doNextChunk && start < self->nextChunkStart)
 	    start = self->nextChunkStart;
 	uint end = annoAssemblySeqSize(self->streamer.assembly, self->queryChrom->name);
 	dyStringPrintf(query, "where %s = '%s' ", self->chromField, chrom);
-	if (start > 0)
+	if (start > 0 || self->doNextChunk)
 	    {
 	    dyStringAppend(query, "and ");
 	    if (self->hasBin)
 		hAddBinToQuery(start, end, query);
+	    if (self->doNextChunk)
+		dyStringPrintf(query, "%s >= %u and ", self->startField, self->nextChunkStart);
 	    // region end is chromSize, so no need to constrain startField here:
-	    dyStringPrintf(query, "%s > %u ",
-			   self->endField, start);
+	    dyStringPrintf(query, "%s > %u ", self->endField, start);
 	    }
 	dyStringPrintf(query, "limit %d", queryMaxItems);
 	bufferRowsFromSqlQuery(self, query->string, queryMaxItems);
 	// If there happens to be no items on chrom, try again with the next chrom:
 	if (! self->eof && self->rowBuf.size == 0)
 	    asdDoQueryChunking(self, minChrom, minEnd);
 	}
     }
 dyStringFree(&query);
 }
 
 static char **nextRowFromBuffer(struct annoStreamDb *self, char *minChrom, uint minEnd)
 /* Instead of streaming directly from self->sr, we have buffered up the results
  * of a chunked query; return the head of that queue. */
 {