Skip to content

Commit

Permalink
add size for inmem
Browse files Browse the repository at this point in the history
  • Loading branch information
ashwanthgoli committed Oct 10, 2024
1 parent 8a2e26e commit 445546e
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions inmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,12 @@ func (b *InMemBucket) Get(_ context.Context, name string) (io.ReadCloser, error)
return nil, errNotFound
}

return io.NopCloser(bytes.NewReader(file)), nil
return ObjectSizerReadCloser{
ReadCloser: io.NopCloser(bytes.NewReader(file)),
Size: func() (int64, error) {
return int64(len(file)), nil
},
}, nil
}

// GetRange returns a new range reader for the given object name and range.
Expand All @@ -136,23 +141,40 @@ func (b *InMemBucket) GetRange(_ context.Context, name string, off, length int64
}

if int64(len(file)) < off {
return io.NopCloser(bytes.NewReader(nil)), nil
return ObjectSizerReadCloser{
ReadCloser: io.NopCloser(bytes.NewReader(nil)),
Size: func() (int64, error) { return 0, nil },
}, nil
}

if length == -1 {
return io.NopCloser(bytes.NewReader(file[off:])), nil
return ObjectSizerReadCloser{
ReadCloser: io.NopCloser(bytes.NewReader(file[off:])),
Size: func() (int64, error) {
return int64(len(file[off:])), nil
},
}, nil
}

if length <= 0 {
return io.NopCloser(bytes.NewReader(nil)), errors.New("length cannot be smaller or equal 0")
// wrap with ObjectSizerReadCloser to return 0 size.
return ObjectSizerReadCloser{
ReadCloser: io.NopCloser(bytes.NewReader(nil)),
Size: func() (int64, error) { return 0, nil },
}, errors.New("length cannot be smaller or equal 0")
}

if int64(len(file)) <= off+length {
// Just return maximum of what we have.
length = int64(len(file)) - off
}

return io.NopCloser(bytes.NewReader(file[off : off+length])), nil
return ObjectSizerReadCloser{
ReadCloser: io.NopCloser(bytes.NewReader(file[off : off+length])),
Size: func() (int64, error) {
return length, nil
},
}, nil
}

// Exists checks if the given directory exists in memory.
Expand Down

0 comments on commit 445546e

Please sign in to comment.