aboutsummaryrefslogtreecommitdiffstats
path: root/library/go/blockcodecs/blockbrotli/brotli.go
blob: 8e806c47ed420e75e7081767fdee12914d4fd420 (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
package blockbrotli

import (
	"bytes"
	"encoding/binary"
	"fmt"
	"io"

	"github.com/andybalholm/brotli"
	"github.com/ydb-platform/ydb/library/go/blockcodecs"
)

type brotliCodec int

func (b brotliCodec) ID() blockcodecs.CodecID {
	switch b {
	case 1:
		return 48947
	case 10:
		return 43475
	case 11:
		return 7241
	case 2:
		return 63895
	case 3:
		return 11408
	case 4:
		return 47136
	case 5:
		return 45284
	case 6:
		return 63219
	case 7:
		return 59675
	case 8:
		return 40233
	case 9:
		return 10380
	default:
		panic("unsupported level")
	}
}

func (b brotliCodec) Name() string {
	return fmt.Sprintf("brotli_%d", b)
}

func (b brotliCodec) DecodedLen(in []byte) (int, error) {
	return blockcodecs.DecodedLen(in)
}

func (b brotliCodec) Encode(dst, src []byte) ([]byte, error) {
	if cap(dst) < 8 {
		dst = make([]byte, 8)
	}

	dst = dst[:8]
	binary.LittleEndian.PutUint64(dst, uint64(len(src)))

	wb := bytes.NewBuffer(dst)
	w := brotli.NewWriterLevel(wb, int(b))

	if _, err := w.Write(src); err != nil {
		return nil, err
	}

	if err := w.Close(); err != nil {
		return nil, err
	}

	return wb.Bytes(), nil
}

func (b brotliCodec) Decode(dst, src []byte) ([]byte, error) {
	if len(src) < 8 {
		return nil, fmt.Errorf("short block: %d < 8", len(src))
	}

	rb := bytes.NewBuffer(src[8:])
	r := brotli.NewReader(rb)

	_, err := io.ReadFull(r, dst)
	if err != nil {
		return nil, err
	}

	return dst, nil
}

func init() {
	for i := 1; i <= 11; i++ {
		blockcodecs.Register(brotliCodec(i))
	}
}