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