From 7d34fa1969007b4a531dd6fdeb8129eba082ba30 Mon Sep 17 00:00:00 2001 From: saxon Date: Mon, 28 Jan 2019 17:29:15 +1030 Subject: [PATCH] stream/mts/meta.go: completed Meta.Encode() function --- stream/mts/meta.go | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/stream/mts/meta.go b/stream/mts/meta.go index 1f482790..c25d3e85 100644 --- a/stream/mts/meta.go +++ b/stream/mts/meta.go @@ -32,6 +32,13 @@ import ( "sync" ) +const headSize = 4 + +const ( + majVer = 1 + minVer = 0 +) + var ( errKeyAbsent = errors.New("Key does not exist in map") ) @@ -39,10 +46,19 @@ var ( type Meta struct { mu sync.RWMutex data map[string]string + enc []byte } func NewMeta() *Meta { - return &Meta{data: make(map[string]string)} + return &Meta{ + data: make(map[string]string), + enc: []byte{ + 0x00, // Reserved byte + (majVer << 4) | minVer, // MS and LS versions + 0x00, // Data len byte1 + 0x00, // Data len byte2 + }, + } } // Add adds metadata with key and val, if already exists return error @@ -87,6 +103,12 @@ func (m *Meta) Delete(key string) error { } func (m *Meta) Encode() []byte { - // TODO: complete this - return []byte("someData") + m.enc = m.enc[:headSize] + for k, v := range m.data { + entry := k + "=" + v + "\t" + m.enc = append(m.enc, []byte(entry)...) + } + // Remove final tab + m.enc = m.enc[:len(m.enc)-1] + return m.enc }