-
Notifications
You must be signed in to change notification settings - Fork 874
Expand file tree
/
Copy pathReplicationTuple.cs
More file actions
79 lines (69 loc) · 2.37 KB
/
ReplicationTuple.cs
File metadata and controls
79 lines (69 loc) · 2.37 KB
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
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Npgsql.BackendMessages;
using Npgsql.Internal;
namespace Npgsql.Replication.PgOutput;
/// <summary>
/// Represents a streaming tuple containing <see cref="ReplicationValue"/>.
/// </summary>
public class ReplicationTuple : IAsyncEnumerable<ReplicationValue>
{
private protected readonly NpgsqlReadBuffer ReadBuffer;
readonly TupleEnumerator _tupleEnumerator;
internal RowState State;
/// <summary>
/// The number of columns in the tuple.
/// </summary>
public ushort NumColumns { get; private set; }
RowDescriptionMessage _rowDescription = null!;
internal ReplicationTuple(NpgsqlConnector connector)
=> (ReadBuffer, _tupleEnumerator) = (connector.ReadBuffer, new(this, connector));
internal void Reset(ushort numColumns, RowDescriptionMessage rowDescription)
{
State = RowState.NotRead;
(NumColumns, _rowDescription) = (numColumns, rowDescription);
}
/// <inheritdoc />
public virtual IAsyncEnumerator<ReplicationValue> GetAsyncEnumerator(CancellationToken cancellationToken = default)
{
switch (State)
{
case RowState.NotRead:
_tupleEnumerator.Reset(NumColumns, _rowDescription, cancellationToken);
State = RowState.Reading;
return _tupleEnumerator;
case RowState.Reading:
throw new InvalidOperationException("The row is already been read.");
case RowState.Consumed:
throw new InvalidOperationException("The row has already been consumed.");
default:
throw new ArgumentOutOfRangeException();
}
}
internal async Task Consume(CancellationToken cancellationToken)
{
switch (State)
{
case RowState.NotRead:
State = RowState.Reading;
_tupleEnumerator.Reset(NumColumns, _rowDescription, cancellationToken);
while (await _tupleEnumerator.MoveNextAsync().ConfigureAwait(false)) { }
break;
case RowState.Reading:
while (await _tupleEnumerator.MoveNextAsync().ConfigureAwait(false)) { }
break;
case RowState.Consumed:
return;
default:
throw new ArgumentOutOfRangeException();
}
}
}
enum RowState
{
NotRead,
Reading,
Consumed
}