github.com/tsuna/gohbase@v0.0.0-20250731002811-4ffcadfba63e/compression/snappy/snappy.go (about)

     1  // Copyright (C) 2020  The GoHBase Authors.  All rights reserved.
     2  // This file is part of GoHBase.
     3  // Use of this source code is governed by the Apache License 2.0
     4  // that can be found in the COPYING file.
     5  
     6  package snappy
     7  
     8  import (
     9  	"github.com/golang/snappy"
    10  )
    11  
    12  // snapppyChunkLen is based on constants in org.apache.hadoop.io.compress.SnappyCodec
    13  // it's buffer len (256*1024) minus snappy overhead.
    14  const snappyChunkLen = 256*1024*5/6 - 32
    15  
    16  type snappyCodec struct{}
    17  
    18  func New() snappyCodec {
    19  	return snappyCodec{}
    20  }
    21  
    22  func (sc snappyCodec) Encode(src, dst []byte) ([]byte, uint32) {
    23  	chunk := snappy.Encode(dst[len(dst):cap(dst)], src)
    24  	// append the chunk, this can be O(1) in case Encode had enough capacity
    25  	return append(dst, chunk...), uint32(len(chunk))
    26  }
    27  
    28  func (sc snappyCodec) Decode(src, dst []byte) ([]byte, uint32, error) {
    29  	chunk, err := snappy.Decode(dst[len(dst):cap(dst)], src)
    30  	if err != nil {
    31  		return nil, 0, err
    32  	}
    33  	return append(dst, chunk...), uint32(len(chunk)), nil
    34  }
    35  
    36  func (sc snappyCodec) ChunkLen() uint32 {
    37  	return snappyChunkLen
    38  }
    39  
    40  func (sc snappyCodec) CellBlockCompressorClass() string {
    41  	return "org.apache.hadoop.io.compress.SnappyCodec"
    42  }