From 6855244424cd8370a61cdc337d3102625c135408 Mon Sep 17 00:00:00 2001 From: Scott Date: Tue, 18 Feb 2020 15:45:48 +1030 Subject: [PATCH 1/2] mts/meta: created NewFromMap method --- container/mts/meta/meta.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/container/mts/meta/meta.go b/container/mts/meta/meta.go index 0e67aa96..8bcdf464 100644 --- a/container/mts/meta/meta.go +++ b/container/mts/meta/meta.go @@ -90,6 +90,17 @@ func NewWith(data [][2]string) *Data { return m } +// NewFromMap creates a meta.Data from a map. +func NewFromMap(data map[string]string) *Data { + m := New() + m.order = make([]string, 0, len(data)) + for k, v := range data { + m.data[k] = v + m.order = append(m.order, k) + } + return m +} + // Add adds metadata with key and val. func (m *Data) Add(key, val string) { m.mu.Lock() From cca292ea17baa356a923be35b19fc546867dbe30 Mon Sep 17 00:00:00 2001 From: Scott Date: Wed, 19 Feb 2020 09:28:57 +1030 Subject: [PATCH 2/2] mts/meta: created EncodeAsString and GetAllFromString methods --- container/mts/meta/meta.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/container/mts/meta/meta.go b/container/mts/meta/meta.go index 8bcdf464..d3c6a8a9 100644 --- a/container/mts/meta/meta.go +++ b/container/mts/meta/meta.go @@ -178,6 +178,23 @@ func (m *Data) Encode() []byte { return m.enc } +// EncodeAsString takes the meta data map and encodes into a string with the data in +// TSV format. Unlike encode, the header with version and length of data is not +// included. This method is used for storing metadata in the store on vidgrind. +func (m *Data) EncodeAsString() string { + // Iterate over map and append entries, only adding tab if we're not on the + // last entry. + var str string + for i, k := range m.order { + v := m.data[k] + str += k + "=" + v + if i+1 < len(m.data) { + str += "\t" + } + } + return str +} + // Keys returns all keys in a slice of metadata d. func Keys(d []byte) ([]string, error) { m, err := GetAll(d) @@ -238,9 +255,15 @@ func GetAllAsMap(d []byte) (map[string]string, error) { // Skip the header, which is our data length and version. d = d[headSize:] + return GetAllFromString(string(d)) +} + +// GetAllFromString returns a map containing keys and values from a string s containing +// metadata. +func GetAllFromString(s string) (map[string]string, error) { // Each metadata entry (key and value) is seperated by a tab, so split at tabs // to get individual entries. - entries := strings.Split(string(d), "\t") + entries := strings.Split(s, "\t") // Go through entries and add to all map. all := make(map[string]string)