blobserver/protocol: use blob.SizedRef to upload

Change-Id: I4b38b07869e08c2ccdcb76898dc908e6358501b0
This commit is contained in:
mpl 2014-08-13 20:07:37 +02:00
parent 3fbc92a4ae
commit d36b42862c
5 changed files with 75 additions and 23 deletions

View File

@ -45,10 +45,19 @@ type Ref struct {
// SizedRef is like a Ref but includes a size.
// It should also be used as a value type and supports equality.
type SizedRef struct {
Ref
Size uint32
Ref Ref `json:"blobRef"`
Size uint32 `json:"size"`
}
// Less reports whether sr sorts before o. Invalid references blobs sort first.
func (sr SizedRef) Less(o SizedRef) bool {
return sr.Ref.Less(o.Ref)
}
func (sr SizedRef) Valid() bool { return sr.Ref.Valid() }
func (sr SizedRef) HashMatches(h hash.Hash) bool { return sr.Ref.HashMatches(h) }
func (sr SizedRef) String() string {
return fmt.Sprintf("[%s; %d bytes]", sr.Ref.String(), sr.Size)
}
@ -582,7 +591,7 @@ func (s ByRef) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
type SizedByRef []SizedRef
func (s SizedByRef) Len() int { return len(s) }
func (s SizedByRef) Less(i, j int) bool { return s[i].Less(s[j].Ref) }
func (s SizedByRef) Less(i, j int) bool { return s[i].Less(s[j]) }
func (s SizedByRef) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
// TypeAlphabet returns the valid characters in the given blobref type.

View File

@ -220,3 +220,58 @@ func BenchmarkParseBlob(b *testing.B) {
}
}
}
func TestJSONUnmarshalSized(t *testing.T) {
var sb SizedRef
if err := json.Unmarshal([]byte(`{
"blobRef": "sha1-ce284c167558a9ef22df04390c87a6d0c9ed9659",
"size": 123}`), &sb); err != nil {
t.Fatalf("Unmarshal: %v", err)
}
want := SizedRef{
Ref: MustParse("sha1-ce284c167558a9ef22df04390c87a6d0c9ed9659"),
Size: 123,
}
if sb != want {
t.Fatalf("got %q, want %q", sb, want)
}
sb = SizedRef{}
if err := json.Unmarshal([]byte(`{}`), &sb); err != nil {
t.Fatalf("Unmarshal: %v", err)
}
if sb.Valid() {
t.Fatal("sized blobref is valid and shouldn't be")
}
sb = SizedRef{}
if err := json.Unmarshal([]byte(`{"blobRef":null, "size": 456}`), &sb); err != nil {
t.Fatalf("Unmarshal: %v", err)
}
if sb.Valid() {
t.Fatal("sized blobref is valid and shouldn't be")
}
}
func TestJSONMarshalSized(t *testing.T) {
sb := SizedRef{
Ref: MustParse("sha1-ce284c167558a9ef22df04390c87a6d0c9ed9659"),
Size: 123,
}
b, err := json.Marshal(sb)
if err != nil {
t.Fatalf("Marshal: %v", err)
}
if g, e := string(b), `{"blobRef":"sha1-ce284c167558a9ef22df04390c87a6d0c9ed9659","size":123}`; g != e {
t.Fatalf("got %q, want %q", g, e)
}
sb = SizedRef{}
b, err = json.Marshal(sb)
if err != nil {
t.Fatalf("Marshal: %v", err)
}
if g, e := string(b), `{"blobRef":null,"size":0}`; g != e {
t.Fatalf("got %q, want %q", g, e)
}
}

View File

@ -118,7 +118,7 @@ func handleStat(conn http.ResponseWriter, req *http.Request, storage blobserver.
}()
for sb := range blobch {
res.Stat = append(res.Stat, &protocol.RefAndSize{
res.Stat = append(res.Stat, blob.SizedRef{
Ref: sb.Ref,
Size: uint32(sb.Size),
})

View File

@ -239,12 +239,7 @@ func handleMultiPartUpload(rw http.ResponseWriter, req *http.Request, blobReceiv
receivedBlobs = append(receivedBlobs, blobGot)
}
for _, got := range receivedBlobs {
res.Received = append(res.Received, &protocol.RefAndSize{
Ref: got.Ref,
Size: uint32(got.Size),
})
}
res.Received = receivedBlobs
if req.Header.Get("X-Camlistore-Vivify") == "1" {
for _, got := range receivedBlobs {

View File

@ -23,26 +23,19 @@ import (
"camlistore.org/pkg/blob"
)
// TODO(mpl): try and replace RefAndSize with blob.SizedRef everywhere, then we can ditch RefAndSize.
type RefAndSize struct {
Ref blob.Ref `json:"blobRef"`
Size uint32 `json:"size"`
}
// StatResponse is the JSON document returned from the blob batch stat
// handler.
//
// See doc/protocol/blob-stat-protocol.txt.
type StatResponse struct {
Stat []*RefAndSize `json:"stat"`
CanLongPoll bool `json:"canLongPoll"` // TODO: move this to discovery?
Stat []blob.SizedRef `json:"stat"`
CanLongPoll bool `json:"canLongPoll"` // TODO: move this to discovery?
}
func (p *StatResponse) MarshalJSON() ([]byte, error) {
v := *p
if v.Stat == nil {
v.Stat = []*RefAndSize{}
v.Stat = []blob.SizedRef{}
}
return json.Marshal(v)
}
@ -52,14 +45,14 @@ func (p *StatResponse) MarshalJSON() ([]byte, error) {
//
// See doc/protocol/blob-upload-protocol.txt.
type UploadResponse struct {
Received []*RefAndSize `json:"received"`
ErrorText string `json:"errorText,omitempty"`
Received []blob.SizedRef `json:"received"`
ErrorText string `json:"errorText,omitempty"`
}
func (p *UploadResponse) MarshalJSON() ([]byte, error) {
v := *p
if v.Received == nil {
v.Received = []*RefAndSize{}
v.Received = []blob.SizedRef{}
}
return json.Marshal(v)
}