调整php版XdbSearcher中某处elseif,并格式化代码
This commit is contained in:
parent
1de639279b
commit
05a2b1d65e
|
|
@ -9,16 +9,14 @@
|
||||||
class XdbSearcher
|
class XdbSearcher
|
||||||
{
|
{
|
||||||
const HeaderInfoLength = 256;
|
const HeaderInfoLength = 256;
|
||||||
const VectorIndexRows = 256;
|
const VectorIndexRows = 256;
|
||||||
const VectorIndexCols = 256;
|
const VectorIndexCols = 256;
|
||||||
const VectorIndexSize = 8;
|
const VectorIndexSize = 8;
|
||||||
const SegmentIndexSize = 14;
|
const SegmentIndexSize = 14;
|
||||||
|
|
||||||
// xdb file handle
|
// xdb file handle
|
||||||
private $handle = null;
|
private $handle = null;
|
||||||
|
|
||||||
// header info
|
|
||||||
private $header = null;
|
|
||||||
private $ioCount = 0;
|
private $ioCount = 0;
|
||||||
|
|
||||||
// vector index in binary string.
|
// vector index in binary string.
|
||||||
|
|
@ -32,23 +30,26 @@ class XdbSearcher
|
||||||
// static function to create searcher
|
// static function to create searcher
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws Exception
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
public static function newWithFileOnly($dbFile) {
|
public static function newWithFileOnly($dbFile)
|
||||||
|
{
|
||||||
return new XdbSearcher($dbFile, null, null);
|
return new XdbSearcher($dbFile, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws Exception
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
public static function newWithVectorIndex($dbFile, $vIndex) {
|
public static function newWithVectorIndex($dbFile, $vIndex)
|
||||||
|
{
|
||||||
return new XdbSearcher($dbFile, $vIndex);
|
return new XdbSearcher($dbFile, $vIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws Exception
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
public static function newWithBuffer($cBuff) {
|
public static function newWithBuffer($cBuff)
|
||||||
|
{
|
||||||
return new XdbSearcher(null, null, $cBuff);
|
return new XdbSearcher(null, null, $cBuff);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -56,9 +57,10 @@ class XdbSearcher
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* initialize the xdb searcher
|
* initialize the xdb searcher
|
||||||
* @throws Exception
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
function __construct($dbFile, $vectorIndex=null, $cBuff=null) {
|
public function __construct($dbFile, $vectorIndex = null, $cBuff = null)
|
||||||
|
{
|
||||||
// check the content buffer first
|
// check the content buffer first
|
||||||
if ($cBuff != null) {
|
if ($cBuff != null) {
|
||||||
$this->vectorIndex = null;
|
$this->vectorIndex = null;
|
||||||
|
|
@ -67,33 +69,36 @@ class XdbSearcher
|
||||||
// open the xdb binary file
|
// open the xdb binary file
|
||||||
$this->handle = fopen($dbFile, "r");
|
$this->handle = fopen($dbFile, "r");
|
||||||
if ($this->handle === false) {
|
if ($this->handle === false) {
|
||||||
throw new Exception("failed to open xdb file '%s'", $dbFile);
|
throw new \Exception("failed to open xdb file '%s'", $dbFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->vectorIndex = $vectorIndex;
|
$this->vectorIndex = $vectorIndex;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function close() {
|
public function close()
|
||||||
|
{
|
||||||
if ($this->handle != null) {
|
if ($this->handle != null) {
|
||||||
fclose($this->handle);
|
fclose($this->handle);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getIOCount() {
|
public function getIOCount()
|
||||||
|
{
|
||||||
return $this->ioCount;
|
return $this->ioCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* find the region info for the specified ip address
|
* find the region info for the specified ip address
|
||||||
* @throws Exception
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
function search($ip) {
|
public function search($ip)
|
||||||
|
{
|
||||||
// check and convert the sting ip to a 4-bytes long
|
// check and convert the sting ip to a 4-bytes long
|
||||||
if (is_string($ip)) {
|
if (is_string($ip)) {
|
||||||
$t = self::ip2long($ip);
|
$t = self::ip2long($ip);
|
||||||
if ($t === null) {
|
if ($t === null) {
|
||||||
throw new Exception("invalid ip address `$ip`");
|
throw new \Exception("invalid ip address `$ip`");
|
||||||
}
|
}
|
||||||
$ip = $t;
|
$ip = $t;
|
||||||
}
|
}
|
||||||
|
|
@ -108,14 +113,14 @@ class XdbSearcher
|
||||||
if ($this->vectorIndex != null) {
|
if ($this->vectorIndex != null) {
|
||||||
$sPtr = self::getLong($this->vectorIndex, $idx);
|
$sPtr = self::getLong($this->vectorIndex, $idx);
|
||||||
$ePtr = self::getLong($this->vectorIndex, $idx + 4);
|
$ePtr = self::getLong($this->vectorIndex, $idx + 4);
|
||||||
} else if ($this->contentBuff != null) {
|
} elseif ($this->contentBuff != null) {
|
||||||
$sPtr = self::getLong($this->contentBuff, self::HeaderInfoLength + $idx);
|
$sPtr = self::getLong($this->contentBuff, self::HeaderInfoLength + $idx);
|
||||||
$ePtr = self::getLong($this->contentBuff, self::HeaderInfoLength + $idx + 4);
|
$ePtr = self::getLong($this->contentBuff, self::HeaderInfoLength + $idx + 4);
|
||||||
} else {
|
} else {
|
||||||
// read the vector index block
|
// read the vector index block
|
||||||
$buff = $this->read(self::HeaderInfoLength + $idx, 8);
|
$buff = $this->read(self::HeaderInfoLength + $idx, 8);
|
||||||
if ($buff === null) {
|
if ($buff === null) {
|
||||||
throw new Exception("failed to read vector index at ${idx}");
|
throw new \Exception("failed to read vector index at ${idx}");
|
||||||
}
|
}
|
||||||
|
|
||||||
$sPtr = self::getLong($buff, 0);
|
$sPtr = self::getLong($buff, 0);
|
||||||
|
|
@ -136,7 +141,7 @@ class XdbSearcher
|
||||||
// read the segment index
|
// read the segment index
|
||||||
$buff = $this->read($p, self::SegmentIndexSize);
|
$buff = $this->read($p, self::SegmentIndexSize);
|
||||||
if ($buff == null) {
|
if ($buff == null) {
|
||||||
throw new Exception("failed to read segment index at ${p}");
|
throw new \Exception("failed to read segment index at ${p}");
|
||||||
}
|
}
|
||||||
|
|
||||||
$sip = self::getLong($buff, 0);
|
$sip = self::getLong($buff, 0);
|
||||||
|
|
@ -171,7 +176,8 @@ class XdbSearcher
|
||||||
}
|
}
|
||||||
|
|
||||||
// read specified bytes from the specified index
|
// read specified bytes from the specified index
|
||||||
private function read($offset, $len) {
|
private function read($offset, $len)
|
||||||
|
{
|
||||||
// check the in-memory buffer first
|
// check the in-memory buffer first
|
||||||
if ($this->contentBuff != null) {
|
if ($this->contentBuff != null) {
|
||||||
return substr($this->contentBuff, $offset, $len);
|
return substr($this->contentBuff, $offset, $len);
|
||||||
|
|
@ -217,8 +223,8 @@ class XdbSearcher
|
||||||
// read a 4bytes long from a byte buffer
|
// read a 4bytes long from a byte buffer
|
||||||
public static function getLong($b, $idx)
|
public static function getLong($b, $idx)
|
||||||
{
|
{
|
||||||
$val = (ord($b[$idx])) | (ord($b[$idx+1]) << 8)
|
$val = (ord($b[$idx])) | (ord($b[$idx + 1]) << 8)
|
||||||
| (ord($b[$idx+2]) << 16) | (ord($b[$idx+3]) << 24);
|
| (ord($b[$idx + 2]) << 16) | (ord($b[$idx + 3]) << 24);
|
||||||
|
|
||||||
// convert signed int to unsigned int if on 32 bit operating system
|
// convert signed int to unsigned int if on 32 bit operating system
|
||||||
if ($val < 0 && PHP_INT_SIZE == 4) {
|
if ($val < 0 && PHP_INT_SIZE == 4) {
|
||||||
|
|
@ -231,11 +237,12 @@ class XdbSearcher
|
||||||
// read a 2bytes short from a byte buffer
|
// read a 2bytes short from a byte buffer
|
||||||
public static function getShort($b, $idx)
|
public static function getShort($b, $idx)
|
||||||
{
|
{
|
||||||
return ((ord($b[$idx])) | (ord($b[$idx+1]) << 8));
|
return ((ord($b[$idx])) | (ord($b[$idx + 1]) << 8));
|
||||||
}
|
}
|
||||||
|
|
||||||
// load header info from a specified file handle
|
// load header info from a specified file handle
|
||||||
public static function loadHeader($handle) {
|
public static function loadHeader($handle)
|
||||||
|
{
|
||||||
if (fseek($handle, 0) == -1) {
|
if (fseek($handle, 0) == -1) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
@ -251,17 +258,18 @@ class XdbSearcher
|
||||||
}
|
}
|
||||||
|
|
||||||
// return the decoded header info
|
// return the decoded header info
|
||||||
return array(
|
return [
|
||||||
'version' => self::getShort($buff, 0),
|
'version' => self::getShort($buff, 0),
|
||||||
'indexPolicy' => self::getShort($buff, 2),
|
'indexPolicy' => self::getShort($buff, 2),
|
||||||
'createdAt' => self::getLong($buff, 4),
|
'createdAt' => self::getLong($buff, 4),
|
||||||
'startIndexPtr' => self::getLong($buff, 8),
|
'startIndexPtr' => self::getLong($buff, 8),
|
||||||
'endIndexPtr' => self::getLong($buff, 12)
|
'endIndexPtr' => self::getLong($buff, 12)
|
||||||
);
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
// load header info from the specified xdb file path
|
// load header info from the specified xdb file path
|
||||||
public static function loadHeaderFromFile($dbFile) {
|
public static function loadHeaderFromFile($dbFile)
|
||||||
|
{
|
||||||
$handle = fopen($dbFile, 'r');
|
$handle = fopen($dbFile, 'r');
|
||||||
if ($handle === false) {
|
if ($handle === false) {
|
||||||
return null;
|
return null;
|
||||||
|
|
@ -271,7 +279,8 @@ class XdbSearcher
|
||||||
}
|
}
|
||||||
|
|
||||||
// load vector index from a file handle
|
// load vector index from a file handle
|
||||||
public static function loadVectorIndex($handle) {
|
public static function loadVectorIndex($handle)
|
||||||
|
{
|
||||||
if (fseek($handle, self::HeaderInfoLength) == -1) {
|
if (fseek($handle, self::HeaderInfoLength) == -1) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
@ -290,7 +299,8 @@ class XdbSearcher
|
||||||
}
|
}
|
||||||
|
|
||||||
// load vector index from a specified xdb file path
|
// load vector index from a specified xdb file path
|
||||||
public static function loadVectorIndexFromFile($dbFile) {
|
public static function loadVectorIndexFromFile($dbFile)
|
||||||
|
{
|
||||||
$handle = fopen($dbFile, 'r');
|
$handle = fopen($dbFile, 'r');
|
||||||
if ($handle === false) {
|
if ($handle === false) {
|
||||||
return null;
|
return null;
|
||||||
|
|
@ -300,7 +310,8 @@ class XdbSearcher
|
||||||
}
|
}
|
||||||
|
|
||||||
// load the xdb content from a file handle
|
// load the xdb content from a file handle
|
||||||
public static function loadContent($handle) {
|
public static function loadContent($handle)
|
||||||
|
{
|
||||||
if (fseek($handle, 0, SEEK_END) == -1) {
|
if (fseek($handle, 0, SEEK_END) == -1) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
@ -329,7 +340,8 @@ class XdbSearcher
|
||||||
}
|
}
|
||||||
|
|
||||||
// load the xdb content from a file path
|
// load the xdb content from a file path
|
||||||
public static function loadContentFromFile($dbFile) {
|
public static function loadContentFromFile($dbFile)
|
||||||
|
{
|
||||||
$str = file_get_contents($dbFile, false);
|
$str = file_get_contents($dbFile, false);
|
||||||
if ($str === false) {
|
if ($str === false) {
|
||||||
return null;
|
return null;
|
||||||
|
|
@ -338,8 +350,8 @@ class XdbSearcher
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function now() {
|
public static function now()
|
||||||
|
{
|
||||||
return (microtime(true) * 1000);
|
return (microtime(true) * 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue