1
0

SplitLargeIn.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\Files\Search\QueryOptimizer;
  7. use OC\Files\Search\SearchBinaryOperator;
  8. use OC\Files\Search\SearchComparison;
  9. use OCP\Files\Search\ISearchBinaryOperator;
  10. use OCP\Files\Search\ISearchComparison;
  11. use OCP\Files\Search\ISearchOperator;
  12. /**
  13. * transform IN (1000+ element) into (IN (1000 elements) OR IN(...))
  14. */
  15. class SplitLargeIn extends ReplacingOptimizerStep {
  16. public function processOperator(ISearchOperator &$operator): bool {
  17. if (
  18. $operator instanceof ISearchComparison &&
  19. $operator->getType() === ISearchComparison::COMPARE_IN &&
  20. count($operator->getValue()) > 1000
  21. ) {
  22. $chunks = array_chunk($operator->getValue(), 1000);
  23. $chunkComparisons = array_map(function (array $values) use ($operator) {
  24. return new SearchComparison(ISearchComparison::COMPARE_IN, $operator->getField(), $values);
  25. }, $chunks);
  26. $operator = new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_OR, $chunkComparisons);
  27. return true;
  28. }
  29. parent::processOperator($operator);
  30. return false;
  31. }
  32. }