Ma poczatku chcialem sie przedstawic, jestem mlodym programista php, moje dosiadczenie nie jest dluzsze niz 1 rok.
Napisalem biblioteke do obslugi plikowej bazy danych,
sam stosuje jej zamiast sqla

ale moze ona tez posluzyc 'normalnym' jaki jej substytut.
Np. kiedy musimy magazynowac duzo malych obrazkow.
oto przyklad uzycia bazy:
<?php require_once('multifile.php'); $db->debug = false; $db = new multifile; $db->dbpath = './example/'; # Dokumentacja szalenie prosta, bo i prosta jest obsluga tego lib'a # Mamy do dyspozycji 5 metod: //foreach ( glob('ZDJECIA_Z_REJSU/*') as $file) { // $data = array($file, file_get_contents($file)); // $db->put($data); //} //exit; # put() $db->put($data); # get() $data = $db->get('klucz'); # delete() $wynik = $db->delete('klucz'); # exists() if ( true === $db->exists('klucz')); # search() $wynik = $db->search("^klucz$"); // wyrazena regularne # get_list() $pelna_lista_recordow = $db->get_list(); # count $ilosc_recordow = $db->count(); exit; ?>
A to właściwa biblioteka:
[/php]
<?php
/*
CHANGELOG
# v.1.0
*/
/*
ToDo:
# Co szybsze, array_key_exists czy issset

Functionlist:
get()
delete()
exists() // exists powinno uzywac searcha

search()
count()
get_list()
update()
*/
class multifile {
/* # TODO
dzielenie duuuzych plikow na porcje
szukanie od najstarszego, od najmlodszego, losowo
*/
var $dbpath = '';
var $debug = false;
var $_update = '';
function update($key, $data) {
$this->_update = $data;
if ( true === $this->_seek($key, 'update'))
return true;
return false;
}
function put($enter) {
$time_start = $this->gmt();
$key = @current($enter) or die('Zapisywac mozna tylko tablice');
$enter = array_slice($enter, 1);
if ( true !== $this->exists($key))
return false;
$path = $this->_get_writable_file();
$data = @file_get_contents($path) or die('Can\'t read from file file.');
$data = unserialize($data);
$data[$key] = $enter;
file_put_contents($path, serialize($data));
if ( $this->debug !== false ) echo 'Czas wstawiania: '.($this->gmt() - $time_start).'<br />';
return true;
}
function _is_dir($path='') {
$path = empty($path) ? $this->dbpath : $path;
if ( ! is_dir($path))
@mkdir($path, '0777') or die('Blad odczytu/zapisu bazy danych.<br />');
return true;
}
function get($key, $how='') {
/* Zrobic random
$how =
'random'
'back'
*/
$time_start = $this->gmt();
$this->_is_dir();
$key = is_array($key) ? current($key) : $key;
if ( false !== $data = $this->_seek($key, 'get')) {
//$data = unserialize($data); ###
if ( $this->debug !== false ) echo 'Czas wyciagania: '.($this->gmt() - $time_start).'<br />';
return ( count($data) < 2 ) ? $data[0] : $data; // jesli tablica ma 1 element, to zwraca tylko string

}
if ( $this->debug !== false ) echo 'Czas wyciagania - nieudany-: '.($this->gmt() - $time_start).'<br />';
return false;
}
function exists($key) {
$time_start = $this->gmt();
$this->_is_dir();
if ( true === $this->_seek($key, 'exists'))
return false;
return true;
}
function delete($key='') {
$time_start = $this->gmt();
$this->_is_dir();
if ( true === $this->_seek($key, 'delete'))
return true;
return false;
}
function count() {
$time_start = $this->gmt();
$this->_is_dir();
return; $this->_seek('', 'count');
}
function get_list($pattent='') {
$time_start = $this->gmt();
$this->_is_dir();
$list = $this->_seek('', 'list');
if ( $this->debug !== false ) echo 'Czas generowania listy: '.($this->gmt() - $time_start).'<br />';
return $list;
}
function search($pat='') {
$time_start = $this->gmt();
$finded = array();
foreach ( $this->_seek('', 'list') as $key ) {
if ( preg_match("#$pat#", $key))
array_push($finded, $key);
}
if ( $this->debug !== false ) echo 'Czas szukania: '.($this->gmt() - $time_start).'<br />';
return $finded;
}
function _seek($key='', $do) {
switch($do) {
case('count'):
$count = 0;
break;
case('list'):
$list = array();
break;
default:
if ( strlen($key) < 1)
return false;
}
foreach ( $this->_get_files_list() as $file ) {
$db_data = @file_get_contents($file) or die('Cam\'t read db file.');
$db_data = unserialize($db_data);
if ( $do == 'count') {
$coun += count($db_data);
continue;
}
if ( $do == 'list' ) {
$list = array_merge( $list, array_keys($db_data) );
continue;
}
if ( ! array_key_exists($key, $db_data))
continue;
switch($do) {
case('get'):
return $db_data[$key];
case('update'):
$db_data[$key] = $this->_update;
file_put_contents($file, serialize($db_data));
return true;
case('delete'):
unset($db_data[$key]);
file_put_contents($file, serialize($db_data));
return true;
case('exists'):
return true;
default:
die('Blad uzycia _seek()');
}
}
switch($do) {
case('count'):
return $count;
case('list'):
return $list;
default:
return false;
}
}
function _get_files_list() {
$time_start = $this->gmt();
$this->_is_dir();
return glob($this->dbpath.'*');
}
function _get_writable_file() {
$time_start = $this->gmt();
$this->_is_dir();
$filelist = glob($this->dbpath . '*');
foreach ( $filelist as $file ) {
if ( filesize($file) < $this->_get_memory_limit())
return $file;
}
sort($filelist);
$new_file_path = is_file(end($filelist)) ? preg_replace_callback( '#^(.*?/)([0-9]+)$#', create_function('$vars', 'return $vars[1].($vars[2] + 1);'), end($filelist)) : $this->dbpath.'0';
file_put_contents($new_file_path, serialize(array())) or die('Nie moge utworzyc pliku bazy danych');
//file_put_contents(is_file(end($filelist)) ? preg_replace_callback( '#^(.*?/)([0-9]+)$#', create_function('$v', 'return $v[1].($v[2] + 1);'), end($filelist)) : $this->dbpath.'0', serialize(array())) or die('Nie moge utworzyc pliku bazy danych');
return $new_file_path;
}
function _get_memory_limit() {
$time_start = $this->gmt();
return abs(intval(ini_get('memory_limit'))) * 1024 * 1024 * 0.3;
}
function gmt(){list($usec,$sec)=explode(' ',microtime());return ((float)$usec + (float)$sec);}
}?>
[/php]
Serdecznie prosze o gro uwag.
Co Wy na to, czy z takim stylem nadaje sie na etat?
