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
)
const (
dataLenIdx1 = 2
dataLenIdx2 = 3
)
var (
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 {
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 {
entry := k + "=" + v + "\t"
m.enc = append(m.enc, []byte(entry)...)
i++
entry += k + "=" + v
if i < len(m.data) {
entry += "\t"
}
}
// Remove final tab
m.enc = m.enc[:len(m.enc)-1]
m.enc = append(m.enc, []byte(entry)...)
// 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
}

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()
}