Avancement + tests de performances
This commit is contained in:
parent
8eaec05488
commit
e661bf99dd
29
automate.php
29
automate.php
|
@ -99,4 +99,31 @@
|
|||
|
||||
// RUN database
|
||||
$db = new lightdb('testdb1');
|
||||
var_dump($db->index());
|
||||
function microtime_float()
|
||||
{
|
||||
list($usec, $sec) = explode(" ", microtime());
|
||||
return ((float)$usec + (float)$sec);
|
||||
}
|
||||
|
||||
$a = microtime_float();
|
||||
/* [1] INSERTION DE 1000*1000 éléments
|
||||
=========================================================*/
|
||||
$data = array();
|
||||
for( $i = 0 ; $i < 1000 ; $i++ )
|
||||
$data['key'.$i] = 'valeur'.$i;
|
||||
|
||||
$average = 0;
|
||||
for( $i = 0 ; $i < 100 ; $i++ ){
|
||||
$a = microtime_float();
|
||||
$db->fetch('data'.$i);
|
||||
$average += microtime_float()-$a;
|
||||
}
|
||||
|
||||
var_dump('1000 * '.strlen(json_encode($data)) );
|
||||
var_dump('INSERT avg. : '.($average/100) );
|
||||
|
||||
/* [2] RECUPERATION DE 1000*1000 ELEMENTS
|
||||
=========================================================*/
|
||||
|
||||
|
||||
$db->close();
|
||||
|
|
|
@ -58,13 +58,11 @@
|
|||
$this->driver = new \SplFileObject($this->dir.'data', 'r+');
|
||||
|
||||
/* (3) On récupère le nombre de lignes */
|
||||
$this->line = 0;
|
||||
$this->line = -1;
|
||||
while( !$this->driver->eof() ){
|
||||
$this->line++;
|
||||
$this->driver->fgetcsv();
|
||||
}
|
||||
|
||||
var_dump($this->line);
|
||||
}
|
||||
|
||||
|
||||
|
@ -98,11 +96,27 @@
|
|||
return false;
|
||||
|
||||
/* (2) On ajoute les données aux fichier */
|
||||
$json_data = json_encode($data);
|
||||
$this->driver->seek($this->line);
|
||||
$this->line++;
|
||||
$written = $this->driver->fwrite( $json_data.PHP_EOL );
|
||||
|
||||
// Si erreur d'écriture, on retourne FALSE
|
||||
if( is_null($written) )
|
||||
return false;
|
||||
|
||||
/* (3) On enregistre l'index */
|
||||
$this->index[$key] = array(
|
||||
'line' => $this->line - 1,
|
||||
'hash' => sha1($json_data)
|
||||
);
|
||||
|
||||
$this->index[$key] = $data;
|
||||
/* (4) On enregistre le fichier index */
|
||||
$fIndex = new \SplFileObject($this->dir.'index', 'w');
|
||||
$fIndex->fwrite( json_encode($this->index) );
|
||||
$fIndex = null;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
@ -124,7 +138,24 @@
|
|||
* @return data<mixed*> Renvoie la valeur associée à la clé, FALSE si erreur
|
||||
*
|
||||
*/
|
||||
public function fetch($key){}
|
||||
public function fetch($key){
|
||||
/* (1) On vérifie que la clé existe bien */
|
||||
if( !array_key_exists($key, $this->index) )
|
||||
return false;
|
||||
|
||||
/* (2) On récupère la ligne */
|
||||
$line = $this->index[$key]['line'];
|
||||
|
||||
/* (3) On récupère le contenu */
|
||||
$this->driver->seek($line);
|
||||
$json = json_decode( $this->driver->current(), true );
|
||||
|
||||
// Si erreur de parsage
|
||||
if( is_null($json) )
|
||||
return false;
|
||||
|
||||
return $json;
|
||||
}
|
||||
|
||||
|
||||
/* RENVOIE LES DONNEES ASSOCIEES AUX CLES DONNEES
|
||||
|
@ -134,7 +165,21 @@
|
|||
* @return data<Array> Renvoie un tableau des valeurs valeur associées aux clés (clé1->valeur1), FALSE si erreur
|
||||
*
|
||||
*/
|
||||
public function fetchAll($keys){}
|
||||
public function fetchAll($keys){
|
||||
/* (1) On récupère fetch() pour toutes les clés */
|
||||
$wrapper = array();
|
||||
|
||||
foreach($keys as $key){
|
||||
$data = $this->fetch($key);
|
||||
|
||||
// Si aucune erreur, on ajoute l'entree au 'wrapper'
|
||||
if( $data !== false )
|
||||
$wrapper[$key] = $data;
|
||||
}
|
||||
|
||||
/* (2) On retourne le résultat */
|
||||
return $wrapper;
|
||||
}
|
||||
|
||||
|
||||
/* RENVOIE LES DONNEES ASSOCIEES A UN CHAMP DE RECHERCHE
|
||||
|
|
101
tmp/testdb1/data
101
tmp/testdb1/data
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue