Skip to content

Commit 5b0a5c5

Browse files
feat: init reader ydb topics (#241)
1 parent 99c3c52 commit 5b0a5c5

File tree

9 files changed

+988
-82
lines changed

9 files changed

+988
-82
lines changed

src/Ydb.Sdk/src/Services/Topic/Exceptions.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,20 @@ public WriterException(string message, Exception inner) : base(message, inner)
1717

1818
public class ReaderException : Exception
1919
{
20-
protected ReaderException(string message) : base(message)
20+
public ReaderException(string message) : base(message)
2121
{
22+
Status = new Status(StatusCode.Unspecified);
2223
}
24+
25+
public ReaderException(string message, Status status) : base(message + ": " + status)
26+
{
27+
Status = status;
28+
}
29+
30+
public ReaderException(string message, Driver.TransportException e) : base(message, e)
31+
{
32+
Status = e.Status;
33+
}
34+
35+
public Status Status { get; }
2336
}

src/Ydb.Sdk/src/Services/Topic/IReader.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace Ydb.Sdk.Services.Topic;
44

5-
public interface IReader<TValue>
5+
public interface IReader<TValue> : IDisposable
66
{
7-
public Task<TValue> ReadAsync();
7+
public ValueTask<Message<TValue>> ReadAsync(CancellationToken cancellationToken = default);
88

9-
public Task<Message<TValue>> ReadMessageAsync();
9+
public ValueTask<BatchMessage<TValue>> ReadBatchAsync(CancellationToken cancellationToken = default);
1010
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using System.Collections.Immutable;
2+
using Google.Protobuf;
3+
using Google.Protobuf.Collections;
4+
using Google.Protobuf.WellKnownTypes;
5+
using Ydb.Topic;
6+
7+
namespace Ydb.Sdk.Services.Topic.Reader;
8+
9+
internal class InternalMessage
10+
{
11+
public InternalMessage(
12+
ByteString data,
13+
string topic,
14+
long partitionId,
15+
string producerId,
16+
OffsetsRange offsetsRange,
17+
Timestamp createdAt,
18+
RepeatedField<MetadataItem> metadataItems,
19+
long approximatelyBytesSize)
20+
{
21+
Data = data;
22+
Topic = topic;
23+
PartitionId = partitionId;
24+
ProducerId = producerId;
25+
OffsetsRange = offsetsRange;
26+
CreatedAt = createdAt;
27+
MetadataItems = metadataItems;
28+
ApproximatelyBytesSize = approximatelyBytesSize;
29+
}
30+
31+
private ByteString Data { get; }
32+
33+
private string Topic { get; }
34+
35+
private long PartitionId { get; }
36+
37+
private string ProducerId { get; }
38+
39+
private OffsetsRange OffsetsRange { get; }
40+
41+
private Timestamp CreatedAt { get; }
42+
43+
private RepeatedField<MetadataItem> MetadataItems { get; }
44+
45+
private long ApproximatelyBytesSize { get; }
46+
47+
internal Message<TValue> ToPublicMessage<TValue>(IDeserializer<TValue> deserializer, ReaderSession readerSession)
48+
{
49+
return new Message<TValue>(
50+
data: deserializer.Deserialize(Data.ToByteArray()),
51+
topic: Topic,
52+
partitionId: PartitionId,
53+
producerId: ProducerId,
54+
createdAt: CreatedAt.ToDateTime(),
55+
metadata: MetadataItems.Select(item => new Metadata(item.Key, item.Value.ToByteArray())).ToImmutableArray(),
56+
offsetsRange: OffsetsRange,
57+
readerSession: readerSession,
58+
approximatelyBytesSize: ApproximatelyBytesSize
59+
);
60+
}
61+
}
62+
63+
internal class InternalBatchMessage
64+
{
65+
public InternalBatchMessage(
66+
OffsetsRange batchOffsetsRange,
67+
Queue<InternalMessage> internalMessages,
68+
ReaderSession readerSession,
69+
long approximatelyBatchSize)
70+
{
71+
BatchOffsetsRange = batchOffsetsRange;
72+
InternalMessages = internalMessages;
73+
ReaderSession = readerSession;
74+
ApproximatelyBatchSize = approximatelyBatchSize;
75+
}
76+
77+
internal OffsetsRange BatchOffsetsRange { get; }
78+
79+
internal Queue<InternalMessage> InternalMessages { get; }
80+
81+
internal ReaderSession ReaderSession { get; }
82+
83+
internal long ApproximatelyBatchSize { get; }
84+
}
85+
86+
internal record CommitSending(
87+
OffsetsRange OffsetsRange,
88+
long PartitionSessionId,
89+
TaskCompletionSource TcsCommit,
90+
long ApproximatelyBytesSize
91+
);
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,100 @@
1+
using System.Collections.Immutable;
2+
using Ydb.Topic;
3+
14
namespace Ydb.Sdk.Services.Topic.Reader;
25

36
public class Message<TValue>
47
{
8+
private readonly OffsetsRange _offsetsRange;
9+
private readonly ReaderSession _readerSession;
10+
private readonly long _approximatelyBytesSize;
11+
12+
internal Message(
13+
TValue data,
14+
string topic,
15+
long partitionId,
16+
string producerId,
17+
DateTime createdAt,
18+
ImmutableArray<Metadata> metadata,
19+
OffsetsRange offsetsRange,
20+
ReaderSession readerSession,
21+
long approximatelyBytesSize)
22+
{
23+
Data = data;
24+
Topic = topic;
25+
PartitionId = partitionId;
26+
ProducerId = producerId;
27+
CreatedAt = createdAt;
28+
Metadata = metadata;
29+
30+
_offsetsRange = offsetsRange;
31+
_readerSession = readerSession;
32+
_approximatelyBytesSize = approximatelyBytesSize;
33+
}
34+
35+
public TValue Data { get; }
36+
37+
/// <summary>
38+
/// The topic associated with the message.
39+
/// </summary>
40+
public string Topic { get; }
41+
42+
public long PartitionId { get; }
43+
44+
public string ProducerId { get; }
45+
46+
public DateTime CreatedAt { get; }
47+
48+
public ImmutableArray<Metadata> Metadata { get; }
49+
50+
internal long Start => _offsetsRange.Start;
51+
internal long End => _offsetsRange.End;
52+
53+
public Task CommitAsync()
54+
{
55+
return _readerSession.CommitOffsetRange(_offsetsRange, PartitionId, _approximatelyBytesSize);
56+
}
57+
}
58+
59+
public class BatchMessage<TValue>
60+
{
61+
private readonly ReaderSession _readerSession;
62+
private readonly long _approximatelyBatchSize;
63+
64+
public ImmutableArray<Message<TValue>> Batch { get; }
65+
66+
internal BatchMessage(
67+
ImmutableArray<Message<TValue>> batch,
68+
ReaderSession readerSession,
69+
long approximatelyBatchSize)
70+
{
71+
Batch = batch;
72+
_readerSession = readerSession;
73+
_approximatelyBatchSize = approximatelyBatchSize;
74+
}
75+
76+
public Task CommitBatchAsync()
77+
{
78+
if (Batch.Length == 0)
79+
{
80+
return Task.CompletedTask;
81+
}
82+
83+
var offsetsRange = new OffsetsRange { Start = Batch.First().Start, End = Batch.Last().End };
84+
85+
return _readerSession.CommitOffsetRange(offsetsRange, Batch.First().PartitionId, _approximatelyBatchSize);
86+
}
87+
}
88+
89+
public class TopicPartitionOffset
90+
{
91+
public TopicPartitionOffset(long offset, long partitionId)
92+
{
93+
Offset = offset;
94+
PartitionId = partitionId;
95+
}
96+
97+
public long Offset { get; }
98+
99+
public long PartitionId { get; }
5100
}

0 commit comments

Comments
 (0)