use uri_register::{PostgresUriRegister, UriService};
#[tokio::main]
async fn main() -> uri_register::Result<()> {
println!("🕷️ Web Crawler URI Deduplication Example\n");
let db_url =
std::env::var("DATABASE_URL").unwrap_or_else(|_| "postgres://localhost/access".to_string());
let register = PostgresUriRegister::new(&db_url, "uri_register", 20, 10_000).await?;
println!("✓ Connected to database\n");
println!("📥 Crawler discovered URLs from Page 1:");
let page1_urls = vec![
"https://example.com/page1".to_string(),
"https://example.com/page2".to_string(),
"https://example.com/page3".to_string(),
];
let page1_ids = register.register_uri_batch(&page1_urls).await?;
for (url, id) in page1_urls.iter().zip(page1_ids.iter()) {
println!(" {} -> ID {}", url, id);
}
println!("\n📥 Crawler discovered URLs from Page 2:");
let page2_urls = vec![
"https://example.com/page2".to_string(), "https://example.com/page4".to_string(), "https://example.com/page5".to_string(), ];
let page2_ids = register.register_uri_batch(&page2_urls).await?;
for (url, id) in page2_urls.iter().zip(page2_ids.iter()) {
let status = if page1_ids.contains(id) {
"(already crawled)"
} else {
"(new)"
};
println!(" {} -> ID {} {}", url, id, status);
}
println!("\n📥 Batch processing discovered links (using HashMap):");
let batch_urls = vec![
"https://example.com/page1".to_string(), "https://example.com/page6".to_string(), "https://example.com/page7".to_string(), "https://example.com/page1".to_string(), ];
let batch_map = register.register_uri_batch_hashmap(&batch_urls).await?;
println!(
" Processed {} URLs into {} unique entries:",
batch_urls.len(),
batch_map.len()
);
for (url, id) in &batch_map {
println!(" {} -> ID {}", url, id);
}
println!("\n📊 URI Register Statistics:");
let stats = register.stats().await?;
println!(" Total unique URLs registered: {}", stats.total_uris);
println!(" Storage size: {} bytes", stats.size_bytes);
println!("\n✅ Deduplication Summary:");
println!(
" Total URLs processed: {}",
page1_urls.len() + page2_urls.len() + batch_urls.len()
);
println!(" Unique URLs in database: {}", stats.total_uris);
println!(
" Duplicates avoided: {}",
(page1_urls.len() + page2_urls.len() + batch_urls.len()) - stats.total_uris as usize
);
Ok(())
}