/
pack.go
219 lines (202 loc) · 3.93 KB
/
pack.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package intarr
import (
"bytes"
"compress/gzip"
"encoding/binary"
"fmt"
"io"
"github.com/golang/snappy"
)
func Pack(in []uint64) (r []byte, e error) {
Sort(in)
w := bytes.NewBuffer(nil)
if len(in) <= 48 {
_, e = w.Write([]byte{'n'})
if e != nil {
return
}
for _, v := range in {
e = binary.Write(w, binary.BigEndian, v)
if e != nil {
return
}
}
return w.Bytes(), nil
// } else {
// _, e = w.Write([]byte{'p'})
// if e != nil {
// return
// }
// b, e := encode(in)
// if e != nil {
// return nil, e
// }
// _, e = w.Write(b)
// return w.Bytes(), nil
// }
}
return nil, nil
}
func Unpack(data []byte) (arr []uint64, e error) {
if len(data) < 5 {
return nil, fmt.Errorf("eror len is %d", len(data))
}
r := bytes.NewReader(data)
var t = make([]byte, 1)
_, e = r.Read(t)
if e != nil {
return
}
switch t[0] {
case 'n':
l := len(data[1:]) / 4
for i := 0; i < l; i++ {
var num uint64
e = binary.Read(r, binary.BigEndian, &num)
if e != nil {
return
}
arr = append(arr, num)
}
return
default:
e = fmt.Errorf("Unknow encoding type")
return
}
}
// func compress(in []uint64) (arr []uint64, e error) {
// l := len(in)
// if len(in)%128 != 0 {
// l = l + (128 - len(in)%128)
// }
//
// arr = make([]uint64, l)
// copy(arr, in)
//
// compdata := make([]uint64, 2*l)
// inpos := cursor.New()
// outpos := cursor.New()
// codec := bp32.New()
// e = codec.Compress(arr, inpos, l, compdata, outpos)
// if e != nil {
// return nil, e
// }
//
// return compdata[:outpos.Get()+1], e
// }
// func encode(in []uint64) ([]byte, error) {
//
// arr, e := compress(in)
// if e != nil {
// return nil, e
// }
//
// w := bytes.NewBuffer(nil)
// for _, v := range arr {
// e = binary.Write(w, binary.BigEndian, v)
// if e != nil {
// return nil, e
// }
// }
// return w.Bytes(), nil
// }
// func uncompress(arr []uint64) ([]uint64, error) {
// newinpos := cursor.New()
// newoutpos := cursor.New()
//
// recov := make([]uint64, 10000)
//
// codec := bp32.New()
// e := codec.Uncompress(arr, newinpos, len(arr)-1, recov, newoutpos)
// l := 0
// for i, v := range recov[:newoutpos.Get()] {
// if v != 0 {
// l = i
// }
// }
// return recov[:l+1], e
// }
// func decode(data []byte) ([]uint64, error) {
// r := bytes.NewReader(data)
//
// var arr []uint64
// var num uint64
// for e := binary.Read(r, binary.BigEndian, &num); e == nil; e = binary.Read(r, binary.BigEndian, &num) {
// arr = append(arr, num)
// }
// return uncompress(arr)
// }
func is2b(s Slice) ([]byte, error) {
w := bytes.NewBuffer(nil)
for _, v := range s {
e := binary.Write(w, binary.BigEndian, v)
if e != nil {
return nil, e
}
}
return w.Bytes(), nil
}
func b2is(b []byte) (s Slice, e error) {
r := bytes.NewReader(b)
var i uint64
for e = binary.Read(r, binary.BigEndian, &i); e == nil; e = binary.Read(r, binary.BigEndian, &i) {
s = append(s, i)
}
if e == io.EOF {
e = nil
}
return
}
func gz(data []byte) ([]byte, error) {
w := bytes.NewBuffer(nil)
gw, e := gzip.NewWriterLevel(w, gzip.BestSpeed)
if e != nil {
return nil, e
}
_, e = gw.Write(data)
if e != nil {
return nil, e
}
e = gw.Close()
return w.Bytes(), e
}
func ungz(data []byte) ([]byte, error) {
r := bytes.NewReader(data)
gr, e := gzip.NewReader(r)
if e != nil {
return nil, e
}
buf := bytes.NewBuffer(nil)
_, e = io.Copy(buf, gr)
return buf.Bytes(), e
}
func PackGzip(s Slice) ([]byte, error) {
b, e := is2b(s)
if e != nil {
return nil, e
}
return gz(b)
}
func UnpackGzip(data []byte) (Slice, error) {
bts, e := ungz(data)
if e != nil {
return Slice{}, e
}
return b2is(bts)
}
func snap(arr []byte) ([]byte, error) {
w := bytes.NewBuffer(nil)
sw := snappy.NewWriter(w)
_, e := sw.Write(arr)
if e != nil {
return nil, e
}
return w.Bytes(), nil
}
func PackSnappy(s Slice) ([]byte, error) {
b, e := is2b(s)
if e != nil {
return nil, e
}
return snap(b)
}