Skip to content
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

Preserve order of operations #620

Open
wants to merge 4 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
Binary file not shown.
4 changes: 3 additions & 1 deletion lib/Doctrine/ODM/PHPCR/DocumentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,9 @@ public function findMany($className, array $ids)
}

$nodes = $this->session->getNodes($ids);
$hints = array('fallback' => true);
$hints = array(
'fallback' => true,
);
$documents = $this->unitOfWork->getOrCreateDocuments($className, $nodes, $hints);

return new ArrayCollection($documents);
Expand Down
104 changes: 104 additions & 0 deletions lib/Doctrine/ODM/PHPCR/Queue/TreeOperation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?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';
const OP_REMOVE = 'remove';
const OP_INSERT = 'insert';

private $oid;
private $type;
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;
}
}
75 changes: 75 additions & 0 deletions lib/Doctrine/ODM/PHPCR/Queue/TreeOperationBatch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?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;
}
}
169 changes: 169 additions & 0 deletions lib/Doctrine/ODM/PHPCR/Queue/TreeOperationQueue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?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()
{
$type = null;
$batches = array();

foreach ($this->queue as $operation) {
if (false === $operation->isValid()) {
continue;
}

if ($operation->getType() !== $type) {
$batch = new TreeOperationBatch($operation->getType());
$type = $operation->getType();
$batches[] = $batch;
}

$batch->schedule(
$operation->getOid(),
$operation->getArgs()
);
}

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();

foreach ($this->queue as $operation) {
if (false === $operation->isValid()) {
continue;
}

if ($type !== $operation->getType()) {
continue;
}

$schedule[$operation->getOid()] = $operation->getArgs();
}

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) {
if (false === $operation->isValid()) {
continue;
}

if ($operation->getType() !== $type) {
continue;
}

if ($oid == $operation->getOid()) {
return true;
}
}

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) {
if ($operation->getType() !== $type) {
continue;
}

if ($operation->getOid() !== $oid) {
continue;
}

$operation->invalidate();
}
}

/**
* Remove all references to the given spl object ID
*
* @param $oid string
*/
public function unregister($oid)
{
foreach ($this->queue as $operation) {
if ($operation->getOid() == $oid) {
$operation->invalidate();
}
}
}
}
Loading