|
| 1 | +// Licensed to Elasticsearch B.V under one or more agreements. |
| 2 | +// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Elastic.Transport; |
| 8 | +using System.IO; |
| 9 | +using System.Collections.Generic; |
| 10 | +using System.Linq; |
| 11 | + |
| 12 | +namespace Elastic.Clients.Elasticsearch |
| 13 | +{ |
| 14 | + public partial class BulkRequest : IStreamSerializable |
| 15 | + { |
| 16 | + protected IRequest Self => this; |
| 17 | + |
| 18 | + public BulkOperationsCollection Operations { get; set; } |
| 19 | + |
| 20 | + protected override string ContentType => "application/x-ndjson"; |
| 21 | + |
| 22 | + protected override string Accept => "application/json"; |
| 23 | + |
| 24 | + public void Serialize(Stream stream, IElasticsearchClientSettings settings, SerializationFormatting formatting = SerializationFormatting.None) |
| 25 | + { |
| 26 | + if (Operations is null) |
| 27 | + return; |
| 28 | + |
| 29 | + var index = Self.RouteValues.Get<IndexName>("index"); |
| 30 | + |
| 31 | + foreach (var op in Operations) |
| 32 | + { |
| 33 | + if (op is not IStreamSerializable serializable) |
| 34 | + throw new InvalidOperationException(""); |
| 35 | + |
| 36 | + op.PrepareIndex(index); |
| 37 | + |
| 38 | + serializable.Serialize(stream, settings, formatting); |
| 39 | + stream.WriteByte((byte)'\n'); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + public async Task SerializeAsync(Stream stream, IElasticsearchClientSettings settings, SerializationFormatting formatting = SerializationFormatting.None) |
| 44 | + { |
| 45 | + if (Operations is null) |
| 46 | + return; |
| 47 | + |
| 48 | + var index = Self.RouteValues.Get<IndexName>("index"); |
| 49 | + |
| 50 | + foreach (var op in Operations) |
| 51 | + { |
| 52 | + if (op is not IStreamSerializable serializable) |
| 53 | + throw new InvalidOperationException(""); |
| 54 | + |
| 55 | + op.PrepareIndex(index); |
| 56 | + |
| 57 | + await serializable.SerializeAsync(stream, settings, formatting).ConfigureAwait(false); |
| 58 | + stream.WriteByte((byte)'\n'); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public sealed partial class BulkRequestDescriptor : IStreamSerializable |
| 64 | + { |
| 65 | + protected override string ContentType => "application/x-ndjson"; |
| 66 | + |
| 67 | + protected override string Accept => "application/json"; |
| 68 | + |
| 69 | + private readonly BulkOperationsCollection _operations = new(); |
| 70 | + |
| 71 | + public BulkRequestDescriptor Index(string index) |
| 72 | + { |
| 73 | + RouteValues.Optional("index", IndexName.Parse(index)); |
| 74 | + return Self; |
| 75 | + } |
| 76 | + |
| 77 | + public BulkRequestDescriptor Create<TSource>(TSource document, Action<BulkCreateOperationDescriptor<TSource>> configure = null) |
| 78 | + { |
| 79 | + var descriptor = new BulkCreateOperationDescriptor<TSource>(document); |
| 80 | + configure?.Invoke(descriptor); |
| 81 | + _operations.Add(descriptor); |
| 82 | + return this; |
| 83 | + } |
| 84 | + |
| 85 | + public BulkRequestDescriptor Create<TSource>(TSource document, IndexName index, Action<BulkCreateOperationDescriptor<TSource>> configure = null) |
| 86 | + { |
| 87 | + var descriptor = new BulkCreateOperationDescriptor<TSource>(document, index); |
| 88 | + configure?.Invoke(descriptor); |
| 89 | + _operations.Add(descriptor); |
| 90 | + return this; |
| 91 | + } |
| 92 | + |
| 93 | + public BulkRequestDescriptor Index<TSource>(TSource document, Action<BulkIndexOperationDescriptor<TSource>> configure = null) |
| 94 | + { |
| 95 | + var descriptor = new BulkIndexOperationDescriptor<TSource>(document); |
| 96 | + configure?.Invoke(descriptor); |
| 97 | + _operations.Add(descriptor); |
| 98 | + return this; |
| 99 | + } |
| 100 | + |
| 101 | + public BulkRequestDescriptor Index<TSource>(TSource document, IndexName index, Action<BulkIndexOperationDescriptor<TSource>> configure = null) |
| 102 | + { |
| 103 | + var descriptor = new BulkIndexOperationDescriptor<TSource>(document, index); |
| 104 | + configure?.Invoke(descriptor); |
| 105 | + _operations.Add(descriptor); |
| 106 | + return this; |
| 107 | + } |
| 108 | + |
| 109 | + public BulkRequestDescriptor Update(BulkUpdateOperationBase update) |
| 110 | + { |
| 111 | + _operations.Add(update); |
| 112 | + return this; |
| 113 | + } |
| 114 | + |
| 115 | + public BulkRequestDescriptor Update<TSource, TPartialDocument>(Action<BulkUpdateOperationDescriptor<TSource, TPartialDocument>> configure) |
| 116 | + { |
| 117 | + var descriptor = new BulkUpdateOperationDescriptor<TSource, TPartialDocument>(); |
| 118 | + configure?.Invoke(descriptor); |
| 119 | + _operations.Add(descriptor); |
| 120 | + return this; |
| 121 | + } |
| 122 | + |
| 123 | + public BulkRequestDescriptor Update<T>(Action<BulkUpdateOperationDescriptor<T, T>> configure) => |
| 124 | + Update<T, T>(configure); |
| 125 | + |
| 126 | + public BulkRequestDescriptor Delete(Id id, Action<BulkDeleteOperationDescriptor> configure = null) |
| 127 | + { |
| 128 | + var descriptor = new BulkDeleteOperationDescriptor(id); |
| 129 | + configure?.Invoke(descriptor); |
| 130 | + _operations.Add(descriptor); |
| 131 | + return this; |
| 132 | + } |
| 133 | + |
| 134 | + public BulkRequestDescriptor Delete(string id, Action<BulkDeleteOperationDescriptor> configure = null) |
| 135 | + { |
| 136 | + var descriptor = new BulkDeleteOperationDescriptor(id); |
| 137 | + configure?.Invoke(descriptor); |
| 138 | + _operations.Add(descriptor); |
| 139 | + return this; |
| 140 | + } |
| 141 | + |
| 142 | + public BulkRequestDescriptor Delete(Action<BulkDeleteOperationDescriptor> configure) |
| 143 | + { |
| 144 | + var descriptor = new BulkDeleteOperationDescriptor(); |
| 145 | + configure?.Invoke(descriptor); |
| 146 | + _operations.Add(descriptor); |
| 147 | + return this; |
| 148 | + } |
| 149 | + |
| 150 | + public BulkRequestDescriptor Delete<TSource>(TSource documentToDelete, Action<BulkDeleteOperationDescriptor> configure = null) |
| 151 | + { |
| 152 | + var descriptor = new BulkDeleteOperationDescriptor(new Id(documentToDelete)); |
| 153 | + configure?.Invoke(descriptor); |
| 154 | + _operations.Add(descriptor); |
| 155 | + return this; |
| 156 | + } |
| 157 | + |
| 158 | + public BulkRequestDescriptor Delete<TSource>(Action<BulkDeleteOperationDescriptor> configure) => Delete(configure); |
| 159 | + |
| 160 | + public BulkRequestDescriptor CreateMany<TSource>(IEnumerable<TSource> documents, Action<BulkCreateOperationDescriptor<TSource>, TSource> bulkCreateSelector) => |
| 161 | + AddOperations(documents, bulkCreateSelector, o => new BulkCreateOperationDescriptor<TSource>(o)); |
| 162 | + |
| 163 | + public BulkRequestDescriptor CreateMany<TSource>(IEnumerable<TSource> documents) => |
| 164 | + AddOperations(documents, null, o => new BulkCreateOperationDescriptor<TSource>(o)); |
| 165 | + |
| 166 | + public BulkRequestDescriptor IndexMany<TSource>(IEnumerable<TSource> documents, Action<BulkIndexOperationDescriptor<TSource>, TSource> bulkIndexSelector) => |
| 167 | + AddOperations(documents, bulkIndexSelector, o => new BulkIndexOperationDescriptor<TSource>(o)); |
| 168 | + |
| 169 | + public BulkRequestDescriptor IndexMany<TSource>(IEnumerable<TSource> documents) => |
| 170 | + AddOperations(documents, null, o => new BulkIndexOperationDescriptor<TSource>(o)); |
| 171 | + |
| 172 | + public BulkRequestDescriptor UpdateMany<TSource>(IEnumerable<TSource> objects, Action<BulkUpdateOperationDescriptor<TSource, TSource>, TSource> bulkIndexSelector) => |
| 173 | + AddOperations(objects, bulkIndexSelector, o => new BulkUpdateOperationDescriptor<TSource, TSource>().IdFrom(o)); |
| 174 | + |
| 175 | + public BulkRequestDescriptor UpdateMany<TSource>(IEnumerable<TSource> objects) => |
| 176 | + AddOperations(objects, null, o => new BulkUpdateOperationDescriptor<TSource, TSource>().IdFrom(o)); |
| 177 | + |
| 178 | + public BulkRequestDescriptor DeleteMany<T>(IEnumerable<T> objects, Action<BulkDeleteOperationDescriptor, T> bulkDeleteSelector) => |
| 179 | + AddOperations(objects, bulkDeleteSelector, obj => new BulkDeleteOperationDescriptor(new Id(obj))); |
| 180 | + |
| 181 | + public BulkRequestDescriptor DeleteMany(IEnumerable<Id> ids, Action<BulkDeleteOperationDescriptor, Id> bulkDeleteSelector) => |
| 182 | + AddOperations(ids, bulkDeleteSelector, id => new BulkDeleteOperationDescriptor(id)); |
| 183 | + |
| 184 | + public BulkRequestDescriptor DeleteMany<T>(IEnumerable<T> objects) => |
| 185 | + AddOperations(objects, null, obj => new BulkDeleteOperationDescriptor<T>(obj)); |
| 186 | + |
| 187 | + public BulkRequestDescriptor DeleteMany(IndexName index, IEnumerable<Id> ids) => |
| 188 | + AddOperations(ids, null, id => new BulkDeleteOperationDescriptor(id).Index(index)); |
| 189 | + |
| 190 | + public void Serialize(Stream stream, IElasticsearchClientSettings settings, SerializationFormatting formatting = SerializationFormatting.None) |
| 191 | + { |
| 192 | + if (_operations is null) |
| 193 | + return; |
| 194 | + |
| 195 | + var index = Self.RouteValues.Get<IndexName>("index"); |
| 196 | + |
| 197 | + foreach (var op in _operations) |
| 198 | + { |
| 199 | + if (op is not IStreamSerializable serializable) |
| 200 | + throw new InvalidOperationException(""); |
| 201 | + |
| 202 | + op.PrepareIndex(index); |
| 203 | + |
| 204 | + serializable.Serialize(stream, settings, formatting); |
| 205 | + stream.WriteByte((byte)'\n'); |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + public async Task SerializeAsync(Stream stream, IElasticsearchClientSettings settings, SerializationFormatting formatting = SerializationFormatting.None) |
| 210 | + { |
| 211 | + if (_operations is null) |
| 212 | + return; |
| 213 | + |
| 214 | + var index = Self.RouteValues.Get<IndexName>("index"); |
| 215 | + |
| 216 | + foreach (var op in _operations) |
| 217 | + { |
| 218 | + if (op is not IStreamSerializable serializable) |
| 219 | + throw new InvalidOperationException(""); |
| 220 | + |
| 221 | + op.PrepareIndex(index); |
| 222 | + |
| 223 | + await serializable.SerializeAsync(stream, settings, formatting).ConfigureAwait(false); |
| 224 | + stream.WriteByte((byte)'\n'); |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + private BulkRequestDescriptor AddOperations<TSource, TDescriptor>( |
| 229 | + IEnumerable<TSource> objects, |
| 230 | + Action<TDescriptor, TSource> configureDescriptor, |
| 231 | + Func<TSource, TDescriptor> createDescriptor |
| 232 | + ) where TDescriptor : IBulkOperation |
| 233 | + { |
| 234 | + if (@objects == null) |
| 235 | + return this; |
| 236 | + |
| 237 | + var objectsList = @objects.ToList(); |
| 238 | + var operations = new List<IBulkOperation>(objectsList.Count()); |
| 239 | + |
| 240 | + foreach (var o in objectsList) |
| 241 | + { |
| 242 | + var descriptor = createDescriptor(o); |
| 243 | + |
| 244 | + if (configureDescriptor is not null) |
| 245 | + { |
| 246 | + configureDescriptor(descriptor, o); |
| 247 | + } |
| 248 | + |
| 249 | + operations.Add(descriptor); |
| 250 | + } |
| 251 | + |
| 252 | + _operations.AddRange(operations); |
| 253 | + return Self; |
| 254 | + } |
| 255 | + } |
| 256 | +} |
0 commit comments