Skip to content
This repository has been archived by the owner on Feb 23, 2020. It is now read-only.

Commit

Permalink
Add random duplication handler
Browse files Browse the repository at this point in the history
Signed-off-by: Jonas Kuche <[email protected]>
  • Loading branch information
Zitrone44 committed Apr 1, 2018
1 parent 14dab7f commit 692145d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "zlnk"
version = "0.3.2"
version = "0.3.3"
authors = ["Jonas Kuche <[email protected]>"]

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ static_file!("/assets/bootstrap.min.js", "../assets/bootstrap.min.js", ContentTy
#[post("/shorten", data="<long_url>")]
fn shorten(long_url: String, env: State<Env>, pool: State<Pool<RedisConnectionManager>>) -> Result<String, Failure> {
let connection = &pool.get().unwrap();
let short_url = short(long_url, env.inner(), connection);
let short_url = short(long_url, env.inner(), connection, None);
match short_url {
Some(short_url) => {
Ok(short_url)
Expand Down
26 changes: 20 additions & 6 deletions src/shortener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,33 @@ pub fn random(length: usize, alphabet_name: String) -> String {
result.join("")
}

pub fn short(long_url: String, env: &Env, connection: &r2d2::PooledConnection<r2d2_redis::RedisConnectionManager>) -> Option<String> {
pub fn short(long_url: String, env: &Env, connection: &r2d2::PooledConnection<r2d2_redis::RedisConnectionManager>, retries: Option<u8>) -> Option<String> {
let valid = env.url_regex.is_match(&long_url);
if valid {
let existing = connection.get(&long_url);
let existing = connection.get(format!("long_{}", &long_url));
match existing {
Ok(existing_short) => {
Some(existing_short)
}
Err(_err) => {
let random_value = random(env.short_length, env.short_alphabet.to_owned());
let _: () = connection.set(&random_value, &long_url).unwrap();
let _: () = connection.set(&long_url, &random_value).unwrap();
Some(random_value)
let exist = connection.get(format!("short_{}", &random_value));
match exist {
Ok(_short) => {
let _: String = _short;
let retry = retries.unwrap_or(0);
if retry == 10 {
None
} else {
short(long_url, env, connection, Some(retry + 1))
}
}
Err(_err) => {
let _: () = connection.set(format!("short_{}", &random_value), &long_url).unwrap();
let _: () = connection.set(format!("long_{}", &long_url), &random_value).unwrap();
Some(random_value)
}
}
}
}
} else {
Expand All @@ -48,7 +62,7 @@ pub fn short(long_url: String, env: &Env, connection: &r2d2::PooledConnection<r2
}

pub fn long(short_url: String, connection: &r2d2::PooledConnection<r2d2_redis::RedisConnectionManager>) -> Option<String> {
let long = connection.get(short_url);
let long = connection.get(format!("short_{}", short_url));
match long {
Ok(long) => {
Some(long)
Expand Down
8 changes: 4 additions & 4 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn shortener_test() {
.unwrap();
let connection = &pool.get().unwrap();
let long_url = "https://zlnk.de".to_string();
let shorted = short(long_url.clone(), env, connection).unwrap();
let shorted = short(long_url.clone(), env, connection, None).unwrap();
let longed = long(shorted, connection).unwrap();
assert_eq!(longed, long_url);
}
Expand All @@ -27,7 +27,7 @@ fn invalid_url_test() {
.unwrap();
let connection = &pool.get().unwrap();
let long_url = "data:text/plain,https://zlnk.de".to_string();
let shorted = short(long_url, env, connection).is_none();
let shorted = short(long_url, env, connection, None).is_none();
assert_eq!(shorted, true);
}

Expand All @@ -40,7 +40,7 @@ fn double_shortening_test() {
.unwrap();
let connection = &pool.get().unwrap();
let long_url = "https://zlnk.de".to_string();
let shorted_one = short(long_url.clone(), env, connection).unwrap();
let shorted_two = short(long_url.clone(), env, connection).unwrap();
let shorted_one = short(long_url.clone(), env, connection, None).unwrap();
let shorted_two = short(long_url.clone(), env, connection, None).unwrap();
assert_eq!(shorted_one, shorted_two);
}

0 comments on commit 692145d

Please sign in to comment.