mirror of https://github.com/perkeep/perkeep.git
blobref: faster String(); use camli/testing
This commit is contained in:
parent
ad37f29ce3
commit
d9bf9a3e14
1
build.pl
1
build.pl
|
@ -297,6 +297,7 @@ TARGET: lib/go/jsonsign
|
|||
- lib/go/ext/openpgp/error
|
||||
|
||||
TARGET: lib/go/blobref
|
||||
- lib/go/testing
|
||||
|
||||
TARGET: lib/go/blobserver
|
||||
- lib/go/blobref
|
||||
|
|
|
@ -26,7 +26,7 @@ import (
|
|||
|
||||
var kBlobRefPattern *regexp.Regexp = regexp.MustCompile(`^([a-z0-9]+)-([a-f0-9]+)$`)
|
||||
|
||||
var supportedDigests = map[string]func()hash.Hash{
|
||||
var supportedDigests = map[string]func() hash.Hash{
|
||||
"sha1": func() hash.Hash {
|
||||
return sha1.New()
|
||||
},
|
||||
|
@ -34,8 +34,10 @@ var supportedDigests = map[string]func()hash.Hash{
|
|||
|
||||
// BlobRef is an immutable reference to a blob.
|
||||
type BlobRef struct {
|
||||
hashName string
|
||||
digest string
|
||||
hashName string
|
||||
digest string
|
||||
|
||||
strValue string // "<hashname>-<digest>"
|
||||
}
|
||||
|
||||
// SizedBlobRef is like a BlobRef but includes a (potentially mutable) size.
|
||||
|
@ -58,8 +60,8 @@ func (b *BlobRef) Digest() string {
|
|||
return b.digest
|
||||
}
|
||||
|
||||
func (o *BlobRef) String() string {
|
||||
return fmt.Sprintf("%s-%s", o.hashName, o.digest)
|
||||
func (b *BlobRef) String() string {
|
||||
return b.strValue
|
||||
}
|
||||
|
||||
func (o *BlobRef) Equals(other *BlobRef) bool {
|
||||
|
@ -88,16 +90,23 @@ var kExpectedDigestSize = map[string]int{
|
|||
"sha1": 40,
|
||||
}
|
||||
|
||||
func newBlob(hashName, digest string) *BlobRef {
|
||||
strValue := fmt.Sprintf("%s-%s", hashName, digest)
|
||||
return &BlobRef{strValue[0:len(hashName)],
|
||||
strValue[len(hashName)+1:],
|
||||
strValue}
|
||||
}
|
||||
|
||||
func blobIfValid(hashname, digest string) *BlobRef {
|
||||
expectedSize := kExpectedDigestSize[hashname]
|
||||
if expectedSize != 0 && len(digest) != expectedSize {
|
||||
return nil
|
||||
}
|
||||
return &BlobRef{hashname, digest}
|
||||
return newBlob(hashname, digest)
|
||||
}
|
||||
|
||||
func FromHash(name string, h hash.Hash) *BlobRef {
|
||||
return &BlobRef{name, fmt.Sprintf("%x", h.Sum())}
|
||||
return newBlob(name, fmt.Sprintf("%x", h.Sum()))
|
||||
}
|
||||
|
||||
// FromPattern takes a pattern and if it matches 's' with two exactly two valid
|
||||
|
|
|
@ -18,10 +18,12 @@ package blobref
|
|||
|
||||
import (
|
||||
"testing"
|
||||
. "camli/testing"
|
||||
)
|
||||
|
||||
func TestAll(t *testing.T) {
|
||||
br := Parse("sha1-0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33")
|
||||
refStr := "sha1-0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"
|
||||
br := Parse(refStr)
|
||||
if br == nil {
|
||||
t.Fatalf("Failed to parse blobref")
|
||||
}
|
||||
|
@ -31,9 +33,8 @@ func TestAll(t *testing.T) {
|
|||
if br.digest != "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33" {
|
||||
t.Errorf("Invalid digest")
|
||||
}
|
||||
if !br.IsSupported() {
|
||||
t.Errorf("sha1 should be supported")
|
||||
}
|
||||
Expect(t, br.IsSupported(), "sha1 should be supported")
|
||||
ExpectString(t, refStr, br.String(), "String() value")
|
||||
|
||||
hash := br.Hash()
|
||||
hash.Write([]byte("foo"))
|
||||
|
@ -47,7 +48,7 @@ func TestAll(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestNotSupported(t *testing.T) {
|
||||
br := Parse("unknownfunc-0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33")
|
||||
br := Parse("unknownfunc-0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33")
|
||||
if br == nil {
|
||||
t.Fatalf("Failed to parse blobref")
|
||||
}
|
||||
|
|
|
@ -47,5 +47,3 @@ func (df *dirFetcher) Fetch(b *BlobRef) (file ReadSeekCloser, size int64, err os
|
|||
size = stat.Size
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue