Skip to content

Commit

Permalink
Modify CustomCompressor interface to create new compressors
Browse files Browse the repository at this point in the history
  • Loading branch information
anacrolix committed Oct 24, 2024
1 parent da15e57 commit b5141c9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
9 changes: 6 additions & 3 deletions go/mcap/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -752,13 +752,16 @@ const (
)

type CustomCompressor interface {
Compressor() ResettableWriteCloser
NewCompressor() ResettableWriteCloser
Compression() CompressionFormat
}

// NewCustomCompressor returns a structure that may be supplied to writer
// options as a custom chunk compressor.
func NewCustomCompressor(compression CompressionFormat, compressor ResettableWriteCloser) CustomCompressor {
func NewCustomCompressor(
compression CompressionFormat,
compressor func() (ResettableWriteCloser, error),
) CustomCompressor {
return &customCompressor{
compression: compression,
compressor: compressor,
Expand All @@ -767,7 +770,7 @@ func NewCustomCompressor(compression CompressionFormat, compressor ResettableWri

type customCompressor struct {
compression CompressionFormat
compressor ResettableWriteCloser
compressor func() (ResettableWriteCloser, error)
}

func (c *customCompressor) Compressor() ResettableWriteCloser {
Expand Down
16 changes: 9 additions & 7 deletions go/mcap/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -703,16 +703,18 @@ func assertReadable(t *testing.T, rs io.ReadSeeker) {
func TestBYOCompressor(t *testing.T) {
buf := &bytes.Buffer{}
// example - custom lz4 settings
lzw := lz4.NewWriter(nil)
blockCount := 0
require.NoError(t, lzw.Apply(lz4.OnBlockDoneOption(func(int) {
blockCount++
})))

writer, err := NewWriter(buf, &WriterOptions{
Chunked: true,
ChunkSize: 1024,
Compressor: NewCustomCompressor("lz4", lzw),
Chunked: true,
ChunkSize: 1024,
Compressor: NewCustomCompressor("lz4", func() (ResettableWriteCloser, error) {
lzw := lz4.NewWriter(nil)
require.NoError(t, lzw.Apply(lz4.OnBlockDoneOption(func(int) {
blockCount++
})))
return lzw, nil
}),
})
require.NoError(t, err)

Expand Down

0 comments on commit b5141c9

Please sign in to comment.