From b2bc2c450b4dfd3aa47ff079c0e938a6bbdbfa80 Mon Sep 17 00:00:00 2001 From: Magnus Date: Fri, 18 Apr 2025 20:26:29 +0200 Subject: [PATCH 1/3] add unique constraint and create index on short URL --- actix/src/database.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/actix/src/database.rs b/actix/src/database.rs index 110bcc4..5a8b565 100644 --- a/actix/src/database.rs +++ b/actix/src/database.rs @@ -92,12 +92,19 @@ pub fn open_db(path: String) -> Connection { "CREATE TABLE IF NOT EXISTS urls ( id INTEGER PRIMARY KEY AUTOINCREMENT, long_url TEXT NOT NULL, - short_url TEXT NOT NULL, + short_url TEXT NOT NULL UNIQUE, hits INTEGER NOT NULL )", [], ) .expect("Unable to initialize empty database."); + // Create index on short_url for faster lookups + db.execute( + "CREATE INDEX IF NOT EXISTS idx_short_url ON urls (short_url)", + [], + ) + .expect("Unable to create index on short_url."); + db } From 88ddb4299a96ce45abdf1d56768dbd11fa9236d2 Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Sat, 19 Apr 2025 19:31:43 -0500 Subject: [PATCH 2/3] chg: Move the uniqueness validation to the INDEX --- actix/src/database.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/actix/src/database.rs b/actix/src/database.rs index 5a8b565..c86142f 100644 --- a/actix/src/database.rs +++ b/actix/src/database.rs @@ -92,7 +92,7 @@ pub fn open_db(path: String) -> Connection { "CREATE TABLE IF NOT EXISTS urls ( id INTEGER PRIMARY KEY AUTOINCREMENT, long_url TEXT NOT NULL, - short_url TEXT NOT NULL UNIQUE, + short_url TEXT NOT NULL, hits INTEGER NOT NULL )", [], @@ -101,7 +101,7 @@ pub fn open_db(path: String) -> Connection { // Create index on short_url for faster lookups db.execute( - "CREATE INDEX IF NOT EXISTS idx_short_url ON urls (short_url)", + "CREATE UNIQUE INDEX IF NOT EXISTS idx_short_url ON urls (short_url)", [], ) .expect("Unable to create index on short_url."); From e39578fa021a7a1120c5f099cddb9afab3e51c2b Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Sat, 19 Apr 2025 20:04:56 -0500 Subject: [PATCH 3/3] chg: Modify shortlink validation logic to utilize the INDEX It's no longer necessary to separately validate uniqueness since the UNIQUE INDEX does that for us already. --- actix/src/utils.rs | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/actix/src/utils.rs b/actix/src/utils.rs index 52dd1f2..f3c4618 100644 --- a/actix/src/utils.rs +++ b/actix/src/utils.rs @@ -63,8 +63,8 @@ pub fn is_api_ok(http: HttpRequest) -> Response { // If the API key isn't set, but an API Key header is provided if auth::api_header(&http).is_some() { Response { - success: false, - error: true, + success: false, + error: true, reason: "An API key was provided, but the 'api_key' environment variable is not configured in the Chhoto URL instance".to_string(), pass: false } @@ -80,7 +80,11 @@ pub fn is_api_ok(http: HttpRequest) -> Response { } // Request the DB for searching an URL -pub fn get_longurl(shortlink: String, db: &Connection, needhits: bool) -> (Option, Option) { +pub fn get_longurl( + shortlink: String, + db: &Connection, + needhits: bool, +) -> (Option, Option) { if validate_link(&shortlink) { database::find_url(shortlink.as_str(), db, needhits) } else { @@ -123,18 +127,14 @@ pub fn add_link(req: String, db: &Connection) -> (bool, String) { chunks.shortlink = gen_link(style, len); } - if validate_link(chunks.shortlink.as_str()) - && get_longurl(chunks.shortlink.clone(), db, false).0.is_none() - { - ( - database::add_link(chunks.shortlink.clone(), chunks.longlink, db), - chunks.shortlink, - ) + if validate_link(chunks.shortlink.as_str()) { + if database::add_link(chunks.shortlink.clone(), chunks.longlink, db) { + (true, chunks.shortlink) + } else { + (false, String::from("Short URL is already in use!")) + } } else { - ( - false, - String::from("Short URL not valid or already in use!"), - ) + (false, String::from("Short URL is not valid!")) } }