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"
)
// This is the headsize of our metadata string,
// which is encoded int the data body of a pmt descriptor.
const headSize = 4
const (
@ -40,6 +42,7 @@ const (
minVer = 0
)
// Indices of bytes for uint16 metadata length.
const (
dataLenIdx1 = 2
dataLenIdx2 = 3
@ -49,12 +52,15 @@ var (
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 {
mu sync.RWMutex
data map[string]string
enc []byte
}
// New returns a pointer to a new Metadata.
func New() *Metadata {
return &Metadata{
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) {
m.mu.Lock()
m.data[key] = val
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 {
m.mu.Lock()
cpy := make(map[string]string)
@ -85,7 +91,7 @@ func (m *Metadata) All() map[string]string {
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) {
m.mu.Lock()
val, ok := m.data[key]
@ -93,11 +99,10 @@ func (m *Metadata) Get(key string) (string, error) {
if !ok {
return "", errKeyAbsent
}
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 {
m.mu.Lock()
defer m.mu.Unlock()
@ -108,12 +113,13 @@ func (m *Metadata) Delete(key string) error {
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.
func (m *Metadata) Encode() []byte {
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 entry string
for k, v := range m.data {
@ -133,6 +139,9 @@ func (m *Metadata) Encode() []byte {
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) {
entries := strings.Split(string(d), "\t")
for _, entry := range entries {
@ -141,5 +150,5 @@ func ReadFrom(d []byte, key string) (string, error) {
return kv[1], nil
}
}
return "", errors.New("could not find key in metadata")
return "", errKeyAbsent
}