aboutsummaryrefslogtreecommitdiffstats
path: root/library/go/core/metrics/solomon/spack_compression.go
blob: 004fe0150d94fae79d2cdd3f042bcbf37386a96b (plain) (blame)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package solomon

import (
	"encoding/binary"
	"io"

	"github.com/OneOfOne/xxhash"
	"github.com/pierrec/lz4"
)

type CompressionType uint8

const (
	CompressionNone CompressionType = 0x0
	CompressionZlib CompressionType = 0x1
	CompressionZstd CompressionType = 0x2
	CompressionLz4  CompressionType = 0x3
)

const (
	compressionFrameLength = 512 * 1024
	hashTableSize          = 64 * 1024
)

type noCompressionWriteCloser struct {
	underlying io.Writer
	written    int
}

func (w *noCompressionWriteCloser) Write(p []byte) (int, error) {
	n, err := w.underlying.Write(p)
	w.written += n
	return n, err
}

func (w *noCompressionWriteCloser) Close() error {
	return nil
}

type lz4CompressionWriteCloser struct {
	underlying io.Writer
	buffer     []byte
	table      []int
	written    int
}

func (w *lz4CompressionWriteCloser) flushFrame() (written int, err error) {
	src := w.buffer
	dst := make([]byte, lz4.CompressBlockBound(len(src)))

	sz, err := lz4.CompressBlock(src, dst, w.table)
	if err != nil {
		return written, err
	}

	if sz == 0 {
		dst = src
	} else {
		dst = dst[:sz]
	}

	err = binary.Write(w.underlying, binary.LittleEndian, uint32(len(dst)))
	if err != nil {
		return written, err
	}
	w.written += 4

	err = binary.Write(w.underlying, binary.LittleEndian, uint32(len(src)))
	if err != nil {
		return written, err
	}
	w.written += 4

	n, err := w.underlying.Write(dst)
	if err != nil {
		return written, err
	}
	w.written += n

	checksum := xxhash.Checksum32S(dst, 0x1337c0de)
	err = binary.Write(w.underlying, binary.LittleEndian, checksum)
	if err != nil {
		return written, err
	}
	w.written += 4

	w.buffer = w.buffer[:0]

	return written, nil
}

func (w *lz4CompressionWriteCloser) Write(p []byte) (written int, err error) {
	q := p[:]
	for len(q) > 0 {
		space := compressionFrameLength - len(w.buffer)
		if space == 0 {
			n, err := w.flushFrame()
			if err != nil {
				return written, err
			}
			w.written += n
			space = compressionFrameLength
		}
		length := len(q)
		if length > space {
			length = space
		}
		w.buffer = append(w.buffer, q[:length]...)
		q = q[length:]
	}
	return written, nil
}

func (w *lz4CompressionWriteCloser) Close() error {
	var err error
	if len(w.buffer) > 0 {
		n, err := w.flushFrame()
		if err != nil {
			return err
		}
		w.written += n
	}
	err = binary.Write(w.underlying, binary.LittleEndian, uint32(0))
	if err != nil {
		return nil
	}
	w.written += 4

	err = binary.Write(w.underlying, binary.LittleEndian, uint32(0))
	if err != nil {
		return nil
	}
	w.written += 4

	err = binary.Write(w.underlying, binary.LittleEndian, uint32(0))
	if err != nil {
		return nil
	}
	w.written += 4

	return nil
}

func newCompressedWriter(w io.Writer, compression CompressionType) io.WriteCloser {
	switch compression {
	case CompressionNone:
		return &noCompressionWriteCloser{w, 0}
	case CompressionZlib:
		panic("zlib compression not supported")
	case CompressionZstd:
		panic("zstd compression not supported")
	case CompressionLz4:
		return &lz4CompressionWriteCloser{
			w,
			make([]byte, 0, compressionFrameLength),
			make([]int, hashTableSize),
			0,
		}
	default:
		panic("unsupported compression algorithm")
	}
}