This repository has been archived by the owner on Feb 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
Add mongodb storage #4
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
<?php | ||
/* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
* This software consists of voluntary contributions made by many individuals | ||
* and is licensed under the MIT license. For more information, see | ||
* <http://www.doctrine-project.org>. | ||
*/ | ||
|
||
namespace Doctrine\KeyValueStore\Storage; | ||
|
||
use Doctrine\KeyValueStore\NotFoundException; | ||
|
||
/** | ||
* MongoDb storage | ||
* | ||
* @author Markus Bachmann <markus.bachmann@bachi.biz> | ||
*/ | ||
class MongoDbStorage implements Storage | ||
{ | ||
/** | ||
* @var \Mongo | ||
*/ | ||
protected $mongo; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $dbOptions; | ||
|
||
/** | ||
* @var \MongoCollection | ||
*/ | ||
protected $collection; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param \Mongo $mongo | ||
* @param array $dbOptions | ||
*/ | ||
public function __construct(\Mongo $mongo, array $dbOptions = array()) | ||
{ | ||
$this->mongo = $mongo; | ||
$this->dbOptions = array_merge(array( | ||
'database' => '', | ||
'collection' => '', | ||
), $dbOptions); | ||
} | ||
|
||
/** | ||
* Initialize the mongodb collection | ||
* | ||
* @throws \RuntimeException | ||
*/ | ||
public function initialize() | ||
{ | ||
if (null !== $this->collection) { | ||
return; | ||
} | ||
|
||
if (empty($this->dbOptions['database'])) { | ||
throw new \RuntimeException('The option "database" must be set'); | ||
} | ||
if (empty($this->dbOptions['collection'])) { | ||
throw new \RuntimeException('The option "collection" must be set'); | ||
} | ||
|
||
$this->collection = $this->mongo->selectDB($this->dbOptions['database'])->selectCollection($this->dbOptions['collection']); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function supportsPartialUpdates() | ||
{ | ||
return false; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function supportsCompositePrimaryKeys() | ||
{ | ||
return false; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function requiresCompositePrimaryKeys() | ||
{ | ||
return false; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function insert($storageName, $key, array $data) | ||
{ | ||
$this->initialize(); | ||
|
||
$value = array( | ||
'key' => $key, | ||
'value' => $data, | ||
); | ||
|
||
$this->collection->insert($value); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function update($storageName, $key, array $data) | ||
{ | ||
$this->initialize(); | ||
|
||
$value = array( | ||
'key' => $key, | ||
'value' => $data, | ||
); | ||
|
||
$this->collection->update(array('key' => $key), $value); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function delete($storageName, $key) | ||
{ | ||
$this->initialize(); | ||
|
||
$this->collection->remove(array('key' => $key)); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function find($storageName, $key) | ||
{ | ||
$this->initialize(); | ||
|
||
$value = $this->collection->findOne(array('key' => $key), array('value')); | ||
|
||
if ($value) { | ||
return $value['value']; | ||
} | ||
|
||
throw new NotFoundException(); | ||
} | ||
|
||
/** | ||
* Return a name of the underlying storage. | ||
* | ||
* @return string | ||
*/ | ||
public function getName() | ||
{ | ||
return 'mongodb'; | ||
} | ||
} |
140 changes: 140 additions & 0 deletions
140
tests/Doctrine/Tests/KeyValueStore/Storage/MongoDbStorageTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
<?php | ||
|
||
namespace Doctrine\Tests\KeyValueStore\Storage; | ||
|
||
use Doctrine\KeyValueStore\Storage\MongoDbStorage; | ||
|
||
/** | ||
* MongoDb storage testcase | ||
* | ||
* @author Markus Bachmann <markus.bachmann@bachi.biz> | ||
*/ | ||
class MongoDbStorageTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
protected function setUp() | ||
{ | ||
$this->mongo = $this->getMock('\Mongo'); | ||
|
||
$this->mongodb = $this->getMockBuilder('\MongoDB')->disableOriginalConstructor()->getMock(); | ||
|
||
$this->mongo->expects($this->any()) | ||
->method('selectDB') | ||
->will($this->returnValue($this->mongodb)); | ||
|
||
$this->collection = $this->getMockBuilder('MongoCollection')->disableOriginalConstructor()->getMock(); | ||
|
||
$this->mongodb->expects($this->once()) | ||
->method('selectCollection') | ||
->will($this->returnValue($this->collection)); | ||
|
||
$this->storage = new MongoDbStorage($this->mongo, array( | ||
'collection' => 'test', | ||
'database' => 'test' | ||
)); | ||
} | ||
|
||
public function testInsert() | ||
{ | ||
$data = array( | ||
'author' => 'John Doe', | ||
'title' => 'example book', | ||
); | ||
|
||
$dbDataset = array(); | ||
|
||
$this->collection->expects($this->once()) | ||
->method('insert') | ||
->will($this->returnCallback(function($data) use (&$dbDataset) { | ||
$dbDataset[] = $data; | ||
})); | ||
|
||
$this->storage->insert('mongodb', '1', $data); | ||
$this->assertCount(1, $dbDataset); | ||
|
||
$this->assertEquals(array(array('key' => '1', 'value' => $data)), $dbDataset); | ||
} | ||
|
||
public function testUpdate() | ||
{ | ||
$data = array( | ||
'author' => 'John Doe', | ||
'title' => 'example book', | ||
); | ||
|
||
$dbDataset = array(); | ||
|
||
$this->collection->expects($this->once()) | ||
->method('update') | ||
->will($this->returnCallback(function($citeria, $data) use (&$dbDataset) { | ||
$dbDataset = array($citeria, $data); | ||
})); | ||
|
||
$this->storage->update('mongodb', '1', $data); | ||
|
||
$this->assertEquals(array('key' => '1'), $dbDataset[0]); | ||
$this->assertEquals(array('key' => '1', 'value' => $data), $dbDataset[1]); | ||
} | ||
|
||
public function testDelete() | ||
{ | ||
$dataset = array( | ||
array( | ||
'key' => 'foobar', | ||
'value' => array( | ||
'author' => 'John Doe', | ||
'title' => 'example book', | ||
), | ||
), | ||
); | ||
|
||
$this->collection->expects($this->once()) | ||
->method('remove') | ||
->will($this->returnCallback(function($citeria) use (&$dataset) { | ||
foreach ($dataset as $key => $row) { | ||
if ($row['key'] === $citeria['key']) { | ||
unset($dataset[$key]); | ||
} | ||
} | ||
} | ||
)); | ||
|
||
$this->storage->delete('test', 'foobar'); | ||
|
||
$this->assertCount(0, $dataset); | ||
} | ||
|
||
public function testFind() | ||
{ | ||
$dataset = array( | ||
array( | ||
'key' => 'foobar', | ||
'value' => array( | ||
'author' => 'John Doe', | ||
'title' => 'example book', | ||
), | ||
), | ||
); | ||
|
||
$this->collection->expects($this->once()) | ||
->method('findOne') | ||
->will($this->returnCallback(function($citeria, $fields) use (&$dataset) { | ||
foreach ($dataset as $key => $row) { | ||
if ($row['key'] === $citeria['key']) { | ||
return $row; | ||
} | ||
} | ||
} | ||
)); | ||
|
||
$data = $this->storage->find('test', 'foobar'); | ||
|
||
$this->assertEquals($dataset[0]['value'], $data); | ||
} | ||
|
||
public function testGetName() | ||
{ | ||
$this->storage->initialize(); | ||
|
||
$this->assertEquals('mongodb', $this->storage->getName()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The license header is missing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I know.
Which licence is used, MIT or LGPL?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
currently, it is still LGPL. but as Doctrine is switching, we need to know if beberlei wants to change before or after merging :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree.
I wait of @beberlei answer :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MIT
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done :)