github.com/zntrio/harp/v2@v2.0.9/pkg/sdk/value/compression/writer.go (about)

     1  // Licensed to Elasticsearch B.V. under one or more contributor
     2  // license agreements. See the NOTICE file distributed with
     3  // this work for additional information regarding copyright
     4  // ownership. Elasticsearch B.V. licenses this file to you under
     5  // the Apache License, Version 2.0 (the "License"); you may
     6  // not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing,
    12  // software distributed under the License is distributed on an
    13  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    14  // KIND, either express or implied.  See the License for the
    15  // specific language governing permissions and limitations
    16  // under the License.
    17  
    18  package compression
    19  
    20  import (
    21  	"compress/lzw"
    22  	"fmt"
    23  	"io"
    24  	"strings"
    25  
    26  	"github.com/klauspost/compress/flate"
    27  	"github.com/klauspost/compress/gzip"
    28  	"github.com/klauspost/compress/s2"
    29  	"github.com/klauspost/compress/zlib"
    30  	"github.com/klauspost/compress/zstd"
    31  	"github.com/pierrec/lz4"
    32  	"github.com/ulikunitz/xz"
    33  )
    34  
    35  // -----------------------------------------------------------------------------
    36  
    37  // NewWriter returns a wrtier implementation according to given algorithm.
    38  func NewWriter(w io.Writer, algorithm string) (io.WriteCloser, error) {
    39  	// Normalize input
    40  	algorithm = strings.TrimSpace(strings.ToLower(algorithm))
    41  
    42  	var (
    43  		compressedWriter io.WriteCloser
    44  		writerErr        error
    45  	)
    46  
    47  	// Apply transformation
    48  	switch algorithm {
    49  	case "gzip":
    50  		compressedWriter = gzip.NewWriter(w)
    51  	case "lzw", "lzw-lsb":
    52  		compressedWriter = lzw.NewWriter(w, lzw.LSB, 8)
    53  	case "lzw-msb":
    54  		compressedWriter = lzw.NewWriter(w, lzw.MSB, 8)
    55  	case "lz4":
    56  		compressedWriter = lz4.NewWriter(w)
    57  	case "s2", "snappy":
    58  		compressedWriter = s2.NewWriter(w)
    59  	case "zlib":
    60  		compressedWriter = zlib.NewWriter(w)
    61  	case "flate":
    62  		compressedWriter, writerErr = flate.NewWriter(w, flate.DefaultCompression)
    63  		if writerErr != nil {
    64  			return nil, fmt.Errorf("unable to initialize flate compressor: %w", writerErr)
    65  		}
    66  	case "lzma":
    67  		compressedWriter, writerErr = xz.NewWriter(w)
    68  		if writerErr != nil {
    69  			return nil, fmt.Errorf("unable to initialize lzma compressor: %w", writerErr)
    70  		}
    71  	case "zstd":
    72  		compressedWriter, writerErr = zstd.NewWriter(w)
    73  		if writerErr != nil {
    74  			return nil, fmt.Errorf("unable to initialize zstd compressor: %w", writerErr)
    75  		}
    76  	default:
    77  		return nil, fmt.Errorf("unhandled compression algorithm %q", algorithm)
    78  	}
    79  
    80  	// No error
    81  	return compressedWriter, nil
    82  }