Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]Multifile, biblioteka do obslugi plikowej bazy danych
-młody skryptadept-
post
Post #1





Goście







Witajcie.
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 (IMG:http://forum.php.pl/style_emoticons/default/tongue.gif)
ale moze ona tez posluzyc 'normalnym' jaki jej substytut.
Np. kiedy musimy magazynowac duzo malych obrazkow.


oto przyklad uzycia bazy:
  1. <?php
  2. require_once('multifile.php');
  3. $db->debug = false;
  4. $db = new multifile;
  5. $db->dbpath = './example/';
  6. # Dokumentacja szalenie prosta, bo i prosta jest obsluga tego lib'a
  7. # Mamy do dyspozycji 5 metod:
  8.  
  9. //foreach ( glob('ZDJECIA_Z_REJSU/*') as $file) {
  10. //    $data = array($file, file_get_contents($file));
  11. //    $db->put($data);
  12. //}
  13. //exit;
  14.  
  15. # put()
  16.    $data = array('klucz', 'string'); # stringi
  17.    $data = array('klucz', array('tablica')); # tablice
  18.    $data = array('klucz', file_get_contents('zdjecie.jpg')); # dane binarne
  19.    $db->put($data);
  20.  
  21. # get()
  22.    $data = $db->get('klucz');
  23.  
  24. # delete()
  25.    $wynik = $db->delete('klucz');
  26.  
  27. # exists()
  28.    if ( true === $db->exists('klucz'));
  29.        echo 'Klucz istnieje';
  30.  
  31. # search()
  32.    $wynik = $db->search("^klucz$"); // wyrazena regularne
  33.    
  34. # get_list()
  35.    $pelna_lista_recordow = $db->get_list();
  36.  
  37. # count
  38.    $ilosc_recordow = $db->count();
  39.  
  40. ?>


A to właściwa biblioteka:

[/php]
<?php
/*
CHANGELOG
# v.1.0



*/
/*

ToDo:
# Co szybsze, array_key_exists czy issset (IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif) ?

Functionlist:
get()
delete()
exists() // exists powinno uzywac searcha (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)
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 (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)
}
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? (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)
Go to the top of the page
+Quote Post

Posty w temacie
- młody skryptadept   [PHP]Multifile   6.11.2008, 08:02:32
- - Gość   Zle wkleila sie biboteka, oto ona jeszcze raz, tym...   6.11.2008, 08:06:12
- - Pilsener   Operujesz na tablicach - moim zdaniem powinieneś i...   6.11.2008, 08:29:27
- - -gox-   Taki kod jest znacznie wydajniejszy niz fgets.... ...   6.11.2008, 11:01:34


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 23.12.2025 - 20:18