php實現簡單key value hash資料庫

2021-06-20 18:33:50 字數 3645 閱讀 4783

類庫檔案:

<?php

//insert 函式插入標記

define('db_insert',1);

define('db_stroe',2); //不管有沒有key 直接儲存

//hash 索引大小 262144

define('db_buckets_size',262144);

//key 的長度

define('db_key_size',128);

//一條索引記錄的長度

define('db_index_size',db_key_size+12);

define('db_key_exists','key is exists!');

define('db_key_size_is_big','key size is too big');

define('db_failure',-1);

define('db_success', 0);

/*** 自己寫的key-value資料庫類,可以儲存資料,提取資料,刪除資料

* * 不足之處:

* key 的大小不能超過128

* 刪除對應key值時資料時庫並沒有真正的從檔案刪除對應的資料

* 隨著時間增長檔案會越來越大

* @author prg

* @date 2014-2-25

*/class mydb

else

$this->idx_fp = fopen($idx_path,$mode);

if(!$this->idx_fp)

if($init)

}$this->dat_fp = fopen($dat_path, $mode);

if(!$this->dat_fp)

$this->pathname=$pathname;

$this->closed = false;

return db_success;

}/**

* time33 hash 函式

* @param string $string

* @return boolean

*/private function _hash($string));}

return $hash & 0x7fffffff;

}/**

* 這裡使用的是尾插法

* @param string $key

* @param string $data

* @return string

*/public function insert($key,$data,$mode=db_insert)

//開始構造一條索引記錄()鍊錶指標4個位元組,鍵128個位元組,資料偏移量4個位元組,資料長度4個位元組

$block = pack('l',0x00000000); //將鍊錶指標,指向嚇一跳索引記錄的指標,為空

$block.= $key;

$space = db_key_size-$keylen; //這裡key長度不夠用0填充

for($i=0;$i<$space;$i++)

$block.=pack('l',$datoff);

$block.=pack('l',strlen($data));

fseek($this->idx_fp, $offset,seek_set); //定位到對應的hash上 

$pos = unpack('l', fread($this->idx_fp, 4));

$pos = $pos[1];

if($pos==0) //如果該hash鏈上還沒有元素

$found = false;

while($pos)

//這裡開始遍歷鍊錶

$prev = $pos;

$pos  = unpack('l', substr($tmp_block, 0,4));

$pos  = $pos[1];

}if($found&&$mode==db_insert)

else if($found&&$mode!=db_insert)

fseek($this->idx_fp,$prev,seek_set);//定位到鍊錶最後一條索引記錄,此時這條索引記錄裡的鍊錶指標必定是空

fwrite($this->idx_fp,pack('l',$idxoff),4); //修改這個指標使它指向下一條索引記錄

//記錄索引塊

fseek($this->idx_fp,0,seek_end);

fwrite($this->idx_fp, $block,db_index_size);

//記錄資料

fseek($this->dat_fp, 0,seek_end);

fwrite($this->dat_fp, $data,strlen($data));

return db_success;

}public function fetch($key)

$pos = unpack('l',substr($block, 0,4));

$pos = $pos[1];

}if(!$found) //這裡沒有找到對應的資料

fseek($this->dat_fp, $dataoff,seek_set);

$data = fread($this->dat_fp, $datalen);

return $data;

} /**

* 這裡只是改變了鍊錶的結構,並沒有真正的吧資料重檔案中刪除,如果要刪除的話開銷回非常大,要改變整個hash結構

* @param string $key

* @return string

*/function delete($key)

$prev = $curr;

$curr = $next;

}if(!$found)

if($prev==0)

else

return db_success;

}/**

* 關閉資料庫

*/public function close()

}/**

* 清空所有資料

* @return string

*/public function flush_all()

$mode  = 'w+b';

$this->idx_fp = fopen($this->pathname.'.idx', $mode);

$this->dat_fp = fopen($this->pathname.'.dat', $mode);

if(!$this->idx_fp)

$elem = pack('l',0x00000000); //無符號二進位制進行打包

for($i=0;$i

return db_success;}}

呼叫檔案:

<?php

header("content-type:text/html;charset=utf-8");

require_once 'mydb.class.php';

$mydb = new mydb();

$mydb->open('test');

$mydb->insert('key', 'value2',db_stroe);

$mydb->fetch('key');

?>

以上大部分**來自:php核心技術與最佳實踐

PHP 多維陣列 Key Value的使用

user 60 array id 60 num 56 count 31 user 61 array id 61 num 22 count 34 user 59 array id 59 num 32 count 43 user 56 array id 56 num 41 count 38 user 5...

如何實現key, value有序的HashMap

想要寫個key,value有序的hashmap,出現效能問題,大家幫忙給個主意吧。b 先說下我的設計思路 b linkedhashmap裡面有乙個模擬的 雙向迴圈鍊錶 用來儲存entry的插入順序,我也可以採用這種方法來在插入的時候儲存key和value的有序。這裡暫定名為orderedhashma...

php 實現 簡單 登入

1.先在mysql 中建表 userinfo 加欄位 userid username password 2.登入頁面login.htm 3.跳轉頁面 success.htm 4.後台login.php error reporting 0 mysql servername localhost 主機位址...