Merge branch 'list-snapshots'
This commit is contained in:
commit
08690b99bf
9 changed files with 223 additions and 21 deletions
|
@ -4,12 +4,24 @@ import (
|
|||
"bytes"
|
||||
"compress/zlib"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"sort"
|
||||
"sync"
|
||||
)
|
||||
|
||||
const (
|
||||
MinPrefixLength = 4
|
||||
)
|
||||
|
||||
var idPool = sync.Pool{New: func() interface{} { return ID(make([]byte, IDSize)) }}
|
||||
|
||||
var (
|
||||
ErrNoIDPrefixFound = errors.New("no ID found")
|
||||
ErrMultipleIDMatches = errors.New("multiple IDs with prefix found")
|
||||
)
|
||||
|
||||
// Each lists all entries of type t in the backend and calls function f() with
|
||||
// the id and data.
|
||||
func Each(be Server, t Type, f func(id ID, data []byte, err error)) error {
|
||||
|
@ -85,3 +97,83 @@ func Hash(data []byte) ID {
|
|||
copy(id, h[:])
|
||||
return id
|
||||
}
|
||||
|
||||
// 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.
|
||||
func Find(be Server, t Type, prefix string) (ID, error) {
|
||||
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.
|
||||
func FindSnapshot(be Server, s string) (ID, error) {
|
||||
// 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
|
||||
}
|
||||
|
||||
// PrefixLength returns the number of bytes required so that all prefixes of
|
||||
// all IDs of type t are unique.
|
||||
func PrefixLength(be Lister, t Type) (int, error) {
|
||||
// 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
|
||||
}
|
||||
|
|
|
@ -6,6 +6,8 @@ import (
|
|||
"reflect"
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
"github.com/fd0/khepri/backend"
|
||||
)
|
||||
|
||||
// assert fails the test if the condition is false.
|
||||
|
@ -34,3 +36,43 @@ func equals(tb testing.TB, exp, act interface{}) {
|
|||
tb.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
func str2id(s string) backend.ID {
|
||||
id, err := backend.ParseID(s)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return id
|
||||
}
|
||||
|
||||
type IDList backend.IDs
|
||||
|
||||
var samples = IDList{
|
||||
str2id("20bdc1402a6fc9b633aaffffffffffffffffffffffffffffffffffffffffffff"),
|
||||
str2id("20bdc1402a6fc9b633ccd578c4a92d0f4ef1a457fa2e16c596bc73fb409d6cc0"),
|
||||
str2id("20bdc1402a6fc9b633ffffffffffffffffffffffffffffffffffffffffffffff"),
|
||||
str2id("20ff988befa5fc40350f00d531a767606efefe242c837aaccb80673f286be53d"),
|
||||
str2id("326cb59dfe802304f96ee9b5b9af93bdee73a30f53981e5ec579aedb6f1d0f07"),
|
||||
str2id("86b60b9594d1d429c4aa98fa9562082cabf53b98c7dc083abe5dae31074dd15a"),
|
||||
str2id("96c8dbe225079e624b5ce509f5bd817d1453cd0a85d30d536d01b64a8669aeae"),
|
||||
str2id("fa31d65b87affcd167b119e9d3d2a27b8236ca4836cb077ed3e96fcbe209b792"),
|
||||
}
|
||||
|
||||
func (l IDList) List(backend.Type) (backend.IDs, error) {
|
||||
return backend.IDs(l), nil
|
||||
}
|
||||
|
||||
func TestPrefixLength(t *testing.T) {
|
||||
l, err := backend.PrefixLength(samples, backend.Snapshot)
|
||||
ok(t, err)
|
||||
equals(t, 10, l)
|
||||
|
||||
l, err = backend.PrefixLength(samples[:3], backend.Snapshot)
|
||||
ok(t, err)
|
||||
equals(t, 10, l)
|
||||
|
||||
l, err = backend.PrefixLength(samples[3:], backend.Snapshot)
|
||||
ok(t, err)
|
||||
equals(t, 4, l)
|
||||
}
|
||||
|
|
|
@ -21,10 +21,14 @@ var (
|
|||
ErrAlreadyPresent = errors.New("blob is already present in backend")
|
||||
)
|
||||
|
||||
type Lister interface {
|
||||
List(Type) (IDs, error)
|
||||
}
|
||||
|
||||
type Server interface {
|
||||
Create(Type, []byte) (ID, error)
|
||||
Get(Type, ID) ([]byte, error)
|
||||
List(Type) (IDs, error)
|
||||
Lister
|
||||
Test(Type, ID) (bool, error)
|
||||
Remove(Type, ID) error
|
||||
Version() uint
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue