1
- package topicreaderinternal
1
+ package topicreadercommon
2
2
3
3
import (
4
4
"context"
@@ -20,17 +20,17 @@ type PublicBatch struct {
20
20
21
21
Messages []* PublicMessage
22
22
23
- commitRange commitRange // от всех сообщений батча
23
+ commitRange CommitRange // от всех сообщений батча
24
24
}
25
25
26
- func newBatch (session * partitionSession , messages []* PublicMessage ) (* PublicBatch , error ) {
26
+ func NewBatch (session * PartitionSession , messages []* PublicMessage ) (* PublicBatch , error ) {
27
27
for i := 0 ; i < len (messages ); i ++ {
28
28
msg := messages [i ]
29
29
30
- if msg .commitRange .partitionSession == nil {
31
- msg .commitRange .partitionSession = session
30
+ if msg .commitRange .PartitionSession == nil {
31
+ msg .commitRange .PartitionSession = session
32
32
}
33
- if session != msg .commitRange .partitionSession {
33
+ if session != msg .commitRange .PartitionSession {
34
34
return nil , xerrors .WithStackTrace (errBadSessionWhileMessageBatchCreate )
35
35
}
36
36
@@ -39,17 +39,17 @@ func newBatch(session *partitionSession, messages []*PublicMessage) (*PublicBatc
39
39
}
40
40
41
41
prev := messages [i - 1 ]
42
- if prev .commitRange .commitOffsetEnd != msg .commitRange .commitOffsetStart {
42
+ if prev .commitRange .CommitOffsetEnd != msg .commitRange .CommitOffsetStart {
43
43
return nil , xerrors .WithStackTrace (errBadMessageOffsetWhileMessageBatchCreate )
44
44
}
45
45
}
46
46
47
- offset := commitRange {
48
- partitionSession : session ,
47
+ offset := CommitRange {
48
+ PartitionSession : session ,
49
49
}
50
50
if len (messages ) > 0 {
51
- offset .commitOffsetStart = messages [0 ].commitRange .commitOffsetStart
52
- offset .commitOffsetEnd = messages [len (messages )- 1 ].commitRange .commitOffsetEnd
51
+ offset .CommitOffsetStart = messages [0 ].commitRange .CommitOffsetStart
52
+ offset .CommitOffsetEnd = messages [len (messages )- 1 ].commitRange .CommitOffsetEnd
53
53
}
54
54
55
55
return & PublicBatch {
@@ -58,13 +58,13 @@ func newBatch(session *partitionSession, messages []*PublicMessage) (*PublicBatc
58
58
}, nil
59
59
}
60
60
61
- func newBatchFromStream (
62
- decoders decoderMap ,
63
- session * partitionSession ,
61
+ func NewBatchFromStream (
62
+ decoders DecoderMap ,
63
+ session * PartitionSession ,
64
64
sb rawtopicreader.Batch , //nolint:gocritic
65
65
) (* PublicBatch , error ) {
66
66
messages := make ([]* PublicMessage , len (sb .MessageData ))
67
- prevOffset := session .lastReceivedMessageOffset ()
67
+ prevOffset := session .LastReceivedMessageOffset ()
68
68
for i := range sb .MessageData {
69
69
sMess := & sb .MessageData [i ]
70
70
@@ -82,9 +82,9 @@ func newBatchFromStream(
82
82
dstMess .data = createReader (decoders , sb .Codec , sMess .Data )
83
83
dstMess .UncompressedSize = int (sMess .UncompressedSize )
84
84
85
- dstMess .commitRange .partitionSession = session
86
- dstMess .commitRange .commitOffsetStart = prevOffset + 1
87
- dstMess .commitRange .commitOffsetEnd = sMess .Offset + 1
85
+ dstMess .commitRange .PartitionSession = session
86
+ dstMess .commitRange .CommitOffsetStart = prevOffset + 1
87
+ dstMess .commitRange .CommitOffsetEnd = sMess .Offset + 1
88
88
89
89
if len (sMess .MetadataItems ) > 0 {
90
90
dstMess .Metadata = make (map [string ][]byte , len (sMess .MetadataItems ))
@@ -96,15 +96,15 @@ func newBatchFromStream(
96
96
prevOffset = sMess .Offset
97
97
}
98
98
99
- session .setLastReceivedMessageOffset (prevOffset )
99
+ session .SetLastReceivedMessageOffset (prevOffset )
100
100
101
- return newBatch (session , messages )
101
+ return NewBatch (session , messages )
102
102
}
103
103
104
104
// Context is cancelled when code should stop to process messages batch
105
105
// for example - lost connection to server or receive stop partition signal without graceful flag
106
106
func (m * PublicBatch ) Context () context.Context {
107
- return m .commitRange .partitionSession .Context ()
107
+ return m .commitRange .PartitionSession .Context ()
108
108
}
109
109
110
110
// Topic is path of source topic of the messages in the batch
@@ -117,56 +117,14 @@ func (m *PublicBatch) PartitionID() int64 {
117
117
return m .partitionSession ().PartitionID
118
118
}
119
119
120
- func (m * PublicBatch ) partitionSession () * partitionSession {
121
- return m .commitRange .partitionSession
120
+ func (m * PublicBatch ) partitionSession () * PartitionSession {
121
+ return m .commitRange .PartitionSession
122
122
}
123
123
124
124
func (m * PublicBatch ) getCommitRange () PublicCommitRange {
125
125
return m .commitRange .getCommitRange ()
126
126
}
127
127
128
- func (m * PublicBatch ) append (b * PublicBatch ) (* PublicBatch , error ) {
129
- var res * PublicBatch
130
- if m == nil {
131
- res = & PublicBatch {}
132
- } else {
133
- res = m
134
- }
135
-
136
- if res .commitRange .partitionSession != b .commitRange .partitionSession {
137
- return nil , xerrors .WithStackTrace (errors .New ("ydb: bad partition session for merge" ))
138
- }
139
-
140
- if res .commitRange .commitOffsetEnd != b .commitRange .commitOffsetStart {
141
- return nil , xerrors .WithStackTrace (errors .New ("ydb: bad offset interval for merge" ))
142
- }
143
-
144
- res .Messages = append (res .Messages , b .Messages ... )
145
- res .commitRange .commitOffsetEnd = b .commitRange .commitOffsetEnd
146
-
147
- return res , nil
148
- }
149
-
150
- func (m * PublicBatch ) cutMessages (count int ) (head , rest * PublicBatch ) {
151
- switch {
152
- case count == 0 :
153
- return nil , m
154
- case count >= len (m .Messages ):
155
- return m , nil
156
- default :
157
- // slice[0:count:count] - limit slice capacity and prevent overwrite rest by append messages to head
158
- // explicit 0 need for prevent typos, when type slice[count:count] instead of slice[:count:count]
159
- head , _ = newBatch (m .commitRange .partitionSession , m .Messages [0 :count :count ])
160
- rest , _ = newBatch (m .commitRange .partitionSession , m .Messages [count :])
161
-
162
- return head , rest
163
- }
164
- }
165
-
166
- func (m * PublicBatch ) isEmpty () bool {
167
- return m == nil || len (m .Messages ) == 0
168
- }
169
-
170
128
func splitBytesByMessagesInBatches (batches []* PublicBatch , totalBytesCount int ) error {
171
129
restBytes := totalBytesCount
172
130
@@ -219,3 +177,55 @@ func splitBytesByMessagesInBatches(batches []*PublicBatch, totalBytesCount int)
219
177
220
178
return nil
221
179
}
180
+
181
+ func BatchAppend (original , appended * PublicBatch ) (* PublicBatch , error ) {
182
+ var res * PublicBatch
183
+ if original == nil {
184
+ res = & PublicBatch {}
185
+ } else {
186
+ res = original
187
+ }
188
+
189
+ if res .commitRange .PartitionSession != appended .commitRange .PartitionSession {
190
+ return nil , xerrors .WithStackTrace (errors .New ("ydb: bad partition session for merge" ))
191
+ }
192
+
193
+ if res .commitRange .CommitOffsetEnd != appended .commitRange .CommitOffsetStart {
194
+ return nil , xerrors .WithStackTrace (errors .New ("ydb: bad offset interval for merge" ))
195
+ }
196
+
197
+ res .Messages = append (res .Messages , appended .Messages ... )
198
+ res .commitRange .CommitOffsetEnd = appended .commitRange .CommitOffsetEnd
199
+
200
+ return res , nil
201
+ }
202
+
203
+ func BatchCutMessages (b * PublicBatch , count int ) (head , rest * PublicBatch ) {
204
+ switch {
205
+ case count == 0 :
206
+ return nil , b
207
+ case count >= len (b .Messages ):
208
+ return b , nil
209
+ default :
210
+ // slice[0:count:count] - limit slice capacity and prevent overwrite rest by append messages to head
211
+ // explicit 0 need for prevent typos, when type slice[count:count] instead of slice[:count:count]
212
+ head , _ = NewBatch (b .commitRange .PartitionSession , b .Messages [0 :count :count ])
213
+ rest , _ = NewBatch (b .commitRange .PartitionSession , b .Messages [count :])
214
+
215
+ return head , rest
216
+ }
217
+ }
218
+
219
+ func BatchIsEmpty (b * PublicBatch ) bool {
220
+ return b == nil || len (b .Messages ) == 0
221
+ }
222
+
223
+ func BatchGetPartitionSessionID (item * PublicBatch ) rawtopicreader.PartitionSessionID {
224
+ return item .partitionSession ().PartitionSessionID
225
+ }
226
+
227
+ func BatchSetCommitRangeForTest (b * PublicBatch , commitRange CommitRange ) * PublicBatch {
228
+ b .commitRange = commitRange
229
+
230
+ return b
231
+ }
0 commit comments