github.com/pingcap/badger@v1.5.1-0.20230103063557-828f39b09b6d/options/options.go (about)

     1  /*
     2   * Copyright 2017 Dgraph Labs, Inc. and Contributors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package options
    18  
    19  import (
    20  	"bytes"
    21  	"errors"
    22  	"io"
    23  	"io/ioutil"
    24  
    25  	"github.com/golang/snappy"
    26  	"github.com/klauspost/compress/zstd"
    27  	"github.com/pingcap/badger/buffer"
    28  )
    29  
    30  // CompressionType specifies how a block should be compressed.
    31  type CompressionType uint32
    32  
    33  const (
    34  	// None mode indicates that a block is not compressed.
    35  	None CompressionType = 0
    36  	// Snappy mode indicates that a block is compressed using Snappy algorithm.
    37  	Snappy CompressionType = 1
    38  	// ZSTD mode indicates that a block is compressed using ZSTD algorithm.
    39  	ZSTD CompressionType = 2
    40  )
    41  
    42  func (c CompressionType) Compress(w io.Writer, data []byte) error {
    43  	switch c {
    44  	case None:
    45  		_, err := w.Write(data)
    46  		return err
    47  	case Snappy:
    48  		dst := buffer.GetBuffer(snappy.MaxEncodedLen(len(data)))
    49  		res := snappy.Encode(dst, data)
    50  		_, err := w.Write(res)
    51  		buffer.PutBuffer(dst)
    52  		return err
    53  	case ZSTD:
    54  		e, err := zstd.NewWriter(w)
    55  		if err != nil {
    56  			return err
    57  		}
    58  		_, err = e.Write(data)
    59  		if err != nil {
    60  			return err
    61  		}
    62  		return e.Close()
    63  	}
    64  	return errors.New("Unsupported compression type")
    65  }
    66  
    67  func (c CompressionType) Decompress(data []byte) ([]byte, error) {
    68  	switch c {
    69  	case None:
    70  		return data, nil
    71  	case Snappy:
    72  		defer buffer.PutBuffer(data)
    73  		length, err := snappy.DecodedLen(data)
    74  		if err != nil {
    75  			return nil, err
    76  		}
    77  		dst := buffer.GetBuffer(length)
    78  		res, err := snappy.Decode(dst, data)
    79  		if &res[0] != &dst[0] {
    80  			buffer.PutBuffer(dst)
    81  		}
    82  		return res, err
    83  	case ZSTD:
    84  		defer buffer.PutBuffer(data)
    85  		reader := bytes.NewBuffer(data)
    86  		r, err := zstd.NewReader(reader)
    87  		if err != nil {
    88  			return nil, err
    89  		}
    90  		defer r.Close()
    91  		return ioutil.ReadAll(r)
    92  	}
    93  	return nil, errors.New("Unsupported compression type")
    94  }
    95  
    96  func compress(in []byte) ([]byte, error) {
    97  	w, err := zstd.NewWriter(nil)
    98  	if err != nil {
    99  		return nil, err
   100  	}
   101  	return w.EncodeAll(in, make([]byte, 0, len(in))), nil
   102  }
   103  
   104  type TableBuilderOptions struct {
   105  	HashUtilRatio       float32
   106  	WriteBufferSize     int
   107  	BytesPerSecond      int
   108  	MaxLevels           int
   109  	LevelSizeMultiplier int
   110  	LogicalBloomFPR     float64
   111  	BlockSize           int
   112  	CompressionPerLevel []CompressionType
   113  	SuRFStartLevel      int
   114  	SuRFOptions         SuRFOptions
   115  	MaxTableSize        int64
   116  }
   117  
   118  type SuRFOptions struct {
   119  	HashSuffixLen  int
   120  	RealSuffixLen  int
   121  	BitsPerKeyHint int
   122  }
   123  
   124  type ValueLogWriterOptions struct {
   125  	WriteBufferSize int
   126  }