-
-
Notifications
You must be signed in to change notification settings - Fork 313
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
a basic command to perform a corpus run
- Loading branch information
Showing
6 changed files
with
149 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
pub struct Engine<P> { | ||
progress: P, | ||
con: rusqlite::Connection, | ||
} | ||
|
||
pub mod engine { | ||
use crate::corpus::Engine; | ||
use anyhow::Context; | ||
use std::path::PathBuf; | ||
|
||
impl<P> Engine<P> | ||
where | ||
P: gix::Progress, | ||
{ | ||
/// Open the corpus DB or create it. | ||
pub fn open_or_create(db: PathBuf, progress: P) -> anyhow::Result<Engine<P>> { | ||
let con = crate::corpus::db::create(db).context("Could not open or create database")?; | ||
Ok(Engine { progress, con }) | ||
} | ||
|
||
/// Run on the existing set of repositories we have already seen or obtain them from `path` if there is none yet. | ||
pub fn run(&self, _path: PathBuf) -> anyhow::Result<()> { | ||
todo!() | ||
} | ||
} | ||
} | ||
|
||
pub mod db { | ||
use anyhow::bail; | ||
use rusqlite::{params, OptionalExtension}; | ||
|
||
/// A version to be incremented whenever the database layout is changed, to refresh it automatically. | ||
const VERSION: usize = 1; | ||
|
||
pub fn create(path: impl AsRef<std::path::Path>) -> anyhow::Result<rusqlite::Connection> { | ||
let path = path.as_ref(); | ||
let con = rusqlite::Connection::open(path)?; | ||
let meta_table = r#" | ||
CREATE TABLE if not exists meta( | ||
version int | ||
)"#; | ||
con.execute_batch(meta_table)?; | ||
let version: Option<usize> = con.query_row("SELECT version FROM meta", [], |r| r.get(0)).optional()?; | ||
match version { | ||
None => { | ||
con.execute("INSERT into meta(version) values(?)", params![VERSION])?; | ||
} | ||
Some(version) if version != VERSION => match con.close() { | ||
Ok(()) => { | ||
bail!("Cannot handle database with version {version}, cannot yet migrate to {VERSION}"); | ||
} | ||
Err((_, err)) => return Err(err.into()), | ||
}, | ||
_ => {} | ||
} | ||
con.execute_batch( | ||
r#" | ||
CREATE TABLE if not exists runner( | ||
hash blob(20) NOT NULL PRIMARY KEY | ||
) | ||
"#, | ||
)?; | ||
// Files are stored as paths which also have an id for referencing purposes | ||
con.execute_batch( | ||
r#" | ||
CREATE TABLE if not exists repository( | ||
file_id integer NOT NULL PRIMARY KEY, | ||
file_path text UNIQUE | ||
) | ||
"#, | ||
)?; | ||
con.execute_batch( | ||
r#" | ||
CREATE TABLE if not exists run( | ||
hash blob(20), | ||
file_id text, | ||
has_diff boolean NOT NULL, | ||
lines_added integer NOT NULL, | ||
lines_removed integer NOT NULL, | ||
lines_before integer NOT NULL, | ||
lines_after integer NOT NULL, | ||
mode integer, | ||
source_file_id integer, | ||
FOREIGN KEY (hash) REFERENCES commits (hash), | ||
FOREIGN KEY (file_id) REFERENCES files (file_id), | ||
PRIMARY KEY (hash, file_id) | ||
) | ||
"#, | ||
)?; | ||
|
||
Ok(con) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters