-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
optimize query pattern used by storage filter
Signed-off-by: Robin Appelman <robin@icewind.nl>
- Loading branch information
1 parent
ef87ff1
commit 4e08186
Showing
15 changed files
with
723 additions
and
39 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
30 changes: 30 additions & 0 deletions
30
lib/private/Files/Search/QueryOptimizer/FlattenNestedBool.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,30 @@ | ||
<?php | ||
|
||
namespace OC\Files\Search\QueryOptimizer; | ||
|
||
use OC\Files\Search\SearchBinaryOperator; | ||
use OCP\Files\Search\ISearchBinaryOperator; | ||
use OCP\Files\Search\ISearchOperator; | ||
|
||
class FlattenNestedBool extends QueryOptimizerStep { | ||
public function processOperator(ISearchOperator &$operator) { | ||
if ( | ||
$operator instanceof SearchBinaryOperator && ( | ||
$operator->getType() === ISearchBinaryOperator::OPERATOR_OR || | ||
$operator->getType() === ISearchBinaryOperator::OPERATOR_AND | ||
) | ||
) { | ||
$newArguments = []; | ||
foreach ($operator->getArguments() as $oldArgument) { | ||
if ($oldArgument instanceof SearchBinaryOperator && $oldArgument->getType() === $operator->getType()) { | ||
$newArguments = array_merge($newArguments, $oldArgument->getArguments()); | ||
} else { | ||
$newArguments[] = $oldArgument; | ||
} | ||
} | ||
$operator->setArguments($newArguments); | ||
} | ||
parent::processOperator($operator); | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
lib/private/Files/Search/QueryOptimizer/FlattenSingleArgumentBinaryOperation.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,27 @@ | ||
<?php | ||
|
||
namespace OC\Files\Search\QueryOptimizer; | ||
|
||
use OCP\Files\Search\ISearchBinaryOperator; | ||
use OCP\Files\Search\ISearchOperator; | ||
|
||
/** | ||
* replace single argument AND and OR operations with their single argument | ||
*/ | ||
class FlattenSingleArgumentBinaryOperation extends ReplacingOptimizerStep { | ||
public function processOperator(ISearchOperator &$operator): bool { | ||
parent::processOperator($operator); | ||
if ( | ||
$operator instanceof ISearchBinaryOperator && | ||
count($operator->getArguments()) === 1 && | ||
( | ||
$operator->getType() === ISearchBinaryOperator::OPERATOR_OR || | ||
$operator->getType() === ISearchBinaryOperator::OPERATOR_AND | ||
) | ||
) { | ||
$operator = $operator->getArguments()[0]; | ||
return true; | ||
} | ||
return false; | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
lib/private/Files/Search/QueryOptimizer/MergeDistributiveOperations.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,96 @@ | ||
<?php | ||
|
||
namespace OC\Files\Search\QueryOptimizer; | ||
|
||
use OC\Files\Search\SearchBinaryOperator; | ||
use OC\Files\Search\SearchComparison; | ||
use OCP\Files\Search\ISearchBinaryOperator; | ||
use OCP\Files\Search\ISearchOperator; | ||
|
||
/** | ||
* Attempt to transform | ||
* | ||
* (A AND B) OR (A AND C) into A AND (B OR C) | ||
*/ | ||
class MergeDistributiveOperations extends ReplacingOptimizerStep { | ||
public function processOperator(ISearchOperator &$operator): bool { | ||
if ( | ||
$operator instanceof SearchBinaryOperator && | ||
$this->isAllSameBinaryOperation($operator->getArguments()) | ||
) { | ||
$topLevelType = $operator->getType(); | ||
|
||
$groups = $this->groupBinaryOperatorsByChild($operator->getArguments(), 0); | ||
$outerOperations = array_map(function (array $operators) use ($topLevelType) { | ||
/** @var ISearchBinaryOperator $firstArgument */ | ||
$firstArgument = $operators[0]; | ||
$outerType = $firstArgument->getType(); | ||
$extractedLeftHand = $firstArgument->getArguments()[0]; | ||
|
||
$rightHandArguments = array_map(function (ISearchOperator $inner) { | ||
/** @var ISearchBinaryOperator $inner */ | ||
$arguments = $inner->getArguments(); | ||
array_shift($arguments); | ||
if (count($arguments) === 1) { | ||
return $arguments[0]; | ||
} | ||
return new SearchBinaryOperator($inner->getType(), $arguments); | ||
}, $operators); | ||
$extractedRightHand = new SearchBinaryOperator($topLevelType, $rightHandArguments); | ||
return new SearchBinaryOperator( | ||
$outerType, | ||
[$extractedLeftHand, $extractedRightHand] | ||
); | ||
}, $groups); | ||
$operator = new SearchBinaryOperator($topLevelType, $outerOperations); | ||
parent::processOperator($operator); | ||
return true; | ||
} | ||
return parent::processOperator($operator); | ||
} | ||
|
||
/** | ||
* Check that a list of operators is all the same type of (non-empty) binary operators | ||
* | ||
* @param ISearchOperator[] $operators | ||
* @return bool | ||
* @psalm-assert-if-true SearchBinaryOperator[] $operators | ||
*/ | ||
private function isAllSameBinaryOperation(array $operators): bool { | ||
$operation = null; | ||
foreach ($operators as $operator) { | ||
if (!$operator instanceof SearchBinaryOperator) { | ||
return false; | ||
} | ||
if (!$operator->getArguments()) { | ||
return false; | ||
} | ||
if ($operation === null) { | ||
$operation = $operator->getType(); | ||
} else { | ||
if ($operation !== $operator->getType()) { | ||
return false; | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* Group a list of binary search operators that have a common argument | ||
* | ||
* @param SearchBinaryOperator[] $operators | ||
* @return SearchBinaryOperator[][] | ||
*/ | ||
private function groupBinaryOperatorsByChild(array $operators, int $index): array { | ||
$result = []; | ||
foreach ($operators as $operator) { | ||
/** @var SearchBinaryOperator|SearchComparison $child */ | ||
$child = $operator->getArguments()[0]; | ||
; | ||
$childKey = (string) $child; | ||
$result[$childKey][] = $operator; | ||
} | ||
return array_values($result); | ||
} | ||
} |
Oops, something went wrong.