@@ -180,16 +180,21 @@ func (seg *segment) writeToBuffer(data []byte, chunkBuffer *bytebufferpool.ByteB
180
180
return nil , ErrClosed
181
181
}
182
182
183
+ // if the left block size can not hold the chunk header, padding the block
183
184
if seg .currentBlockSize + chunkHeaderSize >= blockSize {
184
185
// padding if necessary
185
186
if seg .currentBlockSize < blockSize {
186
- chunkBuffer .Write (make ([]byte , blockSize - seg .currentBlockSize ))
187
+ p := make ([]byte , blockSize - seg .currentBlockSize )
188
+ chunkBuffer .B = append (chunkBuffer .B , p ... )
187
189
padding += blockSize - seg .currentBlockSize
190
+
191
+ // a new block
188
192
seg .currentBlockNumber += 1
189
193
seg .currentBlockSize = 0
190
194
}
191
-
192
195
}
196
+
197
+ // return the start position of the chunk, for reading.
193
198
position := & ChunkPosition {
194
199
SegmentId : seg .id ,
195
200
BlockNumber : seg .currentBlockNumber ,
@@ -240,9 +245,12 @@ func (seg *segment) writeToBuffer(data []byte, chunkBuffer *bytebufferpool.ByteB
240
245
241
246
position .ChunkSize = blockCount * chunkHeaderSize + dataSize
242
247
}
248
+
249
+ // the buffer length must be equal to chunkSize+padding length
243
250
endLen := chunkBuffer .Len ()
244
251
if position .ChunkSize + padding != uint32 (endLen - startLen ) {
245
- panic (fmt .Sprintf ("chunk size %d, len %d" , position .ChunkSize , endLen - startLen - int (padding )))
252
+ panic (fmt .Sprintf ("wrong!!! the chunk size %d is not equal to the buffer len %d" ,
253
+ position .ChunkSize + padding , endLen - startLen ))
246
254
}
247
255
248
256
// update segment status
@@ -261,6 +269,7 @@ func (seg *segment) writeAll(data [][]byte) (positions []*ChunkPosition, err err
261
269
return nil , ErrClosed
262
270
}
263
271
272
+ // if any error occurs, restore the segment status
264
273
originBlockNumber := seg .currentBlockNumber
265
274
originBlockSize := seg .currentBlockSize
266
275
@@ -276,6 +285,7 @@ func (seg *segment) writeAll(data [][]byte) (positions []*ChunkPosition, err err
276
285
bytebufferpool .Put (chunkBuffer )
277
286
}()
278
287
288
+ // write all data to the chunk buffer
279
289
var pos * ChunkPosition
280
290
positions = make ([]* ChunkPosition , len (data ))
281
291
for i := 0 ; i < len (positions ); i ++ {
@@ -285,6 +295,7 @@ func (seg *segment) writeAll(data [][]byte) (positions []*ChunkPosition, err err
285
295
}
286
296
positions [i ] = pos
287
297
}
298
+ // write the chunk buffer to the segment file
288
299
if err = seg .writeChunkBuffer (chunkBuffer ); err != nil {
289
300
return
290
301
}
0 commit comments