Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Commit e1f152f

Browse files
mholtklauspost
andauthored
sz: Support S2 (close #429) (#431)
* sz: Support S2 (close #429) * Update go.mod * Update sz.go Co-authored-by: Klaus Post <[email protected]> --------- Co-authored-by: Klaus Post <[email protected]>
1 parent e53ee3c commit e1f152f

File tree

3 files changed

+93
-12
lines changed

3 files changed

+93
-12
lines changed

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ require (
1717
require (
1818
github.com/STARRY-S/zip v0.2.1
1919
github.com/bodgit/sevenzip v1.6.0
20-
github.com/golang/snappy v0.0.4
2120
github.com/pierrec/lz4/v4 v4.1.21
2221
github.com/sorairolake/lzip-go v0.3.5
2322
golang.org/x/text v0.20.0

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y
5454
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
5555
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
5656
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
57-
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
58-
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
5957
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
6058
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
6159
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=

sz.go

Lines changed: 93 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,52 @@ import (
66
"io"
77
"strings"
88

9-
"github.com/golang/snappy"
9+
"github.com/klauspost/compress/s2"
1010
)
1111

1212
func init() {
1313
RegisterFormat(Sz{})
1414
}
1515

16-
// Sz facilitates Snappy compression.
17-
type Sz struct{}
16+
// Sz facilitates Snappy compression. It uses S2
17+
// for reading and writing, but by default will
18+
// write Snappy-compatible data.
19+
type Sz struct {
20+
// Configurable S2 extension.
21+
S2 S2
22+
}
23+
24+
// S2 is an extension of Snappy that can read Snappy
25+
// streams and write Snappy-compatible streams, but
26+
// can also be configured to write Snappy-incompatible
27+
// streams for greater gains. See
28+
// https://pkg.go.dev/github.com/klauspost/compress/s2
29+
// for details and the documentation for each option.
30+
type S2 struct {
31+
// reader options
32+
MaxBlockSize int
33+
AllocBlock int
34+
IgnoreStreamIdentifier bool
35+
IgnoreCRC bool
36+
37+
// writer options
38+
AddIndex bool
39+
Compression S2Level
40+
BlockSize int
41+
Concurrency int
42+
FlushOnWrite bool
43+
Padding int
44+
SnappyIncompatible bool
45+
}
1846

1947
func (sz Sz) Extension() string { return ".sz" }
2048

2149
func (sz Sz) Match(_ context.Context, filename string, stream io.Reader) (MatchResult, error) {
2250
var mr MatchResult
2351

2452
// match filename
25-
if strings.Contains(strings.ToLower(filename), sz.Extension()) {
53+
if strings.Contains(strings.ToLower(filename), sz.Extension()) ||
54+
strings.Contains(strings.ToLower(filename), ".s2") {
2655
mr.ByName = true
2756
}
2857

@@ -36,13 +65,68 @@ func (sz Sz) Match(_ context.Context, filename string, stream io.Reader) (MatchR
3665
return mr, nil
3766
}
3867

39-
func (Sz) OpenWriter(w io.Writer) (io.WriteCloser, error) {
40-
return snappy.NewBufferedWriter(w), nil
68+
func (sz Sz) OpenWriter(w io.Writer) (io.WriteCloser, error) {
69+
var opts []s2.WriterOption
70+
if sz.S2.AddIndex {
71+
opts = append(opts, s2.WriterAddIndex())
72+
}
73+
switch sz.S2.Compression {
74+
case S2LevelNone:
75+
opts = append(opts, s2.WriterUncompressed())
76+
case S2LevelBetter:
77+
opts = append(opts, s2.WriterBetterCompression())
78+
case S2LevelBest:
79+
opts = append(opts, s2.WriterBestCompression())
80+
}
81+
if sz.S2.BlockSize != 0 {
82+
opts = append(opts, s2.WriterBlockSize(sz.S2.BlockSize))
83+
}
84+
if sz.S2.Concurrency != 0 {
85+
opts = append(opts, s2.WriterConcurrency(sz.S2.Concurrency))
86+
}
87+
if sz.S2.FlushOnWrite {
88+
opts = append(opts, s2.WriterFlushOnWrite())
89+
}
90+
if sz.S2.Padding != 0 {
91+
opts = append(opts, s2.WriterPadding(sz.S2.Padding))
92+
}
93+
if !sz.S2.SnappyIncompatible {
94+
// this option is inverted because by default we should
95+
// probably write Snappy-compatible streams
96+
opts = append(opts, s2.WriterSnappyCompat())
97+
}
98+
return s2.NewWriter(w, opts...), nil
4199
}
42100

43-
func (Sz) OpenReader(r io.Reader) (io.ReadCloser, error) {
44-
return io.NopCloser(snappy.NewReader(r)), nil
101+
func (sz Sz) OpenReader(r io.Reader) (io.ReadCloser, error) {
102+
var opts []s2.ReaderOption
103+
if sz.S2.AllocBlock != 0 {
104+
opts = append(opts, s2.ReaderAllocBlock(sz.S2.AllocBlock))
105+
}
106+
if sz.S2.IgnoreCRC {
107+
opts = append(opts, s2.ReaderIgnoreCRC())
108+
}
109+
if sz.S2.IgnoreStreamIdentifier {
110+
opts = append(opts, s2.ReaderIgnoreStreamIdentifier())
111+
}
112+
if sz.S2.MaxBlockSize != 0 {
113+
opts = append(opts, s2.ReaderMaxBlockSize(sz.S2.MaxBlockSize))
114+
}
115+
return io.NopCloser(s2.NewReader(r, opts...)), nil
45116
}
46117

47-
// https://github.com/google/snappy/blob/master/framing_format.txt
118+
// Compression level for S2 (Snappy/Sz extension).
119+
// EXPERIMENTAL: May be changed or removed without a major version bump.
120+
type S2Level int
121+
122+
// Compression levels for S2.
123+
// EXPERIMENTAL: May be changed or removed without a major version bump.
124+
const (
125+
S2LevelNone S2Level = 0
126+
S2LevelFast S2Level = 1
127+
S2LevelBetter S2Level = 2
128+
S2LevelBest S2Level = 3
129+
)
130+
131+
// https://github.com/google/snappy/blob/master/framing_format.txt - contains "sNaPpY"
48132
var snappyHeader = []byte{0xff, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59}

0 commit comments

Comments
 (0)