Skip to content

Commit

Permalink
add lazy-loading to Jig
Browse files Browse the repository at this point in the history
When working with Jig/Mapper and processing a lot of records, this lazy-mode boosts the performance. A load & save of 5000 records took me about 3min to complete. With lazy-mode it's only 4sec. But notice that lazy-mode could easier produce race-conditions or data loss on higher frequent applications. Perhaps a mutex lock can help here later.
  • Loading branch information
ikkez committed Mar 22, 2018
1 parent 4515797 commit 7e1cd9b
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions db/jig.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ class Jig {
//! Jig log
$log,
//! Memory-held data
$data;
$data,
//! lazy load/save files
$lazy;

/**
* Read data from memory/file
Expand All @@ -54,6 +56,8 @@ function &read($file) {
$this->data[$file]=[];
return $this->data[$file];
}
if ($this->lazy && isset($this->data[$file]))
return $this->data[$file];
$fw=\Base::instance();
$raw=$fw->read($dst);
switch ($this->format) {
Expand All @@ -75,7 +79,7 @@ function &read($file) {
* @param $data array
**/
function write($file,array $data=NULL) {
if (!$this->dir)
if (!$this->dir || $this->lazy)
return count($this->data[$file]=$data);
$fw=\Base::instance();
switch ($this->format) {
Expand Down Expand Up @@ -131,6 +135,8 @@ function jot($frame) {
* @return NULL
**/
function drop() {
if ($this->lazy) // intentional
$this->data=[];
if (!$this->dir)
$this->data=[];
elseif ($glob=@glob($this->dir.'/*',GLOB_NOSORT))
Expand All @@ -147,11 +153,23 @@ private function __clone() {
* @param $dir string
* @param $format int
**/
function __construct($dir=NULL,$format=self::FORMAT_JSON) {
function __construct($dir=NULL,$format=self::FORMAT_JSON,$lazy=FALSE) {
if ($dir && !is_dir($dir))
mkdir($dir,\Base::MODE,TRUE);
$this->uuid=\Base::instance()->hash($this->dir=$dir);
$this->format=$format;
$this->lazy=$lazy;
}

/**
* save file on destruction
**/
function __destruct() {
if ($this->lazy) {
$this->lazy = FALSE;
foreach ($this->data as $file => $data)
$this->write($file,$data);
}
}

}

0 comments on commit 7e1cd9b

Please sign in to comment.