mirror of
https://github.com/taigrr/bitcask
synced 2025-01-18 04:03:17 -08:00
codec: collapse write and save extra alloc (#64)
This commit is contained in:
parent
7204a33512
commit
8f56cffd86
@ -28,28 +28,21 @@ type Encoder struct {
|
|||||||
// Encode takes any Entry and streams it to the underlying writer.
|
// Encode takes any Entry and streams it to the underlying writer.
|
||||||
// Messages are framed with a key-length and value-length prefix.
|
// Messages are framed with a key-length and value-length prefix.
|
||||||
func (e *Encoder) Encode(msg Entry) (int64, error) {
|
func (e *Encoder) Encode(msg Entry) (int64, error) {
|
||||||
var bufKeyValue = make([]byte, ValueSize)
|
var bufKeyValue = make([]byte, KeySize+ValueSize)
|
||||||
|
binary.BigEndian.PutUint32(bufKeyValue[:KeySize], uint32(len(msg.Key)))
|
||||||
bufKeySize := bufKeyValue[:KeySize]
|
binary.BigEndian.PutUint64(bufKeyValue[KeySize:KeySize+ValueSize], uint64(len(msg.Value)))
|
||||||
binary.BigEndian.PutUint32(bufKeySize, uint32(len(msg.Key)))
|
if _, err := e.w.Write(bufKeyValue); err != nil {
|
||||||
if _, err := e.w.Write(bufKeySize); err != nil {
|
return 0, errors.Wrap(err, "failed writing key & value length prefix")
|
||||||
return 0, errors.Wrap(err, "failed writing key length prefix")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bufValueSize := bufKeyValue[:ValueSize]
|
if _, err := e.w.Write(msg.Key); err != nil {
|
||||||
binary.BigEndian.PutUint64(bufValueSize, uint64(len(msg.Value)))
|
|
||||||
if _, err := e.w.Write(bufValueSize); err != nil {
|
|
||||||
return 0, errors.Wrap(err, "failed writing value length prefix")
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := e.w.Write([]byte(msg.Key)); err != nil {
|
|
||||||
return 0, errors.Wrap(err, "failed writing key data")
|
return 0, errors.Wrap(err, "failed writing key data")
|
||||||
}
|
}
|
||||||
if _, err := e.w.Write(msg.Value); err != nil {
|
if _, err := e.w.Write(msg.Value); err != nil {
|
||||||
return 0, errors.Wrap(err, "failed writing value data")
|
return 0, errors.Wrap(err, "failed writing value data")
|
||||||
}
|
}
|
||||||
|
|
||||||
bufChecksumSize := make([]byte, checksumSize)
|
bufChecksumSize := bufKeyValue[:checksumSize]
|
||||||
binary.BigEndian.PutUint32(bufChecksumSize, msg.Checksum)
|
binary.BigEndian.PutUint32(bufChecksumSize, msg.Checksum)
|
||||||
if _, err := e.w.Write(bufChecksumSize); err != nil {
|
if _, err := e.w.Write(bufChecksumSize); err != nil {
|
||||||
return 0, errors.Wrap(err, "failed writing checksum data")
|
return 0, errors.Wrap(err, "failed writing checksum data")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user