11using System . Collections . Immutable ;
22using System . Diagnostics . CodeAnalysis ;
3- using Google . Protobuf ;
4- using Google . Protobuf . Collections ;
5- using Google . Protobuf . WellKnownTypes ;
63using Ydb . Topic ;
74
85namespace Ydb . Sdk . Services . Topic . Reader ;
96
10- internal class InternalBatchMessages
11- {
12- public InternalBatchMessages (
13- ByteString data ,
14- string topic ,
15- long partitionId ,
16- string producerId ,
17- OffsetsRange offsetsRange ,
18- Timestamp createdAt ,
19- RepeatedField < MetadataItem > metadataItems ,
20- long approximatelyBytesSize )
21- {
22- Data = data ;
23- Topic = topic ;
24- PartitionId = partitionId ;
25- ProducerId = producerId ;
26- OffsetsRange = offsetsRange ;
27- CreatedAt = createdAt ;
28- MetadataItems = metadataItems ;
29- ApproximatelyBytesSize = approximatelyBytesSize ;
30- }
31-
32- private ByteString Data { get ; }
33- private string Topic { get ; }
34- private long PartitionId { get ; }
35- private string ProducerId { get ; }
36- private OffsetsRange OffsetsRange { get ; }
37- private Timestamp CreatedAt { get ; }
38- private RepeatedField < MetadataItem > MetadataItems { get ; }
39- private long ApproximatelyBytesSize { get ; }
40-
41- internal Message < TValue > ToPublicMessage < TValue > ( IDeserializer < TValue > deserializer ,
42- ReaderSession < TValue > readerSession )
43- {
44- readerSession . TryReadRequestBytes ( ApproximatelyBytesSize ) ;
45-
46- return new Message < TValue > (
47- data : deserializer . Deserialize ( Data . ToByteArray ( ) ) ,
48- topic : Topic ,
49- partitionId : PartitionId ,
50- producerId : ProducerId ,
51- createdAt : CreatedAt . ToDateTime ( ) ,
52- metadata : MetadataItems . Select ( item => new Metadata ( item . Key , item . Value . ToByteArray ( ) ) ) . ToImmutableArray ( ) ,
53- offsetsRange : OffsetsRange ,
54- readerSession : readerSession ,
55- approximatelyBytesSize : ApproximatelyBytesSize
56- ) ;
57- }
58- }
59-
607internal class InternalBatchMessages < TValue >
618{
629 private readonly StreamReadMessage . Types . ReadResponse . Types . Batch _batch ;
6310 private readonly PartitionSession _partitionSession ;
6411 private readonly IDeserializer < TValue > _deserializer ;
6512 private readonly ReaderSession < TValue > _readerSession ;
66- private readonly long _approximatelyBatchSizeOriginal ;
13+ private readonly long _approximatelyBatchSize ;
6714
68- private int _startMessageDataIndex = 0 ;
69- private long _approximatelyBatchSize ;
15+ private int _startMessageDataIndex ;
7016
7117 private int OriginalMessageCount => _batch . MessageData . Count ;
72-
73- internal bool IsActive => _startMessageDataIndex < OriginalMessageCount && _readerSession . IsActive ;
18+ private bool IsActive => _startMessageDataIndex < OriginalMessageCount && _readerSession . IsActive ;
7419
7520 public InternalBatchMessages (
7621 StreamReadMessage . Types . ReadResponse . Types . Batch batch ,
@@ -83,7 +28,6 @@ public InternalBatchMessages(
8328 _partitionSession = partitionsSession ;
8429 _readerSession = readerSession ;
8530 _deserializer = deserializer ;
86- _approximatelyBatchSizeOriginal = approximatelyBatchSize ;
8731 _approximatelyBatchSize = approximatelyBatchSize ;
8832 }
8933
@@ -97,7 +41,7 @@ internal bool TryDequeueMessage([MaybeNullWhen(false)] out Message<TValue> messa
9741
9842 var index = _startMessageDataIndex ++ ;
9943 var approximatelyMessageBytesSize = Utils
100- . CalculateApproximatelyBytesSize ( _approximatelyBatchSizeOriginal , OriginalMessageCount , index ) ;
44+ . CalculateApproximatelyBytesSize ( _approximatelyBatchSize , OriginalMessageCount , index ) ;
10145 var messageData = _batch . MessageData [ index ] ;
10246
10347 TValue value ;
@@ -110,21 +54,23 @@ internal bool TryDequeueMessage([MaybeNullWhen(false)] out Message<TValue> messa
11054 throw new ReaderException ( "Error when deserializing message data" , e ) ;
11155 }
11256
113- _approximatelyBatchSize -= approximatelyMessageBytesSize ;
57+ _readerSession . TryReadRequestBytes ( approximatelyMessageBytesSize ) ;
58+ var nextCommitedOffset = messageData . Offset + 1 ;
11459
11560 message = new Message < TValue > (
116- value ,
117- _partitionSession . TopicPath ,
118- _partitionSession . PartitionId ,
119- _batch . ProducerId ,
120- messageData . CreatedAt . ToDateTime ( ) ,
121- messageData . MetadataItems . Select ( item => new Metadata ( item . Key , item . Value . ToByteArray ( ) ) )
61+ data : value ,
62+ topic : _partitionSession . TopicPath ,
63+ partitionId : _partitionSession . PartitionId ,
64+ partitionSessionId : _partitionSession . PartitionSessionId ,
65+ producerId : _batch . ProducerId ,
66+ createdAt : messageData . CreatedAt . ToDateTime ( ) ,
67+ metadata : messageData . MetadataItems . Select ( item => new Metadata ( item . Key , item . Value . ToByteArray ( ) ) )
12268 . ToImmutableArray ( ) ,
123- new OffsetsRange { Start = _partitionSession . PrevEndOffsetMessage , End = messageData . Offset } ,
124- _readerSession ,
125- approximatelyMessageBytesSize
69+ offsetsRange : new OffsetsRange
70+ { Start = _partitionSession . PrevEndOffsetMessage , End = nextCommitedOffset } ,
71+ readerSession : _readerSession
12672 ) ;
127- _partitionSession . PrevEndOffsetMessage = messageData . Offset + 1 ;
73+ _partitionSession . PrevEndOffsetMessage = nextCommitedOffset ;
12874
12975 return true ;
13076 }
@@ -137,25 +83,29 @@ internal bool TryPublicBatch([MaybeNullWhen(false)] out BatchMessages<TValue> ba
13783 return false ;
13884 }
13985
86+ var nextCommitedOffset = _batch . MessageData . Last ( ) . Offset + 1 ;
14087 var offsetsRangeBatch = new OffsetsRange
141- { Start = _partitionSession . PrevEndOffsetMessage , End = _batch . MessageData . Last ( ) . Offset } ;
142- var approximatelyBatchSize = _approximatelyBatchSize ;
88+ { Start = _partitionSession . PrevEndOffsetMessage , End = nextCommitedOffset } ;
89+ _partitionSession . PrevEndOffsetMessage = nextCommitedOffset ;
14390
14491 var messages = new List < Message < TValue > > ( ) ;
14592 while ( TryDequeueMessage ( out var message ) )
14693 {
14794 messages . Add ( message ) ;
14895 }
14996
150- batchMessages = new BatchMessages < TValue > ( messages , _readerSession , approximatelyBatchSize , offsetsRangeBatch ) ;
97+ batchMessages = new BatchMessages < TValue > (
98+ batch : messages ,
99+ readerSession : _readerSession ,
100+ offsetsRange : offsetsRangeBatch ,
101+ partitionSessionId : _partitionSession . PartitionSessionId
102+ ) ;
151103
152104 return true ;
153105 }
154106}
155107
156108internal record CommitSending (
157109 OffsetsRange OffsetsRange ,
158- long PartitionSessionId ,
159- TaskCompletionSource TcsCommit ,
160- long ApproximatelyBytesSize
110+ TaskCompletionSource TcsCommit
161111) ;
0 commit comments