|
| 1 | +{{#isIngestionClient}} |
| 2 | +private <T> List<PushTaskRecords> objectsToPushTaskRecords(Iterable<T> objects) { |
| 3 | + try { |
| 4 | + ObjectMapper mapper = new ObjectMapper(); |
| 5 | + String json = mapper.writeValueAsString(objects); |
| 6 | +
|
| 7 | + return mapper.readValue(json, new TypeReference<List<PushTaskRecords>>() {}); |
| 8 | + } catch (Exception e) { |
| 9 | + throw new AlgoliaRuntimeException( |
| 10 | + "each object must have an `objectID` key in order to be indexed" |
| 11 | + ); |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * Helper: Chunks the given `objects` list in subset of 1000 elements max in order to make it fit |
| 17 | + * in `push` requests by leveraging the Transformation pipeline setup in the Push connector |
| 18 | + * (https://www.algolia.com/doc/guides/sending-and-managing-data/send-and-update-your-data/connectors/push/). |
| 19 | + * |
| 20 | + * @summary Helper: Chunks the given `objects` list in subset of 1000 elements max in order to |
| 21 | + * make it fit in `batch` requests. |
| 22 | + * @param indexName - The `indexName` to replace `objects` in. |
| 23 | + * @param objects - The array of `objects` to store in the given Algolia `indexName`. |
| 24 | + * @param action - The `batch` `action` to perform on the given array of `objects`. |
| 25 | + * @param waitForTasks - Whether or not we should wait until every `batch` tasks has been |
| 26 | + * processed, this operation may slow the total execution time of this method but is more |
| 27 | + * reliable. |
| 28 | + * @param batchSize - The size of the chunk of `objects`. The number of `batch` calls will be |
| 29 | + * equal to `length(objects) / batchSize`. Defaults to 1000. |
| 30 | + * @param referenceIndexName - This is required when targeting an index that does not have a push |
| 31 | + * connector setup (e.g. a tmp index), but you wish to attach another index's transformation |
| 32 | + * to it (e.g. the source index name). |
| 33 | + * @param requestOptions - The requestOptions to send along with the query, they will be forwarded |
| 34 | + * to the `getTask` method and merged with the transporter requestOptions. |
| 35 | + */ |
| 36 | +public <T> List<WatchResponse> chunkedPush( |
| 37 | + String indexName, |
| 38 | + Iterable<T> objects, |
| 39 | + Action action, |
| 40 | + boolean waitForTasks, |
| 41 | + int batchSize, |
| 42 | + String referenceIndexName, |
| 43 | + RequestOptions requestOptions |
| 44 | +) { |
| 45 | + List<WatchResponse> responses = new ArrayList<>(); |
| 46 | + List<T> records = new ArrayList<>(); |
| 47 | +
|
| 48 | + for (T item : objects) { |
| 49 | + if (records.size() == batchSize) { |
| 50 | + WatchResponse watch = |
| 51 | + this.push( |
| 52 | + indexName, |
| 53 | + new PushTaskPayload().setAction(action).setRecords(this.objectsToPushTaskRecords(records)), |
| 54 | + waitForTasks, |
| 55 | + referenceIndexName, |
| 56 | + requestOptions |
| 57 | + ); |
| 58 | + responses.add(watch); |
| 59 | + records.clear(); |
| 60 | + } |
| 61 | + |
| 62 | + records.add(item); |
| 63 | + } |
| 64 | + |
| 65 | + if (records.size() > 0) { |
| 66 | + WatchResponse watch = |
| 67 | + this.push( |
| 68 | + indexName, |
| 69 | + new PushTaskPayload().setAction(action).setRecords(this.objectsToPushTaskRecords(records)), |
| 70 | + waitForTasks, |
| 71 | + referenceIndexName, |
| 72 | + requestOptions |
| 73 | + ); |
| 74 | + responses.add(watch); |
| 75 | + } |
| 76 | + |
| 77 | + if (waitForTasks) { |
| 78 | + responses.forEach(response -> { |
| 79 | + TaskUtils.retryUntil( |
| 80 | + () -> { |
| 81 | + try { |
| 82 | + return this.getEvent(response.getRunID(), response.getEventID()); |
| 83 | + } catch (AlgoliaApiException e) { |
| 84 | + if (e.getStatusCode() == 404) { |
| 85 | + return null; |
| 86 | + } |
| 87 | + |
| 88 | + throw e; |
| 89 | + } |
| 90 | + }, |
| 91 | + (Event resp) -> { |
| 92 | + return resp != null; |
| 93 | + }, |
| 94 | + 50, |
| 95 | + null |
| 96 | + ); |
| 97 | + } |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + return responses; |
| 102 | +} |
| 103 | +{{/isIngestionClient}} |
1 | 104 | {{#isSearchClient}} |
2 | 105 | /** |
3 | 106 | * Helper: Wait for a task to complete with `indexName` and `taskID`. |
@@ -585,99 +688,6 @@ public CompletableFuture<List<SearchForFacetValuesResponse>> searchForFacetsAsyn |
585 | 688 | ); |
586 | 689 | } |
587 | 690 |
|
588 | | -/** |
589 | | - * Helper: Chunks the given `objects` list in subset of 1000 elements max in order to make it fit |
590 | | - * in `push` requests by leveraging the Transformation pipeline setup in the Push connector |
591 | | - * (https://www.algolia.com/doc/guides/sending-and-managing-data/send-and-update-your-data/connectors/push/). |
592 | | - * |
593 | | - * @summary Helper: Chunks the given `objects` list in subset of 1000 elements max in order to |
594 | | - * make it fit in `batch` requests. |
595 | | - * @param indexName - The `indexName` to replace `objects` in. |
596 | | - * @param objects - The array of `objects` to store in the given Algolia `indexName`. |
597 | | - * @param action - The `batch` `action` to perform on the given array of `objects`. |
598 | | - * @param waitForTasks - Whether or not we should wait until every `batch` tasks has been |
599 | | - * processed, this operation may slow the total execution time of this method but is more |
600 | | - * reliable. |
601 | | - * @param batchSize - The size of the chunk of `objects`. The number of `batch` calls will be |
602 | | - * equal to `length(objects) / batchSize`. Defaults to 1000. |
603 | | - * @param referenceIndexName - This is required when targeting an index that does not have a push |
604 | | - * connector setup (e.g. a tmp index), but you wish to attach another index's transformation |
605 | | - * to it (e.g. the source index name). |
606 | | - * @param requestOptions - The requestOptions to send along with the query, they will be forwarded |
607 | | - * to the `getTask` method and merged with the transporter requestOptions. |
608 | | - */ |
609 | | -public <T> List<WatchResponse> chunkedPush( |
610 | | - String indexName, |
611 | | - Iterable<T> objects, |
612 | | - com.algolia.model.ingestion.Action action, |
613 | | - boolean waitForTasks, |
614 | | - int batchSize, |
615 | | - String referenceIndexName, |
616 | | - RequestOptions requestOptions |
617 | | -) { |
618 | | - if (this.ingestionTransporter == null) { |
619 | | - throw new AlgoliaRuntimeException("`setTransformationRegion` must have been called before calling this method."); |
620 | | - } |
621 | | - |
622 | | - List<WatchResponse> responses = new ArrayList<>(); |
623 | | - List<T> records = new ArrayList<>(); |
624 | | - |
625 | | - for (T item : objects) { |
626 | | - if (records.size() == batchSize) { |
627 | | - WatchResponse watch = |
628 | | - this.ingestionTransporter.push( |
629 | | - indexName, |
630 | | - new PushTaskPayload().setAction(action).setRecords(this.objectsToPushTaskRecords(records)), |
631 | | - waitForTasks, |
632 | | - referenceIndexName, |
633 | | - requestOptions |
634 | | - ); |
635 | | - responses.add(watch); |
636 | | - records.clear(); |
637 | | - } |
638 | | - |
639 | | - records.add(item); |
640 | | - } |
641 | | - |
642 | | - if (records.size() > 0) { |
643 | | - WatchResponse watch = |
644 | | - this.ingestionTransporter.push( |
645 | | - indexName, |
646 | | - new PushTaskPayload().setAction(action).setRecords(this.objectsToPushTaskRecords(records)), |
647 | | - waitForTasks, |
648 | | - referenceIndexName, |
649 | | - requestOptions |
650 | | - ); |
651 | | - responses.add(watch); |
652 | | - } |
653 | | - |
654 | | - if (waitForTasks) { |
655 | | - responses.forEach(response -> { |
656 | | - TaskUtils.retryUntil( |
657 | | - () -> { |
658 | | - try { |
659 | | - return this.ingestionTransporter.getEvent(response.getRunID(), response.getEventID()); |
660 | | - } catch (AlgoliaApiException e) { |
661 | | - if (e.getStatusCode() == 404) { |
662 | | - return null; |
663 | | - } |
664 | | - |
665 | | - throw e; |
666 | | - } |
667 | | - }, |
668 | | - (Event resp) -> { |
669 | | - return resp != null; |
670 | | - }, |
671 | | - 50, |
672 | | - null |
673 | | - ); |
674 | | - } |
675 | | - ); |
676 | | - } |
677 | | - |
678 | | - return responses; |
679 | | -} |
680 | | - |
681 | 691 | /** |
682 | 692 | * Helper: Chunks the given `objects` list in subset of 1000 elements max in order to make it fit |
683 | 693 | * in `batch` requests. |
@@ -823,20 +833,11 @@ public <T> List<WatchResponse> saveObjectsWithTransformation( |
823 | 833 | int batchSize, |
824 | 834 | RequestOptions requestOptions |
825 | 835 | ) { |
826 | | - return chunkedPush(indexName, objects, com.algolia.model.ingestion.Action.ADD_OBJECT, waitForTasks, batchSize, null, requestOptions); |
827 | | -} |
828 | | - |
829 | | -private <T> List<PushTaskRecords> objectsToPushTaskRecords(Iterable<T> objects) { |
830 | | - try { |
831 | | - ObjectMapper mapper = new ObjectMapper(); |
832 | | - String json = mapper.writeValueAsString(objects); |
| 836 | + if (this.ingestionTransporter == null) { |
| 837 | + throw new AlgoliaRuntimeException("`setTransformationRegion` must have been called before calling this method."); |
| 838 | + } |
833 | 839 |
|
834 | | - return mapper.readValue(json, new TypeReference<List<PushTaskRecords>>() {}); |
835 | | - } catch (Exception e) { |
836 | | - throw new AlgoliaRuntimeException( |
837 | | - "each object must have an `objectID` key in order to be used with the" + " WithTransformation methods" |
838 | | - ); |
839 | | - } |
| 840 | + return this.ingestionTransporter.chunkedPush(indexName, objects, com.algolia.model.ingestion.Action.ADD_OBJECT, waitForTasks, batchSize, null, requestOptions); |
840 | 841 | } |
841 | 842 |
|
842 | 843 | /** |
@@ -1053,7 +1054,11 @@ public <T> List<WatchResponse> partialUpdateObjectsWithTransformation( |
1053 | 1054 | int batchSize, |
1054 | 1055 | RequestOptions requestOptions |
1055 | 1056 | ) { |
1056 | | - return chunkedPush( |
| 1057 | + if (this.ingestionTransporter == null) { |
| 1058 | + throw new AlgoliaRuntimeException("`setTransformationRegion` must have been called before calling this method."); |
| 1059 | + } |
| 1060 | +
|
| 1061 | + return this.ingestionTransporter.chunkedPush( |
1057 | 1062 | indexName, |
1058 | 1063 | objects, |
1059 | 1064 | createIfNotExists ? com.algolia.model.ingestion.Action.PARTIAL_UPDATE_OBJECT : com.algolia.model.ingestion.Action.PARTIAL_UPDATE_OBJECT_NO_CREATE, |
|
0 commit comments