mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-21 04:04:43 +08:00
优化代码规范
This commit is contained in:
@@ -8,22 +8,23 @@ import (
|
||||
"runtime"
|
||||
)
|
||||
|
||||
var memPool bytespool.IBytesMempool = bytespool.NewMemAreaPool()
|
||||
var memPool bytespool.IBytesMemPool = bytespool.NewMemAreaPool()
|
||||
|
||||
type ICompressor interface {
|
||||
CompressBlock(src []byte) ([]byte, error) //dst如果有预申请使用dst内存,传入nil时内部申请
|
||||
UncompressBlock(src []byte) ([]byte, error) //dst如果有预申请使用dst内存,传入nil时内部申请
|
||||
|
||||
CompressBufferCollection(buffer []byte) //压缩的Buffer内存回收
|
||||
UnCompressBufferCollection(buffer []byte) //解压缩的Buffer内存回收
|
||||
CompressBufferCollection(buffer []byte) //压缩的Buffer内存回收
|
||||
UnCompressBufferCollection(buffer []byte) //解压缩的Buffer内存回收
|
||||
}
|
||||
|
||||
var compressor ICompressor
|
||||
func init(){
|
||||
|
||||
func init() {
|
||||
SetCompressor(&Lz4Compressor{})
|
||||
}
|
||||
|
||||
func SetCompressor(cp ICompressor){
|
||||
func SetCompressor(cp ICompressor) {
|
||||
compressor = cp
|
||||
}
|
||||
|
||||
@@ -42,21 +43,21 @@ func (lc *Lz4Compressor) CompressBlock(src []byte) (dest []byte, err error) {
|
||||
|
||||
var c lz4.Compressor
|
||||
var cnt int
|
||||
dest = memPool.MakeBytes(lz4.CompressBlockBound(len(src))+1)
|
||||
dest = memPool.MakeBytes(lz4.CompressBlockBound(len(src)) + 1)
|
||||
cnt, err = c.CompressBlock(src, dest[1:])
|
||||
if err != nil {
|
||||
memPool.ReleaseBytes(dest)
|
||||
return nil,err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ratio := len(src) / cnt
|
||||
if len(src) % cnt > 0 {
|
||||
if len(src)%cnt > 0 {
|
||||
ratio += 1
|
||||
}
|
||||
|
||||
if ratio > 255 {
|
||||
memPool.ReleaseBytes(dest)
|
||||
return nil,fmt.Errorf("Impermissible errors")
|
||||
return nil, fmt.Errorf("impermissible errors")
|
||||
}
|
||||
|
||||
dest[0] = uint8(ratio)
|
||||
@@ -76,24 +77,24 @@ func (lc *Lz4Compressor) UncompressBlock(src []byte) (dest []byte, err error) {
|
||||
|
||||
radio := uint8(src[0])
|
||||
if radio == 0 {
|
||||
return nil,fmt.Errorf("Impermissible errors")
|
||||
return nil, fmt.Errorf("impermissible errors")
|
||||
}
|
||||
|
||||
dest = memPool.MakeBytes(len(src)*int(radio))
|
||||
dest = memPool.MakeBytes(len(src) * int(radio))
|
||||
cnt, err := lz4.UncompressBlock(src[1:], dest)
|
||||
if err != nil {
|
||||
memPool.ReleaseBytes(dest)
|
||||
return nil,err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return dest[:cnt],nil
|
||||
return dest[:cnt], nil
|
||||
}
|
||||
|
||||
func (lc *Lz4Compressor) compressBlockBound(n int) int{
|
||||
func (lc *Lz4Compressor) compressBlockBound(n int) int {
|
||||
return lz4.CompressBlockBound(n)
|
||||
}
|
||||
|
||||
func (lc *Lz4Compressor) CompressBufferCollection(buffer []byte){
|
||||
func (lc *Lz4Compressor) CompressBufferCollection(buffer []byte) {
|
||||
memPool.ReleaseBytes(buffer)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user