<commit_before><?php

namespace kuralab\kdb;

class KDB
{
    private $dir;
    private $path;

    public function __construct($dir)
    {
        $this->dir = $dir . ".kdb/";
    }

    private function createDirectory()
    {
        if (!file_exists($this->dir)) {
            if (!mkdir($this->dir, 0777, true)) {
                return false;
            }
        }

        return true;
    }

    private function generatePath($key)
    {
        $this->path = $this->dir . $this->generateHash($key);
    }

    protected function generateHash($key)
    {
        return md5($key);
    }

    public function set($key, $value)
    {
        if (!$this->createDirectory()) {
            return false;
        }
        $this->generatePath($key);

        $buffer = $this->readAll();
        if (!$buffer) {
            $result = array();
        } else {
            $result = json_decode($buffer, true);
        }
        $result[$key] = $value;
        $fp = fopen($this->path, "w");
        if (!$fp) {
            fclose($fp);
            return false;
        }
        if (flock($fp, LOCK_EX)) {
            $json = json_encode($result);
            fwrite($fp, $json);
        } else {
            return false;
        }
        fclose($fp);

        return true;
    }

    public function get($key)
    {
        if (!$this->createDirectory()) {
            return false;
        }
        $this->generatePath($key);

        $buffer = $this->readAll();
        if (!$buffer) {
            return false;
        }
        $result = json_decode($buffer, true);

        return $result[$key];
    }<commit_msg>fixed<commit_after><?php

namespace kuralab\kdb;

class KDB
{
    private $dir;
    private $path;

    public function __construct($dir)
    {
        $this->dir = $dir . ".kdb/";
    }

    private function createDirectory()
    {
        if (!file_exists($this->dir)) {
            if (!mkdir($this->dir, 0777, true)) {
                return false;
            }
        }

        return true;
    }

    private function generatePath($key)
    {
        $this->path = $this->dir . $this->generateHash($key);
    }

    protected function generateHash($key)
    {
        return md5($key);
    }

    public function set($key, $value)
    {
        if (!$this->createDirectory()) {
            return false;
        }
        $this->generatePath($key);

        $fp = fopen($this->path, "w");
        if (!$fp) {
            fclose($fp);
            return false;
        }
        if (flock($fp, LOCK_EX)) {
            $json = json_encode($value);
            fwrite($fp, $json);
        } else {
            return false;
        }
        fclose($fp);

        return true;
    }

    public function get($key)
    {
        if (!$this->createDirectory()) {
            return false;
        }
        $this->generatePath($key);

        $buffer = $this->readAll();
        if (!$buffer) {
            return false;
        }
        $result = json_decode($buffer, true);

        return $result;
    }
{'fraction_non_alphanumeric': 0.14883108330589398, 'fraction_numerical': 0.0032927230819888046, 'mean_word_length': 1.4779771615008157, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 28, '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': '3359752', 'n_tokens_mistral': 920, 'n_tokens_neox': 897, 'n_words': 188}