-
Notifications
You must be signed in to change notification settings - Fork 53
/
type_dict.go
202 lines (168 loc) · 3.82 KB
/
type_dict.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package goparquet
import (
"io"
"math/bits"
"github.com/pkg/errors"
)
type dictDecoder struct {
values []interface{}
keys decoder
}
// just for tests
func (d *dictDecoder) setValues(v []interface{}) {
d.values = v
}
// the value should be there before the init
func (d *dictDecoder) init(r io.Reader) error {
buf := make([]byte, 1)
if _, err := io.ReadFull(r, buf); err != nil {
return err
}
w := int(buf[0])
if w < 0 || w > 32 {
return errors.Errorf("invalid bitwidth %d", w)
}
if w >= 0 {
d.keys = newHybridDecoder(w)
return d.keys.init(r)
}
return errors.New("bit width zero with non-empty dictionary")
}
func (d *dictDecoder) decodeValues(dst []interface{}) (int, error) {
if d.keys == nil {
return 0, errors.New("no value is inside dictionary")
}
size := int32(len(d.values))
for i := range dst {
key, err := d.keys.next()
if err != nil {
return i, err
}
if key < 0 || key >= size {
return 0, errors.Errorf("dict: invalid index %d, values count are %d", key, size)
}
dst[i] = d.values[key]
}
return len(dst), nil
}
type dictStore struct {
values []interface{}
data []int32
indices map[interface{}]int32
size int64
valueSize int64
readPos int
nullCount int32
noDictMode bool
}
func (d *dictStore) init() {
d.indices = make(map[interface{}]int32)
d.values = d.values[:0]
d.data = d.data[:0]
d.nullCount = 0
d.readPos = 0
d.size = 0
d.valueSize = 0
}
func (d *dictStore) assemble() []interface{} {
if d.noDictMode {
return d.values
}
ret := make([]interface{}, 0, len(d.data))
for i := range d.data {
ret = append(ret, d.values[d.data[i]])
}
return ret
}
func (d *dictStore) getIndex(in interface{}, size int) int32 {
key := mapKey(in)
if idx, ok := d.indices[key]; ok {
return idx
}
d.valueSize += int64(size)
d.values = append(d.values, in)
idx := int32(len(d.values) - 1)
d.indices[key] = idx
return idx
}
func (d *dictStore) addValue(v interface{}, size int) {
if v == nil {
d.nullCount++
return
}
d.size += int64(size)
d.data = append(d.data, d.getIndex(v, size))
}
func (d *dictStore) getNextValue() (interface{}, error) {
if d.noDictMode {
if d.readPos >= len(d.values) {
return nil, errors.New("out of range")
}
d.readPos++
return d.values[d.readPos-1], nil
}
if d.readPos >= len(d.data) {
return nil, errors.New("out of range")
}
d.readPos++
pos := d.data[d.readPos-1]
return d.values[pos], nil
}
func (d *dictStore) numValues() int32 {
return int32(len(d.data))
}
func (d *dictStore) nullValueCount() int32 {
return d.nullCount
}
func (d *dictStore) numDistinctValues() int32 {
return int32(len(d.values))
}
// sizes is an experimental guess for the dictionary size and real value size (when there is no dictionary)
func (d *dictStore) sizes() (dictLen int64, noDictLen int64) {
count := len(d.data)
max := bits.Len(uint(count)) // bits required for any value in data
if max > 0 {
dictLen = int64(count/max) + 1
}
dictLen += d.valueSize
noDictLen = d.size
return
}
type dictEncoder struct {
w io.Writer
dictStore
}
func (d *dictEncoder) Close() error {
v := len(d.values)
if v == 0 { // empty dictionary?
return errors.New("empty dictionary nothing to write")
}
w := bits.Len(uint(v))
// first write the bitLength in a byte
if err := writeFull(d.w, []byte{byte(w)}); err != nil {
return err
}
enc := newHybridEncoder(w)
if err := enc.init(d.w); err != nil {
return err
}
if err := enc.encode(d.data); err != nil {
return err
}
return enc.Close()
}
func (d *dictEncoder) init(w io.Writer) error {
d.w = w
d.dictStore.init()
return nil
}
func (d *dictEncoder) encodeValues(values []interface{}) error {
for i := range values {
d.addValue(values[i], 0) // size is not important here
}
return nil
}
// just for tests
func (d *dictEncoder) getValues() []interface{} {
return d.values
}