From 620f9c0e1274adce6d9b77e403f86b9ca80070c2 Mon Sep 17 00:00:00 2001 From: wiiznokes <78230769+wiiznokes@users.noreply.github.com> Date: Tue, 18 Jun 2024 14:34:10 +0200 Subject: [PATCH 1/4] Update db.rs --- src/db.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/db.rs b/src/db.rs index 1c98b71..624cd22 100644 --- a/src/db.rs +++ b/src/db.rs @@ -406,10 +406,7 @@ impl Db { #[cfg(test)] mod test { use std::{ - fs::{self, File}, - io::{Read, Write}, - path::PathBuf, - time::Duration, + fs::{self, File}, io::{Read, Write}, path::PathBuf, thread::sleep, time::Duration }; use serial_test::serial; @@ -476,6 +473,8 @@ mod test { let data = Data::new("text/plain".into(), "content".as_bytes().into()); db.insert(data).unwrap(); + sleep(Duration::from_millis(100)); + let data = Data::new("text/plain".into(), "content2".as_bytes().into()); db.insert(data).unwrap(); From 57c0a01dfc3dc41f2fd55cfc3058546556c49508 Mon Sep 17 00:00:00 2001 From: wiiznokes <78230769+wiiznokes@users.noreply.github.com> Date: Tue, 18 Jun 2024 14:37:39 +0200 Subject: [PATCH 2/4] Update db.rs --- src/db.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/db.rs b/src/db.rs index 624cd22..b19b4a1 100644 --- a/src/db.rs +++ b/src/db.rs @@ -443,12 +443,19 @@ mod test { assert!(db.len() == 1); + + sleep(Duration::from_millis(1000)); + let data = Data::new("text/plain".into(), "content".as_bytes().into()); db.insert(data).unwrap(); assert!(db.len() == 1); + + sleep(Duration::from_millis(1000)); + + let data = Data::new("text/plain".into(), "content2".as_bytes().into()); db.insert(data.clone()).unwrap(); @@ -515,4 +522,31 @@ mod test { db.insert(data).unwrap(); assert!(db.len() == 1); } + + #[test] + fn different_content_same_time() { + let db_path = PathBuf::from("tests/different_content_same_time"); + let _ = fs::remove_file(&db_path); + + let mut db = Db::inner_new(None, &db_path).unwrap(); + + let now = utils::now_millis(); + + let data = Data { + creation: now, + mime: "text/plain".into(), + content: "content".as_bytes().into(), + }; + + db.insert(data).unwrap(); + + let data = Data { + creation: now, + mime: "text/plain".into(), + content: "content2".as_bytes().into(), + }; + + db.insert(data).unwrap(); + assert!(db.len() == 2); + } } From f4be9f870b98105fcefdd53c99c8aad20040bf8b Mon Sep 17 00:00:00 2001 From: wiiznokes <78230769+wiiznokes@users.noreply.github.com> Date: Tue, 18 Jun 2024 14:46:51 +0200 Subject: [PATCH 3/4] Update db.rs --- src/db.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/db.rs b/src/db.rs index b19b4a1..74dcf0b 100644 --- a/src/db.rs +++ b/src/db.rs @@ -24,7 +24,7 @@ use crate::{ utils, }; -type TimeId = i64; // maybe add some randomness at the end +type TimeId = i64; // maybe add some randomness at the end https://github.com/dylanhart/ulid-rs const DB_PATH: &str = "clipboard-manager-db-1.sqlite"; @@ -406,7 +406,11 @@ impl Db { #[cfg(test)] mod test { use std::{ - fs::{self, File}, io::{Read, Write}, path::PathBuf, thread::sleep, time::Duration + fs::{self, File}, + io::{Read, Write}, + path::PathBuf, + thread::sleep, + time::Duration, }; use serial_test::serial; @@ -443,7 +447,6 @@ mod test { assert!(db.len() == 1); - sleep(Duration::from_millis(1000)); let data = Data::new("text/plain".into(), "content".as_bytes().into()); @@ -452,10 +455,8 @@ mod test { assert!(db.len() == 1); - sleep(Duration::from_millis(1000)); - let data = Data::new("text/plain".into(), "content2".as_bytes().into()); db.insert(data.clone()).unwrap(); @@ -523,7 +524,9 @@ mod test { assert!(db.len() == 1); } - #[test] + // activate if we add randomness on the id. + // see https://github.com/dylanhart/ulid-rs + // #[test] fn different_content_same_time() { let db_path = PathBuf::from("tests/different_content_same_time"); let _ = fs::remove_file(&db_path); From 385368460dc6105f45295dec308411a13617b333 Mon Sep 17 00:00:00 2001 From: wiiznokes <78230769+wiiznokes@users.noreply.github.com> Date: Tue, 18 Jun 2024 15:24:34 +0200 Subject: [PATCH 4/4] Update db.rs --- src/db.rs | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/src/db.rs b/src/db.rs index 74dcf0b..7c11dab 100644 --- a/src/db.rs +++ b/src/db.rs @@ -160,16 +160,31 @@ impl Db { let conn = Connection::open_with_flags(db_path, OpenFlags::SQLITE_OPEN_READ_WRITE)?; + if let Some(max_duration) = &remove_old_entries { + let query_delete_old_one = r#" + DELETE FROM data + WHERE (:now - creation) >= :max; + "#; + + conn.execute( + &query_delete_old_one, + named_params! { + ":now": utils::now_millis(), + ":max": max_duration.as_millis().try_into().unwrap_or(u64::MAX), + }, + )?; + } + let mut hashs = HashMap::default(); let mut state = BTreeMap::default(); - let query = r#" + let query_load_table = r#" SELECT creation, mime, content FROM data "#; { - let mut stmt = conn.prepare(query)?; + let mut stmt = conn.prepare(query_load_table)?; let mut rows = stmt.query(())?; @@ -180,22 +195,6 @@ impl Db { content: row.get(2)?, }; - if let Some(max_duration) = &remove_old_entries { - let delta = utils::now_millis() - data.creation; - let delta: u64 = delta.try_into().unwrap_or(u64::MAX); - - if Duration::from_millis(delta) > *max_duration { - let query = r#" - DELETE FROM data - WHERE creation = ?1; - "#; - - conn.execute(query, [data.creation])?; - - continue; - } - } - hashs.insert(data.get_hash(), data.creation); state.insert(data.creation, data); } @@ -492,7 +491,7 @@ mod test { assert!(db.len() == 2); - let db = Db::inner_new(Some(Duration::from_secs(0)), &db_path).unwrap(); + let db = Db::inner_new(Some(Duration::ZERO), &db_path).unwrap(); assert!(db.len() == 0); }