Created
August 3, 2017 03:02
-
-
Save zhongweili/e05474429ef59ec64cbdb24e4a1b50f0 to your computer and use it in GitHub Desktop.
分表
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 根据uid哈希分表 | |
* @param int $uid | |
* @param string $tbprefix | |
* @param int $tbNum | |
* @return string | |
*/ | |
public static function get_table_name($uid, $tbprefix, $tbNum=256){ | |
//$ext = dechex(fmod(sprintf("%u", crc32($uid)), $tbNum)); | |
$ext = fmod(sprintf("%u", crc32($uid)), $tbNum); | |
$tbsuffix = sprintf("%003s", $ext); | |
return $tbprefix.$tbsuffix; | |
dechex($tbsuffix); | |
}s | |
/** | |
* 根据uid将表打散为128张后获取表名字 | |
* @param $uid | |
* @param $tbprefix | |
* @return string | |
*/ | |
public static function get_tablename($uid,$tbprefix) { | |
$h1 = intval($uid) % 128; | |
$h2 = strtolower(dechex($h1)); | |
$h3 = str_repeat('0', 2 - strlen($h2)) . $h2; | |
return vsprintf($tbprefix, $h3); | |
} | |
/** | |
* 根据uid将表打散为n张后获取表名字 | |
* @param $uid | |
* @param $tbprefix | |
* @return string | |
*/ | |
public static function get_tablename_by_num($uid,$tbprefix,$num) { | |
$h1 = intval($uid) % $num; | |
$h2 = strtolower(dechex($h1)); | |
$h3 = str_repeat('0', 2 - strlen($h2)) . $h2; | |
return vsprintf($tbprefix, $h3); | |
} | |
/** | |
* 获取hash规则后 转变为16进制的表名 | |
* @param unknown_type $uid | |
* @param unknown_type $tbprefix | |
* @param unknown_type $tbNum | |
* @return string | |
*/ | |
public static function get_table_pepcenter_name($uid, $tbprefix, $tbNum=256){ | |
$ext = fmod(sprintf("%u", crc32($uid)), $tbNum); | |
$extnew = dechex($ext); | |
$tbsuffix = sprintf("%02s", $extnew); | |
return $tbprefix."_".$tbsuffix; | |
} | |
/** | |
* 获取hash规则后 转变为10进制的表名000~127 | |
* @param unknown_type $uid | |
* @param unknown_type $tbprefix | |
* @param unknown_type $tbNum | |
* @return string | |
*/ | |
public static function get_shoptable_pepcenter_name($uid, $tbprefix, $tbNum=128){ | |
$ext = fmod(sprintf("%u", crc32($uid)), $tbNum); | |
$tbsuffix = sprintf("%03s", $ext); | |
return $tbprefix."_".$tbsuffix; | |
} | |
/** | |
* hash 分表,返回表格式xxxx_00 | |
* @param $uid | |
*/ | |
public static function get_tablename_hash($uid,$tablename,$tbnum=128) { | |
$h1 = intval($uid) % $tbnum; | |
$h2 = strtolower(dechex($h1)); | |
$h3 = str_repeat('0', 2 - strlen($h2)) . $h2; | |
return vsprintf($tablename, $h3); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment