diff --git a/src/documents/regenerator.rs b/src/documents/regenerator.rs index f43e15d..2a71749 100644 --- a/src/documents/regenerator.rs +++ b/src/documents/regenerator.rs @@ -95,38 +95,15 @@ fn regenerate_one(conn: &Connection, source_type: SourceType, source_id: i64) -> return Ok(true); }; - let existing_hash = get_existing_hash(conn, source_type, source_id)?; - let changed = existing_hash.as_ref() != Some(&doc.content_hash); - - upsert_document(conn, &doc)?; - - Ok(changed) + upsert_document(conn, &doc) } -fn get_existing_hash( - conn: &Connection, - source_type: SourceType, - source_id: i64, -) -> Result> { - let mut stmt = conn.prepare_cached( - "SELECT content_hash FROM documents WHERE source_type = ?1 AND source_id = ?2", - )?; - - let hash: Option = stmt - .query_row(rusqlite::params![source_type.as_str(), source_id], |row| { - row.get(0) - }) - .optional()?; - - Ok(hash) -} - -fn upsert_document(conn: &Connection, doc: &DocumentData) -> Result<()> { +fn upsert_document(conn: &Connection, doc: &DocumentData) -> Result { conn.execute_batch("SAVEPOINT upsert_doc")?; match upsert_document_inner(conn, doc) { - Ok(()) => { + Ok(changed) => { conn.execute_batch("RELEASE upsert_doc")?; - Ok(()) + Ok(changed) } Err(e) => { let _ = conn.execute_batch("ROLLBACK TO upsert_doc; RELEASE upsert_doc"); @@ -135,7 +112,7 @@ fn upsert_document(conn: &Connection, doc: &DocumentData) -> Result<()> { } } -fn upsert_document_inner(conn: &Connection, doc: &DocumentData) -> Result<()> { +fn upsert_document_inner(conn: &Connection, doc: &DocumentData) -> Result { let existing: Option<(i64, String, String, String)> = conn .query_row( "SELECT id, content_hash, labels_hash, paths_hash FROM documents @@ -145,12 +122,17 @@ fn upsert_document_inner(conn: &Connection, doc: &DocumentData) -> Result<()> { ) .optional()?; + let content_changed = match &existing { + Some((_, old_content_hash, _, _)) => old_content_hash != &doc.content_hash, + None => true, + }; + if let Some((_, ref old_content_hash, ref old_labels_hash, ref old_paths_hash)) = existing && old_content_hash == &doc.content_hash && old_labels_hash == &doc.labels_hash && old_paths_hash == &doc.paths_hash { - return Ok(()); + return Ok(false); } let labels_json = serde_json::to_string(&doc.labels).unwrap_or_else(|_| "[]".to_string()); @@ -260,7 +242,7 @@ fn upsert_document_inner(conn: &Connection, doc: &DocumentData) -> Result<()> { } } - Ok(()) + Ok(content_changed) } fn delete_document(conn: &Connection, source_type: SourceType, source_id: i64) -> Result<()> {