Add queue and queuefile support
git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@5264 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
79
queue.php
Normal file
79
queue.php
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
<?php
|
||||||
|
/** Manage a queue in multiple storages
|
||||||
|
* A process can add entries to the end of a queue
|
||||||
|
* A process can get all the entries in a queue
|
||||||
|
* A process can clear all the entries in a queue
|
||||||
|
* A process can get the first entry in the queue (FIFO), with optional
|
||||||
|
* removing of the entry
|
||||||
|
* A process can get the last entry in the queue (LIFO), with optional
|
||||||
|
* removing of the entry
|
||||||
|
* A process can get X entries starting at position Y
|
||||||
|
*/
|
||||||
|
class queue
|
||||||
|
{
|
||||||
|
/** The queue connected object
|
||||||
|
*/
|
||||||
|
private $queue = null;
|
||||||
|
|
||||||
|
/** Connect to the queue
|
||||||
|
*/
|
||||||
|
public function connect ($dsn)
|
||||||
|
{
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Add a new entry to the end of the queue
|
||||||
|
* @param mixed $entry The entry to add
|
||||||
|
*/
|
||||||
|
public function add ($entry)
|
||||||
|
{
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Get all the entries of the queue
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getAll ()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Clear all the entries of the queue
|
||||||
|
*/
|
||||||
|
public function clear ()
|
||||||
|
{
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Get the first entry in the queue with optional removing of the entry
|
||||||
|
* @param boolean|null $delete If true, delete the read entry
|
||||||
|
* @return mixed Return null if there is no entry, or the first entry in the
|
||||||
|
* queue (FIFO)
|
||||||
|
*/
|
||||||
|
public function getFirst ($delete = false)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Get the last entry in the queue with optional removing of the entry
|
||||||
|
* @param boolean|null $delete If true, delete the read entry
|
||||||
|
* @return mixed Return null if there is no entry, or the last entry in the
|
||||||
|
* queue (LIFO)
|
||||||
|
*/
|
||||||
|
public function getLast ($delete = false)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Get X entries starting at position Y
|
||||||
|
* @param integer $start the starting position
|
||||||
|
* @param integer $number The number of entries to get
|
||||||
|
* @param boolean|null $delete If true, delete the read entries
|
||||||
|
* @return array An array of mixed entries
|
||||||
|
*/
|
||||||
|
public function getRange ($start, $number, $delete = false)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
207
queuefile.php
Normal file
207
queuefile.php
Normal file
@@ -0,0 +1,207 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once ("domframework/queue.php");
|
||||||
|
require_once ("domframework/file.php");
|
||||||
|
|
||||||
|
/** Manage a queue in file
|
||||||
|
* A process can add entries to the end of a queue
|
||||||
|
* A process can get all the entries in a queue
|
||||||
|
* A process can clear all the entries in a queue
|
||||||
|
* A process can get the first entry in the queue (FIFO), with optional
|
||||||
|
* removing of the entry
|
||||||
|
* A process can get the last entry in the queue (LIFO), with optional
|
||||||
|
* removing of the entry
|
||||||
|
* A process can get X entries starting at position Y
|
||||||
|
*/
|
||||||
|
class queuefile extends queue
|
||||||
|
{
|
||||||
|
|
||||||
|
/** The queue connected object
|
||||||
|
*/
|
||||||
|
private $queue = null;
|
||||||
|
|
||||||
|
/** Connect to the queue. Create it if not exists
|
||||||
|
*/
|
||||||
|
public function connect ($dsn)
|
||||||
|
// {{{
|
||||||
|
{
|
||||||
|
// The DSN format must be file:///tmp/queuefile.txt
|
||||||
|
if (substr ($dsn, 0, 7) !== "file://")
|
||||||
|
throw new \Exception ("Invalid DSN provided to queuefile : not starting ".
|
||||||
|
"by file://", 500);
|
||||||
|
$this->queue = substr ($dsn, 7);
|
||||||
|
$file = new \file ();
|
||||||
|
if (! $file->file_exists (dirname ($this->queue)))
|
||||||
|
$file->mkdir (dirname ($this->queue), 0777, true);
|
||||||
|
if (! $file->file_exists ($this->queue))
|
||||||
|
$file->touch ($this->queue);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
// }}}
|
||||||
|
|
||||||
|
/** Add a new entry to the end of the queue
|
||||||
|
* @param mixed $entry The entry to add
|
||||||
|
*/
|
||||||
|
public function add ($entry)
|
||||||
|
// {{{
|
||||||
|
{
|
||||||
|
$file = new \file ();
|
||||||
|
$file->lockEX ($this->queue);
|
||||||
|
$file->file_put_contents ($this->queue,
|
||||||
|
json_encode ($entry)."\n", FILE_APPEND);
|
||||||
|
$file->lockUN ($this->queue);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
// }}}
|
||||||
|
|
||||||
|
/** Get all the entries of the queue
|
||||||
|
* @param boolean|null $delete If true, delete the read entry
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getAll ($delete =false)
|
||||||
|
// {{{
|
||||||
|
{
|
||||||
|
$file = new \file ();
|
||||||
|
if ($delete)
|
||||||
|
$file->lockEX ($this->queue);
|
||||||
|
else
|
||||||
|
$file->lockSH ($this->queue);
|
||||||
|
$content = $file->file_get_contents ($this->queue);
|
||||||
|
$entries = array ();
|
||||||
|
foreach (explode ("\n", $content) as $json)
|
||||||
|
{
|
||||||
|
$entries[] = json_decode ($json);
|
||||||
|
}
|
||||||
|
if ($delete)
|
||||||
|
$file->file_put_contents ($this->queue, "");
|
||||||
|
$file->lockUN ($this->queue);
|
||||||
|
return $entries;
|
||||||
|
}
|
||||||
|
// }}}
|
||||||
|
|
||||||
|
/** Get the number of entries in the queue
|
||||||
|
*/
|
||||||
|
public function count ()
|
||||||
|
// {{{
|
||||||
|
{
|
||||||
|
$file = new \file ();
|
||||||
|
$file->lockSH ($this->queue);
|
||||||
|
$content = $file->file_get_contents ($this->queue);
|
||||||
|
$count = substr_count ($content, "\n");
|
||||||
|
$file->lockUN ($this->queue);
|
||||||
|
return $count;
|
||||||
|
}
|
||||||
|
// }}}
|
||||||
|
|
||||||
|
/** Clear all the entries of the queue
|
||||||
|
*/
|
||||||
|
public function clear ()
|
||||||
|
// {{{
|
||||||
|
{
|
||||||
|
$file = new \file ();
|
||||||
|
$file->lockEX ($this->queue);
|
||||||
|
$file->file_put_contents ($this->queue, "");
|
||||||
|
$file->lockUN ($this->queue);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
// }}}
|
||||||
|
|
||||||
|
/** Get the first entry in the queue with optional removing of the entry
|
||||||
|
* @param boolean|null $delete If true, delete the read entry
|
||||||
|
* @return mixed Return null if there is no entry, or the first entry in the
|
||||||
|
* queue (FIFO)
|
||||||
|
*/
|
||||||
|
public function getFirst ($delete = false)
|
||||||
|
// {{{
|
||||||
|
{
|
||||||
|
$file = new \file ();
|
||||||
|
if ($delete)
|
||||||
|
$file->lockEX ($this->queue);
|
||||||
|
else
|
||||||
|
$file->lockSH ($this->queue);
|
||||||
|
$content = $file->file_get_contents ($this->queue);
|
||||||
|
$entries = explode ("\n", $content);
|
||||||
|
if (count ($entries) === 0)
|
||||||
|
$res = null;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$json = array_shift ($entries);
|
||||||
|
$res = json_decode ($json);
|
||||||
|
if ($delete)
|
||||||
|
$file->file_put_contents ($this->queue,
|
||||||
|
implode ("\n", $entries));
|
||||||
|
}
|
||||||
|
$file->lockUN ($this->queue);
|
||||||
|
return $res;
|
||||||
|
}
|
||||||
|
// }}}
|
||||||
|
|
||||||
|
/** Get the last entry in the queue with optional removing of the entry
|
||||||
|
* @param boolean|null $delete If true, delete the read entry
|
||||||
|
* @return mixed Return null if there is no entry, or the last entry in the
|
||||||
|
* queue (LIFO)
|
||||||
|
*/
|
||||||
|
public function getLast ($delete = false)
|
||||||
|
// {{{
|
||||||
|
{
|
||||||
|
$file = new \file ();
|
||||||
|
if ($delete)
|
||||||
|
$file->lockEX ($this->queue);
|
||||||
|
else
|
||||||
|
$file->lockSH ($this->queue);
|
||||||
|
$content = $file->file_get_contents ($this->queue);
|
||||||
|
$entries = explode ("\n", $content);
|
||||||
|
if (count ($entries) === 0)
|
||||||
|
$res = null;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$json = array_pop ($entries);
|
||||||
|
$res = json_decode ($json);
|
||||||
|
if ($delete)
|
||||||
|
$file->file_put_contents ($this->queue,
|
||||||
|
implode ("\n", $entries));
|
||||||
|
}
|
||||||
|
$file->lockUN ($this->queue);
|
||||||
|
return $res;
|
||||||
|
}
|
||||||
|
// }}}
|
||||||
|
|
||||||
|
/** Get X entries starting at position Y
|
||||||
|
* @param integer $start the starting position
|
||||||
|
* @param integer $number The number of entries to get
|
||||||
|
* @param boolean|null $delete If true, delete the read entries
|
||||||
|
* @return array An array of mixed entries
|
||||||
|
*/
|
||||||
|
public function getRange ($start, $number, $delete = false)
|
||||||
|
// {{{
|
||||||
|
{
|
||||||
|
$file = new \file ();
|
||||||
|
if ($delete)
|
||||||
|
$file->lockEX ($this->queue);
|
||||||
|
else
|
||||||
|
$file->lockSH ($this->queue);
|
||||||
|
$content = $file->file_get_contents ($this->queue);
|
||||||
|
$entries = explode ("\n", $content);
|
||||||
|
$res = array ();
|
||||||
|
if (count ($entries) > 0)
|
||||||
|
{
|
||||||
|
for ($i = $start ; $i < $start + $number ; $i++)
|
||||||
|
{
|
||||||
|
if (! key_exists ($i, $entries))
|
||||||
|
throw new \Exception ("Invalid entry requested", 406);
|
||||||
|
$json = $entries[$i];
|
||||||
|
$content = json_decode ($json, true);
|
||||||
|
if (json_last_error() === JSON_ERROR_NONE)
|
||||||
|
$res[] = $content;
|
||||||
|
else
|
||||||
|
$res[] = $json;
|
||||||
|
}
|
||||||
|
if ($delete)
|
||||||
|
$file->file_put_contents ($this->queue,
|
||||||
|
implode ("\n", $entries));
|
||||||
|
}
|
||||||
|
$file->lockUN ($this->queue);
|
||||||
|
return $res;
|
||||||
|
}
|
||||||
|
// }}}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user