<commit_before><?php

namespace Dwo\FlaggingBundle\Cache;

use Doctrine\Common\Cache\CacheProvider;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Types\Type;

/**
 * @author <NAME> <<EMAIL>>
 */
class DbalCache extends CacheProvider
{
    /**
     * The ID field will store the cache key.
     */
    const ID_FIELD = 'k';

    /**
     * The data field will store the serialized PHP value.
     */
    const DATA_FIELD = 'd';

    /**
     * The expiration field will store a date value indicating when the
     * cache entry should expire.
     */
    const EXPIRATION_FIELD = 'e';

    /**
     * @var Connection
     */
    private $connection;

    /**
     * @var string
     */
    private $table;

    /**
     * @param Connection $connection
     * @param string     $table
     */
    public function __construct(Connection $connection, $table)
    {
        $this->connection = $connection;
        $this->table = (string) $table;
    }

    /**
     * {@inheritdoc}
     */
    protected function doFetch($id)
    {
        if ($item = $this->findById($id)) {
            return unserialize($item[self::DATA_FIELD]);
        }

        return false;
    }

    /**
     * {@inheritdoc}
     */
    protected function doContains($id)
    {
        return (boolean) $this->findById($id);
    }

    /**
     * {@inheritdoc}
     */
    protected function doSave($id, $data, $lifeTime = 0, $catched = false)
    {
        list($idField, $dataField, $expField) = $this->getFields();

        $qb = $this->connection->createQueryBuilder()
            ->setParameter(':data', serialize($data))
            ->setParameter(':exp', $lifeTime > 0 ? time() + $lifeTime : null)
            ->setParameter(':id', $id);

        try {
            if ($this->doContains($id)) {
                $qb->update($this->table)
                    ->set($dataField, ':data')
                    ->set($expField, ':exp')
                    ->where($idField.' = :id');
            } else {
                $qb->insert($this->table)
                    ->values(
                        array(
                            $idField   => ':id',
                            $dataField => ':data',
                            $expField  => ':exp',
                        )
                    );
            }

            return $qb->execute();
        } catch (\Doctrine\DBAL\Exception\TableNotFoundException $e) {
            if ($catched) {
                throw $e;
            }
            $this->createTable();

            return $this->doSave($id, $data, $lifeTime, true);
        }
    }

    /**
     * {@inheritdoc}
     */
    protected function doDelete($id)
    {
        list($idField) = $this->getFields();

        $qb = $this->connection->createQueryBuilder()
            ->delete($this->table)
            ->setParameter(':id', $id)
            ->where($idField.' = :id');

        return $qb->execute();
    }

    /**
     * {@inheritdoc}
     */
    protected function doFlush()
    {
        $qb = $this->connection->createQueryBuilder()
            ->delete($this->table);

        return $qb->execute();
    }
<commit_msg>catch duplicated error<commit_after><?php

namespace Dwo\FlaggingBundle\Cache;

use Doctrine\Common\Cache\CacheProvider;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Types\Type;

/**
 * @author <NAME> <<EMAIL>>
 */
class DbalCache extends CacheProvider
{
    /**
     * The ID field will store the cache key.
     */
    const ID_FIELD = 'k';

    /**
     * The data field will store the serialized PHP value.
     */
    const DATA_FIELD = 'd';

    /**
     * The expiration field will store a date value indicating when the
     * cache entry should expire.
     */
    const EXPIRATION_FIELD = 'e';

    /**
     * @var Connection
     */
    private $connection;

    /**
     * @var string
     */
    private $table;

    /**
     * @param Connection $connection
     * @param string     $table
     */
    public function __construct(Connection $connection, $table)
    {
        $this->connection = $connection;
        $this->table = (string) $table;
    }

    /**
     * {@inheritdoc}
     */
    protected function doFetch($id)
    {
        if ($item = $this->findById($id)) {
            return unserialize($item[self::DATA_FIELD]);
        }

        return false;
    }

    /**
     * {@inheritdoc}
     */
    protected function doContains($id)
    {
        return (boolean) $this->findById($id);
    }

    /**
     * {@inheritdoc}
     */
    protected function doSave($id, $data, $lifeTime = 0, $catched = false)
    {
        list($idField, $dataField, $expField) = $this->getFields();

        $qb = $this->connection->createQueryBuilder()
            ->setParameter(':data', serialize($data))
            ->setParameter(':exp', $lifeTime > 0 ? time() + $lifeTime : null)
            ->setParameter(':id', $id);
        $qbClone = clone $qb;

        try {
            if ($this->doContains($id)) {
                $qb->update($this->table)
                    ->set($dataField, ':data')
                    ->set($expField, ':exp')
                    ->where($idField.' = :id');
            } else {
                $qb->insert($this->table)
                    ->values(
                        array(
                            $idField   => ':id',
                            $dataField => ':data',
                            $expField  => ':exp',
                        )
                    );
            }

            return $qb->execute();
        } /**
         * Table not exists
         */
        catch (\Doctrine\DBAL\Exception\TableNotFoundException $e) {
            if ($catched) {
                throw $e;
            }
            $this->createTable();

            return $this->doSave($id, $data, $lifeTime, true);
        } /**
         * Duplicate entry
         */
        catch (\Doctrine\DBAL\Exception\UniqueConstraintViolationException $e) {
            $qb = clone $qbClone;
            $qb->update($this->table)
                ->set($dataField, ':data')
                ->set($expField, ':exp')
                ->where($idField.' = :id');

            return $qb->execute();
        }
    }

    /**
     * {@inheritdoc}
     */
    protected function doDelete($id)
    {
        list($idField) = $this->getFields();

        $qb = $this->connection->createQueryBuilder()
            ->delete($this->table)
            ->setParameter(':id', $id)
            ->where($idField.' = :id');

        return $qb->execute();
    }

    /**
     * {@inheritdoc}
     */
    protected function doFlush()
    {
        $qb = $this->connection->createQueryBuilder()
            ->delete($this->table);

        return $qb->execute();
    }

{'fraction_non_alphanumeric': 0.15422382671480145, 'fraction_numerical': 0.0005776173285198556, 'mean_word_length': 1.5623381428042915, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 93, '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': '662735', 'n_tokens_mistral': 2051, 'n_tokens_neox': 1974, 'n_words': 403}