1
0
mirror of https://github.com/cydrobolt/polr.git synced 2024-11-09 19:52:28 +01:00

Add indexes to link table and use cr32 hashes to increase speed of lookups

This commit is contained in:
Chaoyi Zha 2017-02-03 23:12:47 -05:00
parent 88a4afea07
commit 9e7b992d92
4 changed files with 69 additions and 1 deletions

View File

@ -7,6 +7,8 @@ use App\Helpers\LinkHelper;
class LinkFactory { class LinkFactory {
const MAXIMUM_LINK_LENGTH = 65535;
private static function formatLink($link_ending, $secret_ending=false) { private static function formatLink($link_ending, $secret_ending=false) {
/** /**
* Given a link ending and a boolean indicating whether a secret ending is needed, * Given a link ending and a boolean indicating whether a secret ending is needed,
@ -39,6 +41,13 @@ class LinkFactory {
* @return string $formatted_link * @return string $formatted_link
*/ */
if (strlen($long_url) > self::MAXIMUM_LINK_LENGTH) {
// If $long_url is longer than the maximum length, then
// throw an Exception
throw new \Exception('Sorry, but your link is longer than the
maximum length allowed.');
}
$is_already_short = LinkHelper::checkIfAlreadyShortened($long_url); $is_already_short = LinkHelper::checkIfAlreadyShortened($long_url);
if ($is_already_short) { if ($is_already_short) {

View File

@ -57,7 +57,7 @@ class LinkHelper {
* check whether the link is in the DB. * check whether the link is in the DB.
* @return boolean * @return boolean
*/ */
$link = Link::where('long_url', $long_url) $link = Link::longUrl($long_url)
->where('is_custom', 0) ->where('is_custom', 0)
->where('secret_key', '') ->where('secret_key', '')
->first(); ->first();

View File

@ -4,4 +4,27 @@ use Illuminate\Database\Eloquent\Model;
class Link extends Model { class Link extends Model {
protected $table = 'links'; protected $table = 'links';
public function setLongUrlAttribute($long_url) {
// Set crc32 hash and long_url
// whenever long_url is set on a Link instance
// Generate 32-bit unsigned integer crc32 value
// Use sprintf to prevent compatibility issues with 32-bit systems
// http://php.net/manual/en/function.crc32.php
$crc32_hash = sprintf('%u', crc32($long_url));
$this->attributes['long_url'] = $long_url;
$this->attributes['long_url_hash'] = $crc32_hash;
}
public function scopeLongUrl($query, $long_url) {
// Allow quick lookups with Link::longUrl that make use
// of the indexed crc32 hash to quickly fetch link
$crc32_hash = sprintf('%u', crc32($long_url));
return $query
->where('long_url_hash', $crc32_hash)
->where('long_url', $long_url);
}
} }

View File

@ -0,0 +1,36 @@
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddLinkTableIndexes extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('links', function (Blueprint $table)
{
// Add long_url hashes
$table->unique('short_url');
$table->string('long_url_hash', 10)->nullable();
$table->index('long_url_hash', 'links_long_url_index');
});
DB::query("UPDATE links SET long_url_hash = crc32(long_url)");
}
public function down()
{
Schema::table('links', function (Blueprint $table)
{
$table->longtext('long_url')->change();
$table->dropUnique('links_short_url_unique');
$table->dropIndex('links_long_url_index');
$table->dropColumn('long_url_hash');
});
}
}