<commit_before><?php

class Mongo_Filewrapper
{

    protected $_location = array('127.0.0.1');
    protected $_port     = array('27017');
    protected $_username = '';
    protected $_password = '';
    protected $_database = 'filewrapper';
    protected $_options  = array();

    private $_client = null;
    private $_db = null;

    public function __construct(array $location = null, array $port = null,
        string $username = null, string $password= null, string $database = null, array $options = null)
    {
        if ( !empty($location) )
            $this->_location = $location;

        if ( !empty($port) )
            $this->_port = $port;

        if ( !empty($username) )
            $this->_username = $username;

        if ( !empty($password) )
            $this->_password = <PASSWORD>;

        if ( !empty($database) )
            $this->_database = $database;

        $this->_options = $options;

        $this->init();
    }

    private function init()
    {
        if ( !is_array($this->_options) && !empty($this->_username) )
        {
            $this->_options = array(
                'username' => $this->_username,
                'password' => $<PASSWORD>,
            );
        }
        elseif ( !empty($this->_username) )
        {
            $this->_options['password'] = $<PASSWORD>;
            $this->_options['username'] = $this->_username;
        }
        else
        {
            $this->_options = array();
        }

        $this->_client = new MongoClient('mongodb://'.$this->buildConenctionString(), $this->_options);

        $this->_db = $this->_client->selectDB($this->_database);
    }

    protected function buildConenctionString()
    {
        $str = '';
        if ( count($this->_location) != count($this->_port) )
        {
            throw new MongoException("Number of locations does not match number of ports");
        }

        for ($i = 0; $i < count($this->_location); $i++)
        {
            $str .= $this->_location[$i].':'.$this->_port[$i].',';
        }
        $str = trim($str, ',');

        return $str;
    }

    public function is_writable(string $filename)
    {
        return true;
    }

    public function disk_free_space(string $filename)
    {
        //it is theoretically possible to retrieve free space but it's much effort
        // not only for programming but also for the machine, so let's use this instead
        return PHP_INT_MAX - 999;
    }

    public function disk_total_space(string $filename)
    {
        //see dist_free_space
        return PHP_INT_MAX;
    }

    public function unlink(string $filename)
    {

    }

    public function is_file(string $filename)
    {

    }

    public function is_dir(string $filename)
    {

    }

    public function glob(string $filename)
    {

    }

    public function rmdir(string $filename)
    {

    }

    public function mkdir(string $filename)
    {
        //no need do create directory, files are used as key including directory
        return true;
    }

    public function chmod(string $filename)
    {
        //if an key exists it is modifiable and readable, no support for rights
        return false;
    }

    public function fopen(string $filename)
    {

    }

    public function stream_get_contents(string $filename)
    {

    }

    public function fclose(string $filename)
    {
        //closing does not exsts (i think)
        return true;
    }

    public function flock(string $filename)
    {
        //even if there is something like locking i don't think we want it for speed reasons
        return false;
    }

    public function fseek(string $filename, int $offset, int $mode)
    {

    }

    public function ftruncate(string $filename, int $size)
    {

    }
}<commit_msg>Filled most of the filewrapper - functions with code<commit_after><?php

class Mongo_Filewrapper
{

    protected $_location = array('127.0.0.1');
    protected $_port     = array('27017');
    protected $_username = '';
    protected $_password = '';
    protected $_database = 'filewrapper';
    protected $_collection = 'files';
    protected $_options  = array();

    /**
     * @var MongoClient plain connection to Mongo instance(s)
     */
    private $_client = null;

    /**
     * @var MongoDB connection to database
     */
    private $_db = null;

    /**
     * @var MongoCollection wrapper for default connection for less code to access this ;)
     */
    private $_coll = null;

    public function __construct(array $location = null, array $port = null, string $username = null,
        string $password= null, string $database = null, string $collection = null, array $options = null)
    {
        if ( !empty($location) )
            $this->_location = $location;

        if ( !empty($port) )
            $this->_port = $port;

        if ( !empty($username) )
            $this->_username = $username;

        if ( !empty($password) )
            $this->_password = $password;

        if ( !empty($database) )
            $this->_database = $database;

        if ( !empty($collection) )
            $this->_collection = $collection;

        $this->_options = $options;

        $this->init();
    }

