From 27c6e55d9ee7504d9f60a45b5905212efd02ed3b Mon Sep 17 00:00:00 2001 From: Michael Ionov Date: Fri, 6 Oct 2023 17:46:09 +0300 Subject: [PATCH] feat: hash query --- src-tauri/Cargo.lock | 11 +++++++++++ src-tauri/Cargo.toml | 1 + src-tauri/src/handlers/queries.rs | 7 +++++++ src-tauri/src/queues/query.rs | 9 +++++---- src-tauri/src/utils/crypto.rs | 8 ++++++++ 5 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index daff633..e2014e8 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -2241,6 +2241,16 @@ version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2532096657941c2fea9c289d370a250971c689d4f143798ff67113ec042024a5" +[[package]] +name = "md-5" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d89e7ee0cfbedfc4da3340218492196241d89eefb6dab27de5df917a6d2e78cf" +dependencies = [ + "cfg-if", + "digest", +] + [[package]] name = "memchr" version = "2.5.0" @@ -2998,6 +3008,7 @@ dependencies = [ "env_logger", "futures", "log", + "md-5", "mysql", "rand 0.8.5", "rusqlite", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 7b6aea6..294dc23 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -38,6 +38,7 @@ tokio = { version = "1.32.0", features = ["full"] } tracing = "0.1.37" tracing-subscriber = "0.3.17" sqlparser = "0.38.0" +md-5 = "0.10.6" [features] # this feature is used for production builds or when `devPath` points to the filesystem diff --git a/src-tauri/src/handlers/queries.rs b/src-tauri/src/handlers/queries.rs index cc12cd9..c68179e 100644 --- a/src-tauri/src/handlers/queries.rs +++ b/src-tauri/src/handlers/queries.rs @@ -30,6 +30,13 @@ pub async fn enqueue_query( match statements { Ok(statements) => { if let Some(conn) = conn { + // let mut stmts = vec![]; + // // reduce to unique statements + // for stmt in statements.iter() { + // if !stmts.contains(&stmt.to_string()) { + // stmts.push(stmt.to_string()); + // } + // } let async_proc_input_tx = async_state.tasks.lock().await; for (idx, statement) in statements.iter().enumerate() { info!("Got statement {:?}", statement.to_string()); diff --git a/src-tauri/src/queues/query.rs b/src-tauri/src/queues/query.rs index 38f97cc..7219d34 100644 --- a/src-tauri/src/queues/query.rs +++ b/src-tauri/src/queues/query.rs @@ -1,11 +1,11 @@ use crate::database::connections::ConnectedConnection; +use crate::utils::crypto::md5_hash; use anyhow::Result; use serde::Deserialize; use serde::Serialize; use tauri::Manager; use tokio::sync::mpsc; use tracing::info; -use uuid::Uuid; #[derive(Debug, Clone, Serialize, Deserialize)] pub enum QueryTaskStatus { @@ -33,14 +33,15 @@ pub struct QueryTask { impl QueryTask { pub fn new(conn: ConnectedConnection, tab_id: &str, query: &str, query_idx: usize) -> Self { - let id = Uuid::new_v4(); + let query_hash = md5_hash(query); + let id = conn.config.id.to_string() + tab_id + &query_hash; QueryTask { conn, + id, tab_id: tab_id.to_string(), + query_idx, query: query.to_string(), - id: id.to_string(), status: QueryTaskStatus::Queued, - query_idx, } } } diff --git a/src-tauri/src/utils/crypto.rs b/src-tauri/src/utils/crypto.rs index efed360..1ff11ea 100644 --- a/src-tauri/src/utils/crypto.rs +++ b/src-tauri/src/utils/crypto.rs @@ -1,4 +1,5 @@ use anyhow::Result; +use md5::{Digest, Md5}; use rand::{distributions::Alphanumeric, Rng}; use simplecrypt::{decrypt, encrypt}; use std::fs; @@ -34,6 +35,13 @@ pub fn get_app_key() -> Result { Ok(key) } +pub fn md5_hash(data: &str) -> String { + let mut hasher = Md5::new(); + hasher.update(data); + let result = hasher.finalize(); + format!("{:x}", result) +} + #[cfg(test)] mod test { use anyhow::Result;