forked from DuanWeiFan/parquet-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dictionary.go
59 lines (49 loc) · 1.22 KB
/
dictionary.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
package rle
import (
"math/bits"
"github.com/hhoughgg/parquet-go/encoding"
"github.com/hhoughgg/parquet-go/format"
"github.com/hhoughgg/parquet-go/internal/unsafecast"
)
type DictionaryEncoding struct {
encoding.NotSupported
}
func (e *DictionaryEncoding) String() string {
return "RLE_DICTIONARY"
}
func (e *DictionaryEncoding) Encoding() format.Encoding {
return format.RLEDictionary
}
func (e *DictionaryEncoding) EncodeInt32(dst []byte, src []int32) ([]byte, error) {
bitWidth := maxLenInt32(src)
dst = append(dst[:0], byte(bitWidth))
dst, err := encodeInt32(dst, src, uint(bitWidth))
return dst, e.wrap(err)
}
func (e *DictionaryEncoding) DecodeInt32(dst []int32, src []byte) ([]int32, error) {
if len(src) == 0 {
return dst[:0], nil
}
buf := unsafecast.Int32ToBytes(dst)
buf, err := decodeInt32(buf[:0], src[1:], uint(src[0]))
return unsafecast.BytesToInt32(buf), e.wrap(err)
}
func (e *DictionaryEncoding) wrap(err error) error {
if err != nil {
err = encoding.Error(e, err)
}
return err
}
func clearInt32(data []int32) {
for i := range data {
data[i] = 0
}
}
func maxLenInt32(data []int32) (max int) {
for _, v := range data {
if n := bits.Len32(uint32(v)); n > max {
max = n
}
}
return max
}