[转]改进的PHP文本数据库类

本文介绍了一种改进的PHP文本数据库类,该类通过优化空间利用率和增加并发读取功能来提高性能。在空间利用方面,改进措施包括调整数据库末端指针和更精细地分配空闲块空间。在并发性能方面,新增加了数据库打开模式选项,允许用户在只读模式下并发读取数据。



改进的PHP文本数据库类

作者:bpns
来源:http://space.tju.cn/site/redboy/index.php?item=read&blogID=4&id=11


找了些时间,改进了自己文本数据库系统,从空间利用效率和并发性能两个方面着手,搞高了程序性能。

在空间利用率上,做了两点改进,一是在删除记录的时候,如果记录的存放位置在数据库的末端,就自动修改数据库末端指针,而不是将此记录的空间作为一个闲置块存入闲置块记录中,这样就可以继续在数据文件尾分配任意长度的空间而不是像以前那样将这个删除记录的空间整块分配给下一次请求空间的新记录;第二点改进是在从闲置块中请求空间的时候,不仅继续采用原来的最佳适应法,而且在请求到合适的闲置块的时候并不是将整个块而是将其部分划分给新记录,这样又进一部提高了空间的利用率。

在并发性能上,增加了数据库的打开模式这个选项。为了保持文本数据库操作的原子性,以前都以写模式打开数据库,用户无论是从数据库中读数据或写数据,系统都会锁定数据库而禁止其它用户并发的读写操作,而大多情况下,用户都是浏览网页而不是POST数据,禁止对数据库的并行的读操作,势必成为网页浏览速度的瓶颈,为了加速网页生成速度,在数据库中加入了打开模式这一选项,在只读模式下,可以允许不同的用户并发地从数据库中申请数据,使得网页并发性能得到了一定程序上的提高。

下面给出新文本数据库类的部分代码,其相关注释可以从原来的文章:http://202.113.13.169/site/redboy/index.php?item=read&id=1&blogID=4中获得。

源代码:

<?
classTxtDB
{
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');//lockthedatebase
}
$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;}
elseif(
$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)//returnwithrecordid
{
$res=array('loc'=>0);
if(
$this->mod!='w')return0;
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<1or$id>$this->maxIDor$this->rcdCnt<1)return0;
$left=1;
$right=$this->rcdCnt;
while(
$left<$right)
{
$mid=(int)(($left+$right)/2);
if(
$mid==$leftor$mid==$right)break;
$rcd=$this->getRcd($mid);
if(
$rcd[id]==$id)return$mid;
elseif(
$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')return0;
$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<1or$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.youkuaiyun.com/heiyeshuwu/archive/2006/06/16/804265.aspx

1、密码使用DES算法加密,无法逆向破解,使聊天更安全。 2、自动跟踪私聊状态,自动切换和对象是否私聊。 3、自定义头像功能。 4、滚屏方式。使用PHP中先进的流技术,使得显示流畅,达到实时聊天的效果。 5、方便实用的便签。(可随时让您记下聊友的信息等,以后随时可以查看) 6、内置简洁的论坛,方便大家交流。 7、功能强大的个性设置(进入欢迎语,各个窗口的背景与说话颜色)。不用每次进入时设置。 8、可购买和互相赠送礼品与送金钱。 9、多种个人属性设置(经验、魅力、体力、威望、等级、金钱等)设置合理。 10、公式:经验>1000=魅力+30、魅力>1000=体力+30、体力>1000=威望+30、威望>1000=等级+1 每发言一句,经验+1,在聊天室里体力-1 别人向你发言一句,魅力+1 定时开帮会,人气最高三人刚分别:威望+30,金钱+30000、威望+20,金钱+20000、威望+10,金钱+10000 (注:此功能还没有与网友们一起拟定) 在聊天室里每半分钟,金钱+1、在聊天室里体力恢复2点 11、可自定义动作(以 // 开头, 可替换成对方,如输入://知道不是很笨的了。不能连续使用,如: %,且替换次数有限;也可以在现有的动作后面加上一个以上空格后再接上您的话)。 12、飞鸽传书功能,直接与对方联系(但只能用一般格式,不能有动作表情命令等)。 13、可分屏显示,聊天更轻松。 14、自动防断线,也可手工断线重连(使用[辅助功能]),断线后不用退出聊天室。 15、大众聊天室功能(密谈,屏蔽,表情,动作,贴图,且表情与动作分为非善意与善意)。 16、使用文本数据库,穷人有福了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值