改进的PHP文本数据库类

小静小哥_7745

小静小哥_7745

2016-06-21

1083人浏览

原创

数据|数据库

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

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

在并发性能上,增加了数据库的打开模式这个选项。为了保持文本数据库操作的原子性,以前都以写模式打开数据库,用户无论是从数据库中读数据或写数据,系统都会锁定数据库而禁止其它用户并发的读写操作,而大多情况下,用户都是浏览网页而不是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;
    }

DeepAI
DeepAI

一款提供多种生成式AI创作能力的在线工具平台,覆盖图像生成等功能,适合个人用户进行创意内容制作和AI实验。

下载

    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

 


 



php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!

相关文章

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
Go-Zero API开发使用教程
Go-Zero API开发使用教程

本专题聚焦Go-Zero的API开发,拆解.api描述文件的语法结构、路由与分组定义、参数绑定与校验规则,说明统一响应体、JWT鉴权、跨域与接口文档生成的落地方式,并给出网关聚合与接口测试的实践建议。

2026.11.12

140

15

Go-Zero安装与goctl环境配置教程
Go-Zero安装与goctl环境配置教程

本专题围绕Go-Zero的安装与工具链配置展开,讲清Go版本要求、goctl安装方式、代理镜像、protoc依赖与环境变量设置,说明多版本共存、升级回退与CI流水线安装的做法,并整理安装阶段的常见报错与验证步骤。

2026.11.12

100

15

Go-Zero零基础入门教程
Go-Zero零基础入门教程

本专题整理Go-Zero的入门知识,从框架定位、环境准备到跑通第一个API服务,讲清目录分层、配置加载、日志与中间件的基本用法,并对比Gin、Kratos的差异,帮助Go开发者建立整体认知,判断它是否适合当前项目。

2026.11.12

80

15

Postman身份认证怎么配置
Postman身份认证怎么配置

本专题从实际API接口调试场景出发,讲解Postman身份认证的完整配置流程,包括常见认证协议介绍、授权参数设置、访问令牌填写、OAuth 2.0授权及认证失败排查,帮助用户正确完成接口授权与访问验证。

2026.11.06

160

12

Postman请求头Headers怎么设置
Postman请求头Headers怎么设置

围绕Postman HTTP请求头配置展开,讲解Headers选项的具体使用方式,包括Key和Value参数填写、请求头信息添加、常见Header字段设置及请求头修改方法,帮助用户解决接口调试过程中遇到的请求头配置问题。

2026.11.06

140

13

Postman接口请求发送教程
Postman接口请求发送教程

详细介绍Postman接口请求的创建与发送方法,涵盖GET和POST请求、URL配置、请求参数、Headers请求头、Body请求体设置以及响应结果查看,帮助用户掌握Postman接口请求的完整操作流程。

2026.11.06

120

10

DBeaver怎么导入数据
DBeaver怎么导入数据

DBeaver数据导入专题,整理Excel、CSV、SQL、TXT等常见文件的数据导入方法,涵盖目标数据库选择、数据表创建、字段映射、数据类型设置及导入参数配置等操作,帮助用户将本地文件中的数据写入数据库,并了解批量导入、重复数据处理和导入失败等常见问题的解决方法。

2026.11.05

120

12

DBeaver安全防护教程大全
DBeaver安全防护教程大全

DBeaver安全防护专题,围绕数据库连接安全、账号密码保护和访问权限管理展开,整理SSL/TLS加密连接、SSH隧道、数据库凭据保存及证书验证等配置方法,同时介绍敏感信息保护、远程连接风险和高权限账号使用等注意事项,帮助用户提高数据库日常管理的安全性。

2026.11.05

100

12

DBeaver连接创建与配置教程
DBeaver连接创建与配置教程

本专题聚焦DBeaver数据库连接配置与管理,覆盖从新建连接、选择数据库类型到填写连接参数和验证连接的完整流程。针对MySQL、PostgreSQL、SQL Server等常见数据库,整理相应的连接设置方法,并提供驱动缺失、认证失败和连接异常等问题的排查方向。

2026.11.04

120

16

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
JavaScript高级框架设计视频教程
JavaScript高级框架设计视频教程

共22课时 | 4.4万人学习

AngularJS教程
AngularJS教程

共24课时 | 7.9万人学习

CSS3实现按钮特效视频教程
CSS3实现按钮特效视频教程

共15课时 | 3.8万人学习