stream/mts/meta.go: improved const and function commenting

This commit is contained in:
saxon 2019-02-04 22:38:11 +10:30
parent 0a96d18a10
commit 953d363b3a
1 changed files with 17 additions and 8 deletions

View File

@ -33,6 +33,8 @@ import (
"sync" "sync"
) )
// This is the headsize of our metadata string,
// which is encoded int the data body of a pmt descriptor.
const headSize = 4 const headSize = 4
const ( const (
@ -40,6 +42,7 @@ const (
minVer = 0 minVer = 0
) )
// Indices of bytes for uint16 metadata length.
const ( const (
dataLenIdx1 = 2 dataLenIdx1 = 2
dataLenIdx2 = 3 dataLenIdx2 = 3
@ -49,12 +52,15 @@ var (
errKeyAbsent = errors.New("Key does not exist in map") errKeyAbsent = errors.New("Key does not exist in map")
) )
// Metadata provides functionality for the storage and encoding of metadata
// using a map.
type Metadata struct { type Metadata struct {
mu sync.RWMutex mu sync.RWMutex
data map[string]string data map[string]string
enc []byte enc []byte
} }
// New returns a pointer to a new Metadata.
func New() *Metadata { func New() *Metadata {
return &Metadata{ return &Metadata{
data: make(map[string]string), data: make(map[string]string),
@ -67,14 +73,14 @@ func New() *Metadata {
} }
} }
// Add adds metadata with key and val // Add adds metadata with key and val.
func (m *Metadata) Add(key, val string) { func (m *Metadata) Add(key, val string) {
m.mu.Lock() m.mu.Lock()
m.data[key] = val m.data[key] = val
m.mu.Unlock() m.mu.Unlock()
} }
// All returns the a copy of the map containing the meta data // All returns the a copy of the map containing the meta data.
func (m *Metadata) All() map[string]string { func (m *Metadata) All() map[string]string {
m.mu.Lock() m.mu.Lock()
cpy := make(map[string]string) cpy := make(map[string]string)
@ -85,7 +91,7 @@ func (m *Metadata) All() map[string]string {
return cpy return cpy
} }
// Get returns the meta data for the passed key // Get returns the meta data for the passed key.
func (m *Metadata) Get(key string) (string, error) { func (m *Metadata) Get(key string) (string, error) {
m.mu.Lock() m.mu.Lock()
val, ok := m.data[key] val, ok := m.data[key]
@ -93,11 +99,10 @@ func (m *Metadata) Get(key string) (string, error) {
if !ok { if !ok {
return "", errKeyAbsent return "", errKeyAbsent
} }
return val, nil return val, nil
} }
// Remove deletes a meta entry in the map and returns error if it doesnt exist // Delete deletes a meta entry in the map and returns error if it doesnt exist.
func (m *Metadata) Delete(key string) error { func (m *Metadata) Delete(key string) error {
m.mu.Lock() m.mu.Lock()
defer m.mu.Unlock() defer m.mu.Unlock()
@ -108,12 +113,13 @@ func (m *Metadata) Delete(key string) error {
return errKeyAbsent return errKeyAbsent
} }
// Encode takes the meta data map and encods into a byte slice with header // Encode takes the meta data map and encodes into a byte slice with header
// describing the version, length of data and data in TSV format. // describing the version, length of data and data in TSV format.
func (m *Metadata) Encode() []byte { func (m *Metadata) 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 // Iterate over map and append entries, only adding tab if we're not on the
// last entry.
var i int var i int
var entry string var entry string
for k, v := range m.data { for k, v := range m.data {
@ -133,6 +139,9 @@ func (m *Metadata) Encode() []byte {
return m.enc return m.enc
} }
// ReadFrom extracts a value from a metadata string d, for the given key. If the
// key is not present in the metadata string, an error is returned. If the
// metadata header is not present in the string, an error is returned.
func ReadFrom(d []byte, key string) (string, error) { func ReadFrom(d []byte, key string) (string, error) {
entries := strings.Split(string(d), "\t") entries := strings.Split(string(d), "\t")
for _, entry := range entries { for _, entry := range entries {
@ -141,5 +150,5 @@ func ReadFrom(d []byte, key string) (string, error) {
return kv[1], nil return kv[1], nil
} }
} }
return "", errors.New("could not find key in metadata") return "", errKeyAbsent
} }