2019-01-26 15:06:34 +03:00
|
|
|
|
/*
|
|
|
|
|
NAME
|
|
|
|
|
meta.go
|
|
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
|
See Readme.md
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package mts
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"sync"
|
|
|
|
|
)
|
|
|
|
|
|
2019-01-28 09:59:15 +03:00
|
|
|
|
const headSize = 4
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
majVer = 1
|
|
|
|
|
minVer = 0
|
|
|
|
|
)
|
|
|
|
|
|
2019-01-28 10:15:38 +03:00
|
|
|
|
const (
|
|
|
|
|
dataLenIdx1 = 2
|
|
|
|
|
dataLenIdx2 = 3
|
|
|
|
|
)
|
|
|
|
|
|
2019-01-27 10:45:22 +03:00
|
|
|
|
var (
|
|
|
|
|
errKeyAbsent = errors.New("Key does not exist in map")
|
|
|
|
|
)
|
|
|
|
|
|
2019-01-26 15:06:34 +03:00
|
|
|
|
type Meta struct {
|
|
|
|
|
mu sync.RWMutex
|
|
|
|
|
data map[string]string
|
2019-01-28 09:59:15 +03:00
|
|
|
|
enc []byte
|
2019-01-26 15:06:34 +03:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-27 10:27:42 +03:00
|
|
|
|
func NewMeta() *Meta {
|
2019-01-28 09:59:15 +03:00
|
|
|
|
return &Meta{
|
|
|
|
|
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-01-27 10:27:42 +03:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-26 15:06:34 +03:00
|
|
|
|
// Add adds metadata with key and val, if already exists return error
|
2019-01-27 10:27:42 +03:00
|
|
|
|
func (m *Meta) Add(key, val string) {
|
2019-01-26 15:06:34 +03:00
|
|
|
|
m.mu.Lock()
|
|
|
|
|
m.data[key] = val
|
|
|
|
|
m.mu.Unlock()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// All returns the a copy of the map containing the meta data
|
|
|
|
|
func (m *Meta) All() map[string]string {
|
2019-01-26 15:12:31 +03:00
|
|
|
|
m.mu.Lock()
|
2019-01-27 10:45:22 +03:00
|
|
|
|
cpy := make(map[string]string)
|
2019-01-26 15:06:34 +03:00
|
|
|
|
for k, v := range m.data {
|
|
|
|
|
cpy[k] = v
|
|
|
|
|
}
|
2019-01-26 15:12:31 +03:00
|
|
|
|
m.mu.Unlock()
|
2019-01-26 15:06:34 +03:00
|
|
|
|
return cpy
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get returns the meta data for the passed key
|
|
|
|
|
func (m *Meta) Get(key string) (string, error) {
|
2019-01-26 15:12:31 +03:00
|
|
|
|
m.mu.Lock()
|
2019-01-26 15:06:34 +03:00
|
|
|
|
val, ok := m.data[key]
|
2019-01-26 15:12:31 +03:00
|
|
|
|
m.mu.Unlock()
|
2019-01-26 15:06:34 +03:00
|
|
|
|
if !ok {
|
2019-01-27 10:45:22 +03:00
|
|
|
|
return "", errKeyAbsent
|
2019-01-26 15:06:34 +03:00
|
|
|
|
}
|
2019-01-26 15:12:31 +03:00
|
|
|
|
|
2019-01-26 15:06:34 +03:00
|
|
|
|
return val, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove deletes a meta entry in the map and returns error if it doesn’t exist
|
|
|
|
|
func (m *Meta) Delete(key string) error {
|
2019-01-26 15:12:31 +03:00
|
|
|
|
m.mu.Lock()
|
|
|
|
|
defer m.mu.Unlock()
|
2019-01-26 15:06:34 +03:00
|
|
|
|
if _, ok := m.data[key]; ok {
|
|
|
|
|
delete(m.data, key)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2019-01-27 10:54:26 +03:00
|
|
|
|
return errKeyAbsent
|
2019-01-26 15:06:34 +03:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-27 09:26:14 +03:00
|
|
|
|
func (m *Meta) Encode() []byte {
|
2019-01-28 09:59:15 +03:00
|
|
|
|
m.enc = m.enc[:headSize]
|
2019-01-28 10:15:38 +03:00
|
|
|
|
|
|
|
|
|
// Iterate over map and append entries, only adding tab if we're not on the last entry
|
|
|
|
|
var i int
|
|
|
|
|
var entry string
|
2019-01-28 09:59:15 +03:00
|
|
|
|
for k, v := range m.data {
|
2019-01-28 10:15:38 +03:00
|
|
|
|
i++
|
|
|
|
|
entry += k + "=" + v
|
|
|
|
|
if i < len(m.data) {
|
|
|
|
|
entry += "\t"
|
|
|
|
|
}
|
2019-01-28 09:59:15 +03:00
|
|
|
|
}
|
2019-01-28 10:15:38 +03:00
|
|
|
|
m.enc = append(m.enc, []byte(entry)...)
|
|
|
|
|
|
|
|
|
|
// Calculate and set data length in encoded meta header.
|
|
|
|
|
dataLen := len(m.enc[headSize:])
|
|
|
|
|
m.enc[dataLenIdx1] = byte(dataLen >> 8)
|
|
|
|
|
m.enc[dataLenIdx2] = byte(dataLen)
|
|
|
|
|
|
2019-01-28 09:59:15 +03:00
|
|
|
|
return m.enc
|
2019-01-26 15:06:34 +03:00
|
|
|
|
}
|