<commit_before><?php

namespace Webfactory\Bundle\WfdMetaBundle;

use Doctrine\DBAL\Connection;

class Provider {

    protected $connection;
    protected $cache = array();

    public function __construct(Connection $connection) {
        $this->connection = $connection;
    }

    public function getLastTouched(array $tables) {
        $flip = array_flip($tables);

        if ($cacheMiss = array_diff_key($flip, $this->cache)) {
            $this->cache(array_keys($cacheMiss));
        }

        return max(array_intersect_key($this->cache, $flip));
    }

    protected function cache(array $namesOrIds) {
        $ids = array();
        $names = array();

        foreach ($namesOrIds as $t) {
            $this->cache[$t] = 0; // prevent re-query
            if ($t == '*') {
                foreach ($this->connection->fetchAll('SELECT id FROM wfd_table') as $row) {
                    $ids[] = $row['id'];
                }
            } elseif (is_numeric($t)) {
                $ids[] = $t;
            } else {
                $names[] = $t;
            }
        }

        $result = $this->connection->fetchAll('
            SELECT t.id, t.tablename, UNIX_TIMESTAMP(MAX(m.last_touched)) timestamp
            FROM wfd_meta m 
            JOIN wfd_table t on m.wfd_table_id = t.id
            WHERE '
                . ($ids ? ('t.id IN (' . implode(', ', array_fill(0, count($ids), '?')) . ')') : '')
                . (($ids && $names) ? ' OR ' : '')
                . ($names ? ('t.tablename IN (' . implode(', ', array_fill(0, count($names), '?')). ')') : '') .'
            GROUP BY t.id'
        , array_merge($ids, $names));
        
        foreach ($result as $row) {
            $this->cache[$row['id']] = $this->cache[$row['tablename']] = $row['timestamp'];
        }

        if (in_array('*', $namesOrIds)) {
            $this->cache['*'] = max($this->cache);
        }
    }<commit_msg>Wenn wir fuer @ValidUntilLastTouched(table="*") einfach nur wissen wollen, ob sich irgendwas geaendert hat, ist es so effizienter. Wenn spaeter die einzelnen Tabellen noch relevant werden sollten, laden wir sie halt dann.<commit_after><?php

namespace Webfactory\Bundle\WfdMetaBundle;

use Doctrine\DBAL\Connection;

class Provider {

    protected $connection;
    protected $cache = array();

    public function __construct(Connection $connection) {
        $this->connection = $connection;
    }

    public function getLastTouched(array $tables) {
        $flip = array_flip($tables);

        if ($cacheMiss = array_diff_key($flip, $this->cache)) {
            $this->cache(array_keys($cacheMiss));
        }

        return max(array_intersect_key($this->cache, $flip));
    }

    protected function cache(array $namesOrIds) {
        $ids = array();
        $names = array();

        foreach ($namesOrIds as $t) {
            $this->cache[$t] = 0; // prevent re-query
            if ($t == '*') {
                $this->cache['*'] = reset($this->connection->fetchAssoc('SELECT UNIX_TIMESTAMP(MAX(last_touched)) FROM wfd_meta'));
            } elseif (is_numeric($t)) {
                $ids[] = $t;
            } else {
                $names[] = $t;
            }
        }

        if ($names ||$ids) {
            $result = $this->connection->fetchAll('
                SELECT t.id, t.tablename, UNIX_TIMESTAMP(MAX(m.last_touched)) timestamp
                FROM wfd_meta m
                JOIN wfd_table t on m.wfd_table_id = t.id
                WHERE '
                    . ($ids ? ('t.id IN (' . implode(', ', array_fill(0, count($ids), '?')) . ')') : '')
                    . (($ids && $names) ? ' OR ' : '')
                    . ($names ? ('t.tablename IN (' . implode(', ', array_fill(0, count($names), '?')). ')') : '') .'
                GROUP BY t.id'
            , array_merge($ids, $names));

            foreach ($result as $row) {
                $this->cache[$row['id']] = $this->cache[$row['tablename']] = $row['timestamp'];
            }
        }
    }
{'fraction_non_alphanumeric': 0.16812390706969774, 'fraction_numerical': 0.0014988758431176618, 'mean_word_length': 1.8993482983345402, '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': '4171391', 'n_tokens_mistral': 1269, 'n_tokens_neox': 1207, 'n_words': 254}