Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dagreader: remove Offset() method #5190

Merged
merged 1 commit into from
Jul 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions unixfs/io/bufdagreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package io
import (
"bytes"
"context"
"io"
)

// BufDagReader implements a DagReader that reads from a byte slice
Expand All @@ -30,15 +29,6 @@ func (rd *BufDagReader) CtxReadFull(ctx context.Context, b []byte) (int, error)
return rd.Read(b)
}

// Offset returns the current offset.
func (rd *BufDagReader) Offset() int64 {
of, err := rd.Seek(0, io.SeekCurrent)
if err != nil {
panic("this should never happen " + err.Error())
}
return of
}

// Size returns the size of the buffer.
func (rd *BufDagReader) Size() uint64 {
s := rd.Reader.Size()
Expand Down
1 change: 0 additions & 1 deletion unixfs/io/dagreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ type DagReader interface {
ReadSeekCloser
Size() uint64
CtxReadFull(context.Context, []byte) (int, error)
Offset() int64
}

// A ReadSeekCloser implements interfaces to read, copy, seek and close.
Expand Down
24 changes: 16 additions & 8 deletions unixfs/io/dagreader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestSeekAndRead(t *testing.T) {
for i := 255; i >= 0; i-- {
reader.Seek(int64(i), io.SeekStart)

if reader.Offset() != int64(i) {
if getOffset(reader) != int64(i) {
t.Fatal("expected offset to be increased by one after read")
}

Expand All @@ -67,7 +67,7 @@ func TestSeekAndRead(t *testing.T) {
t.Fatalf("read %d at index %d, expected %d", out, i, i)
}

if reader.Offset() != int64(i+1) {
if getOffset(reader) != int64(i+1) {
t.Fatal("expected offset to be increased by one after read")
}
}
Expand Down Expand Up @@ -142,12 +142,12 @@ func TestRelativeSeek(t *testing.T) {
}

for i := 0; i < 256; i++ {
if reader.Offset() != int64(i*4) {
t.Fatalf("offset should be %d, was %d", i*4, reader.Offset())
if getOffset(reader) != int64(i*4) {
t.Fatalf("offset should be %d, was %d", i*4, getOffset(reader))
}
out := readByte(t, reader)
if int(out) != i {
t.Fatalf("expected to read: %d at %d, read %d", i, reader.Offset()-1, out)
t.Fatalf("expected to read: %d at %d, read %d", i, getOffset(reader)-1, out)
}
if i != 255 {
_, err := reader.Seek(3, io.SeekCurrent)
Expand All @@ -163,12 +163,12 @@ func TestRelativeSeek(t *testing.T) {
}

for i := 0; i < 256; i++ {
if reader.Offset() != int64(1020-i*4) {
t.Fatalf("offset should be %d, was %d", 1020-i*4, reader.Offset())
if getOffset(reader) != int64(1020-i*4) {
t.Fatalf("offset should be %d, was %d", 1020-i*4, getOffset(reader))
}
out := readByte(t, reader)
if int(out) != 255-i {
t.Fatalf("expected to read: %d at %d, read %d", 255-i, reader.Offset()-1, out)
t.Fatalf("expected to read: %d at %d, read %d", 255-i, getOffset(reader)-1, out)
}
reader.Seek(-5, io.SeekCurrent) // seek 4 bytes but we read one byte every time so 5 bytes
}
Expand Down Expand Up @@ -302,3 +302,11 @@ func readByte(t testing.TB, reader DagReader) byte {

return out[0]
}

func getOffset(reader DagReader) int64 {
offset, err := reader.Seek(0, io.SeekCurrent)
if err != nil {
panic("failed to retrieve offset: " + err.Error())
}
return offset
}
5 changes: 0 additions & 5 deletions unixfs/io/pbdagreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,6 @@ func (dr *PBDagReader) Close() error {
return nil
}

// Offset returns the current reader offset
func (dr *PBDagReader) Offset() int64 {
return dr.offset
}

// Seek implements io.Seeker, and will seek to a given offset in the file
// interface matches standard unix seek
// TODO: check if we can do relative seeks, to reduce the amount of dagreader
Expand Down
10 changes: 9 additions & 1 deletion unixfs/mod/dagmodifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ func testReadAndSeek(t *testing.T, opts testu.NodeOpts) {
// skip 4
_, err = dagmod.Seek(1, io.SeekCurrent)
if err != nil {
t.Fatalf("error: %s, offset %d, reader offset %d", err, dagmod.curWrOff, dagmod.read.Offset())
t.Fatalf("error: %s, offset %d, reader offset %d", err, dagmod.curWrOff, getOffset(dagmod.read))
}

//read 5,6,7
Expand Down Expand Up @@ -750,3 +750,11 @@ func BenchmarkDagmodWrite(b *testing.B) {
}
}
}

func getOffset(reader uio.DagReader) int64 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm rewriting getOffset from dagreader_test.go, exporting it doesn't seem to work, is there a cleaner way (without adding it back to dagreader.go)?

offset, err := reader.Seek(0, io.SeekCurrent)
if err != nil {
panic("failed to retrieve offset: " + err.Error())
}
return offset
}