<commit_before><?php
/**
 * Repository for GDS Demo
 *
 * @author <NAME>
 */
namespace GDS\Demo;
class Repository
{

    /**
     * Memcache instance
     *
     * @var \Memcached|null
     */
    private $obj_cache = NULL;

    /**
     * GDS Store instance
     *
     * @var \GDS\Store|null
     */
    private $obj_store = NULL;

    /**
     * @return \Memcached|null
     */
    private function getCache()
    {
        if(NULL === $this->obj_cache) {
            $this->obj_cache = new \Memcached();
        }
        return $this->obj_cache;
    }

    /**
     * Get the most recent posts. From memcache ideally.
     *
     * @return array
     */
    public function getRecentPosts()
    {
        $arr_posts = $this->getCache()->get('recent');
        if(is_array($arr_posts)) {
            return $arr_posts;
        } else {
            return $this->updateCache();
        }
    }

    /**
     * Update the cache from Datastore
     *
     * @return array
     */
    private function updateCache()
    {
        $obj_store = $this->getStore();
        $arr_posts = $obj_store->query("SELECT * FROM Guestbook ORDER BY posted DESC")->fetchPage(POST_LIMIT);
        $this->getCache()->set('recent', $arr_posts);
        return $arr_posts;
    }

    /**
     * Insert the entity (plus limit the data to the same values as the form)
     *
     * @param $str_name
     * @param $str_message
     */
    public function createPost($str_name, $str_message)
    {
        $obj_store = $this->getStore();
        $obj_store->upsert($obj_store->createEntity([
            'posted' => date('Y-m-d H:i:s'),
            'name' => $str_name,
            'message' => $str_message
        ]));

        // Update the cache
        $this->updateCache();
    }

    /**
     * Configure and return a Store
     *
     * @return \GDS\Store
     */
    private function getStore()
    {
        if(NULL === $this->obj_store) {
            $obj_google_client = \GDS\Gateway::createGoogleClient('php-gds-demo', GDS_ACCOUNT, GDS_KEY_FILE);
            $obj_gateway = new \GDS\Gateway($obj_google_client, 'php-gds-demo');
            $this->obj_store = new \GDS\Store($obj_gateway, $this->makeSchema());
        }
        return $this->obj_store;
    }

    /**
     * Build a schema for Guest book entries
     *
     * the posted datetime as an indexed field
     *
     * @return \GDS\Schema
     */
    private function makeSchema()
    {
        return (new \GDS\Schema('Guestbook'))
            ->addDatetime('posted')
            ->addString('name', FALSE)
            ->addString('message', FALSE);
    }

}<commit_msg>Move to version 2.0 of php-gds<commit_after><?php
/**
 * Repository for GDS Demo
 *
 * @author <NAME>
 */
namespace GDS\Demo;
use GDS\Schema;
use GDS\Store;

class Repository
{

    /**
     * Memcache instance
     *
     * @var \Memcached|null
     */
    private $obj_cache = NULL;

    /**
     * GDS Store instance
     *
     * @var Store|null
     */
    private $obj_store = NULL;

    /**
     * @return \Memcached|null
     */
    private function getCache()
    {
        if(NULL === $this->obj_cache) {
            $this->obj_cache = new \Memcached();
        }
        return $this->obj_cache;
    }

    /**
     * Get the most recent posts. From memcache ideally.
     *
     * @return array
     */
    public function getRecentPosts()
    {
        $arr_posts = $this->getCache()->get('recent');
        if(is_array($arr_posts)) {
            return $arr_posts;
        } else {
            return $this->updateCache();
        }
    }

    /**
     * Update the cache from Datastore
     *
     * @return array
     */
    private function updateCache()
    {
        $obj_store = $this->getStore();
        $arr_posts = $obj_store->query("SELECT * FROM Guestbook ORDER BY posted DESC")->fetchPage(POST_LIMIT);
        $this->getCache()->set('recent', $arr_posts);
        return $arr_posts;
    }

    /**
     * Insert the entity (plus limit the data to the same values as the form)
     *
     * @param $str_name
     * @param $str_message
     * @param $str_ip
     */
    public function createPost($str_name, $str_message, $str_ip)
    {
        $obj_store = $this->getStore();
        $obj_store->upsert($obj_store->createEntity([
            'posted' => date('Y-m-d H:i:s'),
            'name' => $str_name,
            'message' => $str_message,
            'ip' => $str_ip
        ]));

        // Update the cache
        $this->updateCache();
    }

    /**
     * Configure and return a Store
     *
     * @return Store
     */
    private function getStore()
    {
        if(NULL === $this->obj_store) {
            $this->obj_store = new Store($this->makeSchema());
        }
        return $this->obj_store;
    }

    /**
     * Build a schema for Guest book entries
     *
     * the posted datetime as an indexed field
     *
     * @return Schema
     */
    private function makeSchema()
    {
        return (new Schema('Guestbook'))
            ->addDatetime('posted')
            ->addString('name', FALSE)
            ->addString('message', FALSE)
            ->addString('ip')
        ;
    }

}
{'fraction_non_alphanumeric': 0.14233152594887685, 'fraction_numerical': 0.0003872966692486445, 'mean_word_length': 1.793401838831801, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 57, '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': '1434328', 'n_tokens_mistral': 1642, 'n_tokens_neox': 1598, 'n_words': 350}