<commit_before><?php
/*
 *	Lizzy - small and fast web-page rendering engine
 *
 * Simple file-based key-value database
 *
 * $db = new DataStorage($dbFile, $sid = '', $lockDB = false, $format = '', $lockTimeout = 120, $secure = false)
 *      $dbFile:        file where data is stored
 *      $sid:           session id - required if file locking is used
 *      $lockDB:        activates file locking
 *      $format:        json, yaml or csv, if not set, file extension is used
 *      $lockTimeout:   time (ms) till a locked file is unlocked automatically
 *      $secure:        prevents accessing files in the 'config/' folder
 *
 * write($key, $value = null)
 *      -> $key == key && $value == value   -> store tuple
 *      -> $key == tuple [key =>value,...]  -> store array
 *      -> $key == array ** $value != null  -> replace data
 *
 * read($key = '*')
 *      -> $key == null || '*'  -> read all records, return array
 *      -> $key == key  -> return value
 *
 * lock($key)
 *
 * unlock($key = true)
 *      -> $key == true  -> unlock all owner's records
 *      -> $key == '*'  -> unlock all records
 *
 * convert($source, $destinationFormat)
 *
 * Modified:
 *      writeAll($data, $destFile = '')  -> write($data)
 *      readValue($key) -> read($key)
 *      readRec($key = '*') -> read('*')
 *
 * 2D Data:
 *      -> when using csv files
 *      -> $key of type 'x,y' (column,row) instead of field id, indices start at 0
 *      e.g. $db->read('0,4');
 *
 * Meta-Data:
 *      is maintained either in same file, then under key '_mega_'
 *      or in separate file in case of 2D data in .csv files -> in this case meta data is not under '_mega_'
*/

define('LIZZY_META', '_meta_');
define('LIZZY_LOCK', 'lock');
define('LIZZY_LOCK_TIME', 'time');
define('LIZZY_SID', 'sid');     // session ID
define('LIZZY_MODIF_TIME', 'modif');
define('LIZZY_LOCK_ALL', 'lock_all');
define('LIZZY_DEFAULT_FILE_TYPE', 'json');

require_once SYSTEM_PATH.'vendor/autoload.php';


use Symfony\Component\Yaml\Yaml;

class DataStorage
{
	private $dataFile;
	private $sid;
	private $dbMetaDBfile = false;
	private $dataModified = false;
	private $meta = [];



    public function __construct($dbFile, $sid = '', $lockDB = false, $format = '', $lockTimeout = 120, $secure = false)
    {
        if (is_array($dbFile)) {
            list($dbFile, $sid, $lockDB, $format, $lockTimeout, $secure, $useRecycleBin) = $this->parseArguments($dbFile);
        } else {
            $useRecycleBin = false;
        }

        if ($secure && (strpos($dbFile, 'config/') !== false)) {
            return null;
        }
        if (file_exists($dbFile)) {
	        $this->dataFile = $dbFile;

		} else {
			if (file_exists(pathinfo($dbFile, PATHINFO_DIRNAME))) {
		        $this->dataFile = $dbFile;
	            touch($this->dataFile);

            } else {
			    if (function_exists('fatalError')) {
                    fatalError("DataStorage: unable to create file '$dbFile'", 'File: ' . __FILE__ . ' Line: ' . __LINE__);
                } else {
			        die("DataStorage: unable to create file '$dbFile'");
                }
			}
        }
        if (strpos($dbFile, '.csv') !== false) {
            $this->dbMetaDBfile = str_replace('.csv', '_meta.'.LIZZY_DEFAULT_FILE_TYPE, $dbFile);
            if (!file_exists($this->dbMetaDBfile)) {
                touch($this->dbMetaDBfile);
            }
        }
        $this->sid = $sid;
        $this->lockDB = $lockDB;
        $this->format = ($format) ? $format : pathinfo($dbFile, PATHINFO_EXTENSION) ;
        $this->lockTimeout = $lockTimeout;
        $this->useRecycleBin = $useRecycleBin;

        if (!isset($this->data)) {
            $this->lowLevelRead();
        }

        $this->checkDB();   // make sure DB is initialized
        return;
    } // __construct



