feat: implement batch queries for improved list performance

Co-authored-by: aider (openai/andrew.openrouter.qwen.qwen3-coder) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-07-29 14:26:26 -03:00
parent 47349802d8
commit 291b6d9587
2 changed files with 76 additions and 21 deletions

View File

@@ -443,3 +443,66 @@ pub fn get_item_meta_value(conn: &Connection, item: &Item, name: String) -> Resu
None => Ok(None),
}
}
pub fn get_tags_for_items(conn: &Connection, item_ids: &[i64]) -> Result<std::collections::HashMap<i64, Vec<Tag>>> {
debug!("DB: Getting tags for items: {:?}", item_ids);
if item_ids.is_empty() {
return Ok(std::collections::HashMap::new());
}
// Create placeholders for the IN clause
let placeholders: Vec<String> = item_ids.iter().map(|_| "?".to_string()).collect();
let placeholders_str = placeholders.join(",");
let sql = format!("SELECT id, name FROM tags WHERE id IN ({}) ORDER BY id ASC, name ASC", placeholders_str);
let mut statement = conn
.prepare_cached(&sql)
.context("Problem preparing SQL statement")?;
let mut rows = statement.query(rusqlite::params_from_iter(item_ids))?;
let mut tags_map: std::collections::HashMap<i64, Vec<Tag>> = std::collections::HashMap::new();
while let Some(row) = rows.next()? {
let id: i64 = row.get(0)?;
let name: String = row.get(1)?;
tags_map.entry(id).or_insert_with(Vec::new).push(Tag { id, name });
}
Ok(tags_map)
}
pub fn get_meta_for_items(conn: &Connection, item_ids: &[i64]) -> Result<std::collections::HashMap<i64, std::collections::HashMap<String, String>>> {
debug!("DB: Getting meta for items: {:?}", item_ids);
if item_ids.is_empty() {
return Ok(std::collections::HashMap::new());
}
// Create placeholders for the IN clause
let placeholders: Vec<String> = item_ids.iter().map(|_| "?".to_string()).collect();
let placeholders_str = placeholders.join(",");
let sql = format!("SELECT id, name, value FROM metas WHERE id IN ({}) ORDER BY id ASC, name ASC", placeholders_str);
let mut statement = conn
.prepare_cached(&sql)
.context("Problem preparing SQL statement")?;
let mut rows = statement.query(rusqlite::params_from_iter(item_ids))?;
let mut meta_map: std::collections::HashMap<i64, std::collections::HashMap<String, String>> = std::collections::HashMap::new();
while let Some(row) = rows.next()? {
let id: i64 = row.get(0)?;
let name: String = row.get(1)?;
let value: String = row.get(2)?;
meta_map.entry(id).or_insert_with(std::collections::HashMap::new).insert(name, value);
}
Ok(meta_map)
}