<commit_before><?php

namespace Kettle;

use Aws\DynamoDb\DynamoDbClient;

class ORM {

    // --------------------------

    /**
     * Class configuration
     *
     * @var array
     *          - key
     *          - secret
     *          - region
     *
     */
    protected static $_config;

    // instance of DynamoDbClient class
    protected static $_client;

    // --------------------------

    // DynamoDB TableName
    protected $_table_name;

    // HashKey
    protected $_hash_key;

    // RangeKey
    protected $_range_key;

    /** 
     * data schema
     *
     * @var array 
     * 
     *        ex) 
     *        $_schema = array(
     *               'field_name_1' => 'S',
     *               'field_name_2' => 'N',
     *               );
     */
    protected $_schema = array();

    /**
     * DynamoDB record data is saved here as an associative array
     *
     * @var array
     */   
    protected $_data   = array();

    // Is this a new object (has create() been called)?
    protected $_is_new = false;

    //-----------------------------------------------
    // PUBLIC METHODS
    //-----------------------------------------------
    public function configure($key, $value) {
        self::$_config[$key] = $value;
    }

    /**
     * Retrive single result using hash_key and range_key
     *
     * @return object  instance of the ORM sub class
     */
    public function findOne($hash_key_value, $range_key_value = null, $options = array()) {
        $query = array(
            $this->_hash_key => $hash_key_value,
        );

        if ($range_key_value) {
            if (!$this->_range_key) {
                throw new \Exception("Range key is not defined.");
            }
            $query[$this->_range_key] = $range_key_value;
        }

        $key = $this->_formatAttributes($query);
        $args = array(
            'TableName' => $this->_table_name,
            'Key'       => $key,
        );

        $item = self::$_client->getItem($args);

        if (!is_array($item['Item'])) {
            return null;
        }

        $result = $this->_formatResult($item['Item']);

        $class_name = get_called_class();
        $instance = self::factory($class_name);
        $instance->hydrate($result);
        return $instance;
    }

    /**
     * Retrive multiple results using query
     *
     */
    public function findMany($query, $options = array()) {

        // query($tableName, $keyConditions, $options = array())
        $result = $this->query($query, $options);

        // scan($tableName, $filter, $limit = null)
        $array  = array();
        $class_name = get_called_class();
        foreach ($result as $row) {
            $instance = self::factory($class_name);
            $instance->hydrate($row);
            $array[] = $instance;
        }
        return $array;
    }

    /**
     * Save data to the DynamoDB
     *
     */
    public function save() {
        $values = $this->_data;

        if ($this->_is_new) { // insert
            // TODO: Expected support
            $expected = array();
            $result = $this->putItem($values, $expected);
        } else { // update
            // TODO: Expected support
            $expected = array();
            $result = $this->updateItem($values, $expected);
        }

        return $result;
    }

    // @see http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.DynamoDb.DynamoDbClient.html#_deleteItem
    public function delete() {
        $conditions = $this->_getKeyConditions();
        $args = array(
            'TableName'    => $this->_table_name,
            'Key'          => $conditions,
            'ReturnValues' => 'ALL_OLD',
        );

        $result = self::$_client->deleteItem($args);
        return $result;
    }

    public function set($key, $value) {<commit_msg>Add limit method.<commit_after><?php

namespace Kettle;

use Aws\DynamoDb\DynamoDbClient;

class ORM {

    // --------------------------

    /**
     * Class configuration
     *
     * @var array
     *          - key
     *          - secret
     *          - region
     *
     */
    protected static $_config;

    // instance of DynamoDbClient class
    protected static $_client;

    // --------------------------

    // DynamoDB TableName
    protected $_table_name;

    // HashKey
    protected $_hash_key;

    // RangeKey
    protected $_range_key;

    /** 
     * data schema
     *
     * @var array 
     * 
     *        ex) 
     *        $_schema = array(
     *               'field_name_1' => 'S',
     *               'field_name_2' => 'N',
     *               );
     */
    protected $_schema = array();

    /**
     * DynamoDB record data is saved here as an associative array
     *
     * @var array
     */   
    protected $_data   = array();

    // LIMIT
    protected $_limit  = null;

    // Array of WHERE clauses
    protected $_where_conditions = array();

    // Is this a new object (has create() been called)?
    protected $_is_new = false;

    //-----------------------------------------------
    // PUBLIC METHODS
    //-----------------------------------------------
    public function configure($key, $value) {
        self::$_config[$key] = $value;
    }

    /**
     * Retrive single result using hash_key and range_key
     *
     * @return object  instance of the ORM sub class
     */
    public function findOne($hash_key_value, $range_key_value = null, $options = array()) {
        $query = array(
            $this->_hash_key => $hash_key_value,
        );

        if ($range_key_value) {
            if (!$this->_range_key) {
                throw new \Exception("Range key is not defined.");
            }
            $query[$this->_range_key] = $range_key_value;
        }

        $key = $this->_formatAttributes($query);
        $args = array(
            'TableName' => $this->_table_name,
            'Key'       => $key,
        );

        $item = self::$_client->getItem($args);

        if (!is_array($item['Item'])) {
            return null;
        }

        $result = $this->_formatResult($item['Item']);

        $class_name = get_called_class();
        $instance = self::factory($class_name);
        $instance->hydrate($result);
        return $instance;
    }

    /**
     * Retrive multiple results using query
     *
     */
    public function findMany($query, $options = array()) {

        // query($tableName, $keyConditions, $options = array())
        $result = $this->query($query, $options);

        // scan($tableName, $filter, $limit = null)
        $array  = array();
        $class_name = get_called_class();
        foreach ($result as $row) {
            $instance = self::factory($class_name);
            $instance->hydrate($row);
            $array[] = $instance;
        }
        return $array;
    }

    /**
     * Save data to the DynamoDB
     *
     */
    public function save() {
        $values = $this->_data;

        if ($this->_is_new) { // insert
            // TODO: Expected support
            $expected = array();
            $result = $this->putItem($values, $expected);
        } else { // update
            // TODO: Expected support
            $expected = array();
            $result = $this->updateItem($values, $expected);
        }

        return $result;
    }

    // @see http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.DynamoDb.DynamoDbClient.html#_deleteItem
    public function delete() {
        $conditions = $this->_getKeyConditions();
        $args = array(
            'TableName'    => $this->_table_name,
            'Key'          => $conditions,
            'ReturnValues' => 'ALL_OLD',
        );

        $result = self::$_client->deleteItem($args);
        return $result;
    }

    /**
     * Add a LIMIT to the query
     */
     public function limit($limit) {
         $this->_limit = $limit;
         return $this;
    }

    public function set($key, $value) {
{'fraction_non_alphanumeric': 0.17059118236472945, 'fraction_numerical': 0.000501002004008016, 'mean_word_length': 1.4828980099502487, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 54, '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': '6909363', 'n_tokens_mistral': 2304, 'n_tokens_neox': 2219, 'n_words': 500}