<commit_before><?php
/**
 * Monga is a swift MongoDB Abstraction for PHP 5.3+
 *
 * @package    Monga
 * @version    1.0
 * @author     <NAME>
 * @license    MIT License
 * @copyright  2011 - 2012 <NAME>
 * @link       http://github.com/FrenkyNet/Monga
 */

namespace Monga;

use Mongo;
use MongoDB;
use MongoCode;
use MongoDBRef;

class Database
{
	/**
	 * @var  object  $database MongoDB instance
	 */
	protected $database;

	/**
	 * @var  object  $connection  Connection instance
	 */
	protected $connection;

	/**
	 * Constructor, sets database and Connection.
	 *
	 * @param  object  $database    MongoDB connection object
	 * @param  object  $connection  Monga\Connection object, optional
	 */
	public function __construct(MongoDB $database, Connection $connection = null)
	{
		$connection and $this->connection = $connection;

		$this->setDatabase($database);
	}

	/**
	 * Retrieve all collection names
	 *
	 * @return  array  array of collection names
	 */
	public function listCollections()
	{
		return $this->database->getCollectionNames();
	}

	/**
	 * Check whether the database contains a given connection
	 *
	 * @param   string   $collection  collection name
	 * @return  boolean  whether the collection exists in the database
	 */
	public function hasCollection($collection)
	{
		return in_array($collection, $this->listCollections());
	}

	/**
	 * Returns all collection.
	 *
	 * @param   boolean  $wrap  whether to wrap the collection instances in Collection classes.
	 * @return  array    connections array
	 */
	public function allCollections($wrap = false)
	{
		$collections = $this->database->listCollections();

		if ( ! $wrap)
		{
			return $collections;
		}

		return array_map(function($collection)
		{
			return new Collection($collection);
		},
		$collections);
	}

	/**
	 * Retrieve the connection.
	 *
	 * @return  object  Connection instance
	 */
	public function getConnection()
	{
		return $this->connection;
	}

	/**
	 * Connection injector
	 *
	 * @param  Connection  $connection  connection object
	 * @param  object      $this
	 */
	public function setConnection(Connection $connection)
	{
		$this->connection = $connection;

		return $this;
	}

	/**
	 * Database injector
	 *
	 * @param   mixed   $database  MongoDB instance, Database instance, string identifier.
	 * @return  object  $this
	 */
	public function setDatabase($database)
	{
		if ( ! $database instanceof MongoDB)
		{
			$database = $this->connection->getConnection()->{$database};
		}

		$this->database = $database;

		return $this;
	}

	/**
	 * Return the MongoDB database instance
	 *
	 * @return  object  MongoDB database instance
	 */
	public function getDatabase()
	{
		return $this->database;
	}

	/**
	 * Retrieve a collection
	 *
	 * @param   string   $collection  collection name
	 * @param   boolean  $wrap        whether to wrap it in a Collection class.
	 * @return  mixed    Collection instance or MongoCollection instance.
	 */
	public function collection($collection, $wrap = true)
	{
		$collection = $this->database->selectCollection($collection);
		$wrap and $collection = new Collection($collection);

		return $collection;
	}

	/**
	 * Retrieve a GridFS object
	 *
	 * @param   string   $prefix  collection name
	 * @param   boolean  $wrap    whether to wrap it in a Collection class.
	 * @return  mixed    Collection instance or MongoCollection instance.
	 */
	public function filesystem($prefix = 'fs', $wrap = true)
	{
		$collection = $this->database->getGridFS($prefix);
		$wrap and $collection = new Filesystem($collection);

		return $collection;
	}

	/**
	 * Retrieve one or more references from the database.
	 *
	 * @param    object|array  $reference  one or more references
	 * @return   array                     one or more documents
	 */
	public function getRef($reference)
	{
		$array = ! isset($reference['$ref']);
		$reference = $array ? $reference : array($reference);
		$database = $this->database;

		$result = array_map(function ($ref) use($database)
		{
			return MongoDBRef::get($database, $ref);
		},
		$reference);

		return $array ? $result : reset($result);
	}