	//---------------------------------------------------------------------------
    public function write($key, $value = null)
    {
        if (isset($this->meta[LIZZY_LOCK_ALL])) { // entire DB is locked
            if (!$this->isMySessionID($this->meta[LIZZY_LOCK_ALL])) {
                return false;
            }
        }

        $data = &$this->data;
        if ($this->dbMetaDBfile) {
            $meta = &$this->meta;
        } else {
            if (!isset($this->data[LIZZY_META])) {
                $this->data[LIZZY_META] = [];
            }
            $meta = $this->data[LIZZY_META];
            $this->meta = $meta;
        }

        if (!is_array($data)) {
            return false;
        }

        if (is_array($key)) {
            if ($value) {
                $data = $key;   // overwrite all //???
                $this->dataModified = true;

            } else {
                $array = $key;
                foreach ($array as $key => $value) {
                    if (!isset($meta[$key][LIZZY_LOCK][LIZZY_LOCK_TIME])) { // skip meta / lock-time<commit_msg>bugfix: proper initialization of data file when it doesn't exist yet.<commit_after><?php
/*
 *	Lizzy - small and fast web-page rendering engine
 *
 * Simple file-based key-value database
 *
 * $db = new DataStorage($dbFile, $sid = '', $lockDB = false, $format = '', $lockTimeout = 120, $secure = false)
 *      $dbFile:        file where data is stored
 *      $sid:           session id - required if file locking is used
 *      $lockDB:        activates file locking
 *      $format:        json, yaml or csv, if not set, file extension is used
 *      $lockTimeout:   time (ms) till a locked file is unlocked automatically
 *      $secure:        prevents accessing files in the 'config/' folder
 *
 * write($key, $value = null)
 *      -> $key == key && $value == value   -> store tuple
 *      -> $key == tuple [key =>value,...]  -> store array
 *      -> $key == array ** $value != null  -> replace data
 *
 * read($key = '*')
 *      -> $key == null || '*'  -> read all records, return array
 *      -> $key == key  -> return value
 *
 * lock($key)
 *
 * unlock($key = true)
 *      -> $key == true  -> unlock all owner's records
 *      -> $key == '*'  -> unlock all records
 *
 * convert($source, $destinationFormat)
 *
 * Modified:
 *      writeAll($data, $destFile = '')  -> write($data)
 *      readValue($key) -> read($key)
 *      readRec($key = '*') -> read('*')
 *
 * 2D Data:
 *      -> when using csv files
 *      -> $key of type 'x,y' (column,row) instead of field id, indices start at 0
 *      e.g. $db->read('0,4');
 *
 * Meta-Data:
 *      is maintained either in same file, then under key '_mega_'
 *      or in separate file in case of 2D data in .csv files -> in this case meta data is not under '_mega_'
*/

define('LIZZY_META', '_meta_');
define('LIZZY_LOCK', 'lock');
define('LIZZY_LOCK_TIME', 'time');
define('LIZZY_SID', 'sid');     // session ID
define('LIZZY_MODIF_TIME', 'modif');
define('LIZZY_LOCK_ALL', 'lock_all');
define('LIZZY_DEFAULT_FILE_TYPE', 'json');

require_once SYSTEM_PATH.'vendor/autoload.php';


use Symfony\Component\Yaml\Yaml;

class DataStorage
{
	private $dataFile;
	private $sid;
	private $dbMetaDBfile = false;
	private $dataModified = false;
	private $meta = [];



    public function __construct($dbFile, $sid = '', $lockDB = false, $format = '', $lockTimeout = 120, $secure = false)
    {
        if (is_array($dbFile)) {
            list($dbFile, $sid, $lockDB, $format, $lockTimeout, $secure, $useRecycleBin) = $this->parseArguments($dbFile);
        } else {
            $useRecycleBin = false;
        }

        if ($secure && (strpos($dbFile, 'config/') !== false)) {
            return null;
        }
        if (file_exists($dbFile)) {
	        $this->dataFile = $dbFile;

		} else {
            $path = pathinfo($dbFile, PATHINFO_DIRNAME);
            $this->dataFile = $dbFile;
			if (file_exists($path)) {
	            touch($this->dataFile);

            } else {
                if (!mkdir($path, 0777, true) || !is_writable($path)) {
                    if (function_exists('fatalError')) {
                        fatalError("DataStorage: unable to create file '$dbFile'", 'File: ' . __FILE__ . ' Line: ' . __LINE__);
                    } else {
                        die("DataStorage: unable to create file '$dbFile'");
                    }
                } else {
                    touch($this->dataFile);
                }
			}
        }
        if (strpos($dbFile, '.csv') !== false) {
            $this->dbMetaDBfile = str_replace('.csv', '_meta.'.LIZZY_DEFAULT_FILE_TYPE, $dbFile);
            if (!file_exists($this->dbMetaDBfile)) {
                touch($this->dbMetaDBfile);
            }
        }
        $this->sid = $sid;
        $this->lockDB = $lockDB;
        $this->format = ($format) ? $format : pathinfo($dbFile, PATHINFO_EXTENSION) ;
        $this->lockTimeout = $lockTimeout;
        $this->useRecycleBin = $useRecycleBin;

        if (!isset($this->data)) {
            $this->lowLevelRead();
        }

        $this->checkDB();   // make sure DB is initialized
        return;
    } // __construct



	//---------------------------------------------------------------------------
    public function write($key, $value = null)
    {
        if (isset($this->meta[LIZZY_LOCK_ALL])) { // entire DB is locked
            if (!$this->isMySessionID($this->meta[LIZZY_LOCK_ALL])) {
                return false;
            }
        }

        $data = &$this->data;
        if ($data === null) {
            $data = [];
        }
        if ($this->dbMetaDBfile) {
            $meta = &$this->meta;
        } else {
            if (!isset($this->data[LIZZY_META])) {
                $this->data[LIZZY_META] = [];
            }
            $meta = $this->data[LIZZY_META];
            $this->meta = $meta;
        }

        if (!is_array($data)) {
            return false;
        }

        if (is_array($key)) {
            if ($value) {
                $data = $key;   // overwrite all //???
                $this->dataModified = true;

            } else {
                $array = $key;
                foreach ($array as $key => $value) {
                    if (!isset($meta[$key][LIZZY_LOCK][LIZZY_LOCK_TIME])) { // skip meta / lock-time
{'fraction_non_alphanumeric': 0.17424689899586532, 'fraction_numerical': 0.0025595589683008466, 'mean_word_length': 1.9067238912732476, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 102, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3909445', 'n_tokens_mistral': 3216, 'n_tokens_neox': 3070, 'n_words': 758}