@@ -27,31 +27,31 @@ |
||
| 27 | 27 | use OCP\Files\Search\ISearchOperator; |
| 28 | 28 | |
| 29 | 29 | class QueryOptimizerStep { |
| 30 | - /** |
|
| 31 | - * Allow optimizer steps to inspect the entire query before starting processing |
|
| 32 | - * |
|
| 33 | - * @param ISearchOperator $operator |
|
| 34 | - * @return void |
|
| 35 | - */ |
|
| 36 | - public function inspectOperator(ISearchOperator $operator): void { |
|
| 37 | - if ($operator instanceof ISearchBinaryOperator) { |
|
| 38 | - foreach ($operator->getArguments() as $argument) { |
|
| 39 | - $this->inspectOperator($argument); |
|
| 40 | - } |
|
| 41 | - } |
|
| 42 | - } |
|
| 30 | + /** |
|
| 31 | + * Allow optimizer steps to inspect the entire query before starting processing |
|
| 32 | + * |
|
| 33 | + * @param ISearchOperator $operator |
|
| 34 | + * @return void |
|
| 35 | + */ |
|
| 36 | + public function inspectOperator(ISearchOperator $operator): void { |
|
| 37 | + if ($operator instanceof ISearchBinaryOperator) { |
|
| 38 | + foreach ($operator->getArguments() as $argument) { |
|
| 39 | + $this->inspectOperator($argument); |
|
| 40 | + } |
|
| 41 | + } |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * Allow optimizer steps to modify query operators |
|
| 46 | - * |
|
| 47 | - * @param ISearchOperator $operator |
|
| 48 | - * @return void |
|
| 49 | - */ |
|
| 50 | - public function processOperator(ISearchOperator &$operator) { |
|
| 51 | - if ($operator instanceof ISearchBinaryOperator) { |
|
| 52 | - foreach ($operator->getArguments() as $argument) { |
|
| 53 | - $this->processOperator($argument); |
|
| 54 | - } |
|
| 55 | - } |
|
| 56 | - } |
|
| 44 | + /** |
|
| 45 | + * Allow optimizer steps to modify query operators |
|
| 46 | + * |
|
| 47 | + * @param ISearchOperator $operator |
|
| 48 | + * @return void |
|
| 49 | + */ |
|
| 50 | + public function processOperator(ISearchOperator &$operator) { |
|
| 51 | + if ($operator instanceof ISearchBinaryOperator) { |
|
| 52 | + foreach ($operator->getArguments() as $argument) { |
|
| 53 | + $this->processOperator($argument); |
|
| 54 | + } |
|
| 55 | + } |
|
| 56 | + } |
|
| 57 | 57 | } |
@@ -47,7 +47,7 @@ |
||
| 47 | 47 | * @param ISearchOperator $operator |
| 48 | 48 | * @return void |
| 49 | 49 | */ |
| 50 | - public function processOperator(ISearchOperator &$operator) { |
|
| 50 | + public function processOperator(ISearchOperator & $operator) { |
|
| 51 | 51 | if ($operator instanceof ISearchBinaryOperator) { |
| 52 | 52 | foreach ($operator->getArguments() as $argument) { |
| 53 | 53 | $this->processOperator($argument); |
@@ -26,23 +26,23 @@ |
||
| 26 | 26 | use OCP\Files\Search\ISearchOperator; |
| 27 | 27 | |
| 28 | 28 | class QueryOptimizer { |
| 29 | - /** @var QueryOptimizerStep[] */ |
|
| 30 | - private $steps = []; |
|
| 29 | + /** @var QueryOptimizerStep[] */ |
|
| 30 | + private $steps = []; |
|
| 31 | 31 | |
| 32 | - public function __construct( |
|
| 33 | - PathPrefixOptimizer $pathPrefixOptimizer |
|
| 34 | - ) { |
|
| 35 | - $this->steps = [ |
|
| 36 | - $pathPrefixOptimizer |
|
| 37 | - ]; |
|
| 38 | - } |
|
| 32 | + public function __construct( |
|
| 33 | + PathPrefixOptimizer $pathPrefixOptimizer |
|
| 34 | + ) { |
|
| 35 | + $this->steps = [ |
|
| 36 | + $pathPrefixOptimizer |
|
| 37 | + ]; |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - public function processOperator(ISearchOperator $operator) { |
|
| 41 | - foreach ($this->steps as $step) { |
|
| 42 | - $step->inspectOperator($operator); |
|
| 43 | - } |
|
| 44 | - foreach ($this->steps as $step) { |
|
| 45 | - $step->processOperator($operator); |
|
| 46 | - } |
|
| 47 | - } |
|
| 40 | + public function processOperator(ISearchOperator $operator) { |
|
| 41 | + foreach ($this->steps as $step) { |
|
| 42 | + $step->inspectOperator($operator); |
|
| 43 | + } |
|
| 44 | + foreach ($this->steps as $step) { |
|
| 45 | + $step->processOperator($operator); |
|
| 46 | + } |
|
| 47 | + } |
|
| 48 | 48 | } |
@@ -29,49 +29,49 @@ |
||
| 29 | 29 | use OCP\Files\Search\ISearchOperator; |
| 30 | 30 | |
| 31 | 31 | class PathPrefixOptimizer extends QueryOptimizerStep { |
| 32 | - private bool $useHashEq = true; |
|
| 32 | + private bool $useHashEq = true; |
|
| 33 | 33 | |
| 34 | - public function inspectOperator(ISearchOperator $operator): void { |
|
| 35 | - // normally any `path = "$path"` search filter would be generated as an `path_hash = md5($path)` sql query |
|
| 36 | - // since the `path_hash` sql column usually provides much faster querying that selecting on the `path` sql column |
|
| 37 | - // |
|
| 38 | - // however, if we're already doing a filter on the `path` column in the form of `path LIKE "$prefix/%"` |
|
| 39 | - // generating a `path = "$prefix"` sql query lets the database handle use the same column for both expressions and potentially use the same index |
|
| 40 | - // |
|
| 41 | - // If there is any operator in the query that matches this pattern, we change all `path = "$path"` instances to not the `path_hash` equality, |
|
| 42 | - // otherwise mariadb has a tendency of ignoring the path_prefix index |
|
| 43 | - if ($this->useHashEq && $this->isPathPrefixOperator($operator)) { |
|
| 44 | - $this->useHashEq = false; |
|
| 45 | - } |
|
| 34 | + public function inspectOperator(ISearchOperator $operator): void { |
|
| 35 | + // normally any `path = "$path"` search filter would be generated as an `path_hash = md5($path)` sql query |
|
| 36 | + // since the `path_hash` sql column usually provides much faster querying that selecting on the `path` sql column |
|
| 37 | + // |
|
| 38 | + // however, if we're already doing a filter on the `path` column in the form of `path LIKE "$prefix/%"` |
|
| 39 | + // generating a `path = "$prefix"` sql query lets the database handle use the same column for both expressions and potentially use the same index |
|
| 40 | + // |
|
| 41 | + // If there is any operator in the query that matches this pattern, we change all `path = "$path"` instances to not the `path_hash` equality, |
|
| 42 | + // otherwise mariadb has a tendency of ignoring the path_prefix index |
|
| 43 | + if ($this->useHashEq && $this->isPathPrefixOperator($operator)) { |
|
| 44 | + $this->useHashEq = false; |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - parent::inspectOperator($operator); |
|
| 48 | - } |
|
| 47 | + parent::inspectOperator($operator); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - public function processOperator(ISearchOperator &$operator) { |
|
| 51 | - if (!$this->useHashEq && $operator instanceof ISearchComparison && $operator->getField() === 'path' && $operator->getType() === ISearchComparison::COMPARE_EQUAL) { |
|
| 52 | - $operator->setQueryHint(ISearchComparison::HINT_PATH_EQ_HASH, false); |
|
| 53 | - } |
|
| 50 | + public function processOperator(ISearchOperator &$operator) { |
|
| 51 | + if (!$this->useHashEq && $operator instanceof ISearchComparison && $operator->getField() === 'path' && $operator->getType() === ISearchComparison::COMPARE_EQUAL) { |
|
| 52 | + $operator->setQueryHint(ISearchComparison::HINT_PATH_EQ_HASH, false); |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - parent::processOperator($operator); |
|
| 56 | - } |
|
| 55 | + parent::processOperator($operator); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - private function isPathPrefixOperator(ISearchOperator $operator): bool { |
|
| 59 | - if ($operator instanceof ISearchBinaryOperator && $operator->getType() === ISearchBinaryOperator::OPERATOR_OR && count($operator->getArguments()) == 2) { |
|
| 60 | - $a = $operator->getArguments()[0]; |
|
| 61 | - $b = $operator->getArguments()[1]; |
|
| 62 | - if ($this->operatorPairIsPathPrefix($a, $b) || $this->operatorPairIsPathPrefix($b, $a)) { |
|
| 63 | - return true; |
|
| 64 | - } |
|
| 65 | - } |
|
| 66 | - return false; |
|
| 67 | - } |
|
| 58 | + private function isPathPrefixOperator(ISearchOperator $operator): bool { |
|
| 59 | + if ($operator instanceof ISearchBinaryOperator && $operator->getType() === ISearchBinaryOperator::OPERATOR_OR && count($operator->getArguments()) == 2) { |
|
| 60 | + $a = $operator->getArguments()[0]; |
|
| 61 | + $b = $operator->getArguments()[1]; |
|
| 62 | + if ($this->operatorPairIsPathPrefix($a, $b) || $this->operatorPairIsPathPrefix($b, $a)) { |
|
| 63 | + return true; |
|
| 64 | + } |
|
| 65 | + } |
|
| 66 | + return false; |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - private function operatorPairIsPathPrefix(ISearchOperator $like, ISearchOperator $equal): bool { |
|
| 70 | - return ( |
|
| 71 | - $like instanceof ISearchComparison && $equal instanceof ISearchComparison && |
|
| 72 | - $like->getField() === 'path' && $equal->getField() === 'path' && |
|
| 73 | - $like->getType() === ISearchComparison::COMPARE_LIKE_CASE_SENSITIVE && $equal->getType() === ISearchComparison::COMPARE_EQUAL |
|
| 74 | - && $like->getValue() === SearchComparison::escapeLikeParameter($equal->getValue()) . '/%' |
|
| 75 | - ); |
|
| 76 | - } |
|
| 69 | + private function operatorPairIsPathPrefix(ISearchOperator $like, ISearchOperator $equal): bool { |
|
| 70 | + return ( |
|
| 71 | + $like instanceof ISearchComparison && $equal instanceof ISearchComparison && |
|
| 72 | + $like->getField() === 'path' && $equal->getField() === 'path' && |
|
| 73 | + $like->getType() === ISearchComparison::COMPARE_LIKE_CASE_SENSITIVE && $equal->getType() === ISearchComparison::COMPARE_EQUAL |
|
| 74 | + && $like->getValue() === SearchComparison::escapeLikeParameter($equal->getValue()) . '/%' |
|
| 75 | + ); |
|
| 76 | + } |
|
| 77 | 77 | } |
@@ -47,7 +47,7 @@ discard block |
||
| 47 | 47 | parent::inspectOperator($operator); |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | - public function processOperator(ISearchOperator &$operator) { |
|
| 50 | + public function processOperator(ISearchOperator & $operator) { |
|
| 51 | 51 | if (!$this->useHashEq && $operator instanceof ISearchComparison && $operator->getField() === 'path' && $operator->getType() === ISearchComparison::COMPARE_EQUAL) { |
| 52 | 52 | $operator->setQueryHint(ISearchComparison::HINT_PATH_EQ_HASH, false); |
| 53 | 53 | } |
@@ -71,7 +71,7 @@ discard block |
||
| 71 | 71 | $like instanceof ISearchComparison && $equal instanceof ISearchComparison && |
| 72 | 72 | $like->getField() === 'path' && $equal->getField() === 'path' && |
| 73 | 73 | $like->getType() === ISearchComparison::COMPARE_LIKE_CASE_SENSITIVE && $equal->getType() === ISearchComparison::COMPARE_EQUAL |
| 74 | - && $like->getValue() === SearchComparison::escapeLikeParameter($equal->getValue()) . '/%' |
|
| 74 | + && $like->getValue() === SearchComparison::escapeLikeParameter($equal->getValue()).'/%' |
|
| 75 | 75 | ); |
| 76 | 76 | } |
| 77 | 77 | } |