Skip to content

Commit

Permalink
chore: improve code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
fufuok committed Oct 4, 2022
1 parent 7383de0 commit 820cd89
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 41 deletions.
52 changes: 15 additions & 37 deletions compress.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package utils

import (
"bytes"
"compress/gzip"
"compress/zlib"
"io/ioutil"
"sync"

"github.com/fufuok/utils/pools/bufferpool"
"github.com/fufuok/utils/pools/readerpool"
)

var (
Expand All @@ -16,25 +18,19 @@ var (
return new(gzip.Reader)
},
}
bufferPool = sync.Pool{
New: func() interface{} {
return bytes.NewBuffer(nil)
},
}
)

func Gzip(data []byte) ([]byte, error) {
return GzipLevel(data, gzip.BestSpeed)
}

func GzipLevel(data []byte, level int) (dst []byte, err error) {
buf := bufferPool.Get().(*bytes.Buffer)
buf := bufferpool.Get()
idx := getWriterPoolIndex(level)
zw := gzipWritePool[idx].Get().(*gzip.Writer)
zw.Reset(buf)
defer func() {
buf.Reset()
bufferPool.Put(buf)
bufferpool.Put(buf)
gzipWritePool[idx].Put(zw)
}()

Expand All @@ -51,28 +47,19 @@ func GzipLevel(data []byte, level int) (dst []byte, err error) {
return
}

dst = buf.Bytes()
dst = CopyBytes(buf.Bytes())
return
}

func Ungzip(data []byte) (src []byte, err error) {
buf := bufferPool.Get().(*bytes.Buffer)
defer func() {
buf.Reset()
bufferPool.Put(buf)
}()

_, err = buf.Write(data)
if err != nil {
return
}

rData := readerpool.New(data)
zr := gzipReaderPool.Get().(*gzip.Reader)
defer func() {
readerpool.Release(rData)
gzipReaderPool.Put(zr)
}()

err = zr.Reset(buf)
err = zr.Reset(rData)
if err != nil {
return
}
Expand All @@ -92,13 +79,12 @@ func Zip(data []byte) ([]byte, error) {
}

func ZipLevel(data []byte, level int) (dst []byte, err error) {
buf := bufferPool.Get().(*bytes.Buffer)
buf := bufferpool.Get()
idx := getWriterPoolIndex(level)
zw := zlibWritePool[idx].Get().(*zlib.Writer)
zw.Reset(buf)
defer func() {
buf.Reset()
bufferPool.Put(buf)
bufferpool.Put(buf)
zlibWritePool[idx].Put(zw)
}()

Expand All @@ -115,22 +101,14 @@ func ZipLevel(data []byte, level int) (dst []byte, err error) {
return
}

dst = buf.Bytes()
dst = CopyBytes(buf.Bytes())
return
}

func Unzip(data []byte) (src []byte, err error) {
buf := bufferPool.Get().(*bytes.Buffer)
defer func() {
buf.Reset()
bufferPool.Put(buf)
}()

_, err = buf.Write(data)
if err != nil {
return
}
zr, err := zlib.NewReader(buf)
rData := readerpool.New(data)
defer readerpool.Release(rData)
zr, err := zlib.NewReader(rData)
if err != nil {
return nil, err
}
Expand Down
6 changes: 2 additions & 4 deletions pools/bufferpool/buffer_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"fmt"
"strconv"
"testing"

"github.com/fufuok/utils"
)

func TestBufferPool(t *testing.T) {
Expand Down Expand Up @@ -51,12 +49,12 @@ func TestBufferPool(t *testing.T) {
t.Fatalf("Unexpected result: true, Expecting false")
}
buf = Get()
buf.WriteString(utils.RandString(64))
buf.Write(make([]byte, 64))
if !Release(buf) {
t.Fatalf("Unexpected result: true, Expecting false")
}
buf = Get()
buf.WriteString(utils.RandString(65))
buf.Write(make([]byte, 65))
if Release(buf) {
t.Fatal("Unexpected result: false, Expecting true")
}
Expand Down

0 comments on commit 820cd89

Please sign in to comment.