diff --git a/binding/php/XdbSearcher.class.php b/binding/php/XdbSearcher.class.php index df8816e..283432a 100644 --- a/binding/php/XdbSearcher.class.php +++ b/binding/php/XdbSearcher.class.php @@ -9,16 +9,14 @@ class XdbSearcher { const HeaderInfoLength = 256; - const VectorIndexRows = 256; - const VectorIndexCols = 256; - const VectorIndexSize = 8; + const VectorIndexRows = 256; + const VectorIndexCols = 256; + const VectorIndexSize = 8; const SegmentIndexSize = 14; // xdb file handle private $handle = null; - // header info - private $header = null; private $ioCount = 0; // vector index in binary string. @@ -32,23 +30,26 @@ class XdbSearcher // 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); } /** - * @throws Exception + * @throws \Exception */ - public static function newWithVectorIndex($dbFile, $vIndex) { + public static function newWithVectorIndex($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); } @@ -56,9 +57,10 @@ class XdbSearcher /** * 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 if ($cBuff != null) { $this->vectorIndex = null; @@ -67,33 +69,36 @@ class XdbSearcher // open the xdb binary file $this->handle = fopen($dbFile, "r"); 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; } } - function close() { + public function close() + { if ($this->handle != null) { fclose($this->handle); } } - function getIOCount() { + public function getIOCount() + { return $this->ioCount; } /** * 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 if (is_string($ip)) { $t = self::ip2long($ip); if ($t === null) { - throw new Exception("invalid ip address `$ip`"); + throw new \Exception("invalid ip address `$ip`"); } $ip = $t; } @@ -108,14 +113,14 @@ class XdbSearcher if ($this->vectorIndex != null) { $sPtr = self::getLong($this->vectorIndex, $idx); $ePtr = self::getLong($this->vectorIndex, $idx + 4); - } else if ($this->contentBuff != null) { + } elseif ($this->contentBuff != null) { $sPtr = self::getLong($this->contentBuff, self::HeaderInfoLength + $idx); $ePtr = self::getLong($this->contentBuff, self::HeaderInfoLength + $idx + 4); } else { // read the vector index block $buff = $this->read(self::HeaderInfoLength + $idx, 8); 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); @@ -136,7 +141,7 @@ class XdbSearcher // read the segment index $buff = $this->read($p, self::SegmentIndexSize); 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); @@ -171,7 +176,8 @@ class XdbSearcher } // read specified bytes from the specified index - private function read($offset, $len) { + private function read($offset, $len) + { // check the in-memory buffer first if ($this->contentBuff != null) { return substr($this->contentBuff, $offset, $len); @@ -217,8 +223,8 @@ class XdbSearcher // read a 4bytes long from a byte buffer public static function getLong($b, $idx) { - $val = (ord($b[$idx])) | (ord($b[$idx+1]) << 8) - | (ord($b[$idx+2]) << 16) | (ord($b[$idx+3]) << 24); + $val = (ord($b[$idx])) | (ord($b[$idx + 1]) << 8) + | (ord($b[$idx + 2]) << 16) | (ord($b[$idx + 3]) << 24); // convert signed int to unsigned int if on 32 bit operating system if ($val < 0 && PHP_INT_SIZE == 4) { @@ -231,11 +237,12 @@ class XdbSearcher // read a 2bytes short from a byte buffer 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 - public static function loadHeader($handle) { + public static function loadHeader($handle) + { if (fseek($handle, 0) == -1) { return null; } @@ -251,17 +258,18 @@ class XdbSearcher } // return the decoded header info - return array( - 'version' => self::getShort($buff, 0), - 'indexPolicy' => self::getShort($buff, 2), - 'createdAt' => self::getLong($buff, 4), + return [ + 'version' => self::getShort($buff, 0), + 'indexPolicy' => self::getShort($buff, 2), + 'createdAt' => self::getLong($buff, 4), 'startIndexPtr' => self::getLong($buff, 8), - 'endIndexPtr' => self::getLong($buff, 12) - ); + 'endIndexPtr' => self::getLong($buff, 12) + ]; } // load header info from the specified xdb file path - public static function loadHeaderFromFile($dbFile) { + public static function loadHeaderFromFile($dbFile) + { $handle = fopen($dbFile, 'r'); if ($handle === false) { return null; @@ -271,7 +279,8 @@ class XdbSearcher } // load vector index from a file handle - public static function loadVectorIndex($handle) { + public static function loadVectorIndex($handle) + { if (fseek($handle, self::HeaderInfoLength) == -1) { return null; } @@ -290,7 +299,8 @@ class XdbSearcher } // load vector index from a specified xdb file path - public static function loadVectorIndexFromFile($dbFile) { + public static function loadVectorIndexFromFile($dbFile) + { $handle = fopen($dbFile, 'r'); if ($handle === false) { return null; @@ -300,7 +310,8 @@ class XdbSearcher } // 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) { return null; } @@ -329,7 +340,8 @@ class XdbSearcher } // load the xdb content from a file path - public static function loadContentFromFile($dbFile) { + public static function loadContentFromFile($dbFile) + { $str = file_get_contents($dbFile, false); if ($str === false) { return null; @@ -338,8 +350,8 @@ class XdbSearcher } } - public static function now() { + public static function now() + { return (microtime(true) * 1000); } - }