From 9f33bdf4d3d0972f52596ff7afc8e7b360094acc Mon Sep 17 00:00:00 2001 From: Alphyron Date: Mon, 19 Aug 2024 07:42:03 +0200 Subject: [PATCH] feat: add inital startup --- .env | 1 + .../20240815064629_initial-database.sql | 6 +-- src/db.rs | 27 ++++++++++- src/main.rs | 48 ++++++++++--------- 4 files changed, 55 insertions(+), 27 deletions(-) diff --git a/.env b/.env index e69de29..1f90a2b 100644 --- a/.env +++ b/.env @@ -0,0 +1 @@ +DATABASE_URL=postgres://anthrove:anthrove@localhost:5432/anthrove \ No newline at end of file diff --git a/migrations/20240815064629_initial-database.sql b/migrations/20240815064629_initial-database.sql index 88723a7..a8c042d 100644 --- a/migrations/20240815064629_initial-database.sql +++ b/migrations/20240815064629_initial-database.sql @@ -1,6 +1,6 @@ -- Add migration script here CREATE TABLE FuzzyHashes( - `id` SERIAL NOT NULL PRIMARY KEY - `hash` BIGINT NOT NULL, - `post_id` CHAR(25) NOT NULL + id SERIAL NOT NULL PRIMARY KEY, + hash BIGINT NOT NULL, + post_id CHAR(25) NOT NULL ) \ No newline at end of file diff --git a/src/db.rs b/src/db.rs index 0597d74..53084f7 100644 --- a/src/db.rs +++ b/src/db.rs @@ -5,11 +5,34 @@ use sqlx::migrate::Migrator; static MIGRATOR: Migrator = sqlx::migrate!(); // defaults to "./migrations" +pub struct FuzzyHash { + pub id: i32, + pub hash: i64, + pub post_id: String, +} + pub async fn connect(uri: &str) -> anyhow::Result> { Ok(PgPoolOptions::new() .connect(uri).await?) } -pub async fn migrate(pool: Pool) -> anyhow::Result<()> { - Ok(MIGRATOR.run(&pool).await?) +pub async fn migrate(pool: &Pool) -> anyhow::Result<()> { + Ok(MIGRATOR.run(pool).await?) +} + +pub async fn get_all_hashes(pool: &Pool) -> anyhow::Result> { + let rows = sqlx::query!("SELECT id, hash, post_id FROM fuzzyhashes") + .fetch_all(pool).await?; + + let mut data = vec![]; + + for row in rows { + data.push(FuzzyHash{ + id: row.id, + hash: row.hash, + post_id: row.post_id, + }); + } + + Ok(data) } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 88c0553..075a785 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,32 +1,36 @@ -use std::fs; -use crate::fuzzy::tree::HashDistance; +use std::{env}; +use dotenvy::dotenv; +use env_logger::TimestampPrecision; +use log::warn; mod fuzzy; mod db; #[tokio::main] -async fn main() { - _ = env_logger::builder().is_test(true).try_init(); - let paths = fs::read_dir("./example").unwrap(); - let tree = fuzzy::tree::Tree::new(); +async fn main() -> anyhow::Result<()> { + dotenv().ok(); + env_logger::builder() + .format_timestamp(Some(TimestampPrecision::Millis)) + .is_test(true) + .init(); - for path in paths { - let hash = fuzzy::image::generate_hash(path.unwrap().path().to_str().unwrap()).await.unwrap(); - println!("{}", hash); - tree.add(hash).await; - } - - let paths = fs::read_dir("./example").unwrap(); - for path in paths { - let path_str = path.unwrap().path(); - let hash = fuzzy::image::generate_hash(path_str.to_str().unwrap()).await.unwrap(); - let founds = tree.find(vec![HashDistance{ - distance: 10, - hash - }]).await; - - println!("{} -> {:?}", path_str.to_str().unwrap(), founds) + let db_url = env::var("DATABASE_URL").expect("'DATABASE_URL' is required"); + + let pool = db::connect(db_url.as_str()).await?; + + db::migrate(&pool).await?; + + let fuzzy_hashes = db::get_all_hashes(&pool).await?; + let fuzzy_tree = fuzzy::tree::Tree::new(); + + for fuzzy_hash in fuzzy_hashes { + let exist = fuzzy_tree.add(fuzzy_hash.hash).await; + + if exist { + warn!("found already existing hash: {}", fuzzy_hash.hash) + } } + Ok(()) }