1
Fork 0
mirror of https://github.com/RGBCube/dix synced 2025-07-28 12:17:45 +00:00

proof of concept direct query with sqlx

This commit is contained in:
Dragyx 2025-05-05 16:55:48 +02:00 committed by Bloxx12
parent 63cf04b29f
commit 783f180716
5 changed files with 2151 additions and 13 deletions

2059
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -10,3 +10,5 @@ yansi = "1.0.1"
thiserror = "2.0.12" thiserror = "2.0.12"
log = "0.4.20" log = "0.4.20"
env_logger = "0.11.3" env_logger = "0.11.3"
sqlx = { version = "0.8.5", features = ["runtime-async-std", "sqlite"] }
async-std = "1.13.1"

View file

@ -20,6 +20,7 @@
inherit inherit
(pkgsFor.${system}) (pkgsFor.${system})
cargo cargo
sqlx-cli
rustc rustc
rustfmt rustfmt
bacon bacon

View file

@ -13,6 +13,7 @@ use std::{
}; };
use thiserror::Error; use thiserror::Error;
use yansi::Paint; use yansi::Paint;
mod store;
/// Application errors with thiserror /// Application errors with thiserror
#[derive(Debug, Error)] #[derive(Debug, Error)]
@ -226,16 +227,17 @@ fn main() {
debug!("Calculating closure sizes in background"); debug!("Calculating closure sizes in background");
let path = args.path.clone(); let path = args.path.clone();
let path2 = args.path2.clone(); let path2 = args.path2.clone();
dbg!(&path);
Some(( Some((
thread::spawn(move || get_closure_size(&path)), thread::spawn(move || store::get_closure_size(&path)),
thread::spawn(move || get_closure_size(&path2)), thread::spawn(move || store::get_closure_size(&path2)),
)) ))
} else { } else {
None None
}; };
// Get package lists and handle potential errors // Get package lists and handle potential errors
let package_list_pre = match get_packages(&args.path) { let package_list_pre = match store::get_packages(&args.path) {
Ok(packages) => { Ok(packages) => {
debug!("Found {} packages in first closure", packages.len()); debug!("Found {} packages in first closure", packages.len());
packages packages

94
src/store.rs Normal file
View file

@ -0,0 +1,94 @@
use async_std::task;
use sqlx::{Connection, SqliteConnection};
use std::future;
// executes a query on the nix db directly
// to gather all derivations that the derivation given by the path
// depends on
//
// in the future, we might wan't to switch to async
pub fn get_packages(path: &std::path::Path) -> Result<Vec<String>, String> {
let p = path.canonicalize().unwrap();
let p = p.to_str().unwrap();
task::block_on(async {
struct Col {
path: String,
}
let mut conn = SqliteConnection::connect("sqlite:///nix/var/nix/db/db.sqlite")
.await
.map_err(|_| "Could not establish DB connection")?;
let query_res = sqlx::query_as!(
Col,
"
WITH RECURSIVE
graph(p) AS (
SELECT id
FROM ValidPaths
WHERE path = ?
UNION
SELECT reference FROM Refs
JOIN graph ON referrer = p
)
SELECT path from graph
JOIN ValidPaths ON id = p;
",
p
)
.fetch_all(&mut conn)
.await
.map_err(|_| "Could not execute package query")?
.into_iter()
.map(|c| c.path)
.collect();
Ok::<_, String>(query_res)
})
}
// executes a query on the nix db directly
// to get the total closure size of the derivation
// by summing up the nar size of all derivations
// depending on the derivation
//
// in the future, we might wan't to switch to async
pub fn get_closure_size(path: &std::path::Path) -> Result<i64, String> {
let p = path.canonicalize().unwrap();
let p = p.to_str().unwrap();
dbg!(p);
let size = task::block_on(async {
struct Res {
sum: Option<i64>,
}
let mut conn = SqliteConnection::connect("sqlite:///nix/var/nix/db/db.sqlite")
.await
.map_err(|_| "Could not establish DB connection")?;
let query_res = sqlx::query_as!(
Res,
"
WITH RECURSIVE
graph(p) AS (
SELECT id
FROM ValidPaths
WHERE path = ?
UNION
SELECT reference FROM Refs
JOIN graph ON referrer = p
)
SELECT SUM(narSize) as sum from graph
JOIN ValidPaths ON p = id;
",
p
)
.fetch_one(&mut conn)
.await
.map_err(|_| "Could not execute package query")?
.sum
.ok_or("Could not get closure size sum")?;
Ok::<_, String>(query_res)
});
dbg!(&size);
size
}
//
//