改进的PHP文本数据库类
数据|数据库
找了些时间,改进了自己文本数据库系统,从空间利用效率和并发性能两个方面着手,搞高了程序性能。
在空间利用率上,做了两点改进,一是在删除记录的时候,如果记录的存放位置在数据库的末端,就自动修改数据库末端指针,而不是将此记录的空间作为一个闲置块存入闲置块记录中,这样就可以继续在数据文件尾分配任意长度的空间而不是像以前那样将这个删除记录的空间整块分配给下一次请求空间的新记录;第二点改进是在从闲置块中请求空间的时候,不仅继续采用原来的最佳适应法,而且在请求到合适的闲置块的时候并不是将整个块而是将其部分划分给新记录,这样又进一部提高了空间的利用率。
在并发性能上,增加了数据库的打开模式这个选项。为了保持文本数据库操作的原子性,以前都以写模式打开数据库,用户无论是从数据库中读数据或写数据,系统都会锁定数据库而禁止其它用户并发的读写操作,而大多情况下,用户都是浏览网页而不是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 $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=$res[len]-$len;
$find=$i;
}
}
}
if ($find)
{
$res=$this->getLeft($find);
if ($res[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 ($lenminLen) $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.'
';
$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$this->maxID or $this->rcdCnt $left=1;
$right=$this->rcdCnt;
while($left {
$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 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] {
$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.'
'.$content.'
'.$len;
}
function selectByRid($rid)
{
$res=array('id'=>0,'content'=>'');
if ($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

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

“你的组织要求你更改PIN消息”将显示在登录屏幕上。当在使用基于组织的帐户设置的电脑上达到PIN过期限制时,就会发生这种情况,在该电脑上,他们可以控制个人设备。但是,如果您使用个人帐户设置了Windows,则理想情况下不应显示错误消息。虽然情况并非总是如此。大多数遇到错误的用户使用个人帐户报告。为什么我的组织要求我在Windows11上更改我的PIN?可能是您的帐户与组织相关联,您的主要方法应该是验证这一点。联系域管理员会有所帮助!此外,配置错误的本地策略设置或不正确的注册表项也可能导致错误。即

Windows11将清新优雅的设计带到了最前沿;现代界面允许您个性化和更改最精细的细节,例如窗口边框。在本指南中,我们将讨论分步说明,以帮助您在Windows操作系统中创建反映您的风格的环境。如何更改窗口边框设置?按+打开“设置”应用。WindowsI转到个性化,然后单击颜色设置。颜色更改窗口边框设置窗口11“宽度=”643“高度=”500“>找到在标题栏和窗口边框上显示强调色选项,然后切换它旁边的开关。若要在“开始”菜单和任务栏上显示主题色,请打开“在开始”菜单和任务栏上显示主题

默认情况下,Windows11上的标题栏颜色取决于您选择的深色/浅色主题。但是,您可以将其更改为所需的任何颜色。在本指南中,我们将讨论三种方法的分步说明,以更改它并个性化您的桌面体验,使其具有视觉吸引力。是否可以更改活动和非活动窗口的标题栏颜色?是的,您可以使用“设置”应用更改活动窗口的标题栏颜色,也可以使用注册表编辑器更改非活动窗口的标题栏颜色。若要了解这些步骤,请转到下一部分。如何在Windows11中更改标题栏的颜色?1.使用“设置”应用按+打开设置窗口。WindowsI前往“个性化”,然

您是否在Windows安装程序页面上看到“出现问题”以及“OOBELANGUAGE”语句?Windows的安装有时会因此类错误而停止。OOBE表示开箱即用的体验。正如错误提示所表示的那样,这是与OOBE语言选择相关的问题。没有什么可担心的,你可以通过OOBE屏幕本身的漂亮注册表编辑来解决这个问题。快速修复–1.单击OOBE应用底部的“重试”按钮。这将继续进行该过程,而不会再打嗝。2.使用电源按钮强制关闭系统。系统重新启动后,OOBE应继续。3.断开系统与互联网的连接。在脱机模式下完成OOBE的所

任务栏缩略图可能很有趣,但它们也可能分散注意力或烦人。考虑到您将鼠标悬停在该区域的频率,您可能无意中关闭了重要窗口几次。另一个缺点是它使用更多的系统资源,因此,如果您一直在寻找一种提高资源效率的方法,我们将向您展示如何禁用它。不过,如果您的硬件规格可以处理它并且您喜欢预览版,则可以启用它。如何在Windows11中启用任务栏缩略图预览?1.使用“设置”应用点击键并单击设置。Windows单击系统,然后选择关于。点击高级系统设置。导航到“高级”选项卡,然后选择“性能”下的“设置”。在“视觉效果”选

许多用户在选择智能手表的时候都会选择的华为的品牌,其中华为GT3pro和GT4都是非常热门的选择,不少用户都很好奇华为GT3pro和GT4有什么区别,下面就就给大家介绍一下二者。华为GT3pro和GT4有什么区别一、外观GT4:46mm和41mm,材质是玻璃表镜+不锈钢机身+高分纤维后壳。GT3pro:46.6mm和42.9mm,材质是蓝宝石玻璃表镜+钛金属机身/陶瓷机身+陶瓷后壳二、健康GT4:采用最新的华为Truseen5.5+算法,结果会更加的精准。GT3pro:多了ECG心电图和血管及安

在Windows11上的显示缩放方面,我们都有不同的偏好。有些人喜欢大图标,有些人喜欢小图标。但是,我们都同意拥有正确的缩放比例很重要。字体缩放不良或图像过度缩放可能是工作时真正的生产力杀手,因此您需要知道如何对其进行自定义以充分利用系统功能。自定义缩放的优点:对于难以阅读屏幕上的文本的人来说,这是一个有用的功能。它可以帮助您一次在屏幕上查看更多内容。您可以创建仅适用于某些监视器和应用程序的自定义扩展配置文件。可以帮助提高低端硬件的性能。它使您可以更好地控制屏幕上的内容。如何在Windows11

屏幕亮度是使用现代计算设备不可或缺的一部分,尤其是当您长时间注视屏幕时。它可以帮助您减轻眼睛疲劳,提高易读性,并轻松有效地查看内容。但是,根据您的设置,有时很难管理亮度,尤其是在具有新UI更改的Windows11上。如果您在调整亮度时遇到问题,以下是在Windows11上管理亮度的所有方法。如何在Windows11上更改亮度[10种方式解释]单显示器用户可以使用以下方法在Windows11上调整亮度。这包括使用单个显示器的台式机系统以及笔记本电脑。让我们开始吧。方法1:使用操作中心操作中心是访问
