Skip to content

Commit

Permalink
Options for shard key could be specified in XML attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
clslrns committed Oct 18, 2015
1 parent ae0c634 commit 6bc01fd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
2 changes: 2 additions & 0 deletions doctrine-mongo-mapping.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@
<xs:element name="key" type="odm:shard-key-key" minOccurs="1" maxOccurs="unbounded"/>
<xs:element name="option" type="odm:shard-key-option" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="unique" type="xs:boolean"/>
<xs:attribute name="numInitialChunks" type="xs:integer"/>
</xs:complexType>

<xs:complexType name="shard-key-key">
Expand Down
20 changes: 12 additions & 8 deletions lib/Doctrine/ODM/MongoDB/Mapping/Driver/XmlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,20 +386,24 @@ private function addIndex(ClassMetadataInfo $class, \SimpleXmlElement $xmlIndex)

private function setShardKey(ClassMetadataInfo $class, \SimpleXmlElement $xmlShardkey)
{
$attributes = $xmlShardkey->attributes();

$keys = array();
$options = array();
foreach ($xmlShardkey->{'key'} as $key) {
$keys[(string) $key['name']] = isset($key['order']) ? (string)$key['order'] : 'asc';
}

$options = array();
if (isset($attributes['unique'])) {
$options['unique'] = ('true' === (string) $attributes['unique']);
}

if (isset($attributes['numInitialChunks'])) {
$options['numInitialChunks'] = (int) $attributes['numInitialChunks'];
}

if (isset($xmlShardkey->{'option'})) {
$allowed = array('unique', 'numInitialChunks');
foreach ($xmlShardkey->{'option'} as $option) {
$name = (string) $option['name'];
if ( ! in_array($name, $allowed, true)) {
continue;
}

$value = (string) $option['value'];
if ($value === 'true') {
$value = true;
Expand All @@ -408,7 +412,7 @@ private function setShardKey(ClassMetadataInfo $class, \SimpleXmlElement $xmlSha
} elseif (is_numeric($value)) {
$value = preg_match('/^[-]?\d+$/', $value) ? (integer) $value : (float) $value;
}
$options[$name] = $value;
$options[(string) $option['name']] = $value;
}
}

Expand Down
18 changes: 18 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/Mapping/XmlMappingDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Doctrine\ODM\MongoDB\Tests\Mapping;

use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo;
use Doctrine\ODM\MongoDB\Mapping\Driver\XmlDriver;

class XmlMappingDriverTest extends AbstractMappingDriverTest
Expand All @@ -10,4 +11,21 @@ protected function _loadDriver()
{
return new XmlDriver(__DIR__ . DIRECTORY_SEPARATOR . 'xml');
}

public function testSetShardKeyOptionsByAttributes()
{
$class = new ClassMetadataInfo('doc');
$driver = $this->_loadDriver();
$element = new \SimpleXmlElement('<shard-key unique="true" numInitialChunks="4096"><key name="_id"/></shard-key>');

/** @uses XmlDriver::setShardKey */
$m = new \ReflectionMethod(get_class($driver), 'setShardKey');
$m->setAccessible(true);
$m->invoke($driver, $class, $element);

$this->assertTrue($class->isSharded());
$shardKey = $class->getShardKey();
$this->assertSame(array('unique' => true, 'numInitialChunks' => 4096), $shardKey['options']);
$this->assertSame(array('_id' => 1), $shardKey['keys']);
}
}

0 comments on commit 6bc01fd

Please sign in to comment.