Skip to content

Commit

Permalink
Added remove_image code for iqdb.rs.
Browse files Browse the repository at this point in the history
  • Loading branch information
M committed Aug 23, 2024
1 parent 5b4198a commit 63882f3
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 15 deletions.
24 changes: 23 additions & 1 deletion src/iqdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ impl IQDB {
data: Arc::new(Mutex::new(ImgBin::new())),
};

// I think this should probably just be an instance of pool and not need cloning
let sql_clone: db::Sql = sql.clone();
let mut sql_rows = sql_clone.each_image();
while let Some(r) = sql_rows.try_next().await? {
Expand All @@ -41,4 +40,27 @@ impl IQDB {
sql: sql,
})
}

/*pub async fn add_image(&self, post_id: imgdb::ImageId) {
self.remove_image()
}*/

pub async fn remove_image(&self, post_id: imgdb::PostId) -> Option<imgdb::PostId> {
let image = self.sql.get_image(post_id).await;
if image.is_none() {
// add some logging ig
return None;
}
let image = image.unwrap();
// TODO: https://itsallaboutthebit.com/arc-mutex/ Might be able to use Mutex without Arc
self.state
.data
.clone()
.lock()
.await
.remove_image(&image.s, image.id);
self.sql.remove_image(post_id).await;

return Some(post_id);
}
}
10 changes: 5 additions & 5 deletions src/iqdb/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct Sql {
}

pub struct SqlRow {
pub id: i32,
pub id: u32,
pub s: HaarSignature,
}

Expand Down Expand Up @@ -76,7 +76,7 @@ impl Sql {
}
}

pub async fn get_image(&self, id: i64) -> Option<SqlRow> {
pub async fn get_image(&self, id: u32) -> Option<SqlRow> {
sqlx::query_as(
r#"
SELECT id, avglf0, avglf1, avglf2, sig0, sig1, sig2
Expand Down Expand Up @@ -119,7 +119,7 @@ impl Sql {
.fetch(&self.pool)
}

pub async fn remove_image(&self, id: i64) -> Result<SqliteQueryResult, Error> {
pub async fn remove_image(&self, id: u32) -> Result<SqliteQueryResult, Error> {
let mut conn = self.pool.acquire().await?;
sqlx::query!(
r#"
Expand Down Expand Up @@ -205,13 +205,13 @@ mod tests {
println!("Added new entry with id {id}.");
let _ = sql.list_rows().await.expect("Error while listing rows");

let img = sql.get_image(id).await.unwrap();
let img = sql.get_image(id as u32).await.unwrap();
println!("For id: {id}, the SqlRow's HaarSignature is: {:?}", img.s);

// Remove image
println!("Running remove image for id: {id}");
let _ = sql
.remove_image(id)
.remove_image(id as u32)
.await
.expect("Error while removing id: {id}");
let _ = sql.list_rows().await.expect("Error while listing rows");
Expand Down
20 changes: 12 additions & 8 deletions src/iqdb/imgdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use std::collections::BinaryHeap;
use std::sync::Arc;
use tokio::sync::Mutex;

type ImageId = i32;
type IqdbId = usize; // An internal IQDB image ID.
type PostId = i32; // An external (booru) post ID.
pub type ImageId = u32;
pub type IqdbId = u32; // An internal IQDB image ID.
pub type PostId = u32; // An external (booru) post ID.
type Score = f32;
type SimVector = Vec<SimValue>;
type Sig = [Idx; NUM_COEFS];
Expand Down Expand Up @@ -91,6 +91,14 @@ impl ImgBin {
self.each_bucket(sig, |bucket: &mut Bucket| bucket.push(iqdb_id));
}

/// Removes a signature from the buckets and from the info vector.
///
/// Serves as a helper function for crate::iqdb::IQDB, since this sort of memory access is easier in C/C++.
pub fn remove_image(&mut self, sig: &HaarSignature, iqdb_id: u32) {
self.remove(sig, iqdb_id);
self.info[iqdb_id as usize].avgl.v[0] = 0.0;
}

pub fn remove(&mut self, sig: &HaarSignature, iqdb_id: u32) {
self.each_bucket(sig, |bucket: &mut Bucket| {
bucket.retain(|&x: &u32| x != iqdb_id)
Expand Down Expand Up @@ -122,7 +130,7 @@ impl ImgBin {
// addImageInMemory

fn is_deleted(&self, iqdb_id: IqdbId) -> bool {
return self.info[iqdb_id].avgl.v[0] == 0.0;
return self.info[iqdb_id as usize].avgl.v[0] == 0.0;
}

fn query_from_blob(&self, image: DynamicImage, limit: i32) {
Expand Down Expand Up @@ -171,10 +179,6 @@ impl ImgBin {
i += 1;
}
}*/

/*fn remove_image(&self, image_id: ImageId) {
let image =
}*/
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use axum::{
routing::{get, post},
Json,
};
use image::{ImageReader, DynamicImage};
use image::{DynamicImage, ImageReader};
use std::io::{Cursor, Error, ErrorKind};
use tokio::{signal, task};

Expand Down

0 comments on commit 63882f3

Please sign in to comment.