github.com/coocood/badger@v1.5.1-0.20200528065104-c02ac3616d04/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  	"errors"
    21  	"io"
    22  
    23  	"github.com/golang/snappy"
    24  	"github.com/klauspost/compress/zstd"
    25  )
    26  
    27  // CompressionType specifies how a block should be compressed.
    28  type CompressionType uint32
    29  
    30  const (
    31  	// None mode indicates that a block is not compressed.
    32  	None CompressionType = 0
    33  	// Snappy mode indicates that a block is compressed using Snappy algorithm.
    34  	Snappy CompressionType = 1
    35  	// ZSTD mode indicates that a block is compressed using ZSTD algorithm.
    36  	ZSTD CompressionType = 2
    37  )
    38  
    39  func (c CompressionType) Compress(w io.Writer, data []byte) error {
    40  	switch c {
    41  	case None:
    42  		_, err := w.Write(data)
    43  		return err
    44  	case Snappy:
    45  		sw := snappy.NewBufferedWriter(w)
    46  		_, err := sw.Write(data)
    47  		if err != nil {
    48  			return err
    49  		}
    50  		return sw.Close()
    51  	case ZSTD:
    52  		e, err := zstd.NewWriter(w)
    53  		if err != nil {
    54  			return err
    55  		}
    56  		_, err = e.Write(data)
    57  		if err != nil {
    58  			return err
    59  		}
    60  		return e.Close()
    61  	}
    62  	return errors.New("Unsupported compression type")
    63  }
    64  
    65  func (c CompressionType) Decompress(data []byte) ([]byte, error) {
    66  	switch c {
    67  	case None:
    68  		return data, nil
    69  	case Snappy:
    70  		return snappy.Decode(nil, data)
    71  	case ZSTD:
    72  		r, err := zstd.NewReader(nil)
    73  		if err != nil {
    74  			return nil, err
    75  		}
    76  		defer r.Close()
    77  		return r.DecodeAll(data, nil)
    78  	}
    79  	return nil, errors.New("Unsupported compression type")
    80  }
    81  
    82  func compress(in []byte) ([]byte, error) {
    83  	w, err := zstd.NewWriter(nil)
    84  	if err != nil {
    85  		return nil, err
    86  	}
    87  	return w.EncodeAll(in, make([]byte, 0, len(in))), nil
    88  }
    89  
    90  type TableBuilderOptions struct {
    91  	HashUtilRatio       float32
    92  	WriteBufferSize     int
    93  	BytesPerSecond      int
    94  	MaxLevels           int
    95  	LevelSizeMultiplier int
    96  	LogicalBloomFPR     float64
    97  	BlockSize           int
    98  	CompressionPerLevel []CompressionType
    99  	SuRFStartLevel      int
   100  	SuRFOptions         SuRFOptions
   101  }
   102  
   103  type SuRFOptions struct {
   104  	HashSuffixLen  int
   105  	RealSuffixLen  int
   106  	BitsPerKeyHint int
   107  }
   108  
   109  type ValueLogWriterOptions struct {
   110  	WriteBufferSize int
   111  }