    private function init()
    {
        if ( !is_array($this->_options) && !empty($this->_username) )
        {
            $this->_options = array(
                'username' => $this->_username,
                'password' => $<PASSWORD>,
            );
        }
        elseif ( !empty($this->_username) )
        {
            $this->_options['password'] = $<PASSWORD>;
            $this->_options['username'] = $this->_username;
        }
        else
        {
            $this->_options = array();
        }

        $this->_client = new MongoClient('mongodb://'.$this->buildConenctionString(), $this->_options);

        $this->_db = $this->_client->selectDB($this->_database);

        $this->_coll = $this->_db->selectCollection($this->_collection);
    }

    protected function buildConenctionString()
    {
        $str = '';
        if ( count($this->_location) != count($this->_port) )
        {
            throw new MongoException("Number of locations does not match number of ports.");
        }

        for ($i = 0; $i < count($this->_location); $i++)
        {
            $str .= $this->_location[$i].':'.$this->_port[$i].',';
        }
        $str = trim($str, ',');

        return $str;
    }

    public function is_writable(string $filename)
    {
        return true;
    }

    public function disk_free_space(string $filename)
    {
        //it is theoretically possible to retrieve free space but it's much effort
        // not only for programming but also for the machine, so let's use this instead
        return PHP_INT_MAX - 999;
    }

    public function disk_total_space(string $filename)
    {
        //see disk_free_space
        return PHP_INT_MAX;
    }

    public function unlink(string $filename)
    {
        return $this->_coll->remove( array('filename' => $filename) );
    }

    public function is_file(string $filename)
    {
        $cursor = $this->_coll->find( array('filename' => $filename) );

        $cnt = 0;
        foreach ( $cursor as $result )
        {
            $cnt++;
        }

        return $cnt > 0;
    }

    public function is_dir(string $filename)
    {
        //we are not interested in directories, we save files including path
        // so just return true so no one tries to create a document for a directory
        return true;
    }

    public function glob(string $filename, int $flag)
    {
        //don't know what to do here, please help!
    }

    public function rmdir(string $filename)
    {
        //remove all documents with given string enclosed in '/' (pattern of a directory - name)
        // if someone finds a better way, please contribute!
        return $this->_coll->remove( array('filename' => '/\/'.$filename.'\//') );
    }

    public function mkdir(string $filename)
    {
        //no need do create directory, files are used as key including directory
        return true;
    }

    public function chmod(string $filename)
    {
        //if an key exists it is modifiable and readable, no support for rights
        return false;
    }

    public function fopen(string $filename)
    {
        //normally returns a filehandle, we just return the filename
        // so that it will be passed to wrapper - functions which
        // expect a filename
        return $filename;
    }

    public function stream_get_contents(string $filename)
    {
        $cursor = $this->_coll->find( array('filename' => $filename) );

        $content = '';
        foreach ( $cursor as $result )
        {
            //if we have more than 1 file we just read the first
            $content = $result['content'];
            break;
        }

        return $content;
    }

    public function fclose(string $filename)
    {
        //closing does not exsts (i think)
        return true;
    }

    public function flock(string $filename)
    {
        //even if there is something like locking i don't think we want it for speed reasons
        return false;
    }

    public function fseek(string $filename, int $offset, int $mode)
    {
        //we just return $offset even if SEEK_CUR is used
        // because we do not maintain a position
        if ( $mode == SEEK_SET || $mode == SEEK_CUR )
        {
            return $offset;
        }
        if ( $mode == SEEK_END )
        {
            $cursor = $this->_coll->find( array('filename' => $filename) );

            $content = '';
            foreach ( $cursor as $result )
            {
                //if we have more than 1 file we just read the first
                $content = $result['content'];
                break;
            }

            $len = strlen($content);

            return $len + $offset;
        }
    }

    public function ftruncate(string $filename, int $size)
    {
        $cursor = $this->_coll->find( array('filename' => $filename) );

        $content = '';
        $res = null;
        foreach ( $cursor as $result )
        {
            //if we have more than 1 file we just read the first
            $content = $result['content'];
            $res = $result;
            break;
        }

        $newStr = substr($content, 0, $size);
        $res['content'] = $newStr;
        return $this->_coll->update( array('filename' => $filename), $res );
    }
}
{'fraction_non_alphanumeric': 0.12483323804078521, 'fraction_numerical': 0.003430531732418525, 'mean_word_length': 1.9160878021672687, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 95, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '325470', 'n_tokens_mistral': 2962, 'n_tokens_neox': 2815, 'n_words': 841}