Skip to content

Commit d401427

Browse files
committed
refactor: remove getEncodedLength methods and use utils.GetEncodedLength for encoding length calculation
1 parent 2eead3f commit d401427

File tree

2 files changed

+2
-49
lines changed

2 files changed

+2
-49
lines changed

protocol/api/record_batch.go

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,12 @@ import (
88
"github.com/codecrafters-io/kafka-tester/protocol/decoder"
99
"github.com/codecrafters-io/kafka-tester/protocol/encoder"
1010
"github.com/codecrafters-io/kafka-tester/protocol/errors"
11+
"github.com/codecrafters-io/kafka-tester/protocol/utils"
1112
"github.com/codecrafters-io/tester-utils/logger"
1213
)
1314

1415
type RecordBatches []RecordBatch
1516

16-
func (rbs RecordBatches) getEncodedLength() int {
17-
encodedLength := 0
18-
for _, rb := range rbs {
19-
encodedLength += rb.getEncodedLength()
20-
}
21-
return encodedLength
22-
}
23-
2417
func (rbs RecordBatches) Encode(pe *encoder.Encoder) {
2518
for _, rb := range rbs {
2619
rb.Encode(pe)
@@ -76,13 +69,6 @@ func (rb RecordBatch) Encode(pe *encoder.Encoder) {
7669
pe.PutInt32At(int32(computedChecksum), crcStartOffset, 4)
7770
}
7871

79-
func (rb RecordBatch) getEncodedLength() int {
80-
encoder := encoder.Encoder{}
81-
encoder.Init(make([]byte, 1024))
82-
rb.Encode(&encoder)
83-
return encoder.Offset()
84-
}
85-
8672
func (rb *RecordBatch) Decode(pd *decoder.Decoder, logger *logger.Logger, indentation int) (err error) {
8773
if rb.BaseOffset, err = pd.GetInt64(); err != nil {
8874
if decodingErr, ok := err.(*errors.PacketDecodingError); ok {
@@ -234,7 +220,7 @@ type Record struct {
234220
}
235221

236222
func (r Record) Encode(pe *encoder.Encoder) {
237-
pe.PutVarint(int64(r.getEncodedLength())) // Length placeholder
223+
pe.PutVarint(int64(utils.GetEncodedLength(r))) // Length placeholder
238224
// As this is variable length, we can't use placeholders and update later reliably.
239225
// We need to have a value, close to the actual value, such that it takes the same space
240226
// This is an approx value, the actual value will be computed at the end
@@ -254,28 +240,6 @@ func (r Record) Encode(pe *encoder.Encoder) {
254240
}
255241
}
256242

257-
func (r Record) getEncodedLength() int {
258-
encoder := encoder.Encoder{}
259-
encoder.Init(make([]byte, 1024))
260-
261-
encoder.PutInt8(r.Attributes)
262-
encoder.PutVarint(r.TimestampDelta)
263-
encoder.PutVarint(int64(r.OffsetDelta))
264-
if string(r.Key) == "null" {
265-
encoder.PutCompactBytes([]byte{})
266-
} else {
267-
encoder.PutCompactBytes(r.Key)
268-
}
269-
encoder.PutVarint(int64(len(r.Value)))
270-
encoder.PutRawBytes(r.Value)
271-
encoder.PutVarint(int64(len(r.Headers)))
272-
for _, header := range r.Headers {
273-
header.Encode(&encoder)
274-
}
275-
276-
return encoder.Offset()
277-
}
278-
279243
func (r *Record) Decode(pd *decoder.Decoder, logger *logger.Logger, indentation int) (err error) {
280244
length, err := pd.GetSignedVarint()
281245
if err != nil {

protocol/serializer/utils.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,9 @@ import (
66
"os"
77
"strings"
88

9-
"github.com/codecrafters-io/kafka-tester/protocol/encoder"
10-
kafka_interface "github.com/codecrafters-io/kafka-tester/protocol/interface"
119
"github.com/google/uuid"
1210
)
1311

14-
func GetEncodedBytes(encodableObject kafka_interface.Encodable) []byte {
15-
encoder := encoder.Encoder{}
16-
encoder.Init(make([]byte, 1024))
17-
encodableObject.Encode(&encoder)
18-
encodedBytes := encoder.Bytes()[:encoder.Offset()]
19-
20-
return encodedBytes
21-
}
22-
2312
func generateDirectories(paths []string) error {
2413
for _, path := range paths {
2514
err := generateDirectory(path)

0 commit comments

Comments
 (0)