2019-01-31 13:53:06 +03:00
|
|
|
|
/*
|
|
|
|
|
NAME
|
|
|
|
|
meta.go
|
|
|
|
|
|
|
|
|
|
DESCRIPTION
|
2020-02-26 06:29:36 +03:00
|
|
|
|
Package meta provides functions for adding to, modifying and reading
|
|
|
|
|
metadata, as well as encoding and decoding functions.
|
2019-01-31 13:53:06 +03:00
|
|
|
|
|
|
|
|
|
AUTHOR
|
|
|
|
|
Saxon Nelson-Milton <saxon@ausocean.org>
|
|
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
|
meta.go is Copyright (C) 2017-2019 the Australian Ocean Lab (AusOcean)
|
|
|
|
|
|
|
|
|
|
It is free software: you can redistribute it and/or modify them
|
|
|
|
|
under the terms of the GNU General Public License as published by the
|
|
|
|
|
Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
|
option) any later version.
|
|
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
|
for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with revid in gpl.txt. If not, see http://www.gnu.org/licenses.
|
|
|
|
|
*/
|
|
|
|
|
|
2020-02-26 06:29:36 +03:00
|
|
|
|
// Package meta provides functions for adding to, modifying and reading
|
|
|
|
|
// metadata, as well as encoding and decoding functions.
|
2019-01-31 14:00:08 +03:00
|
|
|
|
package meta
|
2019-01-31 13:53:06 +03:00
|
|
|
|
|
|
|
|
|
import (
|
2019-02-04 15:18:51 +03:00
|
|
|
|
"encoding/binary"
|
2019-01-31 13:53:06 +03:00
|
|
|
|
"errors"
|
|
|
|
|
"strings"
|
|
|
|
|
"sync"
|
|
|
|
|
)
|
|
|
|
|
|
2019-02-04 15:08:11 +03:00
|
|
|
|
// This is the headsize of our metadata string,
|
|
|
|
|
// which is encoded int the data body of a pmt descriptor.
|
2019-01-31 13:53:06 +03:00
|
|
|
|
const headSize = 4
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
majVer = 1
|
|
|
|
|
minVer = 0
|
|
|
|
|
)
|
|
|
|
|
|
2019-02-04 15:08:11 +03:00
|
|
|
|
// Indices of bytes for uint16 metadata length.
|
2019-01-31 13:53:06 +03:00
|
|
|
|
const (
|
2019-02-05 05:25:32 +03:00
|
|
|
|
dataLenIdx = 2
|
2019-01-31 13:53:06 +03:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
2019-02-08 14:01:00 +03:00
|
|
|
|
errKeyAbsent = errors.New("Key does not exist in map")
|
2019-02-09 14:55:36 +03:00
|
|
|
|
errInvalidMeta = errors.New("Invalid metadata given")
|
2019-06-16 22:00:52 +03:00
|
|
|
|
ErrUnexpectedMetaFormat = errors.New("Unexpected meta format")
|
2019-01-31 13:53:06 +03:00
|
|
|
|
)
|
|
|
|
|
|
2019-02-04 15:08:11 +03:00
|
|
|
|
// Metadata provides functionality for the storage and encoding of metadata
|
|
|
|
|
// using a map.
|
2019-02-06 02:29:55 +03:00
|
|
|
|
type Data struct {
|
2019-02-05 05:25:32 +03:00
|
|
|
|
mu sync.RWMutex
|
|
|
|
|
data map[string]string
|
|
|
|
|
order []string
|
|
|
|
|
enc []byte
|
2019-01-31 13:53:06 +03:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-04 15:08:11 +03:00
|
|
|
|
// New returns a pointer to a new Metadata.
|
2019-02-06 02:29:55 +03:00
|
|
|
|
func New() *Data {
|
|
|
|
|
return &Data{
|
2019-01-31 13:53:06 +03:00
|
|
|
|
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
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-06 04:46:44 +03:00
|
|
|
|
// NewWith creates a meta.Data and fills map with initial data given. If there
|
|
|
|
|
// is repeated key, then the latter overwrites the prior.
|
|
|
|
|
func NewWith(data [][2]string) *Data {
|
|
|
|
|
m := New()
|
|
|
|
|
m.order = make([]string, 0, len(data))
|
|
|
|
|
for _, d := range data {
|
|
|
|
|
if _, exists := m.data[d[0]]; !exists {
|
|
|
|
|
m.order = append(m.order, d[0])
|
|
|
|
|
}
|
|
|
|
|
m.data[d[0]] = d[1]
|
2019-02-06 02:49:12 +03:00
|
|
|
|
}
|
2019-02-06 04:46:44 +03:00
|
|
|
|
return m
|
2019-02-06 02:49:12 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 08:15:48 +03:00
|
|
|
|
// 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
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-04 15:08:11 +03:00
|
|
|
|
// Add adds metadata with key and val.
|
2019-02-06 02:29:55 +03:00
|
|
|
|
func (m *Data) Add(key, val string) {
|
2019-01-31 13:53:06 +03:00
|
|
|
|
m.mu.Lock()
|
2019-02-05 15:55:46 +03:00
|
|
|
|
defer m.mu.Unlock()
|
2019-01-31 13:53:06 +03:00
|
|
|
|
m.data[key] = val
|
2019-02-05 05:41:11 +03:00
|
|
|
|
for _, k := range m.order {
|
|
|
|
|
if k == key {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-05 05:25:32 +03:00
|
|
|
|
m.order = append(m.order, key)
|
2019-02-05 15:55:46 +03:00
|
|
|
|
return
|
2019-01-31 13:53:06 +03:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-04 15:08:11 +03:00
|
|
|
|
// All returns the a copy of the map containing the meta data.
|
2019-02-06 02:29:55 +03:00
|
|
|
|
func (m *Data) All() map[string]string {
|
2019-01-31 13:53:06 +03:00
|
|
|
|
m.mu.Lock()
|
|
|
|
|
cpy := make(map[string]string)
|
|
|
|
|
for k, v := range m.data {
|
|
|
|
|
cpy[k] = v
|
|
|
|
|
}
|
|
|
|
|
m.mu.Unlock()
|
|
|
|
|
return cpy
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-04 15:08:11 +03:00
|
|
|
|
// Get returns the meta data for the passed key.
|
2019-02-08 02:55:57 +03:00
|
|
|
|
func (m *Data) Get(key string) (val string, ok bool) {
|
2019-01-31 13:53:06 +03:00
|
|
|
|
m.mu.Lock()
|
2019-02-08 02:55:57 +03:00
|
|
|
|
val, ok = m.data[key]
|
2019-01-31 13:53:06 +03:00
|
|
|
|
m.mu.Unlock()
|
2019-02-08 02:55:57 +03:00
|
|
|
|
return
|
2019-01-31 13:53:06 +03:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-04 15:08:11 +03:00
|
|
|
|
// Delete deletes a meta entry in the map and returns error if it doesn’t exist.
|
2019-02-08 03:26:19 +03:00
|
|
|
|
func (m *Data) Delete(key string) {
|
2019-01-31 13:53:06 +03:00
|
|
|
|
m.mu.Lock()
|
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
if _, ok := m.data[key]; ok {
|
|
|
|
|
delete(m.data, key)
|
2019-02-05 05:25:32 +03:00
|
|
|
|
for i, k := range m.order {
|
|
|
|
|
if k == key {
|
2020-02-26 03:23:22 +03:00
|
|
|
|
copy(m.order[i:], m.order[i+1:])
|
2019-02-05 15:59:07 +03:00
|
|
|
|
m.order = m.order[:len(m.order)-1]
|
2019-02-05 05:25:32 +03:00
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-08 03:26:19 +03:00
|
|
|
|
return
|
2019-01-31 13:53:06 +03:00
|
|
|
|
}
|
2019-02-08 03:26:19 +03:00
|
|
|
|
return
|
2019-01-31 13:53:06 +03:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-04 15:08:11 +03:00
|
|
|
|
// Encode takes the meta data map and encodes into a byte slice with header
|
2019-01-31 13:53:06 +03:00
|
|
|
|
// describing the version, length of data and data in TSV format.
|
2019-02-06 02:29:55 +03:00
|
|
|
|
func (m *Data) Encode() []byte {
|
2019-02-16 16:22:40 +03:00
|
|
|
|
if m.enc == nil {
|
|
|
|
|
panic("Meta has not been initialized yet")
|
|
|
|
|
}
|
2019-01-31 13:53:06 +03:00
|
|
|
|
m.enc = m.enc[:headSize]
|
|
|
|
|
|
2019-02-04 15:08:11 +03:00
|
|
|
|
// Iterate over map and append entries, only adding tab if we're not on the
|
|
|
|
|
// last entry.
|
2019-01-31 13:53:06 +03:00
|
|
|
|
var entry string
|
2019-02-05 05:25:32 +03:00
|
|
|
|
for i, k := range m.order {
|
|
|
|
|
v := m.data[k]
|
2019-01-31 13:53:06 +03:00
|
|
|
|
entry += k + "=" + v
|
2019-02-05 05:25:32 +03:00
|
|
|
|
if i+1 < len(m.data) {
|
2019-01-31 13:53:06 +03:00
|
|
|
|
entry += "\t"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
m.enc = append(m.enc, []byte(entry)...)
|
|
|
|
|
|
|
|
|
|
// Calculate and set data length in encoded meta header.
|
|
|
|
|
dataLen := len(m.enc[headSize:])
|
2019-02-05 05:25:32 +03:00
|
|
|
|
binary.BigEndian.PutUint16(m.enc[dataLenIdx:dataLenIdx+2], uint16(dataLen))
|
2019-01-31 13:53:06 +03:00
|
|
|
|
return m.enc
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-19 01:58:57 +03:00
|
|
|
|
// 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
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-10 01:28:38 +03:00
|
|
|
|
// Keys returns all keys in a slice of metadata d.
|
|
|
|
|
func Keys(d []byte) ([]string, error) {
|
2019-02-11 07:00:37 +03:00
|
|
|
|
m, err := GetAll(d)
|
2019-02-10 01:28:38 +03:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2019-02-11 07:00:37 +03:00
|
|
|
|
k := make([]string, len(m))
|
|
|
|
|
for i, kv := range m {
|
|
|
|
|
k[i] = kv[0]
|
2019-02-10 01:28:38 +03:00
|
|
|
|
}
|
2019-02-11 07:00:37 +03:00
|
|
|
|
return k, nil
|
2019-02-10 01:28:38 +03:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-09 14:05:35 +03:00
|
|
|
|
// Get returns the value for the given key in d.
|
2019-02-09 04:44:32 +03:00
|
|
|
|
func Get(key string, d []byte) (string, error) {
|
2019-02-09 14:55:36 +03:00
|
|
|
|
err := checkMeta(d)
|
2019-02-08 14:01:00 +03:00
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
2019-02-04 15:18:51 +03:00
|
|
|
|
}
|
|
|
|
|
d = d[headSize:]
|
2019-01-31 13:53:06 +03:00
|
|
|
|
entries := strings.Split(string(d), "\t")
|
|
|
|
|
for _, entry := range entries {
|
|
|
|
|
kv := strings.Split(entry, "=")
|
|
|
|
|
if kv[0] == key {
|
|
|
|
|
return kv[1], nil
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-04 15:08:11 +03:00
|
|
|
|
return "", errKeyAbsent
|
2019-01-31 13:53:06 +03:00
|
|
|
|
}
|
2019-02-08 14:01:00 +03:00
|
|
|
|
|
2019-02-09 14:05:35 +03:00
|
|
|
|
// GetAll returns metadata keys and values from d.
|
2019-02-09 04:44:32 +03:00
|
|
|
|
func GetAll(d []byte) ([][2]string, error) {
|
2019-02-09 14:55:36 +03:00
|
|
|
|
err := checkMeta(d)
|
2019-02-08 14:01:00 +03:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
d = d[headSize:]
|
|
|
|
|
entries := strings.Split(string(d), "\t")
|
|
|
|
|
all := make([][2]string, len(entries))
|
|
|
|
|
for i, entry := range entries {
|
|
|
|
|
kv := strings.Split(entry, "=")
|
|
|
|
|
if len(kv) != 2 {
|
2019-06-16 22:00:52 +03:00
|
|
|
|
return nil, ErrUnexpectedMetaFormat
|
2019-02-08 14:01:00 +03:00
|
|
|
|
}
|
|
|
|
|
copy(all[i][:], kv)
|
|
|
|
|
}
|
|
|
|
|
return all, nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-13 09:22:25 +03:00
|
|
|
|
// GetAllAsMap returns a map containing keys and values from a slice d containing
|
2019-07-04 08:03:12 +03:00
|
|
|
|
// metadata.
|
2019-06-11 15:37:51 +03:00
|
|
|
|
func GetAllAsMap(d []byte) (map[string]string, error) {
|
|
|
|
|
err := checkMeta(d)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Skip the header, which is our data length and version.
|
|
|
|
|
d = d[headSize:]
|
|
|
|
|
|
2020-02-19 01:58:57 +03:00
|
|
|
|
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) {
|
2019-06-11 15:37:51 +03:00
|
|
|
|
// Each metadata entry (key and value) is seperated by a tab, so split at tabs
|
|
|
|
|
// to get individual entries.
|
2020-02-19 01:58:57 +03:00
|
|
|
|
entries := strings.Split(s, "\t")
|
2019-06-11 15:37:51 +03:00
|
|
|
|
|
|
|
|
|
// Go through entries and add to all map.
|
|
|
|
|
all := make(map[string]string)
|
|
|
|
|
for _, entry := range entries {
|
|
|
|
|
// Keys and values are seperated by '=', so split and check that len(kv)=2.
|
|
|
|
|
kv := strings.Split(entry, "=")
|
|
|
|
|
if len(kv) != 2 {
|
2019-06-16 22:00:52 +03:00
|
|
|
|
return nil, ErrUnexpectedMetaFormat
|
2019-06-11 15:37:51 +03:00
|
|
|
|
}
|
|
|
|
|
all[kv[0]] = kv[1]
|
|
|
|
|
}
|
|
|
|
|
return all, nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-09 14:05:35 +03:00
|
|
|
|
// checkHeader checks that a valid metadata header exists in the given data.
|
2019-02-09 14:55:36 +03:00
|
|
|
|
func checkMeta(d []byte) error {
|
|
|
|
|
if len(d) == 0 || d[0] != 0 || binary.BigEndian.Uint16(d[2:headSize]) != uint16(len(d[headSize:])) {
|
|
|
|
|
return errInvalidMeta
|
2019-02-08 14:01:00 +03:00
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|