mirror of https://bitbucket.org/ausocean/av.git
stream/mts/meta: using order slice so that encoded order reamins consistent with order that we added metadata. And also now using binary.BigEndian.PutUint16() to encode len into metdata rather than doing ugly byte manipulation
This commit is contained in:
parent
ad25785b77
commit
834625a9f7
|
@ -45,8 +45,7 @@ const (
|
||||||
|
|
||||||
// Indices of bytes for uint16 metadata length.
|
// Indices of bytes for uint16 metadata length.
|
||||||
const (
|
const (
|
||||||
dataLenIdx1 = 2
|
dataLenIdx = 2
|
||||||
dataLenIdx2 = 3
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -58,9 +57,10 @@ var (
|
||||||
// Metadata provides functionality for the storage and encoding of metadata
|
// Metadata provides functionality for the storage and encoding of metadata
|
||||||
// using a map.
|
// 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
|
order []string
|
||||||
|
enc []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns a pointer to a new Metadata.
|
// New returns a pointer to a new Metadata.
|
||||||
|
@ -80,6 +80,7 @@ func New() *Metadata {
|
||||||
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.order = append(m.order, key)
|
||||||
m.mu.Unlock()
|
m.mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -111,6 +112,12 @@ func (m *Metadata) Delete(key string) error {
|
||||||
defer m.mu.Unlock()
|
defer m.mu.Unlock()
|
||||||
if _, ok := m.data[key]; ok {
|
if _, ok := m.data[key]; ok {
|
||||||
delete(m.data, key)
|
delete(m.data, key)
|
||||||
|
for i, k := range m.order {
|
||||||
|
if k == key {
|
||||||
|
m.order = append(m.order[:i], m.order[i+1:]...)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return errKeyAbsent
|
return errKeyAbsent
|
||||||
|
@ -123,12 +130,11 @@ func (m *Metadata) Encode() []byte {
|
||||||
|
|
||||||
// Iterate over map and append entries, only adding tab if we're not on the
|
// Iterate over map and append entries, only adding tab if we're not on the
|
||||||
// last entry.
|
// last entry.
|
||||||
var i int
|
|
||||||
var entry string
|
var entry string
|
||||||
for k, v := range m.data {
|
for i, k := range m.order {
|
||||||
i++
|
v := m.data[k]
|
||||||
entry += k + "=" + v
|
entry += k + "=" + v
|
||||||
if i < len(m.data) {
|
if i+1 < len(m.data) {
|
||||||
entry += "\t"
|
entry += "\t"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -136,9 +142,7 @@ func (m *Metadata) Encode() []byte {
|
||||||
|
|
||||||
// Calculate and set data length in encoded meta header.
|
// Calculate and set data length in encoded meta header.
|
||||||
dataLen := len(m.enc[headSize:])
|
dataLen := len(m.enc[headSize:])
|
||||||
m.enc[dataLenIdx1] = byte(dataLen >> 8)
|
binary.BigEndian.PutUint16(m.enc[dataLenIdx:dataLenIdx+2], uint16(dataLen))
|
||||||
m.enc[dataLenIdx2] = byte(dataLen)
|
|
||||||
|
|
||||||
return m.enc
|
return m.enc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue