Local, full Windows search replacement, clipboard manager, and launcher: files, apps, settings and semantic media search with Google's EmbeddingGemma 2.
Rust clipboard-historyembedding-modelsembeddinggemma-2embeddinggemma2embeddingslocal-firstmcp-serversearchsemantic-searchwindows-11
use anyhow::Result;
use rusqlite::{Connection, OptionalExtension};
use std::{
collections::HashMap,
sync::{Arc, Mutex},
};
#[derive(Clone)]
pub struct Group {
pub path: String,
pub kind: String,
pub channel: String,
pub namespace: String,
}
pub struct Matrix {
pub revision: i64,
pub dimension: usize,
pub ids: Vec<i64>,
pub groups: Vec<Group>,
pub group_ids: Vec<usize>,
pub values: Vec<f32>,
pub halves: Vec<half::f16>,
pub fp16: bool,
inverse_norms: Vec<f32>,
pub bytes: usize,
}
#[derive(Clone, Default)]
pub struct Cache {
inner: Arc<Mutex<Option<Arc<Matrix>>>>,
}
impl Cache {
pub fn bytes(&self) -> usize {
self.inner
.lock()
.ok()
.and_then(|v| v.as_ref().map(|m| m.bytes))
.unwrap_or(0)
}
pub fn clear(&self) {
if let Ok(mut entry) = self.inner.lock() {
*entry = None;
}
}
pub fn get(
&self,
db: &Connection,
dimension: usize,
profile: &str,
precision: &str,
) -> Result<Option<Arc<Matrix>>> {
let fp16 = precision == "FP16";
let limit = match profile {
"Low memory" => 0,
"Fast search" => 512 * 1024 * 1024,
_ => 128 * 1024 * 1024,
};
if limit == 0 {
self.clear();
return Ok(None);
}
let revision = db
.query_row(
"SELECT cast(value AS INTEGER) FROM metadata WHERE key='native_vectors_generation'",
[],
|r| r.get::<_, i64>(0),
)
.optional()?
.unwrap_or(0);
let mut lock = self
.inner
.lock()
.map_err(|_| anyhow::anyhow!("Vector cache unavailable"))?;
if let Some(matrix) = lock.as_ref().filter(|m| {
m.revision == revision && m.dimension == dimension && m.bytes <= limit && m.fp16 == fp16
}) {
return Ok(Some(matrix.clone()));
}
*lock = None;
let(count,estimated):(usize,usize)=db.query_row("SELECT count(*),coalesce(sum(length(vector)/?2+length(path)+length(kind)+length(channel)+128),0) FROM segment_vectors WHERE dimension=?1",rusqlite::params![dimension, if fp16 {2} else {1}],|r|Ok((r.get(0)?,r.get(1)?)))?;
if count == 0 {
let indexed: Option<usize> = db
.query_row("SELECT dimension FROM segment_vectors LIMIT 1", [], |r| {
r.get(0)
})
.optional()?;
anyhow::ensure!(
indexed.is_none(),
"Embedding dimensions differ from the existing index (query: {dimension}, indexed: {}). Select a compatible embedding model or explicitly use Rebuild full content.",
indexed.unwrap_or(0)
);
return Ok(None);
}
if estimated > limit {
return Ok(None);
}
let mut matrix = Matrix {
revision,
dimension,
ids: Vec::with_capacity(count),
groups: vec![],
group_ids: Vec::with_capacity(count),
values: Vec::with_capacity(if fp16 { 0 } else { count * dimension }),
halves: Vec::with_capacity(if fp16 { count * dimension } else { 0 }),
fp16,
inverse_norms: Vec::with_capacity(if fp16 { count } else { 0 }),
bytes: 0,
};
let mut lookup = HashMap::new();
let mut stmt=db.prepare("SELECT id,path,kind,channel,namespace,vector FROM segment_vectors WHERE dimension=?1 ORDER BY id")?;
let mut rows = stmt.query([dimension])?;
while let Some(row) = rows.next()? {
let id: i64 = row.get(0)?;
let path: String = row.get(1)?;
let kind: String = row.get(2)?;
let channel: String = row.get(3)?;
let namespace: String = row.get(4)?;
let key = (
path.clone(),
kind.clone(),
channel.clone(),
namespace.clone(),
);
let group = *lookup.entry(key).or_insert_with(|| {
let i = matrix.groups.len();
matrix.bytes += path.len() + kind.len() + channel.len() + namespace.len() + 128;
matrix.groups.push(Group {
path,
kind,
channel,
namespace,
});
i
});
let blob: Vec<u8> = row.get(5)?;
anyhow::ensure!(
blob.len() == dimension * 4,
"Invalid vector length in index"
);
let values = blob
.chunks_exact(4)
.map(|b| f32::from_le_bytes([b[0], b[1], b[2], b[3]]));
if fp16 {
matrix.halves.extend(values.map(half::f16::from_f32));
let norm = matrix.halves[matrix.halves.len() - dimension..]
.iter()
.map(|x| x.to_f32().powi(2))
.sum::<f32>()
.sqrt();
anyhow::ensure!(norm.is_finite() && norm > 0.0, "Invalid cached embedding");
matrix.inverse_norms.push(1.0 / norm);
} else {
matrix.values.extend(values);
}
matrix.ids.push(id);
matrix.group_ids.push(group);
matrix.bytes += dimension * if fp16 { 2 } else { 4 } + if fp16 { 20 } else { 16 };
if matrix.bytes > limit {
return Ok(None);
}
}
let matrix = Arc::new(matrix);
*lock = Some(matrix.clone());
Ok(Some(matrix))
}
}
pub fn dot(a: &[f32], b: &[f32]) -> f32 {
let mut accum = [0.0f32; 8];
let mut total = 0.0;
let mut i = 0;
while i + 8 <= a.len().min(b.len()) {
for lane in 0..8 {
accum[lane] += a[i + lane] * b[i + lane];
}
i += 8;
}
for j in i..a.len().min(b.len()) {
total += a[j] * b[j];
}
total + accum.iter().sum::<f32>()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn dot_matches_scalar() {
let a = (0..768).map(|i| i as f32 / 768.0).collect::<Vec<_>>();
let b = (0..768)
.map(|i| (768 - i) as f32 / 768.0)
.collect::<Vec<_>>();
let scalar: f32 = a.iter().zip(&b).map(|(a, b)| a * b).sum();
assert!((dot(&a, &b) - scalar).abs() < 0.001);
}
}
#[cfg(test)]
mod cache_tests {
use super::*;
use crate::{config::Settings, content::Segment, db::Database};
#[test]
fn revision_and_limits() {
let dir = tempfile::tempdir().unwrap();
let db = Database::new(
dir.path(),
&Settings {
encrypt_index: false,
..Default::default()
},
)
.unwrap();
let c = db.connect(false).unwrap();
let segment = Segment::text("text", "hello".into(), 0, 1);
Database::save_segments(
&c,
"C:\\note.txt",
&[segment.clone()],
&[vec![1.0, 0.0]],
"first",
)
.unwrap();
let cache = Cache::default();
assert!(cache.get(&c, 2, "Low memory", "FP32").unwrap().is_none());
let a = cache.get(&c, 2, "Balanced", "FP32").unwrap().unwrap();
assert_eq!(a.ids.len(), 1);
assert!(a.bytes <= 128 * 1024 * 1024);
let again = cache.get(&c, 2, "Balanced", "FP32").unwrap().unwrap();
assert!(Arc::ptr_eq(&a, &again));
Database::save_segments(&c, "C:\\note.txt", &[segment], &[vec![0.0, 1.0]], "second")
.unwrap();
let b = cache.get(&c, 2, "Balanced", "FP32").unwrap().unwrap();
assert_ne!(a.revision, b.revision);
assert_eq!(b.values, vec![0.0, 1.0]);
}
}
impl Matrix {
pub fn score(&self, row: usize, query: &[f32]) -> f32 {
let start = row * self.dimension;
if self.fp16 {
dot_half(&self.halves[start..start + self.dimension], query) * self.inverse_norms[row]
} else {
dot(&self.values[start..start + self.dimension], query)
}
}
}
pub fn dot_half(a: &[half::f16], b: &[f32]) -> f32 {
use half::slice::HalfFloatSliceExt;
let mut converted = [0.0f32; 1024];
let mut result = 0.0;
for (i, chunk) in a.chunks(1024).enumerate() {
chunk.convert_to_f32_slice(&mut converted[..chunk.len()]);
let start = i * 1024;
result += dot(
&converted[..chunk.len()],
&b[start..(start + chunk.len()).min(b.len())],
);
}
result
}