stream/mts/meta.go: fixed Meta.Encode() func so that it calculates data length correctly

This commit is contained in:
saxon 2019-01-28 17:45:38 +10:30
parent 7d34fa1969
commit 960c110acb
2 changed files with 24 additions and 5 deletions

View File

@ -39,6 +39,11 @@ const (
minVer = 0 minVer = 0
) )
const (
dataLenIdx1 = 2
dataLenIdx2 = 3
)
var ( var (
errKeyAbsent = errors.New("Key does not exist in map") errKeyAbsent = errors.New("Key does not exist in map")
) )
@ -104,11 +109,23 @@ func (m *Meta) Delete(key string) error {
func (m *Meta) Encode() []byte { func (m *Meta) Encode() []byte {
m.enc = m.enc[:headSize] m.enc = m.enc[:headSize]
// Iterate over map and append entries, only adding tab if we're not on the last entry
var i int
var entry string
for k, v := range m.data { for k, v := range m.data {
entry := k + "=" + v + "\t" i++
m.enc = append(m.enc, []byte(entry)...) entry += k + "=" + v
if i < len(m.data) {
entry += "\t"
}
} }
// Remove final tab m.enc = append(m.enc, []byte(entry)...)
m.enc = m.enc[:len(m.enc)-1]
// Calculate and set data length in encoded meta header.
dataLen := len(m.enc[headSize:])
m.enc[dataLenIdx1] = byte(dataLen >> 8)
m.enc[dataLenIdx2] = byte(dataLen)
return m.enc return m.enc
} }

View File

@ -125,4 +125,6 @@ func TestDeleteAbsentKey(t *testing.T) {
} }
} }
// TODO: add test function for Encoding the Meta map data func TestEncode(t *testing.T) {
meta := NewMeta()
}