-
Notifications
You must be signed in to change notification settings - Fork 4
/
writer.go
116 lines (93 loc) · 2.34 KB
/
writer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package seekable
import (
"fmt"
"io"
"sync"
"go.uber.org/multierr"
"github.com/SaveTheRbtz/zstd-seekable-format-go/options"
)
// writerEnvImpl is the environment implementation of for the underlying WriteCloser.
type writerEnvImpl struct {
w io.Writer
}
func (w *writerEnvImpl) WriteFrame(p []byte) (n int, err error) {
return w.w.Write(p)
}
func (w *writerEnvImpl) WriteSeekTable(p []byte) (n int, err error) {
return w.w.Write(p)
}
type writerImpl struct {
enc ZSTDEncoder
frameEntries []seekTableEntry
o options.WriterOptions
once *sync.Once
}
var (
_ io.Writer = (*writerImpl)(nil)
_ io.Closer = (*writerImpl)(nil)
)
type Writer interface {
// Write writes a chunk of data as a separate frame into the datastream.
//
// Note that Write does not do any coalescing nor splitting of data,
// so each write will map to a separate ZSTD Frame.
Write(src []byte) (int, error)
// Close implement io.Closer interface. It writes the seek table footer
// and releases occupied memory.
//
// Caller is still responsible to Close the underlying writer.
Close() (err error)
}
// ZSTDEncoder is the compressor. Tested with github.com/klauspost/compress/zstd.
type ZSTDEncoder interface {
EncodeAll(src, dst []byte) []byte
}
// NewWriter wraps the passed io.Writer and Encoder into and indexed ZSTD stream.
// Resulting stream then can be randomly accessed through the Reader and Decoder interfaces.
func NewWriter(w io.Writer, encoder ZSTDEncoder, opts ...options.WOption) (Writer, error) {
sw := writerImpl{
once: &sync.Once{},
enc: encoder,
}
sw.o.SetDefault()
for _, o := range opts {
err := o(&sw.o)
if err != nil {
return nil, err
}
}
if sw.o.Env == nil {
sw.o.Env = &writerEnvImpl{
w: w,
}
}
return &sw, nil
}
func (s *writerImpl) Write(src []byte) (int, error) {
dst, err := s.Encode(src)
if err != nil {
return 0, err
}
n, err := s.o.Env.WriteFrame(dst)
if err != nil {
return 0, err
}
if n != len(dst) {
return 0, fmt.Errorf("partial write: %d out of %d", n, len(dst))
}
return len(src), nil
}
func (s *writerImpl) Close() (err error) {
s.once.Do(func() {
err = multierr.Append(err, s.writeSeekTable())
})
return
}
func (s *writerImpl) writeSeekTable() error {
seekTableBytes, err := s.EndStream()
if err != nil {
return err
}
_, err = s.o.Env.WriteSeekTable(seekTableBytes)
return err
}