Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixed inefficient/incorrect sql query that outputs stories of a topic
across all its categories.
  • Loading branch information
subbuss committed Apr 4, 2012
1 parent 8f808ed commit d5fc27b
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 8 deletions.
76 changes: 69 additions & 7 deletions src/newsrack/database/sql/SQL_DB.java
Expand Up @@ -2259,15 +2259,77 @@ public List<NewsItem> getNews(Category cat, int numArts)
return getNews(cat, null, null, null, 0, numArts); return getNews(cat, null, null, null, 0, numArts);
} }


public List<NewsItem> getNews(Issue i, Date start, Date end, Source src, int startId, int numArts) public List<NewsItem> getNews(Issue issue, Date start, Date end, Source src, int startId, int numArts)
{ {
Long issueKey = issue.getKey();
String cacheKey = (start == null) ? "ISSUE:" + issueKey + (src == null ? "" : ":" + src.getKey()) + ":" + startId + ":" + numArts : null;
Object keys = (start == null) ? (List)_cache.get("LIST", cacheKey) : null;

if (keys == null) {
// Initialize ...
StringBuffer queryBuf = new StringBuffer();
List argList = new ArrayList();
List<SQL_ValType> argTypeList = new ArrayList<SQL_ValType>();

// Init query
queryBuf.append("SELECT DISTINCT(c.n_key) FROM cat_news c");

// Add conditions for feed-specific news
if (src != null) {
queryBuf.append(" JOIN news_collections nc ON nc.n_key = c.n_key AND nc.feed_key = ?");
argList.add(src.getFeed().getKey());
argTypeList.add(SQL_ValType.LONG);
}

// Add category-specific conditions
queryBuf.append(" WHERE c.c_key IN (");
List<Long> allLeafCatKeys = (List<Long>)GET_LEAF_CAT_KEYS_FOR_ISSUE.get(issueKey);
Iterator<Long> it = allLeafCatKeys.iterator();
while (it.hasNext()) {
queryBuf.append(it.next());
if (it.hasNext())
queryBuf.append(",");
}
queryBuf.append(")");

// Add conditions for date-limited news
if (start != null) {
queryBuf.append(" AND date_stamp >= ? AND date_stamp <= ?");
argList.add(new java.sql.Date(start.getTime()));
argList.add(new java.sql.Date(end.getTime()));
argTypeList.add(SQL_ValType.DATE);
argTypeList.add(SQL_ValType.DATE);
}

// Add sorting and limiting constraints
queryBuf.append(" ORDER by date_stamp DESC, n_key DESC LIMIT ?, ?");
argList.add(startId);
argList.add(numArts);
argTypeList.add(SQL_ValType.INT);
argTypeList.add(SQL_ValType.INT);

// Have to do this nonsense because generic type info and type parameter info is lost at runtime ...
Object[] tmp = argTypeList.toArray();
SQL_ValType[] argTypes = new SQL_ValType[tmp.length];
int i = 0;
for (Object v: tmp) {
argTypes[i] = (SQL_ValType)v;
i++;
}

if (_log.isDebugEnabled()) _log.debug("Executing: " + queryBuf.toString() + " with start value " + startId);

// Run the query and fetch news!
keys = SQL_StmtExecutor.execute(queryBuf.toString(), SQL_StmtType.QUERY, argList.toArray(), argTypes, null, new GetLongResultProcessor(), false);

// Caching non-datestamp requests right now
if (start == null)
_cache.add("LIST", new String[]{issue.getUser().getKey().toString(), "ISSUE:" + issueKey}, cacheKey, keys);
}

// Set up the list of news items
List<NewsItem> news = new ArrayList<NewsItem>(); List<NewsItem> news = new ArrayList<NewsItem>();
List<Long> keys; for (Long k: (List<Long>)keys)
if (start == null)
keys = (List<Long>)GET_NEWS_KEYS_FROM_ISSUE.execute(new Object[] {i.getKey(), startId, numArts});
else
keys = (List<Long>)GET_NEWS_KEYS_FROM_ISSUE_BETWEEN_DATES.execute(new Object[] {i.getKey(), new java.sql.Date(start.getTime()), new java.sql.Date(end.getTime()), startId, numArts});
for (Long k: keys)
news.add(getNewsItem(k)); news.add(getNewsItem(k));


return news; return news;
Expand Down
10 changes: 9 additions & 1 deletion src/newsrack/database/sql/SQL_Stmt.java
Expand Up @@ -562,7 +562,6 @@ public enum SQL_Stmt
new GetNewsItemResultProcessor(), new GetNewsItemResultProcessor(),
false false
), ),
**/
GET_NEWS_KEYS_FROM_ISSUE( GET_NEWS_KEYS_FROM_ISSUE(
"SELECT n_key FROM cat_news cn, categories c WHERE c.t_key = ? AND cn.c_key = c.c_key ORDER by date_stamp DESC, n_key DESC LIMIT ?, ?", "SELECT n_key FROM cat_news cn, categories c WHERE c.t_key = ? AND cn.c_key = c.c_key ORDER by date_stamp DESC, n_key DESC LIMIT ?, ?",
new SQL_ValType[] {LONG, INT, INT}, new SQL_ValType[] {LONG, INT, INT},
Expand All @@ -579,6 +578,7 @@ public enum SQL_Stmt
new GetLongResultProcessor(), new GetLongResultProcessor(),
false false
), ),
**/
GET_LEAF_CAT_KEYS_FOR_NEWSITEM( GET_LEAF_CAT_KEYS_FOR_NEWSITEM(
"SELECT cn.c_key FROM cat_news cn, categories c WHERE n_key = ? AND cn.c_key = c.c_key AND c.valid = true", "SELECT cn.c_key FROM cat_news cn, categories c WHERE n_key = ? AND cn.c_key = c.c_key AND c.valid = true",
new SQL_ValType[] {LONG}, new SQL_ValType[] {LONG},
Expand Down Expand Up @@ -805,6 +805,14 @@ public enum SQL_Stmt
new GetIssueResultProcessor(), new GetIssueResultProcessor(),
false false
), ),
GET_LEAF_CAT_KEYS_FOR_ISSUE(
"SELECT c.c_key FROM categories c WHERE c.t_key = ? AND c.valid = TRUE AND c.rgt = c.lft + 1",
new SQL_ValType[] {LONG},
SQL_StmtType.QUERY,
null,
new GetLongResultProcessor(),
false
),
GET_CAT_KEYS_FOR_ISSUE( GET_CAT_KEYS_FOR_ISSUE(
"SELECT c_key FROM categories WHERE t_key = ? AND valid = true", "SELECT c_key FROM categories WHERE t_key = ? AND valid = true",
new SQL_ValType[] {LONG}, new SQL_ValType[] {LONG},
Expand Down

0 comments on commit d5fc27b

Please sign in to comment.