|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
用C语言重新编写,包括可以访问数据库。他将这些程序和一些表单直译器整合起来,称为 PHP/FI。PHP/FI 可以和数据库连接,产生简单的动态网页程序。数据|数据库 找了些工夫,改善了本人文本数据库体系,从空间使用效力和并发功能两个方面着手,弄高了法式功能。
在空间使用率上,做了两点改善,一是在删除纪录的时分,假如纪录的寄存地位在数据库的末尾,就主动修正数据库末尾指针,而不是将此纪录的空间作为一个闲置块存入闲置块纪录中,如许就能够持续在数据文件尾分派恣意长度的空间而不是像之前那样将这个删除纪录的空间整块分派给下一次恳求空间的新纪录;第二点改善是在从闲置块中恳求空间的时分,不但持续采取本来的最好顺应法,并且在恳求到适合的闲置块的时分并非将全部块而是将其局部划分给新纪录,如许又进一部进步了空间的使用率。
在并发功能上,增添了数据库的翻开形式这个选项。为了坚持文本数据库操作的原子性,之前都以写形式翻开数据库,用户不管是从数据库中读数据或写数据,体系城市锁定命据库而制止其它用户并发的读写操作,而大多情形下,用户都是阅读网页而不是POST数据,制止对数据库的并行的读操作,必将成为网页阅读速度的瓶颈,为了减速网页生成速度,在数据库中到场了翻开形式这一选项,在只读形式下,可以答应分歧的用户并发地从数据库中请求数据,使得网页并发功能失掉了必定法式上的进步。
上面给出新文本数据库类的局部代码,其相干正文可以从本来的文章:http://202.113.13.169/site/redboy/index.php?item=read&id=1&blogID=4中取得。
源代码:
<?
class TxtDB
{
var $name='';//文本数据库名
var $path='';
var $minLen=20;
var $isError;
var $dbh;
var $indxh;
var $lfth;
var $lckh;
var $rcdCnt=0;
var $maxID=0;
var $leftCnt=0;
var $DBend=0;
var $mod='w';
function TxtDB($name,$mod='w',$path='bpns_dbm')
{
$this->name=$name;
$this->path=$path.'/'.$name;
$this->isError=0;
$this->mod=$mod;
$path=$this->path;
if ($name!='')
{
@mkdir($this->path,0777);
if (!file_exists($path.'/'.$name.'.tdb')) $this->dbh=fopen($this->path.'/'.$name.'.tdb','w+');
else $this->dbh=fopen($path.'/'.$name.'.tdb','r+');
if (!file_exists($path.'/'.$name.'.indx')) $this->indxh=fopen($this->path.'/'.$name.'.indx','w+');
else $this->indxh=fopen($path.'/'.$name.'.indx','r+');
if (!file_exists($path.'/'.$name.'.lft')) $this->lfth=fopen($this->path.'/'.$name.'.lft','w+');
else $this->lfth=fopen($this->path.'/'.$name.'.lft','r+');
if ($this->mod=='w')
{
$this->lckh=fopen($this->path.'/'.$name.'.lck','w');
flock($this->lckh,2);
fwrite($this->lckh,'lck');//lock the datebase
}
$rcd=$this->getRcd(0);
$this->rcdCnt=$rcd[id];
$this->maxID=$rcd[loc];
$this->DBend=$rcd[len];
$rcd=$this->getLeft(0);
$this->leftCnt=$rcd[loc];
}
else $this->isError=1;
}
function setRcd($rid,$id,$loc,$len)
{
fseek($this->indxh,$rid*12);
$str=pack('III',$id,$loc,$len);
fwrite($this->indxh,$str,12);
}
function getRcd($rid)
{
fseek($this->indxh,$rid*12);
$str=fread($this->indxh,12);
$rcd=array();
$rcd[id]=str2int($str);
$rcd[loc]=str2int(substr($str,4,4));
$rcd[len]=str2int(substr($str,8,4));
return $rcd;
}
function setLeft($lid,$loc,$len)
{
fseek($this->lfth,$lid*8);
$str=pack('II',$loc,$len);
fwrite($this->lfth,$str,8);
}
function getLeft($lid)
{
fseek($this->lfth,$lid*8);
$str=fread($this->lfth,8);
$rcd[loc]=str2int($str);
$rcd[len]=str2int(substr($str,4,4));
return $rcd;
}
function clear()
{
$this->setRcd(0,0,0,0);
$this->setLeft(0,0,0);
}
function close()
{
@fclose($this->dbh);
@fclose($this->indxh);
@fclose($this->lfth);
@fclose($this->lckh);
}
function seekSpace($len)
{
$res=array('loc'=>0,'len'=>0);
if ($this->leftCnt<1) return $res;
$find=0;
$min=1000000;
for ($i=$this->leftCnt;$i>0;$i--)
{
$res=$this->getLeft($i);
if ($res[len]==$len) {$find=$i;break;}
else if($res[len]>$len)
{
if ($res[len]-$len<$min)
{
$min=$res[len]-$len;
$find=$i;
}
}
}
if ($find)
{
$res=$this->getLeft($find);
if ($res[len]<2*$len)
{
fseek($this->lfth,($find+1)*8);
$str=fread($this->lfth,($this->leftCnt-$find)*8);
fseek($this->lfth,$find*8);
fwrite($this->lfth,$str);
$this->leftCnt--;
$this->setLeft(0,$this->leftCnt,0);
return $res;
}
else
{
$rs=array();
$rs[loc]=$res[loc];
$rs[len]=$len;
$res[loc]+=$len;
$this->setLeft($find,$res[loc],$res[len]-$len);
return $rs;
}
}
else//fail
{
$res[len]=0;
return $res;
}
}
function insert($content,$len=0)//return with record id
{
$res=array('loc'=>0);
if ($this->mod!='w') return 0;
if (!$len) $len=strlen($content);
if ($len<$this->minLen) $len=$this->minLen;
if ($this->leftCnt) $res=$this->seekSpace($len);
if (!$res[len])
{
$res[loc]=$this->DBend;
$res[len]=$len;
}
if ($res[loc]+$res[len]>$this->DBend) $this->DBend=$res[loc]+$res[len];
//echo $this->DBend.'<br>';
$this->maxID++;
$this->rcdCnt++;
$this->setRcd(0,$this->rcdCnt,$this->maxID,$this->DBend);
$this->setRcd($this->rcdCnt,$this->maxID,$res[loc],$res[len]);
fseek($this->dbh,$res[loc]);
fwrite($this->dbh,$content,$len);
return $this->maxID;
}
function findByID($id)
{
if ($id<1 or $id>$this->maxID or $this->rcdCnt<1) return 0;
$left=1;
$right=$this->rcdCnt;
while($left<$right)
{
$mid=(int)(($left+$right)/2);
if ($mid==$left or $mid==$right) break;
$rcd=$this->getRcd($mid);
if ($rcd[id]==$id) return $mid;
else if($id<$rcd[id]) $right=$mid;
else $left=$mid;
}
//$rcd=$this->getRcd($mid);
//if ($rcd[id]==$id) return $mid;
$rcd=$this->getRcd($left);
if ($rcd[id]==$id) return $left;
$rcd=$this->getRcd($right);
if ($rcd[id]==$id) return $right;
return 0;
}
function delete($id)
{
if ($this->mod!='w') return 0;
$rid=$this->findByID($id);
if (!$rid) return;
$res=$this->getRcd($rid);
fseek($this->indxh,($rid+1)*12);
$str=fread($this->indxh,($this->rcdCnt-$i)*12);
fseek($this->indxh,$rid*12);
fwrite($this->indxh,$str);
$this->rcdCnt--;
if ($res[loc]+$res[len]==$this->DBend)
{
$this->DBend=$res[loc];
$this->setRcd(0,$this->rcdCnt,$this->maxID,$this->DBend);
}
else
{
$this->setRcd(0,$this->rcdCnt,$this->maxID,$this->DBend);
$this->leftCnt++;
$this->setLeft(0,$this->leftCnt,0);
$this->setLeft($this->leftCnt,$res[loc],$res[len]);
}
}
function update($id,$newcontent,$len=0)
{
if ($this->mod!='w') return;
$rid=$this->findByID($id);
if (!$rid) return;
if (!$len) $len=strlen($newcontent);
$rcd=$this->getRcd($rid);
if ($rcd[len]<$len)
{
$this->leftCnt++;
$this->setLeft(0,$this->leftCnt,0);
$this->setLeft($this->leftCnt,$rcd[loc],$rcd[len]);
$rcd[loc]=$this->DBend;
$rcd[len]=$len;
$this->DBend+=$len;
$this->setRcd(0,$this->rcdCnt,$this->maxID,$this->DBend);
$this->setRcd($rid,$rcd[id],$rcd[loc],$rcd[len]);
}
fseek($this->dbh,$rcd[loc]);
fwrite($this->dbh,$newcontent,$len);
//echo $id.'<br>'.$content.'<br>'.$len;
}
function selectByRid($rid)
{
$res=array('id'=>0,'content'=>'');
if ($rid<1 or $rid>$this->rcdCnt) return $res;
else $rcd=$this->getRcd($rid);
$res[id]=$rcd[id];
$res[len]=$rcd[len];
fseek($this->dbh,$rcd[loc]);
$res[content]=fread($this->dbh,$rcd[len]);
//$res[rid]=$rid;
return $res;
}
function select($id)
{
return $this->selectByRid($this->findByID($id));
}
function backup()
{
copy($this->path.'/'.$this->name.'.tdb',$this->path.'/'.$this->name.'.tdb.bck');
copy($this->path.'/'.$this->name.'.indx',$this->path.'/'.$this->name.'.indx.bck');
copy($this->path.'/'.$this->name.'.lft',$this->path.'/'.$this->name.'.lft.bck');
}
function recover()
{
copy($this->path.'/'.$this->name.'.tdb.bck',$this->path.'/'.$this->name.'.tdb');
copy($this->path.'/'.$this->name.'.indx.bck',$this->path.'/'.$this->name.'.indx');
copy($this->path.'/'.$this->name.'.lft.bck',$this->path.'/'.$this->name.'.lft');
}
}
?>
PS: 其他可以参考上一篇文章:http://blog.csdn.net/heiyeshuwu/archive/2006/06/16/804265.aspx
现在,也有了从事软件工程的想法,在经过了一个月的PHP培训学习之后,发现其实软件工程并没有想像中的那么难。 |
|