Skip to content

Commit 7965edc

Browse files
committed
feat: maybe php idk
1 parent 17a9821 commit 7965edc

File tree

1 file changed

+72
-6
lines changed

1 file changed

+72
-6
lines changed

templates/php/api.mustache

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -555,11 +555,7 @@ use Algolia\AlgoliaSearch\Exceptions\NotFoundException;
555555
*/
556556
public function saveObjectsWithTransformation($indexName, $objects, $waitForTasks = false, $batchSize = 1000, $requestOptions = [])
557557
{
558-
if (null == $this->ingestionTransporter) {
559-
throw new \InvalidArgumentException('`setTransformationRegion` must have been called before calling this method.');
560-
}
561-
562-
return $this->ingestionTransporter->push($indexName, ['action'=>'addObject', 'records'=>$objects], $waitForTasks, $requestOptions);
558+
return $this->chunkedPush($indexName, $objects, 'addObject', $waitForTasks, $batchSize, $requestOptions);
563559
}
564560

565561
/**
@@ -611,11 +607,81 @@ use Algolia\AlgoliaSearch\Exceptions\NotFoundException;
611607
*/
612608
public function partialUpdateObjectsWithTransformation($indexName, $objects, $createIfNotExists, $waitForTasks = false, $batchSize = 1000, $requestOptions = [])
613609
{
610+
return $this->chunkedPush($indexName, $objects, ($createIfNotExists == TRUE) ? 'partialUpdateObject' : 'partialUpdateObjectNoCreate', $waitForTasks, $batchSize, $requestOptions);
611+
}
612+
613+
/**
614+
* Helper: Chunks the given `objects` list in subset of 1000 elements max in order to make it fit in `batch` requests.
615+
*
616+
* @param string $indexName the `indexName` to replace `objects` in
617+
* @param array $objects the array of `objects` to store in the given Algolia `indexName`
618+
* @param array $action the `batch` `action` to perform on the given array of `objects`, defaults to `addObject`
619+
* @param bool $waitForTasks whether or not we should wait until every `batch` tasks has been processed, this operation may slow the total execution time of this method but is more reliable
620+
* @param array $batchSize The size of the chunk of `objects`. The number of `batch` calls will be equal to `length(objects) / batchSize`. Defaults to 1000.
621+
* @param array $referenceIndexName This is required when targeting an index that does not have a push connector setup (e.g. a tmp index), but you wish to attach another index's transformation to it (e.g. the source index name).
622+
* @param array $requestOptions Request options
623+
*/
624+
public function chunkedPush(
625+
$indexName,
626+
$objects,
627+
$action = 'addObject',
628+
$waitForTasks = true,
629+
$referenceIndexName = null,
630+
$batchSize = 1000,
631+
$requestOptions = []
632+
) {
614633
if (null == $this->ingestionTransporter) {
615634
throw new \InvalidArgumentException('`setTransformationRegion` must have been called before calling this method.');
616635
}
617636

618-
return $this->ingestionTransporter->push($indexName, ['action'=>(true == $createIfNotExists) ? 'partialUpdateObject' : 'partialUpdateObjectNoCreate', 'records'=>$objects], $waitForTasks, $requestOptions);
637+
$responses = [];
638+
$records = [];
639+
$count = 0;
640+
641+
foreach ($objects as $object) {
642+
$records[] = $object;
643+
644+
if (sizeof($records) === $batchSize || $count === sizeof($objects) - 1) {
645+
$responses[] = $this->ingestionTransporter->push($indexName, ['action' => $action, 'records' => $records], false, $referenceIndexName, $requestOptions);
646+
$records = [];
647+
}
648+
649+
++$count;
650+
}
651+
652+
if (!empty($records)) {
653+
$responses[] = $this->ingestionTransporter->push($indexName, ['action' => $action, 'records' => $records], false, $referenceIndexName, $requestOptions);
654+
}
655+
656+
if ($waitForTasks && !empty($responses)) {
657+
$timeoutCalculation = 'Algolia\AlgoliaSearch\Support\Helpers::linearTimeout';
658+
659+
foreach ($responses as $response) {
660+
$this->waitForTask($indexName, $response['taskID']);
661+
$retry = 0;
662+
663+
while ($retry < $50) {
664+
try {
665+
$resp = $this->ingestionTransporter->getEvent($response->runID, $response->eventID);
666+
667+
break;
668+
} catch (NotFoundException $e) {
669+
if (404 === $e->getCode()) {
670+
return null;
671+
}
672+
}
673+
674+
++$retry;
675+
usleep(
676+
call_user_func_array($timeoutCalculation, [$this->config->getWaitTaskTimeBeforeRetry(), $retry])
677+
);
678+
}
679+
680+
throw new ExceededRetriesException('Maximum number of retries (50) exceeded.');
681+
}
682+
}
683+
684+
return $responses;
619685
}
620686

621687
/**

0 commit comments

Comments
 (0)