Skip to content

Commit 47f15b0

Browse files
authored
GH-40113 [Go][Parquet] New RegisterCodec function (#40114)
This is to allow addition/overwrite of custom codec implementation This allows other modules to provide alternative implementations for the compression algorithms, such as using libdeflate for Gzip, or CGO version of ZSTD. In addition, it allows others to supply codecs that cannot be easily supported by this library such as LZO due to license reasons or LZ4. ### Rationale for this change See #40113 ### What changes are included in this PR? A new RegisterCodec function added ### Are these changes tested? yes ### Are there any user-facing changes? It's an addition more targeted towards library writers. * Closes: #40113 Authored-by: Yan Zhou <zhouyan@me.com> Signed-off-by: Matt Topol <zotthewizard@gmail.com>
1 parent a690088 commit 47f15b0

File tree

5 files changed

+24
-4
lines changed

5 files changed

+24
-4
lines changed

go/parquet/compress/brotli.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,5 @@ func (brotliCodec) NewWriterLevel(w io.Writer, level int) (io.WriteCloser, error
110110
}
111111

112112
func init() {
113-
codecs[Codecs.Brotli] = brotliCodec{}
113+
RegisterCodec(Codecs.Brotli, brotliCodec{})
114114
}

go/parquet/compress/compress.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,26 @@ type Codec interface {
9292

9393
var codecs = map[Compression]Codec{}
9494

95+
// RegisterCodec adds or overrides a codec implementation for a given compression algorithm.
96+
// The intended use case is within the init() section of a package. For example,
97+
//
98+
// // inside a custom codec package, say czstd
99+
//
100+
// func init() {
101+
// RegisterCodec(compress.Codecs.Zstd, czstdCodec{})
102+
// }
103+
//
104+
// type czstdCodec struct{} // implementing Codec interface using CGO based ZSTD wrapper
105+
//
106+
// And user of the custom codec can import the above package like below,
107+
//
108+
// package main
109+
//
110+
// import _ "package/path/to/czstd"
111+
func RegisterCodec(compression Compression, codec Codec) {
112+
codecs[compression] = codec
113+
}
114+
95115
type nocodec struct{}
96116

97117
func (nocodec) NewReader(r io.Reader) io.ReadCloser {

go/parquet/compress/gzip.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,5 @@ func (gzipCodec) NewWriterLevel(w io.Writer, level int) (io.WriteCloser, error)
9393
}
9494

9595
func init() {
96-
codecs[Codecs.Gzip] = gzipCodec{}
96+
RegisterCodec(Codecs.Gzip, gzipCodec{})
9797
}

go/parquet/compress/snappy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ func (s snappyCodec) NewWriterLevel(w io.Writer, _ int) (io.WriteCloser, error)
5757
}
5858

5959
func init() {
60-
codecs[Codecs.Snappy] = snappyCodec{}
60+
RegisterCodec(Codecs.Snappy, snappyCodec{})
6161
}

go/parquet/compress/zstd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,5 @@ func (zstdCodec) CompressBound(len int64) int64 {
108108
}
109109

110110
func init() {
111-
codecs[Codecs.Zstd] = zstdCodec{}
111+
RegisterCodec(Codecs.Zstd, zstdCodec{})
112112
}

0 commit comments

Comments
 (0)