|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text.Json; |
| 5 | +using System.Threading; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Algolia.Search.Exceptions; |
| 8 | +using Algolia.Search.Http; |
| 9 | +using Algolia.Search.Models.Ingestion; |
| 10 | +using Algolia.Search.Serializer; |
| 11 | +using Algolia.Search.Utils; |
| 12 | + |
| 13 | +namespace Algolia.Search.Clients; |
| 14 | + |
| 15 | +public partial interface IIngestionClient |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// Helper method to call ChunkedPushAsync and convert the response types. |
| 19 | + /// This simplifies SearchClient helpers that need to use IngestionClient. |
| 20 | + /// </summary> |
| 21 | + Task<List<WatchResponse>> ChunkedPushAsync( |
| 22 | + string indexName, |
| 23 | + IEnumerable<object> objects, |
| 24 | + Models.Ingestion.Action action, |
| 25 | + bool waitForTasks = false, |
| 26 | + int batchSize = 1000, |
| 27 | + string referenceIndexName = null, |
| 28 | + RequestOptions options = null, |
| 29 | + CancellationToken cancellationToken = default |
| 30 | + ); |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Synchronous version of ChunkedPushAsync |
| 34 | + /// </summary> |
| 35 | + List<WatchResponse> ChunkedPush( |
| 36 | + string indexName, |
| 37 | + IEnumerable<object> objects, |
| 38 | + Models.Ingestion.Action action, |
| 39 | + bool waitForTasks = false, |
| 40 | + int batchSize = 1000, |
| 41 | + string referenceIndexName = null, |
| 42 | + RequestOptions options = null, |
| 43 | + CancellationToken cancellationToken = default |
| 44 | + ); |
| 45 | +} |
| 46 | + |
| 47 | +public partial class IngestionClient : IIngestionClient |
| 48 | +{ |
| 49 | + /// <summary> |
| 50 | + /// Helper: Chunks the given `objects` list in subset of 1000 elements max in order to make it fit |
| 51 | + /// in `push` requests by leveraging the Transformation pipeline setup in the Push connector |
| 52 | + /// (https://www.algolia.com/doc/guides/sending-and-managing-data/send-and-update-your-data/connectors/push/). |
| 53 | + /// </summary> |
| 54 | + /// <param name="indexName">The `indexName` to push `objects` to.</param> |
| 55 | + /// <param name="objects">The array of `objects` to store in the given Algolia `indexName`.</param> |
| 56 | + /// <param name="action">The `action` to perform on the given array of `objects`.</param> |
| 57 | + /// <param name="waitForTasks">Whether or not we should wait until every push task has been processed. This operation may slow the total execution time of this method but is more reliable.</param> |
| 58 | + /// <param name="batchSize">The size of the chunk of `objects`. The number of push calls will be equal to `length(objects) / batchSize`. Defaults to 1000.</param> |
| 59 | + /// <param name="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).</param> |
| 60 | + /// <param name="options">Add extra http header or query parameters to Algolia.</param> |
| 61 | + /// <param name="cancellationToken">Cancellation token to cancel the request</param> |
| 62 | + /// <returns>List of WatchResponse objects from the push operations</returns> |
| 63 | + public async Task<List<WatchResponse>> ChunkedPushAsync( |
| 64 | + string indexName, |
| 65 | + IEnumerable<object> objects, |
| 66 | + Algolia.Search.Models.Ingestion.Action action, |
| 67 | + bool waitForTasks = false, |
| 68 | + int batchSize = 1000, |
| 69 | + string referenceIndexName = null, |
| 70 | + RequestOptions options = null, |
| 71 | + CancellationToken cancellationToken = default |
| 72 | + ) |
| 73 | + { |
| 74 | + var objectsList = objects.ToList(); |
| 75 | + var responses = new List<WatchResponse>(); |
| 76 | + var waitBatchSize = Math.Max(batchSize / 10, 1); |
| 77 | + var offset = 0; |
| 78 | + |
| 79 | + for (var i = 0; i < objectsList.Count; i += batchSize) |
| 80 | + { |
| 81 | + var chunk = objectsList.Skip(i).Take(batchSize); |
| 82 | + var records = new List<PushTaskRecords>(); |
| 83 | + |
| 84 | + foreach (var obj in chunk) |
| 85 | + { |
| 86 | + var jsonString = JsonSerializer.Serialize(obj, JsonConfig.Options); |
| 87 | + var record = JsonSerializer.Deserialize<PushTaskRecords>(jsonString, JsonConfig.Options); |
| 88 | + records.Add(record); |
| 89 | + } |
| 90 | + |
| 91 | + var payload = new PushTaskPayload(action, records); |
| 92 | + |
| 93 | + var response = await PushAsync( |
| 94 | + indexName, |
| 95 | + payload, |
| 96 | + watch: null, |
| 97 | + referenceIndexName: referenceIndexName, |
| 98 | + options: options, |
| 99 | + cancellationToken: cancellationToken |
| 100 | + ) |
| 101 | + .ConfigureAwait(false); |
| 102 | + |
| 103 | + responses.Add(response); |
| 104 | + |
| 105 | + if ( |
| 106 | + waitForTasks |
| 107 | + && responses.Count > 0 |
| 108 | + && (responses.Count % waitBatchSize == 0 || i + batchSize >= objectsList.Count) |
| 109 | + ) |
| 110 | + { |
| 111 | + for (var j = offset; j < responses.Count; j++) |
| 112 | + { |
| 113 | + var resp = responses[j]; |
| 114 | + if (string.IsNullOrEmpty(resp.EventID)) |
| 115 | + { |
| 116 | + throw new AlgoliaException( |
| 117 | + "Received unexpected response from the push endpoint, eventID must not be null or empty" |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + await RetryHelper |
| 122 | + .RetryUntil( |
| 123 | + async () => |
| 124 | + { |
| 125 | + try |
| 126 | + { |
| 127 | + return await GetEventAsync( |
| 128 | + resp.RunID, |
| 129 | + resp.EventID, |
| 130 | + cancellationToken: cancellationToken |
| 131 | + ) |
| 132 | + .ConfigureAwait(false); |
| 133 | + } |
| 134 | + catch (AlgoliaApiException ex) when (ex.HttpErrorCode == 404) |
| 135 | + { |
| 136 | + return await Task.FromResult<Algolia.Search.Models.Ingestion.Event>(null); |
| 137 | + } |
| 138 | + }, |
| 139 | + eventResponse => eventResponse != null, |
| 140 | + maxRetries: 50, |
| 141 | + ct: cancellationToken |
| 142 | + ) |
| 143 | + .ConfigureAwait(false); |
| 144 | + } |
| 145 | + offset = responses.Count; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + return responses; |
| 150 | + } |
| 151 | + |
| 152 | + /// <summary> |
| 153 | + /// Synchronous version of ChunkedPushAsync |
| 154 | + /// </summary> |
| 155 | + public List<WatchResponse> ChunkedPush( |
| 156 | + string indexName, |
| 157 | + IEnumerable<object> objects, |
| 158 | + Algolia.Search.Models.Ingestion.Action action, |
| 159 | + bool waitForTasks = false, |
| 160 | + int batchSize = 1000, |
| 161 | + string referenceIndexName = null, |
| 162 | + RequestOptions options = null, |
| 163 | + CancellationToken cancellationToken = default |
| 164 | + ) => |
| 165 | + AsyncHelper.RunSync(() => |
| 166 | + ChunkedPushAsync( |
| 167 | + indexName, |
| 168 | + objects, |
| 169 | + action, |
| 170 | + waitForTasks, |
| 171 | + batchSize, |
| 172 | + referenceIndexName, |
| 173 | + options, |
| 174 | + cancellationToken |
| 175 | + ) |
| 176 | + ); |
| 177 | +} |
0 commit comments