Skip to content

Commit

Permalink
feat: add buffer truncation (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevmo314 authored Jan 1, 2024
1 parent 226a914 commit 3ee5ced
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pkg/btree/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,15 @@ func (b *seekableBuffer) Read(p []byte) (int, error) {
return n, nil
}

func (b *seekableBuffer) Truncate(size int64) error {
if size < 0 {
return io.ErrShortBuffer
}
if size > int64(len(b.buf)) {
return io.ErrShortWrite
}
b.buf = b.buf[:size]
return nil
}

var _ io.ReadWriteSeeker = &seekableBuffer{}
13 changes: 13 additions & 0 deletions pkg/btree/buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,17 @@ func TestSeekableBuffer(t *testing.T) {
t.Fatalf("expected to read 'llo', read %s", string(buf))
}
})

t.Run("truncate", func(t *testing.T) {
b := newSeekableBuffer()
if _, err := b.Write([]byte("hello")); err != nil {
t.Fatal(err)
}
if err := b.Truncate(3); err != nil {
t.Fatal(err)
}
if string(b.buf) != "hel" {
t.Fatalf("expected to truncate to 'hel', truncated to %s", string(b.buf))
}
})
}

0 comments on commit 3ee5ced

Please sign in to comment.