Implement GetObjectNInfo object layer call (#6290)

This combines calling GetObjectInfo and GetObject while returning a
io.ReadCloser for the object's body. This allows the two operations to
be under a single lock, fixing a race between getting object info and
reading the object body.
This commit is contained in:
Aditya Manthramurthy
2018-08-27 02:58:23 -07:00
committed by Nitish Tiwari
parent cea4f82586
commit e6d740ce09
19 changed files with 926 additions and 69 deletions

View File

@@ -133,3 +133,108 @@ func parseRequestRange(rangeString string, resourceSize int64) (hrange *httpRang
return &httpRange{offsetBegin, offsetEnd, resourceSize}, nil
}
// HTTPRangeSpec represents a range specification as supported by S3 GET
// object request.
//
// Case 1: Not present -> represented by a nil RangeSpec
// Case 2: bytes=1-10 (absolute start and end offsets) -> RangeSpec{false, 1, 10}
// Case 3: bytes=10- (absolute start offset with end offset unspecified) -> RangeSpec{false, 10, -1}
// Case 4: bytes=-30 (suffix length specification) -> RangeSpec{true, -30, -1}
type HTTPRangeSpec struct {
// Does the range spec refer to a suffix of the object?
IsSuffixLength bool
// Start and end offset specified in range spec
Start, End int64
}
// ContentRangeString populate range stringer interface
func (h *HTTPRangeSpec) ContentRangeString(resourceSize int64) string {
start, rangeLength := h.GetOffsetLength(resourceSize)
return fmt.Sprintf("bytes %d-%d/%d", start, start+rangeLength-1, resourceSize)
}
// GetLength - get length of range
func (h *HTTPRangeSpec) GetLength(resourceSize int64) int64 {
switch {
case h.IsSuffixLength:
specifiedLen := -h.Start
if specifiedLen > resourceSize {
specifiedLen = resourceSize
}
return specifiedLen
case h.End > -1:
end := h.End
if resourceSize < end {
end = resourceSize - 1
}
return end - h.Start + 1
default:
return resourceSize - h.Start
}
}
// GetOffsetLength computes the start offset and length of the range
// given the size of the resource
func (h *HTTPRangeSpec) GetOffsetLength(resourceSize int64) (start int64, length int64) {
length = h.GetLength(resourceSize)
start = h.Start
if h.IsSuffixLength {
start = resourceSize + h.Start
}
return
}
// Parses a range header value into a HTTPRangeSpec
func parseRequestRangeSpec(rangeString string) (hrange *HTTPRangeSpec, err error) {
// Return error if given range string doesn't start with byte range prefix.
if !strings.HasPrefix(rangeString, byteRangePrefix) {
return nil, fmt.Errorf("'%s' does not start with '%s'", rangeString, byteRangePrefix)
}
// Trim byte range prefix.
byteRangeString := strings.TrimPrefix(rangeString, byteRangePrefix)
// Check if range string contains delimiter '-', else return error. eg. "bytes=8"
sepIndex := strings.Index(byteRangeString, "-")
if sepIndex == -1 {
return nil, fmt.Errorf("'%s' does not have a valid range value", rangeString)
}
offsetBeginString := byteRangeString[:sepIndex]
offsetBegin := int64(-1)
// Convert offsetBeginString only if its not empty.
if len(offsetBeginString) > 0 {
if offsetBegin, err = strconv.ParseInt(offsetBeginString, 10, 64); err != nil {
return nil, fmt.Errorf("'%s' does not have a valid first byte position value", rangeString)
}
}
offsetEndString := byteRangeString[sepIndex+1:]
offsetEnd := int64(-1)
// Convert offsetEndString only if its not empty.
if len(offsetEndString) > 0 {
if offsetEnd, err = strconv.ParseInt(offsetEndString, 10, 64); err != nil {
return nil, fmt.Errorf("'%s' does not have a valid last byte position value", rangeString)
}
}
switch {
case offsetBegin > -1 && offsetEnd > -1:
if offsetBegin > offsetEnd {
return nil, errInvalidRange
}
return &HTTPRangeSpec{false, offsetBegin, offsetEnd}, nil
case offsetBegin > -1:
return &HTTPRangeSpec{false, offsetBegin, -1}, nil
case offsetEnd > -1:
if offsetEnd == 0 {
return nil, errInvalidRange
}
return &HTTPRangeSpec{true, -offsetEnd, -1}, nil
default:
// rangeString contains first and last byte positions missing. eg. "bytes=-"
return nil, fmt.Errorf("'%s' does not have valid range value", rangeString)
}
}