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

optimise DbNestedSetStorage::findParent #36

Open
wants to merge 1 commit into
base: 1.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
14 changes: 10 additions & 4 deletions src/Storage/DbalNestedSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,17 @@ public function findRoot(NodeKey $nodeKey) {
* {@inheritdoc}
*/
public function findParent(NodeKey $nodeKey) {
$ancestors = $this->findAncestors($nodeKey);
if (count($ancestors) > 1) {
// Parent is 2nd-last element.
return $ancestors[count($ancestors) - 2];
// Only selecting the closest ancestor node in the tree.
$stmt = $this->connection->executeQuery('SELECT parent.id, parent.revision_id, parent.left_pos, parent.right_pos, parent.depth FROM ' . $this->tableName . ' AS child, ' . $this->tableName . ' AS parent WHERE child.left_pos > parent.left_pos AND child.right_pos < parent.right_pos AND child.id = ? AND child.revision_id = ? ORDER BY parent.left_pos DESC LIMIT 1',
[$nodeKey->getId(), $nodeKey->getRevisionId()]
);
$row = $stmt->fetch();

if ($row) {
// There is a parent.
return new Node(new NodeKey($row['id'], $row['revision_id']), $row['left_pos'], $row['right_pos'], $row['depth']);
}
// No parent.
return NULL;
}

Expand Down
27 changes: 26 additions & 1 deletion tests/Functional/DbalNestedSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,40 @@ public function testFindAncestors() {
* Tests finding the parent node.
*/
public function testFindParent() {
// Node 2 is child of Node 1 (root).
$nodeKey = new NodeKey(2, 1);
$parent = $this->nestedSet->findParent($nodeKey);

$nodeKey = new NodeKey(7, 1);
$this->assertEquals(1, $parent->getId());
$this->assertEquals(1, $parent->getRevisionId());
$this->assertEquals(1, $parent->getLeft());
$this->assertEquals(22, $parent->getRight());
$this->assertEquals(0, $parent->getDepth());

// Node 7 is child of Node 3.
$nodeKey = new NodeKey(7, 1);
$parent = $this->nestedSet->findParent($nodeKey);

$this->assertEquals(3, $parent->getId());
$this->assertEquals(1, $parent->getRevisionId());
$this->assertEquals(10, $parent->getLeft());
$this->assertEquals(21, $parent->getRight());
$this->assertEquals(1, $parent->getDepth());

// Node 6 (leaf) is child of Node 4.
$nodeKey = new NodeKey(6, 1);
$parent = $this->nestedSet->findParent($nodeKey);

$this->assertEquals(4, $parent->getId());
$this->assertEquals(1, $parent->getRevisionId());
$this->assertEquals(3, $parent->getLeft());
$this->assertEquals(8, $parent->getRight());
$this->assertEquals(2, $parent->getDepth());

// Node 1 (Root) has no parent.
$root = new NodeKey(1, 1);
$rootParent = $this->nestedSet->findParent($root);
$this->assertNull($rootParent);

}

Expand Down