<commit_before><?php


namespace SOPHP\Core\Service\Discovery\Registry\Storage;
use MongoClient;

/**
 * Class Mongo
 * @package SOPHP\Core\Service\Discovery\Registry\Storage
 * TODO expand this very limited implementation of mongo driver
 *  -fully support mongoClient options w/ multiple hosts & authentication
 */
class Mongo implements AdapterInterface {
    /** @var  array */
    protected $options = array(
        'host' => 'localhost',
        'port' => '27017',
        'database' => 'so-php-registry',
        'collection' => 'so-php-services',
        'username' => null,
        'password' => <PASSWORD>,
    );
    /** @var  MongoClient */
    protected $client;

    public function __construct($options = array()) {
        $this->setOptions($options);
    }

    /**
     * @param array $options
     */
    public function setOptions(array $options) {
        if(isset($options['host'])){
            $this->setHost($options['host']);
        }
        if(isset($options['port'])){
            $this->setPort($options['port']);
        }
        if(isset($options['database'])) {
            $this->setDatabase($options['database']);
        }
        if(isset($options['collection'])){
            $this->setCollection($options['collection']);
        }
        if(isset($options['username'])){
            $this->setUsername($options['username']);
        }
        if(isset($options['password'])){
            $this->setPassword($options['password']);
        }
    }

    /**
     * @return array
     */
    public function getOptions(){
        return $this->options;
    }

    /**
     * @return MongoClient
     */
    public function getClient() {
        if(!$this->client) {
            $this->connect();
        }
        return $this->client;
    }

    /**
     * @param string $key
     * @param string $value
     */
    public function add($key, $value)
    {
        $doc = array('key' => $key, 'value' => $value);
        $this->getMongoCollection()->insert($doc);
    }

    /**
     * @param string $key
     */
    public function remove($key)
    {
        $criteria = array('key' => $key);
        $this->getMongoCollection()->remove($criteria);
    }

    /**
     * Get entry for $key, or get all entries if $key is null
     * @param string $key
     * @return string
     */
    public function get($key)
    {
        $query = array('key' => $key);
        $result = $this->getMongoCollection()->findOne($query, array('value'));
        if($result) {
            return $result['value'];
        }
        return null;
    }

    /**
     * @return array
     */
    public function getAll()
    {
        $results = $this->getMongoCollection()->find(array(),array('value'));
        $values = array();
        foreach($results as $result) {
            $values[] = $result['value'];
        }
        return $values;<commit_msg>updated how getAll works for mongoDb<commit_after><?php


namespace SOPHP\Core\Service\Discovery\Registry\Storage;
use MongoClient;

/**
 * Class Mongo
 * @package SOPHP\Core\Service\Discovery\Registry\Storage
 * TODO expand this very limited implementation of mongo driver
 *  -fully support mongoClient options w/ multiple hosts & authentication
 */
class Mongo implements AdapterInterface {
    /** @var  array */
    protected $options = array(
        'host' => 'localhost',
        'port' => '27017',
        'database' => 'so-php-registry',
        'collection' => 'so-php-services',
        'username' => null,
        'password' => <PASSWORD>,
    );
    /** @var  MongoClient */
    protected $client;

    public function __construct($options = array()) {
        $this->setOptions($options);
    }

    /**
     * @param array $options
     */
    public function setOptions(array $options) {
        if(isset($options['host'])){
            $this->setHost($options['host']);
        }
        if(isset($options['port'])){
            $this->setPort($options['port']);
        }
        if(isset($options['database'])) {
            $this->setDatabase($options['database']);
        }
        if(isset($options['collection'])){
            $this->setCollection($options['collection']);
        }
        if(isset($options['username'])){
            $this->setUsername($options['username']);
        }
        if(isset($options['password'])){
            $this->setPassword($options['password']);
        }
    }

    /**
     * @return array
     */
    public function getOptions(){
        return $this->options;
    }

    /**
     * @return MongoClient
     */
    public function getClient() {
        if(!$this->client) {
            $this->connect();
        }
        return $this->client;
    }

    /**
     * @param string $key
     * @param string $value
     */
    public function add($key, $value)
    {
        $doc = array('key' => $key, 'value' => $value);
        $this->getMongoCollection()->insert($doc);
    }

    /**
     * @param string $key
     */
    public function remove($key)
    {
        $criteria = array('key' => $key);
        $this->getMongoCollection()->remove($criteria);
    }

    /**
     * Get entry for $key, or get all entries if $key is null
     * @param string $key
     * @return string
     */
    public function get($key)
    {
        $query = array('key' => $key);
        $result = $this->getMongoCollection()->findOne($query, array('value'));
        if($result) {
            return $result['value'];
        }
        return null;
    }

    /**
     * @return array
     */
    public function getAll()
    {
        $results = $this->getMongoCollection()->find(array(),array('key', 'value'));
        $values = array();
        foreach($results as $result) {
            $values[$result['key']] = $result['value'];
        }
        return $values;
{'fraction_non_alphanumeric': 0.16971369437737152, 'fraction_numerical': 0.0017247326664367024, 'mean_word_length': 2.0961025093432997, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 63, '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': '978828', 'n_tokens_mistral': 1659, 'n_tokens_neox': 1629, 'n_words': 333}