feat(suggestions): improve vector store and add cache management
- Add get_all_related_artists for direct relationship lookup - Add get_related_artists helper method - Add clear_all method to vector store for cache management - Add clear_vector_store Tauri command - Add Artist Vectors cache section to Settings view - Use artist_info tuples (mbid, name) for better track searching
This commit is contained in:
1 parent
9a89074b5f
commit
79c68d292e
4 files changed
+177
-7
No files matched your search
@@ -390,6 +390,94 @@ impl ArtistVectorStore {
|
||||
.flatten()
|
||||
}
|
||||
|
||||
/// Get related artists for a given artist (from their vector entries)
|
||||
///
|
||||
/// This returns artists that the given artist is connected to via
|
||||
/// MusicBrainz relationships (members, collaborators, etc.)
|
||||
pub fn get_related_artists(&self, mbid: &str) -> Result<Vec<SimilarArtist>, String> {
|
||||
let Some(artist_idx) = self.get_idx(mbid) else {
|
||||
return Ok(Vec::new());
|
||||
};
|
||||
|
||||
let mut stmt = self
|
||||
.conn
|
||||
.prepare(
|
||||
"SELECT target_idx, SUM(weight) as total_weight
|
||||
FROM vector_entries
|
||||
WHERE artist_idx = ?1
|
||||
GROUP BY target_idx
|
||||
ORDER BY total_weight DESC",
|
||||
)
|
||||
.map_err(|e| format!("Failed to prepare query: {}", e))?;
|
||||
|
||||
let rows = stmt
|
||||
.query_map(params![artist_idx], |row| {
|
||||
Ok((row.get::<_, u32>(0)?, row.get::<_, f32>(1)?))
|
||||
})
|
||||
.map_err(|e| format!("Failed to query relations: {}", e))?;
|
||||
|
||||
let mut results = Vec::new();
|
||||
for row in rows.flatten() {
|
||||
let (target_idx, weight) = row;
|
||||
if let Some(target_mbid) = self.get_mbid(target_idx) {
|
||||
let name = self.get_artist_name(target_mbid);
|
||||
results.push(SimilarArtist {
|
||||
mbid: target_mbid.to_string(),
|
||||
name,
|
||||
similarity: weight,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Ok(results)
|
||||
}
|
||||
|
||||
/// Get all related artists for multiple source artists, excluding specified MBIDs
|
||||
///
|
||||
/// Returns a deduplicated list sorted by total weight across all sources
|
||||
pub fn get_all_related_artists(
|
||||
&self,
|
||||
source_mbids: &[String],
|
||||
exclude_mbids: &[String],
|
||||
limit: usize,
|
||||
) -> Result<Vec<SimilarArtist>, String> {
|
||||
use std::collections::HashMap;
|
||||
|
||||
let exclude_set: std::collections::HashSet<_> = exclude_mbids.iter().collect();
|
||||
let mut artist_weights: HashMap<String, (Option<String>, f32)> = HashMap::new();
|
||||
|
||||
for mbid in source_mbids {
|
||||
let related = self.get_related_artists(mbid)?;
|
||||
for artist in related {
|
||||
if exclude_set.contains(&artist.mbid) {
|
||||
continue;
|
||||
}
|
||||
let entry = artist_weights
|
||||
.entry(artist.mbid.clone())
|
||||
.or_insert((artist.name.clone(), 0.0));
|
||||
entry.1 += artist.similarity;
|
||||
}
|
||||
}
|
||||
|
||||
let mut results: Vec<_> = artist_weights
|
||||
.into_iter()
|
||||
.map(|(mbid, (name, weight))| SimilarArtist {
|
||||
mbid,
|
||||
name,
|
||||
similarity: weight,
|
||||
})
|
||||
.collect();
|
||||
|
||||
results.sort_by(|a, b| {
|
||||
b.similarity
|
||||
.partial_cmp(&a.similarity)
|
||||
.unwrap_or(std::cmp::Ordering::Equal)
|
||||
});
|
||||
results.truncate(limit);
|
||||
|
||||
Ok(results)
|
||||
}
|
||||
|
||||
/// Get statistics about the store
|
||||
pub fn get_stats(&self) -> Result<StoreStats, String> {
|
||||
let artist_count = self.artist_to_idx.len();
|
||||
@@ -435,6 +523,30 @@ impl ArtistVectorStore {
|
||||
|
||||
Ok(deleted)
|
||||
}
|
||||
|
||||
/// Clear all data from the store
|
||||
pub fn clear_all(&mut self) -> Result<usize, String> {
|
||||
let deleted = self
|
||||
.conn
|
||||
.execute("DELETE FROM vector_entries", [])
|
||||
.map_err(|e| format!("Failed to delete vector entries: {}", e))?;
|
||||
|
||||
self.conn
|
||||
.execute("DELETE FROM vector_metadata", [])
|
||||
.map_err(|e| format!("Failed to delete metadata: {}", e))?;
|
||||
|
||||
self.conn
|
||||
.execute("DELETE FROM artist_index", [])
|
||||
.map_err(|e| format!("Failed to delete artist index: {}", e))?;
|
||||
|
||||
// Reset in-memory state
|
||||
self.artist_to_idx.clear();
|
||||
self.idx_to_artist.clear();
|
||||
self.next_idx = 0;
|
||||
|
||||
log::info!("Artist vector store cleared: {} entries deleted", deleted);
|
||||
Ok(deleted)
|
||||
}
|
||||
}
|
||||
|
||||
/// Statistics about the vector store
|
||||
|
||||
Reference in new issue
Block a user