Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Various optimizations and removed Enum value type

  • Loading branch information...
commit bb21a67f0b26d6ae0d442a6391f1089794746b33 1 parent e861c0e
@marcqualie authored
View
3  .gitignore
@@ -1,3 +1,4 @@
composer.lock
phpunit.xml
-vendor
+vendor
+tests/Benchmark
View
19 lib/MongoMinify/Collection.php
@@ -10,6 +10,8 @@ class Collection {
public $native;
public $schema = array();
+ public $schema_raw = array();
+ public $schema_index = array();
public $schema_reverse_index = array();
@@ -27,18 +29,6 @@ public function __construct($name, $db)
/**
- * 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]
@@ -112,11 +102,11 @@ public function setSchema(array $schema = array())
{
$this->schema = array();
$this->schema_raw = $schema;
+ $this->schema_index = array();
$this->schema_reverse_index = array();
$this->setSchemaArray($schema);
-// print_r($this->schema_reverse_index);
}
- private function setSchemaArray(Array $array, $namespace = null)
+ private function setSchemaArray(array $array, $namespace = null)
{
foreach ($array as $key => $value)
{
@@ -130,6 +120,7 @@ private function setSchemaArray(Array $array, $namespace = null)
if (isset($value['short']))
{
$parent_short = $this->getShort($namespace) ? $this->getShort($namespace) . '.' : '';
+ $this->schema_index[$subkey] = $value['short'];
$this->schema_reverse_index[$parent_short . $value['short']] = $key;
}
}
View
9 lib/MongoMinify/Cursor.php
@@ -40,6 +40,15 @@ public function current()
/**
+ * Counting results
+ */
+ public function count()
+ {
+ return $this->native->count();
+ }
+
+
+ /**
* Data retreival
*/
public function sort(array $fields = array())
View
10 lib/MongoMinify/Db.php
@@ -8,6 +8,8 @@ class Db {
public $client;
public $native;
+ public $collection_cache = array();
+
public function __construct($name, $client)
{
$this->name = $name;
@@ -30,8 +32,12 @@ public function __get($name)
*/
public function selectCollection($name)
{
- $collection = new Collection($name, $this);
- return $collection;
+ if ( ! isset($this->collection_cache[$name]))
+ {
+ $collection = new Collection($name, $this);
+ $collection_cache[$name] = $collection;
+ }
+ return $collection_cache[$name];
}
View
33 lib/MongoMinify/Document.php
@@ -9,23 +9,22 @@ class Document {
public $compressed = array();
protected $collection = array();
- public function __construct(Array &$data = array(), &$collection = null)
+ public function __construct(array $data = array(), $collection = null)
{
$this->collection = $collection;
$this->data = $data;
- $this->compressed = $data;
}
/**
* Save
*/
- public function save(Array $options = array())
+ public function save(array $options = array())
{
$this->compress();
$this->collection->native->save($this->compressed, $options);
$this->data['_id'] = $this->compressed['_id'];
}
- public function insert(Array $options = array())
+ public function insert(array $options = array())
{
$this->compress();
$this->collection->native->insert($this->compressed, $options);
@@ -37,6 +36,11 @@ public function insert(Array $options = array())
*/
public function compress()
{
+ if ( ! $this->collection->schema)
+ {
+ $this->compressed =& $this->data;
+ return;
+ }
if ($this->state !== 'compressed' && $this->collection)
{
$this->compressed = $this->applyCompression($this->data);
@@ -75,28 +79,12 @@ private function applyCompression($document, $parent = null)
$doc = array();
foreach ($document as $key => $value)
{
- $namespace = ($parent ? $parent . '.' : ''). $key;
+ $namespace = ($parent ? $parent . '.' : '') . $key;
if (is_array($value))
{
$value = $this->applyCompression($value, $namespace);
}
- else
- {
- $values = isset($this->collection->schema[$namespace]['values']) ? $this->collection->schema[$namespace]['values'] : false;
- if ($values)
- {
- $position = array_search($value, $values);
- if ($position !== false)
- {
- $value = $position;
- }
- }
- }
- $short = isset($this->collection->schema[$namespace]['short']) ? $this->collection->schema[$namespace]['short'] : $key;
- if ($this->collection->client->debug)
- {
- echo $namespace .'.[ ' . "\033[0;31m" . $key . "\033[0m" . ' > ' . "\033[0;32m" . $short . "\033[0m ]" . PHP_EOL;
- }
+ $short = isset($this->collection->schema_index[$namespace]) ? $this->collection->schema_index[$namespace] : $key;
$doc[$short] = $value;
}
return $doc;
@@ -131,7 +119,6 @@ private function applyDecompression($document, $parent = null)
foreach ($document as $key => $value)
{
$namespace = ($parent ? $parent . '.' : '') . $key;
-// echo $namespace . PHP_EOL;
if (isset($this->collection->schema_reverse_index[$namespace]))
{
if (is_array($value))
View
3  tests/MongoMinify/Tests/IndexTest.php
@@ -11,7 +11,6 @@ public function testEnsureSingle()
// Create a collection
$collection = $this->getTestCollection();
-
$collection->ensureIndex(array('user_id' => 1));
$indexes = $collection->getIndexInfo();
@@ -28,14 +27,12 @@ public function testEnsureEmbedded()
// Create a collection
$collection = $this->getTestCollection();
-
$collection->ensureIndex(array('user_id' => 1, 'tags.slug' => 1));
$indexes = $collection->getIndexInfo();
$this->assertArrayHasKey(1, $indexes);
$this->assertEquals($indexes[1]['name'], 'u_1_t_s_1');
-
}
View
9 tests/bootstrap.php
@@ -10,7 +10,14 @@ public function setUp()
{
// Connecto to MongoDB
- $this->client = new MongoMinify\Client('mongodb://localhost:27017');
+ try {
+ $this->client = new MongoMinify\Client('mongodb://localhost:27017');
+ }
+ catch (MongoConnectionException $e)
+ {
+ throw new Exception('Could not connect to MongoDB. Tests are unable to run');
+ exit;
+ }
$this->client->schema_dir = __DIR__ . '/Schema';
}
Please sign in to comment.
Something went wrong with that request. Please try again.