2014-09-23 20:39:12 +00:00
|
|
|
package backend
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"compress/zlib"
|
|
|
|
"crypto/sha256"
|
2014-11-24 20:12:32 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
2014-09-23 20:39:12 +00:00
|
|
|
"io/ioutil"
|
2014-11-24 21:09:22 +00:00
|
|
|
"sort"
|
2014-09-23 20:39:12 +00:00
|
|
|
)
|
|
|
|
|
2014-11-24 21:09:22 +00:00
|
|
|
const (
|
|
|
|
MinPrefixLength = 4
|
|
|
|
)
|
|
|
|
|
2014-11-24 20:12:32 +00:00
|
|
|
var (
|
|
|
|
ErrNoIDPrefixFound = errors.New("no ID found")
|
|
|
|
ErrMultipleIDMatches = errors.New("multiple IDs with prefix found")
|
|
|
|
)
|
|
|
|
|
2014-09-23 20:39:12 +00:00
|
|
|
// Each lists all entries of type t in the backend and calls function f() with
|
|
|
|
// the id and data.
|
2014-12-21 14:57:41 +00:00
|
|
|
func Each(be interface {
|
2014-12-21 16:02:49 +00:00
|
|
|
Lister
|
|
|
|
Getter
|
2014-12-21 14:57:41 +00:00
|
|
|
}, t Type, f func(id ID, data []byte, err error)) error {
|
2014-09-23 20:39:12 +00:00
|
|
|
ids, err := be.List(t)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, id := range ids {
|
|
|
|
data, err := be.Get(t, id)
|
|
|
|
if err != nil {
|
|
|
|
f(id, nil, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
f(id, data, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Each lists all entries of type t in the backend and calls function f() with
|
|
|
|
// the id.
|
2014-12-21 16:02:49 +00:00
|
|
|
func EachID(be Lister, t Type, f func(ID)) error {
|
2014-09-23 20:39:12 +00:00
|
|
|
ids, err := be.List(t)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, id := range ids {
|
|
|
|
f(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compress applies zlib compression to data.
|
|
|
|
func Compress(data []byte) []byte {
|
|
|
|
// apply zlib compression
|
|
|
|
var b bytes.Buffer
|
|
|
|
w := zlib.NewWriter(&b)
|
|
|
|
_, err := w.Write(data)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
w.Close()
|
|
|
|
|
|
|
|
return b.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Uncompress reverses zlib compression on data.
|
|
|
|
func Uncompress(data []byte) []byte {
|
|
|
|
b := bytes.NewBuffer(data)
|
|
|
|
r, err := zlib.NewReader(b)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf, err := ioutil.ReadAll(r)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Close()
|
|
|
|
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hash returns the ID for data.
|
|
|
|
func Hash(data []byte) ID {
|
|
|
|
h := sha256.Sum256(data)
|
2014-11-23 15:48:00 +00:00
|
|
|
id := idPool.Get().(ID)
|
2014-09-23 20:39:12 +00:00
|
|
|
copy(id, h[:])
|
|
|
|
return id
|
|
|
|
}
|
2014-11-24 20:12:32 +00:00
|
|
|
|
|
|
|
// Find loads the list of all blobs of type t and searches for IDs which start
|
|
|
|
// with prefix. If none is found, nil and ErrNoIDPrefixFound is returned. If
|
|
|
|
// more than one is found, nil and ErrMultipleIDMatches is returned.
|
2014-12-21 16:02:49 +00:00
|
|
|
func Find(be Lister, t Type, prefix string) (ID, error) {
|
2014-11-24 20:12:32 +00:00
|
|
|
p, err := hex.DecodeString(prefix)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
list, err := be.List(t)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
match := ID(nil)
|
|
|
|
|
|
|
|
// TODO: optimize by sorting list etc.
|
|
|
|
for _, id := range list {
|
|
|
|
if bytes.Equal(p, id[:len(p)]) {
|
|
|
|
if match == nil {
|
|
|
|
match = id
|
|
|
|
} else {
|
|
|
|
return nil, ErrMultipleIDMatches
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if match != nil {
|
|
|
|
return match, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, ErrNoIDPrefixFound
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindSnapshot takes a string and tries to find a snapshot whose ID matches
|
|
|
|
// the string as closely as possible.
|
2014-12-21 16:02:49 +00:00
|
|
|
func FindSnapshot(be Lister, s string) (ID, error) {
|
2014-11-24 20:12:32 +00:00
|
|
|
// parse ID directly
|
|
|
|
if id, err := ParseID(s); err == nil {
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// find snapshot id with prefix
|
|
|
|
id, err := Find(be, Snapshot, s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return id, nil
|
|
|
|
}
|
2014-11-24 21:09:22 +00:00
|
|
|
|
|
|
|
// PrefixLength returns the number of bytes required so that all prefixes of
|
|
|
|
// all IDs of type t are unique.
|
2014-12-21 16:02:49 +00:00
|
|
|
func PrefixLength(be Lister, t Type) (int, error) {
|
2014-11-24 21:09:22 +00:00
|
|
|
// load all IDs of the given type
|
|
|
|
list, err := be.List(t)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(list)
|
|
|
|
|
|
|
|
// select prefixes of length l, test if the last one is the same as the current one
|
|
|
|
outer:
|
|
|
|
for l := MinPrefixLength; l < IDSize; l++ {
|
|
|
|
var last ID
|
|
|
|
|
|
|
|
for _, id := range list {
|
|
|
|
if bytes.Equal(last, id[:l]) {
|
|
|
|
continue outer
|
|
|
|
}
|
|
|
|
last = id[:l]
|
|
|
|
}
|
|
|
|
|
|
|
|
return l, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return IDSize, nil
|
|
|
|
}
|