Skip to content

Preserve order of operations #620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: 2.0.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added tests
  • Loading branch information
dantleech committed Mar 25, 2015
commit 5d78fd7f35eb12fcfa4a5fdcde13c94d141c7593
57 changes: 57 additions & 0 deletions lib/Doctrine/ODM/PHPCR/Queue/TreeOperation.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
<?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\ODM\PHPCR\Queue;

/**
* Represents a horizontal tree operation, encompassing
* MOVE, REMOVE and INSERT operations.
*
* @author Daniel Leech <[email protected]>
*/
class TreeOperation
{
const OP_MOVE = 'move';
Expand All @@ -13,33 +36,67 @@ class TreeOperation
private $args;
private $valid = true;

/**
* @param mixed $type
* @param mixed $oid
* @param mixed $args
*/
public function __construct($type, $oid, $args)
{
$this->oid = $oid;
$this->type = $type;
$this->args = $args;
}

/**
* Return the SPL object ID which maps
* to the document on which the operaton should be
* performed.
*
* @return string
*/
public function getOid()
{
return $this->oid;
}

/**
* Return the type of operation, one of
* the self::OP_* constants.
*
* @return string
*/
public function getType()
{
return $this->type;
}

/**
* Return the arguments for the operation
*
* @return mixed
*/
public function getArgs()
{
return $this->args;
}

/**
* Invalidate this item so that it will not be processed.
* Invalidation is quicker than removing the item from the queue.
*
* @param boolean
*/
public function invalidate()
{
$this->valid = false;
}

/**
* Return true if this item should be processed
*
* @return boolean
*/
public function isValid()
{
return $this->valid;
Expand Down
46 changes: 46 additions & 0 deletions lib/Doctrine/ODM/PHPCR/Queue/TreeOperationBatch.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,73 @@
<?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\ODM\PHPCR\Queue;

/**
* Represents a batch of operations of the same type
*
* @author Daniel Leech <[email protected]>
*/
class TreeOperationBatch
{
private $type;
private $schedule = array();

/**
* @param mixed $type
*/
public function __construct($type)
{
$this->type = $type;
}

/**
* Return the operation schedule, e.g.
*
* array(
* $oid => $args
* )
*
* @return array
*/
public function getSchedule()
{
return $this->schedule;
}

/**
* Schedule an operation
*
* @param string $oid
* @param array $args
*/
public function schedule($oid, $args)
{
$this->schedule[$oid] = $args;
}

/**
* Return the operation type, one of the
* TreeOperation::OP_* constants.
*
* @return string
*/
public function getType()
{
return $this->type;
Expand Down
58 changes: 58 additions & 0 deletions lib/Doctrine/ODM/PHPCR/Queue/TreeOperationQueue.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,50 @@
<?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\ODM\PHPCR\Queue;

use Doctrine\ODM\PHPCR\Queue\TreeOperation;
use Doctrine\ODM\PHPCR\Queue\TreeOperationBatch;

/**
* The tree operation queue
*
* @author Daniel Leech <[email protected]>
*/
class TreeOperationQueue
{
private $queue = array();

/**
* Push a new operation onto the queue
*
* @param TreeOperation $operation
*/
public function push(TreeOperation $operation)
{
$this->queue[] = $operation;
}

/**
* Partition into contiguous sets of operations
*
* @return TreeOperationBatch[]
*/
public function getBatches()
{
Expand All @@ -42,11 +71,21 @@ public function getBatches()
return $batches;
}

/**
* Clear the queue
*/
public function clear()
{
$this->queue = array();
}

/**
* Return the entire schedule for the given operation type
*
* @param string $type
*
* @return array
*/
public function getSchedule($type)
{
$schedule = array();
Expand All @@ -62,6 +101,14 @@ public function getSchedule($type)
return $schedule;
}

/**
* Return true if the spl object id is scheduled for the given type
*
* @param string $type
* @param string $oid
*
* @return boolean
*/
public function isQueued($type, $oid)
{
foreach ($this->queue as $operation) {
Expand All @@ -81,6 +128,12 @@ public function isQueued($type, $oid)
return false;
}

/**
* Remove all operations with the given spl object ID and type
*
* @param string $type
* @param string $oid
*/
public function unqueue($type, $oid)
{
foreach ($this->queue as $operation) {
Expand All @@ -96,6 +149,11 @@ public function unqueue($type, $oid)
}
}

/**
* Remove all references to the given spl object ID
*
* @param $oid string
*/
public function unregister($oid)
{
foreach ($this->queue as $operation) {
Expand Down
1 change: 1 addition & 0 deletions lib/Doctrine/ODM/PHPCR/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -3035,6 +3035,7 @@ private function unregisterDocument($document)
}

$this->treeOpQueue->unregister($oid);

unset(
$this->scheduledUpdates[$oid],
$this->scheduledReorders[$oid],
Expand Down
33 changes: 33 additions & 0 deletions tests/Doctrine/Tests/ODM/PHPCR/Queue/TreeOperationBatchTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Doctrine\Tests\ODM\PHPCR\Queue;

use Doctrine\ODM\PHPCR\Queue\TreeOperationBatch;
use Doctrine\ODM\PHPCR\Queue\TreeOperation;

class TreeOperationBatchTest extends \PHPUnit_Framework_Testcase
{
private $batch;

public function setUp()
{
$this->batch = new TreeOperationBatch(TreeOperation::OP_MOVE);
}

public function testGetters()
{
$this->assertEquals(TreeOperation::OP_MOVE, $this->batch->getType());
}

public function testSchedule()
{
$this->batch->schedule('1234', 'arg');
$this->batch->schedule('4321', 'arg');

$this->assertEquals(array(
'1234' => 'arg',
'4321' => 'arg',
), $this->batch->getSchedule());
}
}

Loading