Skip to content

Commit

Permalink
Different funcs for write and clear blob pages (Azure#28)
Browse files Browse the repository at this point in the history
* Different funcs for write and clear blob pages

* Fixed tests, and changed a name for consistency
  • Loading branch information
mcardosos authored and marstr committed Apr 26, 2017
1 parent 9cc3bda commit 3d4c1c8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
24 changes: 20 additions & 4 deletions storage/pageblob.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,28 @@ type PutPageOptions struct {
RequestID string `header:"x-ms-client-request-id"`
}

// PutPage writes a range of pages to a page blob or clears the given range.
// In case of 'clear' writes, given chunk is discarded. Ranges must be aligned
// with 512-byte boundaries and chunk must be of size multiplies by 512.
// WriteRange writes a range of pages to a page blob.
// Ranges must be aligned with 512-byte boundaries and chunk must be of size
// multiplies by 512.
//
// See https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/Put-Page
func (b *Blob) PutPage(blobRange BlobRange, bytes io.Reader, options *PutPageOptions) error {
func (b *Blob) WriteRange(blobRange BlobRange, bytes io.Reader, options *PutPageOptions) error {
if bytes == nil {
return errors.New("bytes cannot be nil")
}
return b.modifyRange(blobRange, bytes, options)
}

// ClearRange clears the given range in a page blob.
// Ranges must be aligned with 512-byte boundaries and chunk must be of size
// multiplies by 512.
//
// See https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/Put-Page
func (b *Blob) ClearRange(blobRange BlobRange, options *PutPageOptions) error {
return b.modifyRange(blobRange, nil, options)
}

func (b *Blob) modifyRange(blobRange BlobRange, bytes io.Reader, options *PutPageOptions) error {
if blobRange.End < blobRange.Start {
return errors.New("the value for rangeEnd must be greater than or equal to rangeStart")
}
Expand Down
14 changes: 7 additions & 7 deletions storage/pageblob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ func (s *PageBlobSuite) TestPutPagesUpdate(c *chk.C) {
blobRange := BlobRange{
End: uint64(len(chunk1) - 1),
}
c.Assert(b.PutPage(blobRange, bytes.NewReader(chunk1), nil), chk.IsNil)
c.Assert(b.WriteRange(blobRange, bytes.NewReader(chunk1), nil), chk.IsNil)
blobRange.Start = uint64(len(chunk1))
blobRange.End = uint64(len(chunk1) + len(chunk2) - 1)
c.Assert(b.PutPage(blobRange, bytes.NewReader(chunk2), nil), chk.IsNil)
c.Assert(b.WriteRange(blobRange, bytes.NewReader(chunk2), nil), chk.IsNil)

// Verify contents
options := GetBlobRangeOptions{
Expand All @@ -69,7 +69,7 @@ func (s *PageBlobSuite) TestPutPagesUpdate(c *chk.C) {
chunk0 := []byte(randString(512))
blobRange.Start = 0
blobRange.End = uint64(len(chunk0) - 1)
c.Assert(b.PutPage(blobRange, bytes.NewReader(chunk0), nil), chk.IsNil)
c.Assert(b.WriteRange(blobRange, bytes.NewReader(chunk0), nil), chk.IsNil)

// Verify contents
out, err = b.GetRange(&options)
Expand All @@ -96,12 +96,12 @@ func (s *PageBlobSuite) TestPutPagesClear(c *chk.C) {
blobRange := BlobRange{
End: 2047,
}
c.Assert(b.PutPage(blobRange, bytes.NewReader(chunk), nil), chk.IsNil)
c.Assert(b.WriteRange(blobRange, bytes.NewReader(chunk), nil), chk.IsNil)

// Clear 512-1023
blobRange.Start = 512
blobRange.End = 1023
c.Assert(b.PutPage(blobRange, nil, nil), chk.IsNil)
c.Assert(b.ClearRange(blobRange, nil), chk.IsNil)

// Verify contents
options := GetBlobRangeOptions{
Expand Down Expand Up @@ -138,7 +138,7 @@ func (s *PageBlobSuite) TestGetPageRanges(c *chk.C) {
blobRange := BlobRange{
End: 511,
}
c.Assert(b.PutPage(blobRange, bytes.NewReader([]byte(randString(512))), nil), chk.IsNil)
c.Assert(b.WriteRange(blobRange, bytes.NewReader([]byte(randString(512))), nil), chk.IsNil)

out, err = b.GetPageRanges(nil)
c.Assert(err, chk.IsNil)
Expand All @@ -147,7 +147,7 @@ func (s *PageBlobSuite) TestGetPageRanges(c *chk.C) {
// Add 1024-2048
blobRange.Start = 1024
blobRange.End = 2047
c.Assert(b.PutPage(blobRange, bytes.NewReader([]byte(randString(1024))), nil), chk.IsNil)
c.Assert(b.WriteRange(blobRange, bytes.NewReader([]byte(randString(1024))), nil), chk.IsNil)

out, err = b.GetPageRanges(nil)
c.Assert(err, chk.IsNil)
Expand Down

0 comments on commit 3d4c1c8

Please sign in to comment.