0

0

某文本数据库blog作者写的文本数据库操作类,很不错

php中文网

php中文网

发布时间:2016-06-21 09:05:31

|

1176人浏览过

|

来源于php中文网

原创

数据|数据库

作者的1.0版:


/*
本代码开源,您可以对其进行修改.
下面文字请不要修改.
*********************************************
php文本数据库类1.0版
powerd by bpns
mysite:http://space.tju.cn/site/redboy/
2006-4-20
*********************************************

为了您的数据库安全,请在此程序中更改您的数据默认目录

将function TxtDB($name,$mod='w',$path='my_dbm')
中$path='my_dbm'修改成你自己设定的目录,如$path='abc123_dbm',
并将根目录下的my_dbm文件夹对行对应的更名.
*/

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]);
    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');
  }
}
?>

 

LANUX蓝脑商务网站系统
LANUX蓝脑商务网站系统

LANUX V1.0 蓝脑商务网站系统 适用于网店、公司宣传自己的品牌和产品。 系统在代码、页面方面设计简约,浏览和后台管理操作效率高。 此版本带可见即可得的html编辑器, 方便直观添加和编辑要发布的内容。 安装: 1.解压后,更换logo、分类名称、幻灯片的图片及名称和链接、联系我们等等页面。 2.将dbconfig.php里面的数据库配置更改为你的mysql数据库配置 3.将整个文件夹上传至

下载

作者的2.0版,生成dbm:


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');
  }
}

?>


 



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

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
pixiv网页版官网登录与阅读指南_pixiv官网直达入口与在线访问方法
pixiv网页版官网登录与阅读指南_pixiv官网直达入口与在线访问方法

本专题系统整理pixiv网页版官网入口及登录访问方式,涵盖官网登录页面直达路径、在线阅读入口及快速进入方法说明,帮助用户高效找到pixiv官方网站,实现便捷、安全的网页端浏览与账号登录体验。

463

2026.02.13

微博网页版主页入口与登录指南_官方网页端快速访问方法
微博网页版主页入口与登录指南_官方网页端快速访问方法

本专题系统整理微博网页版官方入口及网页端登录方式,涵盖首页直达地址、账号登录流程与常见访问问题说明,帮助用户快速找到微博官网主页,实现便捷、安全的网页端登录与内容浏览体验。

135

2026.02.13

Flutter跨平台开发与状态管理实战
Flutter跨平台开发与状态管理实战

本专题围绕Flutter框架展开,系统讲解跨平台UI构建原理与状态管理方案。内容涵盖Widget生命周期、路由管理、Provider与Bloc状态管理模式、网络请求封装及性能优化技巧。通过实战项目演示,帮助开发者构建流畅、可维护的跨平台移动应用。

64

2026.02.13

TypeScript工程化开发与Vite构建优化实践
TypeScript工程化开发与Vite构建优化实践

本专题面向前端开发者,深入讲解 TypeScript 类型系统与大型项目结构设计方法,并结合 Vite 构建工具优化前端工程化流程。内容包括模块化设计、类型声明管理、代码分割、热更新原理以及构建性能调优。通过完整项目示例,帮助开发者提升代码可维护性与开发效率。

20

2026.02.13

Redis高可用架构与分布式缓存实战
Redis高可用架构与分布式缓存实战

本专题围绕 Redis 在高并发系统中的应用展开,系统讲解主从复制、哨兵机制、Cluster 集群模式及数据分片原理。内容涵盖缓存穿透与雪崩解决方案、分布式锁实现、热点数据优化及持久化策略。通过真实业务场景演示,帮助开发者构建高可用、可扩展的分布式缓存系统。

26

2026.02.13

c语言 数据类型
c语言 数据类型

本专题整合了c语言数据类型相关内容,阅读专题下面的文章了解更多详细内容。

29

2026.02.12

雨课堂网页版登录入口与使用指南_官方在线教学平台访问方法
雨课堂网页版登录入口与使用指南_官方在线教学平台访问方法

本专题系统整理雨课堂网页版官方入口及在线登录方式,涵盖账号登录流程、官方直连入口及平台访问方法说明,帮助师生用户快速进入雨课堂在线教学平台,实现便捷、高效的课程学习与教学管理体验。

14

2026.02.12

豆包AI网页版入口与智能创作指南_官方在线写作与图片生成使用方法
豆包AI网页版入口与智能创作指南_官方在线写作与图片生成使用方法

本专题汇总豆包AI官方网页版入口及在线使用方式,涵盖智能写作工具、图片生成体验入口和官网登录方法,帮助用户快速直达豆包AI平台,高效完成文本创作与AI生图任务,实现便捷智能创作体验。

524

2026.02.12

PostgreSQL性能优化与索引调优实战
PostgreSQL性能优化与索引调优实战

本专题面向后端开发与数据库工程师,深入讲解 PostgreSQL 查询优化原理与索引机制。内容包括执行计划分析、常见索引类型对比、慢查询优化策略、事务隔离级别以及高并发场景下的性能调优技巧。通过实战案例解析,帮助开发者提升数据库响应速度与系统稳定性。

53

2026.02.12

热门下载

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

精品课程

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

共22课时 | 3.6万人学习

AngularJS教程
AngularJS教程

共24课时 | 3.7万人学习

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

共15课时 | 3.3万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号