stream/mts/meta.go: completed Meta.Encode() function

This commit is contained in:
saxon 2019-01-28 17:29:15 +10:30
parent 00816ecf84
commit 7d34fa1969
1 changed files with 25 additions and 3 deletions

View File

@ -32,6 +32,13 @@ import (
"sync" "sync"
) )
const headSize = 4
const (
majVer = 1
minVer = 0
)
var ( var (
errKeyAbsent = errors.New("Key does not exist in map") errKeyAbsent = errors.New("Key does not exist in map")
) )
@ -39,10 +46,19 @@ var (
type Meta struct { type Meta struct {
mu sync.RWMutex mu sync.RWMutex
data map[string]string data map[string]string
enc []byte
} }
func NewMeta() *Meta { 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 // 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 { func (m *Meta) Encode() []byte {
// TODO: complete this m.enc = m.enc[:headSize]
return []byte("someData") 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
} }