<commit_before><?php

/**
 * Flintstone - A key/value database store using flat files for PHP
 * Copyright (c) 2014 <NAME>
 */

namespace Flintstone;

use Flintstone\Formatter\FormatterInterface;
use Flintstone\Formatter\SerializeFormatter;
use SplFileObject;
use SplTempFileObject;

/**
 * The Flintstone database specific class
 */
class FlintstoneDB
{
    /**
     * File read flag
     *
     * @var integer
     */
    const FILE_READ = 1;

    /**
     * File write flag
     *
     * @var integer
     */
    const FILE_WRITE = 2;

    /**
     * File append flag
     *
     * @var integer
     */
    const FILE_APPEND = 3;

    /**
     * File Access Mode
     *
     * @var array
     */
    private $file_access_mode = array(
        self::FILE_READ => array(
            'mode' => 'rb',
            'operation' => LOCK_SH
        ),
        self::FILE_WRITE => array(
            'mode' => 'wb',
            'operation' => LOCK_EX,
        ),
        self::FILE_APPEND => array(
            'mode' => 'ab',
            'operation' => LOCK_EX,
        ),
    );

    /**
     * Database Memory cache
     *
     * @var array
     */
    private $cache = array();

    /**
     * Tell whether the cache is enabled or not
     *
     * @var boolean
     */
    private $cache_enabled = true;

    /**
     * Tell whether gzip is enabled or not
     *
     * @var boolean
     */
    private $gzip_enabled = false;

    /**
     * Database File Path
     *
     * @var string
     */
    private $file;

    /**
     * Swap Memory Limit
     *
     * @var  integer
     */
    private $swap_memory_limit;

    /**
     * Formatter
     *
     * @var object
     */
    private $formatter;

    /**
     * Flintstone options:
     *
     * - string     $dir                the directory where the database files are stored
     * - string     $ext                the database file extension to use
     * - boolean    $gzip               use gzip to compress the database
     * - boolean    $cache              store get() results in memory
     * - object     $formatter          the formatter class used to encode/decode data
     * - integer    $swap_memory_limit  amount of memory to use before writing to a temporary file
     *
     * @var array
     */
    private $default_options = array(
        'dir' => '',
        'ext' => '.dat',
        'gzip' => false,
        'cache' => true,
        'formatter' => null,
        'swap_memory_limit' => 1048576,
    );

    /**
     * Flintstone constructor
     *
     * @param string $database the database name
     * @param array  $options  an array of options
     *
     * @throws FlintstoneException when database cannot be loaded
     *
     * @return void
     */
    public function __construct($database, array $options = array())
    {
        if (! preg_match("/^[A-Za-z0-9_\-]+$/", $database)) {
            throw new FlintstoneException('Invalid characters in database name');
        }
        $options = array_merge($this->default_options, $options);
        $options['database'] = $database;

        $this->init($options);
    }

    /**
     * Options setter
     *
     * @param array $options an array of options
     *
     * @throws FlintstoneException when using incorrect options values
     *
     * @return void
     */
    private function init(array $options)
    {
        $options['dir'] = rtrim($options['dir'], '/\\') . DIRECTORY_SEPARATOR;
        if (!is_dir($options['dir'])) {
            throw new FlintstoneException($options['dir'].' is not a valid directory');
        } elseif (! is_null($options['formatter']) && ! $options['formatter'] instanceof FormatterInterface) {
            throw new FlintstoneException("Formatter must implement Flintstone\Formatter\FormatterInterface");
        }
        $this->formatter = $options['formatter'] ?: new Formatter\SerializeFormatter;

        $this->swap_memory_limit = filter_var(
            $options['swap_memory_limit'],
            FILTER_VALIDATE_INT,
            array('options' => array('min_range' => 0, 'default' => $this->default_options['swap_memory_limit']))
        );

        if ($options['cache'] != $this->default_options['cache']) {
            $this->cache_enabled = !$this->cache_enabled;
        }

        if ($options['gzip'] != $this->default_options['gzip']) {
            $this->gzip_enabled = !$this->gzip_enabled;
        }

        $this->file = $options['dir'].$options['database'].$this->formatExtension($options['ext']);
    }

