<commit_before><?php

namespace MongoMinify;

class Collection {
	
	public $name = '';
	public $namespace = '';
	public $db;
	public $native;


	public function __construct($name, $db)
	{
		$this->name = $name;
		$this->db = $db;
		$this->client = $db->client;
		$this->namespace = $db->name . '.' . $this->name;
		$this->native = $db->native->selectCollection($this->name);

		// Apply schema to collection
		$this->setSchemaByName($this->namespace);
	}


	/**
	 * Compression
	 * This is useful for testing what a document will look like during development or debugging
	 */
	public function compress($data)
	{
		$document = new Document($data, $this);
		$document->compress();
		return $document->data;
	}


	/**
	 * Save Document
	 * @param  [type] $data    [description]
	 * @param  array  $options [description]
	 * @return [type]          [description]
	 */
	public function save(&$data, Array $options = array())
	{
		$document = new Document($data, $this);
		$document->compress();
		$save = $this->native->save($document->data, $options);
		$data = $document->data;
		return $save;
	}


	/**
	 * Insert new document
	 * @param  [type] $document [description]
	 * @param  array  $options  [description]
	 * @return [type]           [description]
	 */
	public function insert(&$data, Array $options = array())
	{
		$document = new Document($data, $this);
		$document->compress();
		$insert = $this->native->insert($document->data, $options);
		$data = $document->data;
		return $insert;
	}


	/**
	 * Find document
	 * @param  array  $document [description]
	 * @return [type]           [description]
	 */
	public function find(Array $document = array())
	{
		return array();
	}


	/**
	 * Apply internal schema
	 * @param [type] $schema [description]
	 */
	public function setSchemaByName($schema_name = null)
	{
		if ( ! $schema_name)
		{
			$schema_name = $this->namespace;
		}
		if (strpos($schema_name, '.') === false)
		{
			$schema_name = $this->db->name . '.' . $schema_name;
		}
		$schema_file = $this->client->schema_dir . '/' . $schema_name . '.php';
		if (file_exists($schema_file))
		{
			$schema = include $schema_file;<commit_msg>Basic cursors for querying against compressed data transparently<commit_after><?php

namespace MongoMinify;

class Collection {
	
	public $name = '';
	public $namespace = '';
	public $db;
	public $native;


	public function __construct($name, $db)
	{
		$this->name = $name;
		$this->db = $db;
		$this->client = $db->client;
		$this->namespace = $db->name . '.' . $this->name;
		$this->native = $db->native->selectCollection($this->name);

		// Apply schema to collection
		$this->setSchemaByName($this->namespace);
	}


	/**
	 * Compression
	 * This is useful for testing what a document will look like during development or debugging
	 */
	public function compress($data)
	{
		$document = new Document($data, $this);
		$document->compress();
		return $document->data;
	}


	/**
	 * Save Document
	 * @param  [type] $data    [description]
	 * @param  array  $options [description]
	 * @return [type]          [description]
	 */
	public function save(&$data, Array $options = array())
	{
		$document = new Document($data, $this);
		$document->compress();
		$save = $this->native->save($document->data, $options);
		$data = $document->data;
		return $save;
	}


	/**
	 * Insert new document
	 * @param  [type] $document [description]
	 * @param  array  $options  [description]
	 * @return [type]           [description]
	 */
	public function insert(&$data, Array $options = array())
	{
		$document = new Document($data, $this);
		$document->compress();
		$insert = $this->native->insert($document->data, $options);
		$data = $document->data;
		return $insert;
	}


	/**
	 * Find document
	 * @param  array  $document [description]
	 * @return [type]           [description]
	 */
	public function findOne(Array $query = array(), Array $options = array())
	{
		$cursor = $this->find($query, $options);
		return $cursor->getNext();
	}
	public function find(Array $query = array(), Array $options = array())
	{
		$document = new Document($query, $this);
		$document->compress();
		$cursor = $this->native->find($document->data, $options);
		return $cursor;
	}


	/**
	 * Apply internal schema
	 * @param [type] $schema [description]
	 */
	public function setSchemaByName($schema_name = null)
	{
		if ( ! $schema_name)
		{
			$schema_name = $this->namespace;
		}
		if (strpos($schema_name, '.') === false)
		{
			$schema_name = $this->db->name . '.' . $schema_name;
		}
		$schema_file = $this->client->schema_dir . '/' . $schema_name . '.php';
		if (file_exists($schema_file))
		{
			$schema = include $schema_file;
{'fraction_non_alphanumeric': 0.17571336623042266, 'fraction_numerical': 0.0, 'mean_word_length': 3.444232602478551, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 69, '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': '6487144', 'n_tokens_mistral': 1635, 'n_tokens_neox': 1488, 'n_words': 366}