Skip to content

Commit

Permalink
readwriter: fix bug when writing index. (#10)
Browse files Browse the repository at this point in the history
When using ReadWriter on an existing siva file, absolute offset for
index entries was not being calculated correctly.
  • Loading branch information
smola authored Nov 11, 2016
1 parent 1b9e8dc commit df70709
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
12 changes: 6 additions & 6 deletions readwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ func NewReaderWriter(rw io.ReadWriteSeeker) (*ReadWriter, error) {
return nil, err
}

end, err := rw.Seek(0, io.SeekEnd)
if err != nil {
return nil, err
}

w := newWriter(rw)
getIndexFunc := func() (Index, error) {
start := uint64(0)
if len(i) > 0 {
lastEntry := i[len(i) - 1]
start = lastEntry.absStart + lastEntry.Size
}
for _, e := range w.index {
e.absStart = start + e.Start
e.absStart = uint64(end) + e.Start
}
return append(i, w.index...), nil
}
Expand Down
26 changes: 22 additions & 4 deletions readwriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,33 @@ func (s *ReadWriterSuite) SetUpSuite(c *C) {
}

func (s *ReadWriterSuite) TestWriteRead(c *C) {
tmpFile, err := os.Create(filepath.Join(s.tmpDir, c.TestName()))
path := filepath.Join(s.tmpDir, c.TestName())
tmpFile, err := os.Create(path)
c.Assert(err, IsNil)
c.Assert(tmpFile, NotNil)
s.testWriteRead(c, tmpFile, 0)
c.Assert(tmpFile.Close(), IsNil)

rw, err := siva.NewReaderWriter(tmpFile)
tmpFile, err = os.OpenFile(path, os.O_RDWR, 0)
c.Assert(err, IsNil)
c.Assert(tmpFile, NotNil)
s.testWriteRead(c, tmpFile, 1)
c.Assert(tmpFile.Close(), IsNil)

tmpFile, err = os.OpenFile(path, os.O_RDWR, 0)
c.Assert(err, IsNil)
c.Assert(tmpFile, NotNil)
s.testWriteRead(c, tmpFile, 2)
c.Assert(tmpFile.Close(), IsNil)
}

func (s *ReadWriterSuite) testWriteRead(c *C, f *os.File, iter int) {
rw, err := siva.NewReaderWriter(f)
c.Assert(err, IsNil)
c.Assert(rw, NotNil)

for i := 0; i < 100; i++ {
iters := 100
for i := 0; i < iters; i++ {
curName := fmt.Sprintf("foo-%d", i)
content := strings.Repeat("#", i)

Expand All @@ -49,7 +67,7 @@ func (s *ReadWriterSuite) TestWriteRead(c *C) {

index, err := rw.Index()
c.Assert(err, IsNil)
c.Assert(len(index), Equals, i+1)
c.Assert(len(index), Equals, iters*iter + i+1)

e := index.Find(curName)
c.Assert(e, NotNil)
Expand Down

0 comments on commit df70709

Please sign in to comment.