    /**
     * Format the file extension
     *
     * @param string $ext
     *
     * @return string
     */
    private function formatExtension($ext)
    {
        $ext = filter_var(
            $ext,
            FILTER_SANITIZE_STRING,
            array('flags' => FILTER_FLAG_STRIP_LOW|FILTER_FLAG_STRIP_HIGH)<commit_msg>Update FlintstoneDB.php<commit_after><?php

/**
 * Flintstone - A key/value database store using flat files for PHP
 * Copyright (c) 2014 <NAME>
 */

namespace Flintstone;

use Flintstone\Formatter\FormatterInterface;
use Flintstone\Formatter\SerializeFormatter;
use SplFileObject;
use SplTempFileObject;

/**
 * The Flintstone database specific class
 */
class FlintstoneDB
{
    /**
     * File read flag
     *
     * @var integer
     */
    const FILE_READ = 1;

    /**
     * File write flag
     *
     * @var integer
     */
    const FILE_WRITE = 2;

    /**
     * File append flag
     *
     * @var integer
     */
    const FILE_APPEND = 3;

    /**
     * File Access Mode
     *
     * @var array
     */
    private $file_access_mode = array(
        self::FILE_READ => array(
            'mode' => 'rb',
            'operation' => LOCK_SH
        ),
        self::FILE_WRITE => array(
            'mode' => 'wb',
            'operation' => LOCK_EX,
        ),
        self::FILE_APPEND => array(
            'mode' => 'ab',
            'operation' => LOCK_EX,
        ),
    );

    /**
     * Database Memory cache
     *
     * @var array
     */
    private $cache = array();

    /**
     * Tell whether the cache is enabled or not
     *
     * @var boolean
     */
    private $cache_enabled = true;

    /**
     * Tell whether gzip is enabled or not
     *
     * @var boolean
     */
    private $gzip_enabled = false;

    /**
     * Database File Path
     *
     * @var string
     */
    private $file;

    /**
     * Swap Memory Limit
     *
     * @var  integer
     */
    private $swap_memory_limit;

    /**
     * Formatter
     *
     * @var object
     */
    private $formatter;

    /**
     * Flintstone options:
     *
     * - string     $dir                the directory where the database files are stored
     * - string     $ext                the database file extension to use
     * - boolean    $gzip               use gzip to compress the database
     * - boolean    $cache              store get() results in memory
     * - object     $formatter          the formatter class used to encode/decode data
     * - integer    $swap_memory_limit  amount of memory to use before writing to a temporary file
     *
     * @var array
     */
    private $default_options = array(
        'dir' => '',
        'ext' => '.dat',
        'gzip' => false,
        'cache' => true,
        'formatter' => null,
        'swap_memory_limit' => 1048576,
    );

    /**
     * Flintstone constructor
     *
     * @param string $database the database name
     * @param array  $options  an array of options
     *
     * @throws FlintstoneException when database cannot be loaded
     *
     * @return void
     */
    public function __construct($database, array $options = array())
    {
        if (! preg_match("/^[A-Za-z0-9_\-]+$/", $database)) {
            throw new FlintstoneException('Invalid characters in database name');
        }
        $options = array_merge($this->default_options, $options);
        $options['database'] = $database;

        $this->init($options);
    }

    /**
     * Options setter
     *
     * @param array $options an array of options
     *
     * @throws FlintstoneException when using incorrect options values
     *
     * @return void
     */
    private function init(array $options)
    {
        $options['dir'] = rtrim($options['dir'], '/\\') . DIRECTORY_SEPARATOR;
        if (!is_dir($options['dir'])) {
            throw new FlintstoneException($options['dir'].' is not a valid directory');
        } elseif (! is_null($options['formatter']) && ! $options['formatter'] instanceof FormatterInterface) {
            throw new FlintstoneException("Formatter must implement Flintstone\Formatter\FormatterInterface");
        }
        $this->formatter = $options['formatter'] ?: new SerializeFormatter;

        $this->swap_memory_limit = filter_var(
            $options['swap_memory_limit'],
            FILTER_VALIDATE_INT,
            array('options' => array('min_range' => 0, 'default' => $this->default_options['swap_memory_limit']))
        );

        if ($options['cache'] != $this->default_options['cache']) {
            $this->cache_enabled = !$this->cache_enabled;
        }

        if ($options['gzip'] != $this->default_options['gzip']) {
            $this->gzip_enabled = !$this->gzip_enabled;
        }

        $this->file = $options['dir'].$options['database'].$this->formatExtension($options['ext']);
    }

    /**
     * Format the file extension
     *
     * @param string $ext
     *
     * @return string
     */
    private function formatExtension($ext)
    {
        $ext = filter_var(
            $ext,
            FILTER_SANITIZE_STRING,
            array('flags' => FILTER_FLAG_STRIP_LOW|FILTER_FLAG_STRIP_HIGH)
{'fraction_non_alphanumeric': 0.12740434332988623, 'fraction_numerical': 0.003516028955532575, 'mean_word_length': 1.9111980734497291, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 69, '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': '2739837', 'n_tokens_mistral': 2843, 'n_tokens_neox': 2717, 'n_words': 686}