diff options
author | hcpp <hcpp@ydb.tech> | 2023-11-08 12:09:41 +0300 |
---|---|---|
committer | hcpp <hcpp@ydb.tech> | 2023-11-08 12:56:14 +0300 |
commit | a361f5b98b98b44ea510d274f6769164640dd5e1 (patch) | |
tree | c47c80962c6e2e7b06798238752fd3da0191a3f6 /library/go/blockcodecs/blockbrotli/brotli.go | |
parent | 9478806fde1f4d40bd5a45e7cbe77237dab613e9 (diff) | |
download | ydb-a361f5b98b98b44ea510d274f6769164640dd5e1.tar.gz |
metrics have been added
Diffstat (limited to 'library/go/blockcodecs/blockbrotli/brotli.go')
-rw-r--r-- | library/go/blockcodecs/blockbrotli/brotli.go | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/library/go/blockcodecs/blockbrotli/brotli.go b/library/go/blockcodecs/blockbrotli/brotli.go new file mode 100644 index 0000000000..8e806c47ed --- /dev/null +++ b/library/go/blockcodecs/blockbrotli/brotli.go @@ -0,0 +1,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)) + } +} |