Skip to content

Commit bd0984a

Browse files
committed
feat: php
1 parent 6eef87e commit bd0984a

File tree

1 file changed

+79
-76
lines changed

1 file changed

+79
-76
lines changed

templates/php/api.mustache

Lines changed: 79 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,83 @@ use Algolia\AlgoliaSearch\Exceptions\NotFoundException;
313313

314314
{{/operation}}
315315

316+
{{#isIngestionClient}}
317+
/**
318+
* Helper: Chunks the given `objects` list in subset of 1000 elements max in order to make it fit in `batch` requests.
319+
*
320+
* @param string $indexName the `indexName` to replace `objects` in
321+
* @param array $objects the array of `objects` to store in the given Algolia `indexName`
322+
* @param array $action the `batch` `action` to perform on the given array of `objects`, defaults to `addObject`
323+
* @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
324+
* @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.
325+
* @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).
326+
* @param array $requestOptions Request options
327+
*/
328+
public function chunkedPush(
329+
$indexName,
330+
$objects,
331+
$action = 'addObject',
332+
$waitForTasks = true,
333+
$referenceIndexName = null,
334+
$batchSize = 1000,
335+
$requestOptions = []
336+
) {
337+
if (null == $this->ingestionTransporter) {
338+
throw new \InvalidArgumentException('`setTransformationRegion` must have been called before calling this method.');
339+
}
340+
341+
$responses = [];
342+
$records = [];
343+
$count = 0;
344+
345+
foreach ($objects as $object) {
346+
$records[] = $object;
347+
348+
if (sizeof($records) === $batchSize || $count === sizeof($objects) - 1) {
349+
$responses[] = $this->ingestionTransporter->push($indexName, ['action' => $action, 'records' => $records], false, $referenceIndexName, $requestOptions);
350+
$records = [];
351+
}
352+
353+
++$count;
354+
}
355+
356+
if (!empty($records)) {
357+
$responses[] = $this->ingestionTransporter->push($indexName, ['action' => $action, 'records' => $records], false, $referenceIndexName, $requestOptions);
358+
}
359+
360+
if ($waitForTasks && !empty($responses)) {
361+
$timeoutCalculation = 'Algolia\AlgoliaSearch\Support\Helpers::linearTimeout';
362+
363+
foreach ($responses as $response) {
364+
$this->waitForTask($indexName, $response['taskID']);
365+
$retry = 0;
366+
367+
while ($retry < 50) {
368+
try {
369+
$resp = $this->ingestionTransporter->getEvent($response->runID, $response->eventID);
370+
371+
break;
372+
} catch (NotFoundException $e) {
373+
if (404 === $e->getCode()) {
374+
return null;
375+
}
376+
}
377+
378+
++$retry;
379+
usleep(
380+
call_user_func_array($timeoutCalculation, [$this->config->getWaitTaskTimeBeforeRetry(), $retry])
381+
);
382+
}
383+
384+
throw new ExceededRetriesException('Maximum number of retries (50) exceeded.');
385+
}
386+
}
387+
388+
return $responses;
389+
}
390+
391+
{{/isIngestionClient}}
392+
316393
{{#isSearchClient}}
317394
/**
318395
* Wait for a task to complete with `indexName` and `taskID`.
@@ -555,7 +632,7 @@ use Algolia\AlgoliaSearch\Exceptions\NotFoundException;
555632
*/
556633
public function saveObjectsWithTransformation($indexName, $objects, $waitForTasks = false, $batchSize = 1000, $requestOptions = [])
557634
{
558-
return $this->chunkedPush($indexName, $objects, 'addObject', $waitForTasks, $batchSize, $requestOptions);
635+
return $this->ingestionTransporter->chunkedPush($indexName, $objects, 'addObject', $waitForTasks, $batchSize, $requestOptions);
559636
}
560637

561638
/**
@@ -607,81 +684,7 @@ use Algolia\AlgoliaSearch\Exceptions\NotFoundException;
607684
*/
608685
public function partialUpdateObjectsWithTransformation($indexName, $objects, $createIfNotExists, $waitForTasks = false, $batchSize = 1000, $requestOptions = [])
609686
{
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-
) {
633-
if (null == $this->ingestionTransporter) {
634-
throw new \InvalidArgumentException('`setTransformationRegion` must have been called before calling this method.');
635-
}
636-
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;
687+
return $this->ingestionTransporter->chunkedPush($indexName, $objects, ($createIfNotExists == TRUE) ? 'partialUpdateObject' : 'partialUpdateObjectNoCreate', $waitForTasks, $batchSize, $requestOptions);
685688
}
686689

687690
/**

0 commit comments

Comments
 (0)