mirror of
https://github.com/RGBCube/dix
synced 2025-07-28 04:07:46 +00:00
proof of concept direct query with sqlx
This commit is contained in:
parent
63cf04b29f
commit
783f180716
5 changed files with 2151 additions and 13 deletions
2059
Cargo.lock
generated
2059
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -10,3 +10,5 @@ yansi = "1.0.1"
|
|||
thiserror = "2.0.12"
|
||||
log = "0.4.20"
|
||||
env_logger = "0.11.3"
|
||||
sqlx = { version = "0.8.5", features = ["runtime-async-std", "sqlite"] }
|
||||
async-std = "1.13.1"
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
inherit
|
||||
(pkgsFor.${system})
|
||||
cargo
|
||||
sqlx-cli
|
||||
rustc
|
||||
rustfmt
|
||||
bacon
|
||||
|
|
|
@ -13,6 +13,7 @@ use std::{
|
|||
};
|
||||
use thiserror::Error;
|
||||
use yansi::Paint;
|
||||
mod store;
|
||||
|
||||
/// Application errors with thiserror
|
||||
#[derive(Debug, Error)]
|
||||
|
@ -226,16 +227,17 @@ fn main() {
|
|||
debug!("Calculating closure sizes in background");
|
||||
let path = args.path.clone();
|
||||
let path2 = args.path2.clone();
|
||||
dbg!(&path);
|
||||
Some((
|
||||
thread::spawn(move || get_closure_size(&path)),
|
||||
thread::spawn(move || get_closure_size(&path2)),
|
||||
thread::spawn(move || store::get_closure_size(&path)),
|
||||
thread::spawn(move || store::get_closure_size(&path2)),
|
||||
))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
// 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) => {
|
||||
debug!("Found {} packages in first closure", packages.len());
|
||||
packages
|
||||
|
|
94
src/store.rs
Normal file
94
src/store.rs
Normal 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
|
||||
}
|
||||
|
||||
//
|
||||
//
|
Loading…
Add table
Add a link
Reference in a new issue