-
Notifications
You must be signed in to change notification settings - Fork 4
/
varint.go
78 lines (59 loc) · 1.49 KB
/
varint.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
package compression
import (
"io"
"github.com/suggest-go/suggest/pkg/store"
)
// VBEncoder returns new instance of vbEnc that encodes posting list using
// delta encoding
// variable length byte string compression
func VBEncoder() Encoder {
return &vbEnc{}
}
// VBDecoder decodes given bytes array to posting list which was encoded by VBEncoder
func VBDecoder() Decoder {
return &vbEnc{}
}
// vbEnc implements VBEncoder and VBDecoder
type vbEnc struct{}
// Encode encodes the given positing list into the buf array
// Returns a number of written bytes
func (b *vbEnc) Encode(list []uint32, out store.Output) (int, error) {
return varIntEncode(list, out, 0)
}
// Decode decodes the given byte array to the buf list
// Returns a number of elements encoded
func (b *vbEnc) Decode(in store.Input, buf []uint32) (int, error) {
return varIntDecode(in, buf, 0)
}
func varIntEncode(list []uint32, out store.Output, prev uint32) (int, error) {
total := 0
for _, v := range list {
delta := v - prev
prev = v
n, err := out.WriteVUInt32(delta)
total += n
if err != nil {
if err == io.EOF {
return total, nil
}
return total, err
}
}
return total, nil
}
func varIntDecode(in store.Input, buf []uint32, prev uint32) (int, error) {
total := 0
for total < len(buf) {
v, err := in.ReadVUInt32()
if err != nil {
if err == io.EOF || err == io.ErrUnexpectedEOF {
return total, nil
}
return total, err
}
prev += v
buf[total] = prev
total++
}
return total, nil
}