	/**<commit_msg>Added Database::drop<commit_after><?php
/**
 * Monga is a swift MongoDB Abstraction for PHP 5.3+
 *
 * @package    Monga
 * @version    1.0
 * @author     <NAME>
 * @license    MIT License
 * @copyright  2011 - 2012 <NAME>
 * @link       http://github.com/FrenkyNet/Monga
 */

namespace Monga;

use Mongo;
use MongoDB;
use MongoCode;
use MongoDBRef;

class Database
{
	/**
	 * @var  object  $database MongoDB instance
	 */
	protected $database;

	/**
	 * @var  object  $connection  Connection instance
	 */
	protected $connection;

	/**
	 * Constructor, sets database and Connection.
	 *
	 * @param  object  $database    MongoDB connection object
	 * @param  object  $connection  Monga\Connection object, optional
	 */
	public function __construct(MongoDB $database, Connection $connection = null)
	{
		$connection and $this->connection = $connection;

		$this->setDatabase($database);
	}

	/**
	 * Retrieve all collection names
	 *
	 * @return  array  array of collection names
	 */
	public function listCollections()
	{
		return $this->database->getCollectionNames();
	}

	/**
	 * Check whether the database contains a given connection
	 *
	 * @param   string   $collection  collection name
	 * @return  boolean  whether the collection exists in the database
	 */
	public function hasCollection($collection)
	{
		return in_array($collection, $this->listCollections());
	}

	/**
	 * Returns all collection.
	 *
	 * @param   boolean  $wrap  whether to wrap the collection instances in Collection classes.
	 * @return  array    connections array
	 */
	public function allCollections($wrap = false)
	{
		$collections = $this->database->listCollections();

		if ( ! $wrap)
		{
			return $collections;
		}

		return array_map(function($collection)
		{
			return new Collection($collection);
		},
		$collections);
	}

	/**
	 * Retrieve the connection.
	 *
	 * @return  object  Connection instance
	 */
	public function getConnection()
	{
		return $this->connection;
	}

	/**
	 * Connection injector
	 *
	 * @param  Connection  $connection  connection object
	 * @param  object      $this
	 */
	public function setConnection(Connection $connection)
	{
		$this->connection = $connection;

		return $this;
	}

	/**
	 * Database injector
	 *
	 * @param   mixed   $database  MongoDB instance, Database instance, string identifier.
	 * @return  object  $this
	 */
	public function setDatabase($database)
	{
		if ( ! $database instanceof MongoDB)
		{
			$database = $this->connection->getConnection()->{$database};
		}

		$this->database = $database;

		return $this;
	}

	/**
	 * Return the MongoDB database instance
	 *
	 * @return  object  MongoDB database instance
	 */
	public function getDatabase()
	{
		return $this->database;
	}

	/**
	 * Retrieve a collection
	 *
	 * @param   string   $collection  collection name
	 * @param   boolean  $wrap        whether to wrap it in a Collection class.
	 * @return  mixed    Collection instance or MongoCollection instance.
	 */
	public function collection($collection, $wrap = true)
	{
		$collection = $this->database->selectCollection($collection);
		$wrap and $collection = new Collection($collection);

		return $collection;
	}

	/**
	 * Retrieve a GridFS object
	 *
	 * @param   string   $prefix  collection name
	 * @param   boolean  $wrap    whether to wrap it in a Collection class.
	 * @return  mixed    Collection instance or MongoCollection instance.
	 */
	public function filesystem($prefix = 'fs', $wrap = true)
	{
		$collection = $this->database->getGridFS($prefix);
		$wrap and $collection = new Filesystem($collection);

		return $collection;
	}

	/**
	 * Drops the current database.
	 *
	 * @return  boolean  success boolean
	 */
	public function drop()
	{
		$result = $this->database->drop();

		return $result === true or (bool) $result['ok'];
	}

	/**
	 * Retrieve one or more references from the database.
	 *
	 * @param    object|array  $reference  one or more references
	 * @return   array                     one or more documents
	 */
	public function getRef($reference)
	{
		$array = ! isset($reference['$ref']);
		$reference = $array ? $reference : array($reference);
		$database = $this->database;

		$result = array_map(function ($ref) use($database)
		{
			return MongoDBRef::get($database, $ref);
		},
		$reference);

		return $array ? $result : reset($result);
	}

	/**
{'fraction_non_alphanumeric': 0.11978735971647962, 'fraction_numerical': 0.002835203780271707, 'mean_word_length': 3.218236173393124, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 47, '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': '3673766', 'n_tokens_mistral': 2701, 'n_tokens_neox': 2409, 'n_words': 732}