![]() So the short URL will be the counter and here is a code snippet assuming that your server is running properly.Typically, a long URL is a system-generated URL, usually containing multiple query parameters that may be encrypted/encoded to mask those parameters. Since the counter will be unique if we are storing the data in the same machine we can get it with no doubts that it will be duplicate. j1k2l3 represents the counter, starting with a random value.a1b2c3d4 represents the seconds since the Unix epoch,.a 3-byte counter (in your machine), starting with a random value.a 4-byte value representing the seconds since the Unix epoch,.Since we know the format that MongoDB uses to create a new ObjectId with 12 bytes. Now find your database-record with WHERE id = 19158 and do the redirect.Įxample implementations (provided by commenters) You just do a reverse lookup in your alphabet.Į9a 62 will be resolved to "4th, 61st, and 0th letter in the alphabet".Į9a 62 = 4×62 2 + 61×62 1 + 0×62 0 = 19158 10 How to resolve a shortened URL to the initial ID With 2 → c and 1 → b, you will receive cb 62 as the shortened URL. This is how your mapping (with an array for example) could look like: 0 → a Now map the indices 2 and 1 to your alphabet. This requires the use of integer division and modulo. Now you have to convert 125 10 to X 62 (base 62). Take an auto-generated, unique numerical key (the auto-incremented id of a MySQL table for example).įor this example, I will use 125 10 (125 with a base of 10). and for every y you must be able to find an x so that f(x) = y.There must be no x1, x2 (with x1 ≠ x2) that will make f(x1) = f(x2),.This is necessary so that you can find a inverse function g('abc') = 123 for your f(123) = 'abc' function. However, you will realize that your proposed algorithm fails if your ID is a prime and greater than 52. I would continue your "convert number to string" approach. ![]() Do you think this is a good approach? Do you have a better idea?ĭue to the ongoing interest in this topic, I've published an efficient solution to GitHub, with implementations for JavaScript, PHP, Python and Java. That could be repeated until the number isn't divisible any more. If divisible by 3, add "b"+the result to short If divisible by 2, add "a"+the result to short Then I do the following steps: short = '' A self-built algorithm will work, too.įor " " I get the auto-increment id 239472. But what sort of hash should I build? Hash algorithms like MD5 create too long strings. This hash should then be inserted as " short". Then I would select the auto-increment value for " id" and build a hash of it. I would then insert the long URL into the table. short, string, the shortened URL (or just the six characters).long, string, the long URL the user entered.I have a database table with three columns: That makes 56~57 billion possible strings. Instead of " abcdef" there can be any other string with six characters containing a-z, A-Z and 0-9. I want to create a URL shortener service where you can write a long URL into an input field and the service shortens the URL to " ". It is not currently accepting new answers or interactions. This question and its answers are locked because the question is off-topic but has historical significance.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |