Skip to content

Commit 79b08c2

Browse files
fix(clients): allow chunked requests on WithTransformation methods (generated)
algolia/api-clients-automation#5011 Co-authored-by: algolia-bot <[email protected]> Co-authored-by: Clément Vannicatte <[email protected]>
1 parent 4955e2d commit 79b08c2

File tree

2 files changed

+76
-4
lines changed

2 files changed

+76
-4
lines changed

lib/Api/IngestionClient.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use Algolia\AlgoliaSearch\Algolia;
88
use Algolia\AlgoliaSearch\Configuration\IngestionConfig;
9+
use Algolia\AlgoliaSearch\Exceptions\ExceededRetriesException;
10+
use Algolia\AlgoliaSearch\Exceptions\NotFoundException;
911
use Algolia\AlgoliaSearch\Model\Ingestion\Authentication;
1012
use Algolia\AlgoliaSearch\Model\Ingestion\AuthenticationCreate;
1113
use Algolia\AlgoliaSearch\Model\Ingestion\AuthenticationCreateResponse;
@@ -2824,6 +2826,76 @@ public function validateSourceBeforeUpdate($sourceID, $sourceUpdate, $requestOpt
28242826
return $this->sendRequest('POST', $resourcePath, $headers, $queryParameters, $httpBody, $requestOptions);
28252827
}
28262828

2829+
/**
2830+
* Helper: Chunks the given `objects` list in subset of 1000 elements max in order to make it fit in `batch` requests.
2831+
*
2832+
* @param string $indexName the `indexName` to replace `objects` in
2833+
* @param array $objects the array of `objects` to store in the given Algolia `indexName`
2834+
* @param array $action the `batch` `action` to perform on the given array of `objects`, defaults to `addObject`
2835+
* @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
2836+
* @param array $batchSize The size of the chunk of `objects`. The number of `push` calls will be equal to `length(objects) / batchSize`. Defaults to 1000.
2837+
* @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).
2838+
* @param array $requestOptions Request options
2839+
*/
2840+
public function chunkedPush(
2841+
$indexName,
2842+
$objects,
2843+
$action = 'addObject',
2844+
$waitForTasks = true,
2845+
$referenceIndexName = null,
2846+
$batchSize = 1000,
2847+
$requestOptions = []
2848+
) {
2849+
$responses = [];
2850+
$records = [];
2851+
$count = 0;
2852+
2853+
foreach ($objects as $object) {
2854+
$records[] = $object;
2855+
2856+
if (sizeof($records) === $batchSize || $count === sizeof($objects) - 1) {
2857+
$responses[] = $this->push($indexName, ['action' => $action, 'records' => $records], false, $referenceIndexName, $requestOptions);
2858+
$records = [];
2859+
}
2860+
2861+
++$count;
2862+
}
2863+
2864+
if (!empty($records)) {
2865+
$responses[] = $this->push($indexName, ['action' => $action, 'records' => $records], false, $referenceIndexName, $requestOptions);
2866+
}
2867+
2868+
if ($waitForTasks && !empty($responses)) {
2869+
$timeoutCalculation = 'Algolia\AlgoliaSearch\Support\Helpers::linearTimeout';
2870+
2871+
foreach ($responses as $response) {
2872+
$this->waitForTask($indexName, $response['taskID']);
2873+
$retry = 0;
2874+
2875+
while ($retry < 50) {
2876+
try {
2877+
$resp = $this->getEvent($response->runID, $response->eventID);
2878+
2879+
break;
2880+
} catch (NotFoundException $e) {
2881+
if (404 === $e->getCode()) {
2882+
return null;
2883+
}
2884+
}
2885+
2886+
++$retry;
2887+
usleep(
2888+
call_user_func_array($timeoutCalculation, [$this->config->getWaitTaskTimeBeforeRetry(), $retry])
2889+
);
2890+
}
2891+
2892+
throw new ExceededRetriesException('Maximum number of retries (50) exceeded.');
2893+
}
2894+
}
2895+
2896+
return $responses;
2897+
}
2898+
28272899
private function sendRequest($method, $resourcePath, $headers, $queryParameters, $httpBody, $requestOptions, $useReadTransporter = false)
28282900
{
28292901
if (!isset($requestOptions['headers'])) {

lib/Api/SearchClient.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3045,7 +3045,7 @@ public function saveObjects($indexName, $objects, $waitForTasks = false, $batchS
30453045
* @param string $indexName the `indexName` to replace `objects` in
30463046
* @param array $objects the array of `objects` to store in the given Algolia `indexName`
30473047
* @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
3048-
* @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.
3048+
* @param array $batchSize The size of the chunk of `objects`. The number of `push` calls will be equal to `length(objects) / batchSize`. Defaults to 1000.
30493049
* @param array $requestOptions Request options
30503050
*/
30513051
public function saveObjectsWithTransformation($indexName, $objects, $waitForTasks = false, $batchSize = 1000, $requestOptions = [])
@@ -3054,7 +3054,7 @@ public function saveObjectsWithTransformation($indexName, $objects, $waitForTask
30543054
throw new \InvalidArgumentException('`setTransformationRegion` must have been called before calling this method.');
30553055
}
30563056

3057-
return $this->ingestionTransporter->push($indexName, ['action' => 'addObject', 'records' => $objects], $waitForTasks, $requestOptions);
3057+
return $this->ingestionTransporter->chunkedPush($indexName, $objects, 'addObject', $waitForTasks, $batchSize, $requestOptions);
30583058
}
30593059

30603060
/**
@@ -3102,7 +3102,7 @@ public function partialUpdateObjects($indexName, $objects, $createIfNotExists, $
31023102
* @param array $objects the array of `objects` to store in the given Algolia `indexName`
31033103
* @param bool $createIfNotExists To be provided if non-existing objects are passed, otherwise, the call will fail..
31043104
* @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
3105-
* @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.
3105+
* @param array $batchSize The size of the chunk of `objects`. The number of `push` calls will be equal to `length(objects) / batchSize`. Defaults to 1000.
31063106
* @param array $requestOptions Request options
31073107
*/
31083108
public function partialUpdateObjectsWithTransformation($indexName, $objects, $createIfNotExists, $waitForTasks = false, $batchSize = 1000, $requestOptions = [])
@@ -3111,7 +3111,7 @@ public function partialUpdateObjectsWithTransformation($indexName, $objects, $cr
31113111
throw new \InvalidArgumentException('`setTransformationRegion` must have been called before calling this method.');
31123112
}
31133113

3114-
return $this->ingestionTransporter->push($indexName, ['action' => (true == $createIfNotExists) ? 'partialUpdateObject' : 'partialUpdateObjectNoCreate', 'records' => $objects], $waitForTasks, $requestOptions);
3114+
return $this->ingestionTransporter->chunkedPush($indexName, $objects, (true == $createIfNotExists) ? 'partialUpdateObject' : 'partialUpdateObjectNoCreate', $waitForTasks, $batchSize, $requestOptions);
31153115
}
31163116

31173117
/**

0 commit comments

Comments
 (0)