-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from arthurdarcet/bulk
Bulk operations
- Loading branch information
Showing
5 changed files
with
258 additions
and
0 deletions.
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
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
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,89 @@ | ||
<?php | ||
namespace Mongovel; | ||
|
||
use MongoCollection; | ||
use MongoInsertBatch; | ||
use MongoUpdateBatch; | ||
use MongoDeleteBatch; | ||
|
||
|
||
class Bulk | ||
{ | ||
public function __construct(MongoCollection $collection, array $options = array()) | ||
{ | ||
$this->bulks = array( | ||
// operation => [hasOne, phpBatch] | ||
'insert' => [false, new MongoInsertBatch($collection, $options)], | ||
'update' => [false, new MongoUpdateBatch($collection, $options)], | ||
'remove' => [false, new MongoDeleteBatch($collection, $options)], | ||
); | ||
} | ||
|
||
public function find($query) | ||
{ | ||
return new BulkOperation($this, $query); | ||
} | ||
|
||
public function insert($doc) | ||
{ | ||
return $this->_add('insert', $doc); | ||
} | ||
|
||
public function _add($operation, $arg) | ||
{ | ||
$this->bulks[$operation][0] = true; | ||
$this->bulks[$operation][1]->add($arg); | ||
} | ||
|
||
public function execute() | ||
{ | ||
foreach ($this->bulks as $b) { | ||
if ($b[0]) { | ||
$b[1]->execute(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
class BulkOperation | ||
{ | ||
public function __construct(Bulk $bulk, array $query) | ||
{ | ||
$this->bulk = $bulk; | ||
$this->call = array('q' => $query); | ||
} | ||
|
||
public function remove($limit = 0) | ||
{ | ||
if (!isset($this->call['limit'])) { | ||
$this->call['limit'] = $limit; | ||
} | ||
return $this->bulk->_add('remove', $this->call); | ||
} | ||
|
||
public function removeOne() | ||
{ | ||
return $this->remove(1); | ||
} | ||
|
||
public function update($u) | ||
{ | ||
$this->call['multi'] = true; | ||
$this->call['u'] = $u; | ||
return $this->bulk->_add('update', $this->call); | ||
} | ||
|
||
public function updateOne($u) | ||
{ | ||
$this->call['multi'] = false; | ||
$this->call['u'] = $u; | ||
return $this->bulk->_add('update', $this->call); | ||
} | ||
|
||
public function upsert() | ||
{ | ||
$this->call['upsert'] = true; | ||
return $this; | ||
} | ||
|
||
} |
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
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,123 @@ | ||
<?php | ||
|
||
require_once '_start.php'; | ||
|
||
class BulkTest extends MongovelTests | ||
{ | ||
|
||
public function testCanBulkInsert() | ||
{ | ||
$bulk = Book::initializeUnorderedBulkOp(); | ||
|
||
$bulk->insert(array('key' => "A")); | ||
$bulk->insert(array('key' => "B")); | ||
$bulk->execute(); | ||
|
||
$this->assert(array("A", "B")); | ||
} | ||
|
||
public function testCanBulkUpdateMultiple() | ||
{ | ||
$bulk = Book::initializeUnorderedBulkOp(); | ||
static::insertFixture(); | ||
|
||
$bulk->find(array('key' => "B")) | ||
->update(array('$set' => array('key' => "C"))); | ||
$bulk->execute(); | ||
|
||
$this->assert(array("A", "C", "C")); | ||
} | ||
|
||
public function testCanBulkUpdateOne() | ||
{ | ||
$bulk = Book::initializeUnorderedBulkOp(); | ||
static::insertFixture(); | ||
|
||
$bulk->find(array('key' => "B")) | ||
->updateOne(array('$set' => array('key' => "C"))); | ||
$bulk->execute(); | ||
|
||
$this->assert(array("A", "C", "B")); | ||
} | ||
|
||
public function testCanBulkUpsert() | ||
{ | ||
$bulk = Book::initializeUnorderedBulkOp(); | ||
static::insertFixture(); | ||
|
||
$bulk->find(array('missing' => "attr")) | ||
->upsert() | ||
->updateOne(array('$set' => array('key' => "C"))); | ||
$bulk->execute(); | ||
|
||
$this->assert(array("A", "B", "B", "C")); | ||
} | ||
|
||
public function testCanBulkRemove() | ||
{ | ||
$bulk = Book::initializeUnorderedBulkOp(); | ||
static::insertFixture(); | ||
|
||
$bulk->find(array('key' => "B")) | ||
->remove(); | ||
$bulk->execute(); | ||
|
||
$this->assert(array("A")); | ||
} | ||
|
||
public function testCanBulkRemoveOne() | ||
{ | ||
$bulk = Book::initializeUnorderedBulkOp(); | ||
static::insertFixture(); | ||
|
||
$bulk->find(array('key' => "B")) | ||
->removeOne(); | ||
$bulk->execute(); | ||
|
||
$this->assert(array("A", "B")); | ||
} | ||
|
||
public function testOrderedBulk() | ||
{ | ||
$bulk = Book::initializeOrderedBulkOp(); | ||
static::insertFixture(); | ||
|
||
$bulk->find(array('key' => "B")) | ||
->update(array('$set' => array('key' => "C"))); | ||
$bulk->find(array('key' => "A")) | ||
->update(array('$set' => array('key' => "B"))); | ||
$bulk->execute(); | ||
|
||
$this->assert(array("B", "C", "C")); | ||
} | ||
|
||
public function assert($vals) | ||
{ | ||
$res = array_pluck(iterator_to_array(Book::find()), 'key'); | ||
$this->assertEquals($vals, array_values($res)); | ||
} | ||
|
||
////////////////////////////////////////////////////////// | ||
//////////////////////// Fixtures //////////////////////// | ||
////////////////////////////////////////////////////////// | ||
|
||
public static function insertFixture() | ||
{ | ||
self::$db->db()->books->batchInsert(array( | ||
array('key' => "A"), | ||
array('key' => "B"), | ||
array('key' => "B"), | ||
)); | ||
} | ||
|
||
|
||
////////////////////////////////////////////////////////// | ||
//////// Drop database before and after each test //////// | ||
////////////////////////////////////////////////////////// | ||
|
||
protected function setUp() | ||
{ | ||
self::$db->db()->drop(); | ||
} | ||
|
||
} |