-
Notifications
You must be signed in to change notification settings - Fork 765
/
Batch.cs
251 lines (214 loc) · 7.97 KB
/
Batch.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using System.Collections;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using OpenTelemetry.Internal;
using OpenTelemetry.Logs;
namespace OpenTelemetry;
/// <summary>
/// Stores a batch of completed <typeparamref name="T"/> objects to be exported.
/// </summary>
/// <typeparam name="T">The type of object in the <see cref="Batch{T}"/>.</typeparam>
public readonly struct Batch<T> : IDisposable
where T : class
{
private readonly T? item = null;
private readonly CircularBuffer<T>? circularBuffer = null;
private readonly T[]? items = null;
private readonly long targetCount;
/// <summary>
/// Initializes a new instance of the <see cref="Batch{T}"/> struct.
/// </summary>
/// <param name="items">The items to store in the batch.</param>
/// <param name="count">The number of items in the batch.</param>
public Batch(T[] items, int count)
{
Guard.ThrowIfNull(items);
Guard.ThrowIfOutOfRange(count, min: 0, max: items.Length);
this.items = items;
this.Count = this.targetCount = count;
}
/// <summary>
/// Initializes a new instance of the <see cref="Batch{T}"/> struct.
/// </summary>
/// <param name="item">The item to store in the batch.</param>
public Batch(T item)
{
Guard.ThrowIfNull(item);
this.item = item;
this.Count = this.targetCount = 1;
}
internal Batch(CircularBuffer<T> circularBuffer, int maxSize)
{
Debug.Assert(maxSize > 0, $"{nameof(maxSize)} should be a positive number.");
Debug.Assert(circularBuffer != null, $"{nameof(circularBuffer)} was null.");
this.circularBuffer = circularBuffer;
this.Count = Math.Min(maxSize, circularBuffer!.Count);
this.targetCount = circularBuffer.RemovedCount + this.Count;
}
private delegate bool BatchEnumeratorMoveNextFunc(ref Enumerator enumerator);
/// <summary>
/// Gets the count of items in the batch.
/// </summary>
public long Count { get; }
/// <inheritdoc/>
public void Dispose()
{
if (this.circularBuffer != null)
{
// Drain anything left in the batch.
while (this.circularBuffer.RemovedCount < this.targetCount)
{
T item = this.circularBuffer.Read();
if (typeof(T) == typeof(LogRecord))
{
var logRecord = (LogRecord)(object)item;
if (logRecord.Source == LogRecord.LogRecordSource.FromSharedPool)
{
LogRecordSharedPool.Current.Return(logRecord);
}
}
}
}
}
/// <summary>
/// Returns an enumerator that iterates through the <see cref="Batch{T}"/>.
/// </summary>
/// <returns><see cref="Enumerator"/>.</returns>
public Enumerator GetEnumerator()
{
return this.circularBuffer != null
? new Enumerator(this.circularBuffer, this.targetCount)
: this.item != null
? new Enumerator(this.item)
/* In the event someone uses default/new Batch() to create Batch we fallback to empty items mode. */
: new Enumerator(this.items ?? Array.Empty<T>(), this.targetCount);
}
/// <summary>
/// Enumerates the elements of a <see cref="Batch{T}"/>.
/// </summary>
public struct Enumerator : IEnumerator<T>
{
private static readonly BatchEnumeratorMoveNextFunc MoveNextSingleItem = (ref Enumerator enumerator) =>
{
if (enumerator.targetCount >= 0)
{
enumerator.current = null;
return false;
}
enumerator.targetCount++;
return true;
};
private static readonly BatchEnumeratorMoveNextFunc MoveNextCircularBuffer = (ref Enumerator enumerator) =>
{
var circularBuffer = enumerator.circularBuffer;
if (circularBuffer!.RemovedCount < enumerator.targetCount)
{
enumerator.current = circularBuffer.Read();
return true;
}
enumerator.current = null;
return false;
};
private static readonly BatchEnumeratorMoveNextFunc MoveNextCircularBufferLogRecord = (ref Enumerator enumerator) =>
{
// Note: This type check here is to give the JIT a hint it can
// remove all of this code when T != LogRecord
if (typeof(T) == typeof(LogRecord))
{
var circularBuffer = enumerator.circularBuffer;
var currentItem = enumerator.Current;
if (currentItem != null)
{
var logRecord = (LogRecord)(object)currentItem;
if (logRecord.Source == LogRecord.LogRecordSource.FromSharedPool)
{
LogRecordSharedPool.Current.Return(logRecord);
}
}
if (circularBuffer!.RemovedCount < enumerator.targetCount)
{
enumerator.current = circularBuffer.Read();
return true;
}
enumerator.current = null;
}
return false;
};
private static readonly BatchEnumeratorMoveNextFunc MoveNextArray = (ref Enumerator enumerator) =>
{
var items = enumerator.items;
if (enumerator.itemIndex < enumerator.targetCount)
{
enumerator.current = items![enumerator.itemIndex++];
return true;
}
enumerator.current = null;
return false;
};
private readonly CircularBuffer<T>? circularBuffer;
private readonly T[]? items;
private readonly BatchEnumeratorMoveNextFunc moveNextFunc;
private long targetCount;
private int itemIndex;
[AllowNull]
private T current;
internal Enumerator(T item)
{
this.current = item;
this.circularBuffer = null;
this.items = null;
this.targetCount = -1;
this.itemIndex = 0;
this.moveNextFunc = MoveNextSingleItem;
}
internal Enumerator(CircularBuffer<T> circularBuffer, long targetCount)
{
this.current = null;
this.items = null;
this.circularBuffer = circularBuffer;
this.targetCount = targetCount;
this.itemIndex = 0;
this.moveNextFunc = typeof(T) == typeof(LogRecord) ? MoveNextCircularBufferLogRecord : MoveNextCircularBuffer;
}
internal Enumerator(T[] items, long targetCount)
{
this.current = null;
this.circularBuffer = null;
this.items = items;
this.targetCount = targetCount;
this.itemIndex = 0;
this.moveNextFunc = MoveNextArray;
}
/// <inheritdoc/>
public readonly T Current => this.current;
/// <inheritdoc/>
readonly object? IEnumerator.Current => this.current;
/// <inheritdoc/>
public void Dispose()
{
if (typeof(T) == typeof(LogRecord))
{
var currentItem = this.current;
if (currentItem != null)
{
var logRecord = (LogRecord)(object)currentItem;
if (logRecord.Source == LogRecord.LogRecordSource.FromSharedPool)
{
LogRecordSharedPool.Current.Return(logRecord);
}
this.current = null;
}
}
}
/// <inheritdoc/>
public bool MoveNext()
{
return this.moveNextFunc(ref this);
}
/// <inheritdoc/>
public readonly void Reset()
=> throw new NotSupportedException();
}
}