调整php版XdbSearcher中某处elseif,并格式化代码

This commit is contained in:
lei.tian 2022-06-30 19:47:31 +08:00
parent 1de639279b
commit 05a2b1d65e
1 changed files with 52 additions and 40 deletions

View File

@ -17,8 +17,6 @@ class XdbSearcher
// 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;
}
@ -115,7 +120,7 @@ class XdbSearcher
// 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);
@ -235,7 +241,8 @@ class XdbSearcher
}
// 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(
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)
);
];
}
// 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);
}
}