github.com/balzaczyy/golucene@v0.0.0-20151210033525-d0be9ee89713/core/codec/compressing/mode.go (about)

     1  package compressing
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  )
     7  
     8  type CompressionMode interface {
     9  	NewCompressor() Compressor
    10  	NewDecompressor() Decompressor
    11  }
    12  
    13  const (
    14  	COMPRESSION_MODE_FAST = CompressionModeDefaults(1)
    15  )
    16  
    17  type CompressionModeDefaults int
    18  
    19  func (m CompressionModeDefaults) NewCompressor() Compressor {
    20  	switch int(m) {
    21  	case 1:
    22  		var ht = new(LZ4HashTable)
    23  		return Compressor(func(bytes []byte, out DataOutput) error {
    24  			return LZ4Compress(bytes, out, ht)
    25  		})
    26  	default:
    27  		panic("not implemented yet")
    28  	}
    29  }
    30  
    31  func (m CompressionModeDefaults) NewDecompressor() Decompressor {
    32  	switch int(m) {
    33  	case 1:
    34  		return LZ4Decompressor
    35  	default:
    36  		panic("not implemented yet")
    37  	}
    38  }
    39  
    40  // codec/compressing/Compressor.java
    41  
    42  /*
    43  Compress bytes into out. It is the responsibility of the compressor
    44  to add all necessary information so that a Decompressor will know
    45  when to stop decompressing bytes from the stream.
    46  */
    47  type Compressor func(bytes []byte, out DataOutput) error
    48  
    49  // codec/compressing/Decompressor.java
    50  
    51  /*
    52  Decompress 'bytes' that were stored between [offset:offset+length]
    53  in the original stream from the compressed stream 'in' to 'bytes'.
    54  The length of the returned bytes (len(buf)) must be equal to 'length'.
    55  Implementations of this method are free to resize 'bytes' depending
    56  on their needs.
    57  */
    58  type Decompressor func(in DataInput, originalLength, offset, length int, bytes []byte) (buf []byte, err error)
    59  
    60  func LZ4Decompressor(in DataInput, originalLength, offset, length int, bytes []byte) (res []byte, err error) {
    61  	assert(offset+length <= originalLength)
    62  	// add 7 padding bytes, this is not necessary but can help decompression run faster
    63  	res = bytes
    64  	if len(res) < originalLength+7 {
    65  		// res = make([]byte, util.Oversize(originalLength+7, 1))
    66  		// FIXME util cause dep circle
    67  		res = make([]byte, originalLength+7)
    68  	}
    69  	decompressedLength, err := LZ4Decompress(in, offset+length, res)
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  	if decompressedLength > originalLength {
    74  		return nil, errors.New(fmt.Sprintf("Corrupted: lengths mismatch: %v > %v (resource=%v)", decompressedLength, originalLength, in))
    75  	}
    76  	return res[offset : offset+length], nil
    77  }