|
|
@@ -1,7 +1,5 @@
|
|
|
package sarama
|
|
|
|
|
|
-import "encoding/binary"
|
|
|
-
|
|
|
const (
|
|
|
controlMask = 0x20
|
|
|
)
|
|
|
@@ -37,17 +35,12 @@ type Record struct {
|
|
|
Value []byte
|
|
|
Headers []*RecordHeader
|
|
|
|
|
|
- lengthComputed bool
|
|
|
- length int64
|
|
|
- totalLength int
|
|
|
+ length varintLengthField
|
|
|
+ totalLength int
|
|
|
}
|
|
|
|
|
|
func (r *Record) encode(pe packetEncoder) error {
|
|
|
- if err := r.computeLength(); err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
-
|
|
|
- pe.putVarint(r.length)
|
|
|
+ pe.push(&r.length)
|
|
|
pe.putInt8(r.Attributes)
|
|
|
pe.putVarint(r.TimestampDelta)
|
|
|
pe.putVarint(r.OffsetDelta)
|
|
|
@@ -65,19 +58,16 @@ func (r *Record) encode(pe packetEncoder) error {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- return nil
|
|
|
+ return pe.pop()
|
|
|
}
|
|
|
|
|
|
func (r *Record) decode(pd packetDecoder) (err error) {
|
|
|
- length, err := newVarintLengthField(pd)
|
|
|
- if err != nil {
|
|
|
+ if err := r.length.decode(pd); err != nil {
|
|
|
return err
|
|
|
}
|
|
|
- if err = pd.push(length); err != nil {
|
|
|
+ if err = pd.push(&r.length); err != nil {
|
|
|
return err
|
|
|
}
|
|
|
- r.length = length.length
|
|
|
- r.lengthComputed = true
|
|
|
|
|
|
if r.Attributes, err = pd.getInt8(); err != nil {
|
|
|
return err
|
|
|
@@ -118,32 +108,12 @@ func (r *Record) decode(pd packetDecoder) (err error) {
|
|
|
return pd.pop()
|
|
|
}
|
|
|
|
|
|
-// Because the length is varint we can't reserve a fixed amount of bytes for it.
|
|
|
-// We use the prepEncoder to figure out the length of the record and then we cache it.
|
|
|
-func (r *Record) computeLength() error {
|
|
|
- if !r.lengthComputed {
|
|
|
- r.lengthComputed = true
|
|
|
-
|
|
|
- var prep prepEncoder
|
|
|
- if err := r.encode(&prep); err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- // subtract 1 because we don't want to include the length field itself (which 1 byte, the
|
|
|
- // length of varint encoding of 0)
|
|
|
- r.length = int64(prep.length) - 1
|
|
|
- }
|
|
|
-
|
|
|
- return nil
|
|
|
-}
|
|
|
-
|
|
|
func (r *Record) getTotalLength() (int, error) {
|
|
|
- if r.totalLength == 0 {
|
|
|
- if err := r.computeLength(); err != nil {
|
|
|
+ var prep prepEncoder
|
|
|
+ if !r.length.adjusted {
|
|
|
+ if err := r.encode(&prep); err != nil {
|
|
|
return 0, err
|
|
|
}
|
|
|
- var buf [binary.MaxVarintLen64]byte
|
|
|
- r.totalLength = int(r.length) + binary.PutVarint(buf[:], r.length)
|
|
|
}
|
|
|
-
|
|
|
- return r.totalLength, nil
|
|
|
+ return int(r.length.length) + r.length.size, nil
|
|
|
}
|