-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodec_optimized.go
More file actions
35 lines (27 loc) · 800 Bytes
/
codec_optimized.go
File metadata and controls
35 lines (27 loc) · 800 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package bmdb
import (
"errors"
)
var errShortBuffer = errors.New("short buffer for decode")
// DecodeIntoEntry decodes bytes produced by Entry.Encode into the provided Entry.
// It reuses backing arrays for Key and Value to minimize allocations.
func DecodeIntoEntry(b []byte, e *Entry) error {
if e == nil {
return errors.New("nil entry")
}
// Parse header/meta once and get header length
headerLen, err := e.ParseMeta(b)
if err != nil {
return err
}
// Ensure payload length is sufficient
payloadStart := int(headerLen)
keyEnd := payloadStart + int(e.Meta.KeySize)
valEnd := keyEnd + int(e.Meta.ValueSize)
if len(b) < valEnd {
return errShortBuffer
}
e.Key = append(e.Key[:0], b[payloadStart:keyEnd]...)
e.Value = append(e.Value[:0], b[keyEnd:valEnd]...)
return nil
}