Implémentation de 'lightdb.delete()'
This commit is contained in:
parent
207eb09583
commit
7d91473262
23
automate.php
23
automate.php
|
@ -7,6 +7,7 @@
|
|||
use \manager\ManagerError;
|
||||
use \manager\Repo;
|
||||
use \manager\Database;
|
||||
use \manager\lightdb;
|
||||
|
||||
use \api\client;
|
||||
|
||||
|
@ -86,4 +87,26 @@
|
|||
// }
|
||||
|
||||
|
||||
$db = new lightdb('phone_storage/db');
|
||||
|
||||
for( $i = 0 ; $i < 10 ; $i ++)
|
||||
$db->insert("i$i", 'test'.$i);
|
||||
|
||||
var_dump( $db->fetch('i0') );
|
||||
var_dump( $db->fetch('i1') );
|
||||
var_dump( $db->fetch('i2') );
|
||||
var_dump( $db->fetch('i3') );
|
||||
|
||||
$last = $db->index();
|
||||
$db->delete('i2');
|
||||
$db->delete('i3');
|
||||
$db->delete('i6');
|
||||
|
||||
foreach($last as $i=>$d)
|
||||
echo $i.' -> '.$d['line'].' -> '.( isset($db->index()[$i]) ? $db->index()[$i]['line'] : '.').'<br>';
|
||||
|
||||
|
||||
|
||||
$db->close();
|
||||
|
||||
?>
|
||||
|
|
|
@ -101,12 +101,16 @@
|
|||
if( array_key_exists($key, $this->index) )
|
||||
return true;
|
||||
|
||||
$key = (string) $key;
|
||||
|
||||
/* (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 );
|
||||
|
||||
var_dump($written);
|
||||
|
||||
// Si erreur d'écriture, on retourne FALSE
|
||||
if( is_null($written) )
|
||||
return false;
|
||||
|
@ -143,7 +147,7 @@
|
|||
|
||||
/* (3) On récupère le contenu */
|
||||
$this->driver->seek($line);
|
||||
$json = json_decode( $this->driver->fgets(), true );
|
||||
$json = json_decode( $this->driver->current(), true );
|
||||
|
||||
// Si erreur de parsage
|
||||
if( is_null($json) )
|
||||
|
@ -165,9 +169,47 @@
|
|||
if( !array_key_exists($key, $this->index) )
|
||||
return true; // On considère que l'action souhaitée est effectuée
|
||||
|
||||
/* (2) On supprime la ligne de l'index */
|
||||
$line = $this->index[$key]['line'];
|
||||
|
||||
/* (2) On réarrange la bd pour supprimer la ligne */
|
||||
$tmpfilename = __ROOT__.'/tmp/'.uniqid().'.dat';
|
||||
$tmpfile = new \SplFileObject($tmpfilename, 'w');
|
||||
$this->driver->seek(0);
|
||||
|
||||
// On recopie toutes les lignes sauf celle à supprimer dans un fichier temporaire
|
||||
while( $this->driver->key() < $this->line ){
|
||||
|
||||
if( $this->driver->key() != $line )
|
||||
$tmpfile->fwrite( $this->driver->current() );
|
||||
|
||||
$this->driver->next();
|
||||
}
|
||||
|
||||
// On décrémente le nb de lignes
|
||||
$this->line--;
|
||||
|
||||
$tmpfile = null;
|
||||
|
||||
/* (3) On remplace le fichier original par le fichier temporaire */
|
||||
$this->driver = null;
|
||||
rename($tmpfilename, $this->dir.'data');
|
||||
$this->driver = new \SplFileObject($this->dir.'data', 'r+');
|
||||
|
||||
/* (3) On supprime la ligne de l'index */
|
||||
unset( $this->index[$key] );
|
||||
|
||||
/* (4) On met à jour les index des lignes déplacées */
|
||||
foreach($this->index as $i=>$indexData)
|
||||
if( $indexData['line'] > $line )
|
||||
$this->index[$i]['line']--; // on décrémente les lignes au dessus de la ligne supprimée
|
||||
|
||||
|
||||
/* (5) On enregistre le fichier index */
|
||||
$fIndex = new \SplFileObject($this->dir.'index', 'w');
|
||||
$fIndex->fwrite( json_encode($this->index) );
|
||||
$fIndex = null;
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -74,7 +74,7 @@
|
|||
$uniqid = 0;
|
||||
|
||||
// Décalage à appliquer à tous les ids
|
||||
$offset = $uniqid + 2;
|
||||
$offset = intval($uniqid) + 2;
|
||||
|
||||
// Contiendra la valeur de l'id maximum
|
||||
$maxId = $offset;
|
||||
|
@ -89,12 +89,13 @@
|
|||
// Contiendra le contenu du fichier
|
||||
$file = array( 'logs' => array() );
|
||||
|
||||
/* (1) Si on a déja crée le fichier avec le journal d'appel dedans, on le récupère */
|
||||
$storage_already = is_string($subject['tmp_id']) && strlen($subject['tmp_id']) == 40;
|
||||
|
||||
/* (2) On définit les 2 fichiers utiles */
|
||||
/* (1) On définit les 2 fichiers utiles */
|
||||
$tmpfile = __ROOT__.'/tmp/phone_storage_'.$subject['tmp_id'].'.json';
|
||||
|
||||
/* (2) Si on a déja crée le fichier avec le journal d'appel dedans, on le récupère */
|
||||
$storage_already = is_string($subject['tmp_id']) && strlen($subject['tmp_id']) == 40 && file_exists($tmpfile);
|
||||
|
||||
|
||||
/* (3) Si on a déja crée le fichier, on essaie de récupérer son contenu */
|
||||
if( $storage_already ){
|
||||
|
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
|
@ -1 +1 @@
|
|||
{"1":{"line":0,"hash":"e87fa66f038f05904a1f4992c45b9f6430cbd78b"},"98":{"line":1,"hash":"3f04358b47ac0d1e9b5abcf1c5736a3231f46ed9"},"199":{"line":2,"hash":"f1de47c3dad31faf596e7f5dd74d2d5168eedf04"},"401":{"line":3,"hash":"a36813d7b22135df742fd0f441ea9bf34cee8508"},"805":{"line":4,"hash":"db7ae3d6b56e5598c29a53ddad013b27554ac2e3"},"1613":{"line":5,"hash":"3b05c357f3bb4b6e872642b2d1541a8927954e5d"},"3229":{"line":6,"hash":"21b2a4cc8307efeb2d6b1645859b5cd1324734e8"},"6461":{"line":7,"hash":"42c194779fec0cceaad90767fa5ed4129094abe6"},"12925":{"line":8,"hash":"df57e696ca3ffbee2955467aa073bf38e24384e6"}}
|
||||
{"i0":{"line":0,"hash":"426ae5243a83a66f3576abce1ac408fafc2beb3d"},"i1":{"line":1,"hash":"592e37f137ca31408b89b51dfaf18eb3ecd8dc76"},"i4":{"line":2,"hash":"188e6de350407bc64aa0607e5a161217b9a66249"},"i5":{"line":3,"hash":"3f2f86bc21e79444b15c8f9176ee87bfaef4e29e"},"i7":{"line":4,"hash":"0db82a5b803c7078ea5a1258540029b75bfc8c59"},"i8":{"line":5,"hash":"f2fc2c964d54eb5911c952512e0bf3c462bab473"},"i9":{"line":6,"hash":"46ed412fe6a52cce25613bff05cb4bcbb3fcae47"}}
|
|
@ -1 +1 @@
|
|||
25852
|
||||
294908
|
|
@ -0,0 +1,9 @@
|
|||
"test0"
|
||||
"test1"
|
||||
"test2"
|
||||
"test3"
|
||||
"test4"
|
||||
"test5"
|
||||
"test6"
|
||||
"test7"
|
||||
"test8"
|
Loading…
Reference in New Issue