added nocache option

This commit is contained in:
sreedev
2022-12-29 23:48:34 -05:00
parent 61df73e146
commit 57083a7346
6 changed files with 35 additions and 101 deletions

View File

@@ -1,4 +1,5 @@
use anyhow::Result;
use crate::cli::App;
#[derive(Debug, Clone)]
pub struct File {
@@ -6,6 +7,18 @@ pub struct File {
pub hash: String,
}
pub fn get_connection(args: &App) -> Result<sqlite::Connection, sqlite::Error> {
let connection_url = match args.nocache {
false => "/tmp/deduplicator.db",
true => ":memory:"
};
sqlite::open(connection_url).and_then(|conn| {
setup(&conn).ok();
Ok(conn)
})
}
pub fn setup(connection: &sqlite::Connection) -> Result<()> {
let query = "CREATE TABLE files (file_identifier STRING, hash STRING)";
connection.execute(query).ok();
@@ -33,7 +46,7 @@ pub fn indexed_paths(connection: &sqlite::Connection) -> Result<Vec<File>> {
.map(|row_result| row_result.unwrap())
.map(|row| {
let path = row.read::<&str, _>("file_identifier").to_string();
let hash = row.read::<&str, _>("hash").to_string();
let hash = row.read::<i64, _>("hash").to_string();
File { path, hash }
})
.collect();
@@ -60,7 +73,7 @@ pub fn duplicate_hashes(connection: &sqlite::Connection, path: &String) -> Resul
.map(|row_result| row_result.unwrap())
.map(|row| {
let path = row.read::<&str, _>("file_identifier").to_string();
let hash = row.read::<&str, _>("hash").to_string();
let hash = row.read::<i64, _>("hash").to_string();
File { path, hash }
})
.collect();