Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rpm test #44

Merged
merged 4 commits into from
Jun 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 55 additions & 20 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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(())?;

Expand All @@ -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);
}
Expand Down Expand Up @@ -409,6 +408,7 @@ mod test {
fs::{self, File},
io::{Read, Write},
path::PathBuf,
thread::sleep,
time::Duration,
};

Expand Down Expand Up @@ -446,12 +446,16 @@ 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();
Expand All @@ -476,6 +480,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();

Expand All @@ -485,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);
}
Expand Down Expand Up @@ -516,4 +522,33 @@ mod test {
db.insert(data).unwrap();
assert!(db.len() == 1);
}

// 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);

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);
}
}