Skip to content

Commit eabc8c0

Browse files
authored
add s2 and snappy and gzip algorithm for compress (#2)
* add s2 and snappy algorithm for compress * add zip algorithm for compress * update go mod
1 parent b465e42 commit eabc8c0

File tree

12 files changed

+199
-12
lines changed

12 files changed

+199
-12
lines changed

compress/compress.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* Copyright 2022 Zinc Labs Inc. and Contributors
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package compress
17+
18+
import (
19+
"errors"
20+
)
21+
22+
const (
23+
SNAPPY = iota
24+
S2
25+
ZSTD
26+
GZIP
27+
)
28+
29+
var Algorithm = S2
30+
31+
func Decompress(dst, src []byte) ([]byte, error) {
32+
switch Algorithm {
33+
case SNAPPY:
34+
return SnappyDecompress(dst, src)
35+
case S2:
36+
return S2Decompress(dst, src)
37+
case ZSTD:
38+
return ZSTDDecompress(dst, src)
39+
case GZIP:
40+
return GzipDecompress(dst, src)
41+
default:
42+
return nil, errors.New("unknown compress algorithm")
43+
}
44+
}
45+
46+
func Compress(dst, src []byte) ([]byte, error) {
47+
switch Algorithm {
48+
case SNAPPY:
49+
return SnappyCompress(dst, src)
50+
case S2:
51+
return S2Compress(dst, src)
52+
case ZSTD:
53+
return ZSTDCompress(dst, src, ZSTDCompressionLevel)
54+
case GZIP:
55+
return GzipCompress(dst, src)
56+
default:
57+
return nil, errors.New("unknown compress algorithm")
58+
}
59+
}

compress/gzip.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* Copyright 2022 Zinc Labs Inc. and Contributors
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package compress
17+
18+
import (
19+
"bytes"
20+
"compress/gzip"
21+
)
22+
23+
// GzipDecompress decompresses a block using gzip algorithm.
24+
func GzipDecompress(dst, src []byte) ([]byte, error) {
25+
buf := bytes.NewBuffer(src)
26+
r, err := gzip.NewReader(buf)
27+
if err != nil {
28+
return nil, err
29+
}
30+
out := bytes.NewBuffer(dst[:0])
31+
out.ReadFrom(r)
32+
return out.Bytes(), nil
33+
}
34+
35+
// GzipCompress compresses a block using gzip algorithm.
36+
func GzipCompress(dst, src []byte) ([]byte, error) {
37+
buf := bytes.NewBuffer(dst[:0])
38+
w := gzip.NewWriter(buf)
39+
_, err := w.Write(src)
40+
if err != nil {
41+
return nil, err
42+
}
43+
if err = w.Close(); err != nil {
44+
return nil, err
45+
}
46+
return buf.Bytes(), nil
47+
}

compress/s2.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* Copyright 2022 Zinc Labs Inc. and Contributors
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package compress
17+
18+
import (
19+
s2c "github.com/klauspost/compress/s2"
20+
)
21+
22+
// S2Decompress decompresses a block using s2 algorithm.
23+
func S2Decompress(dst, src []byte) ([]byte, error) {
24+
return s2c.Decode(dst, src)
25+
}
26+
27+
// S2Compress compresses a block using s2 algorithm.
28+
func S2Compress(dst, src []byte) ([]byte, error) {
29+
return s2c.EncodeBetter(dst, src), nil
30+
}

compress/snappy.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* Copyright 2022 Zinc Labs Inc. and Contributors
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package compress
17+
18+
import "github.com/klauspost/compress/snappy"
19+
20+
// SnappyDecompress decompresses a block using snappy algorithm.
21+
func SnappyDecompress(dst, src []byte) ([]byte, error) {
22+
return snappy.Decode(dst, src)
23+
}
24+
25+
// SnappyCompress compresses a block using snappy algorithm.
26+
func SnappyCompress(dst, src []byte) ([]byte, error) {
27+
return snappy.Encode(dst, src), nil
28+
}

zstd.go renamed to compress/zstd.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package ice
17+
package compress
1818

1919
import (
2020
"log"
@@ -23,7 +23,7 @@ import (
2323
"github.com/klauspost/compress/zstd"
2424
)
2525

26-
const ZSTDCompressionLevel = 3 // 1, 3, 9
26+
var ZSTDCompressionLevel = 3 // 1, 3, 9
2727

2828
var (
2929
decoder *zstd.Decoder

contentcoder.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
"bytes"
1919
"encoding/binary"
2020
"io"
21+
22+
"github.com/blugelabs/ice/compress"
2123
)
2224

2325
var termSeparator byte = 0xff
@@ -120,7 +122,7 @@ func (c *chunkedContentCoder) flushContents() error {
120122
metaData := c.chunkMetaBuf.Bytes()
121123
c.final = append(c.final, c.chunkMetaBuf.Bytes()...)
122124
// write the compressed data to the final data
123-
c.compressed, err = ZSTDCompress(c.compressed[:cap(c.compressed)], c.chunkBuf.Bytes(), ZSTDCompressionLevel)
125+
c.compressed, err = compress.Compress(c.compressed[:cap(c.compressed)], c.chunkBuf.Bytes())
124126
if err != nil {
125127
return err
126128
}

documentcoder.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"bytes"
55
"encoding/binary"
66
"io"
7+
8+
"github.com/blugelabs/ice/compress"
79
)
810

911
const defaultDocumentChunkSize uint32 = 128
@@ -70,7 +72,7 @@ func (c *chunkedDocumentCoder) newLine() error {
7072
func (c *chunkedDocumentCoder) flush() error {
7173
if c.buf.Len() > 0 {
7274
var err error
73-
c.compressed, err = ZSTDCompress(c.compressed[:cap(c.compressed)], c.buf.Bytes(), ZSTDCompressionLevel)
75+
c.compressed, err = compress.Compress(c.compressed[:cap(c.compressed)], c.buf.Bytes())
7476
if err != nil {
7577
return err
7678
}

docvalues.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"sort"
2323

2424
segment "github.com/blugelabs/bluge_segment_api"
25+
"github.com/blugelabs/ice/compress"
2526
)
2627

2728
type docNumTermsVisitor func(docNum uint64, terms []byte) error
@@ -209,7 +210,7 @@ func (di *docValueReader) iterateAllDocValues(s *Segment, visitor docNumTermsVis
209210
}
210211

211212
// uncompress the already loaded data
212-
uncompressed, err := ZSTDDecompress(di.uncompressed[:cap(di.uncompressed)], di.curChunkData)
213+
uncompressed, err := compress.Decompress(di.uncompressed[:cap(di.uncompressed)], di.curChunkData)
213214
if err != nil {
214215
return err
215216
}
@@ -244,7 +245,7 @@ func (di *docValueReader) visitDocValues(docNum uint64,
244245
uncompressed = di.uncompressed
245246
} else {
246247
// uncompress the already loaded data
247-
uncompressed, err = ZSTDDecompress(di.uncompressed[:cap(di.uncompressed)], di.curChunkData)
248+
uncompressed, err = compress.Decompress(di.uncompressed[:cap(di.uncompressed)], di.curChunkData)
248249
if err != nil {
249250
return err
250251
}

intcoder.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
"bytes"
1919
"encoding/binary"
2020
"io"
21+
22+
"github.com/blugelabs/ice/compress"
2123
)
2224

2325
// We can safely use 0 to represent termNotEncoded since 0
@@ -104,7 +106,7 @@ func (c *chunkedIntCoder) Add(docNum uint64, vals ...uint64) error {
104106
// to be encoded.
105107
func (c *chunkedIntCoder) Close() error {
106108
var err error
107-
c.compressed, err = ZSTDCompress(c.compressed[:cap(c.compressed)], c.chunkBuf.Bytes(), ZSTDCompressionLevel)
109+
c.compressed, err = compress.Compress(c.compressed[:cap(c.compressed)], c.chunkBuf.Bytes())
108110
if err != nil {
109111
return err
110112
}

intdecoder.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"fmt"
2020

2121
segment "github.com/blugelabs/bluge_segment_api"
22+
"github.com/blugelabs/ice/compress"
2223
)
2324

2425
type chunkedIntDecoder struct {
@@ -87,7 +88,10 @@ func (d *chunkedIntDecoder) loadChunk(chunk int) error {
8788
if err != nil {
8889
return err
8990
}
90-
d.uncompressed, err = ZSTDDecompress(d.uncompressed[:cap(d.uncompressed)], curChunkBytesData)
91+
if len(curChunkBytesData) == 0 {
92+
return nil
93+
}
94+
d.uncompressed, err = compress.Decompress(d.uncompressed[:cap(d.uncompressed)], curChunkBytesData)
9195
if err != nil {
9296
return err
9397
}

0 commit comments

Comments
 (0)