-
Notifications
You must be signed in to change notification settings - Fork 46
/
runlengthintegerwriter.go
159 lines (151 loc) · 3.3 KB
/
runlengthintegerwriter.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
package orc
import (
"io"
)
const (
MaxRepeatSize = 127 + MinRepeatSize
MinDelta = -128
MaxDelta = 127
)
type RunLengthIntegerWriter struct {
w io.ByteWriter
signed bool
literals []int64
numLiterals int
delta int
repeat bool
tailRunLength int
minRepeatSize int
maxRepeatSize int
minDelta int
maxDelta int
maxLiteralSize int
}
func NewRunLengthIntegerWriter(w io.ByteWriter, signed bool) *RunLengthIntegerWriter {
return &RunLengthIntegerWriter{
w: w,
signed: signed,
literals: make([]int64, MaxLiteralSize),
minRepeatSize: MinRepeatSize,
maxRepeatSize: MaxRepeatSize,
minDelta: MinDelta,
maxDelta: MaxDelta,
maxLiteralSize: MaxLiteralSize,
}
}
func (w *RunLengthIntegerWriter) writeValues() error {
if w.numLiterals != 0 {
if w.repeat {
err := w.w.WriteByte(byte(w.numLiterals - w.minRepeatSize))
if err != nil {
return err
}
err = w.w.WriteByte(byte(w.delta))
if err != nil {
return err
}
if w.signed {
err := writeVslong(w.w, w.literals[0])
if err != nil {
return err
}
} else {
err := writeVulong(w.w, w.literals[0])
if err != nil {
return err
}
}
} else {
err := w.w.WriteByte(byte(-w.numLiterals))
if err != nil {
return err
}
for i := 0; i < w.numLiterals; i++ {
if w.signed {
err := writeVslong(w.w, w.literals[i])
if err != nil {
return err
}
} else {
err := writeVulong(w.w, w.literals[i])
if err != nil {
return err
}
}
}
}
w.repeat = false
w.numLiterals = 0
w.tailRunLength = 0
}
return nil
}
func (w *RunLengthIntegerWriter) Flush() error {
return w.writeValues()
}
func (w *RunLengthIntegerWriter) WriteInt(value int64) error {
if w.numLiterals == 0 {
w.literals[w.numLiterals] = value
w.numLiterals++
w.tailRunLength = 1
} else if w.repeat {
if value == int64((w.literals[0])+int64(w.delta*w.numLiterals)) {
w.numLiterals++
if w.numLiterals == w.maxRepeatSize {
return w.writeValues()
}
} else {
err := w.writeValues()
if err != nil {
return err
}
w.literals[w.numLiterals] = value
w.numLiterals++
w.tailRunLength = 1
}
} else {
if w.tailRunLength == 1 {
w.delta = int(value - w.literals[w.numLiterals-1])
if w.delta < w.minDelta || w.delta > w.maxDelta {
w.tailRunLength = 1
} else {
w.tailRunLength = 2
}
} else if value == w.literals[w.numLiterals-1]+int64(w.delta) {
w.tailRunLength++
} else {
w.delta = int(value - w.literals[w.numLiterals-1])
if w.delta < w.minDelta || w.delta > w.maxDelta {
w.tailRunLength = 1
} else {
w.tailRunLength = 2
}
}
if w.tailRunLength == w.minRepeatSize {
if w.numLiterals+1 == w.minRepeatSize {
w.repeat = true
w.numLiterals++
} else {
w.numLiterals -= w.minRepeatSize - 1
base := w.literals[w.numLiterals]
err := w.writeValues()
if err != nil {
return err
}
w.literals[0] = base
w.repeat = true
w.numLiterals = w.minRepeatSize
}
} else {
w.literals[w.numLiterals] = value
w.numLiterals++
if w.numLiterals == w.maxLiteralSize {
return w.writeValues()
}
}
}
return nil
}
func (w *RunLengthIntegerWriter) Close() error {
return w.Flush()
}