Debian Codegraph @

dstream/gen/bcols.tmpl

raw
1 package dstream
2
3 import (
4 "encoding/json"
5 "encoding/binary"
6 "compress/gzip"
7 "bufio"
8 "strings"
9 "io"
10 "io/ioutil"
11 "fmt"
12 "os"
13 "path"
14 "time"
15 "github.com/golang/snappy"
16 )
17
18 func (b *bcols) init() {
19
20 // Prepare to read the dtypes structure
21 fname := path.Join(b.bpath, "dtypes.json")
22 fid, err := os.Open(fname)
23 if err != nil {
24 panic(err)
25 }
26 defer fid.Close()
27 dec := json.NewDecoder(fid)
28
29 // Dtypes for all variables
30 b.dtypesAll = make(map[string]string)
31 err = dec.Decode(&b.dtypesAll)
32 if err != nil {
33 panic(err)
34 }
35
36 // Names of variables to be used
37 usenames := b.usenames()
38 b.names = usenames
39
40 // Dtypes for variables to be used
41 dtypes := make(map[string]string)
42 for _, na := range usenames {
43 dtypes[na] = b.dtypesAll[na]
44 }
45 b.dtypes = dtypes
46
47 // Map from variable names to file names
48 fi, err := ioutil.ReadDir(b.bpath)
49 if err != nil {
50 panic(err)
51 }
52 vf := make(map[string]string)
53 for _, v := range fi {
54 na := strings.Split(v.Name(), ".")[0]
55 vf[na] = v.Name()
56 }
57
58 b.namepos = make(map[string]int)
59 for k, na := range b.names {
60 b.namepos[na] = k
61 }
62
63 b.bdata = b.bdata[0:0]
64
65 for _, na := range b.names {
66
67 // Create concrete types to hold the data
68 switch b.dtypes[na] {
69
70 // Standard dtypes
71 {{- range . }}
72 case "{{ .Type }}":
73 var x []{{ .Type }}
74 b.bdata = append(b.bdata, x)
75 {{- end }}
76
77 // Additional dtypes
78 case "uvarint":
79 var x []uint64
80 b.bdata = append(b.bdata, x)
81 case "varint":
82 var x []int64
83 b.bdata = append(b.bdata, x)
84 case "default":
85 panic("bcols.init: unknown type")
86 }
87
88 fn, ok := vf[na]
89 if !ok {
90 msg := fmt.Sprintf("bcols.init: can't find variable %s in %s\n", na, b.bpath)
91 os.Stderr.Write([]byte(msg))
92 os.Exit(1)
93 }
94
95 if strings.HasSuffix(fn, ".bin.gz") {
96 // gzip compression
97 s := path.Join(b.bpath, fn)
98 fid, err := os.Open(s)
99 if err != nil {
100 panic(err)
101 }
102 b.toclose = append(b.toclose, fid)
103 gid, err := gzip.NewReader(fid)
104 if err != nil {
105 panic(err)
106 }
107 b.toclose = append(b.toclose, gid)
108 bid := bufio.NewReader(gid) // adds ReadByte
109 b.toclose = append(b.toclose, gid)
110 b.rdrs = append(b.rdrs, bid)
111 } else if strings.HasSuffix(fn, ".bin.sz") {
112 // Snappy compression
113 s := path.Join(b.bpath, fn)
114 fid, err := os.Open(s)
115 if err != nil {
116 panic(err)
117 }
118 b.toclose = append(b.toclose, fid)
119 gid := snappy.NewReader(fid)
120 bid := bufio.NewReader(gid) // adds ReadByte
121 b.rdrs = append(b.rdrs, bid)
122 } else {
123 panic("compression type not recognized")
124 }
125 }
126
127 b.doneInit = true
128 }
129
130 func (b *bcols) Next() bool {
131
132 if !b.doneInit {
133 panic("Call Done before using stream")
134 }
135
136 if b.done {
137 return false
138 }
139
140 truncate(b.bdata)
141
142 for j, na := range b.names {
143 rdr := b.rdrs[j]
144
145 // Handle variable-width data
146 if b.dtypes[na] == "varint" {
147 var v []int64
148 for k := 0; k < b.chunksize; k++ {
149 var x int64
150 x, err := binary.ReadVarint(rdr)
151 if err == io.EOF {
152 b.done = true
153 break
154 } else if err != nil {
155 msg := fmt.Sprintf("Error reading variable '%s' at position %d\n", na, b.nobs)
156 print(msg)
157 panic(err)
158 }
159 v = append(v, x)
160 if j == 0 {
161 b.nobs++
162 }
163 }
164 b.bdata[j] = v
165 continue
166 } else if b.dtypes[na] == "uvarint" {
167 var v []uint64
168 for k := 0; k<b.chunksize; k++ {
169 var x uint64
170 x, err := binary.ReadUvarint(rdr)
171 if err == io.EOF {
172 b.done = true
173 break
174 } else if err != nil {
175 msg := fmt.Sprintf("Error reading variable '%s' at position %d\n", na, b.nobs)
176 print(msg)
177 panic(err)
178 }
179 v = append(v, x)
180 if j == 0 {
181 b.nobs++
182 }
183 }
184 b.bdata[j] = v
185 continue
186 }
187
188 // Handle fixed-width data
189 switch v := b.bdata[j].(type) {
190 {{- range . }}
191 case []{{ .Type }}:
192 for k := 0; k<b.chunksize; k++ {
193 var x {{ .Type }}
194 {{- if eq .Type "string"}}
195 y, isprefix, err := rdr.ReadLine()
196 if isprefix {
197 panic("buffer overflow")
198 }
199 x = string(y)
200 {{- else }}
201 err := binary.Read(rdr, binary.LittleEndian, &x)
202 {{- end }}
203 if err == io.EOF {
204 b.done = true
205 break
206 } else if err != nil {
207 msg := fmt.Sprintf("Error reading variable '%s' at position %d\n", na, b.nobs)
208 print(msg)
209 panic(err)
210 }
211 v = append(v, x)
212 if j == 0 {
213 b.nobs++
214 }
215 }
216 b.bdata[j] = v
217 {{- end }}
218 }
219 }
220
221 return true
222 }
223
224 func (bw *BColsWriter) writeDtypes() {
225
226 dtypes := make(map[string]string)
227
228 bw.stream.Reset()
229 names := bw.stream.Names()
230 bw.stream.Next()
231 for j, na := range names {
232 u := bw.stream.GetPos(j)
233 switch u.(type) {
234 {{- range . }}
235 case []{{ .Type }}:
236 dtypes[na] = "{{ .Type }}"
237 {{- end }}
238 }
239 }
240
241 f, err := os.Create(path.Join(bw.path, "dtypes.json"))
242 if err != nil {
243 panic(err)
244 }
245 defer f.Close()
246 enc := json.NewEncoder(f)
247 enc.Encode(dtypes)
248 }
249
250 func (bw *BColsWriter) write() {
251
252 bw.stream.Reset()
253 names := bw.stream.Names()
254
255 for bw.stream.Next() {
256 for j := range names {
257 u := bw.stream.GetPos(j)
258 switch u.(type) {
259 {{- range . }}
260 case []{{ .Type }}:
261 v := u.([]{{ .Type }})
262 {{- if eq .Type "string" }}
263 for _, x := range v {
264 _, err := bw.wtrs[j].Write([]byte(x + "\n"))
265 if err != nil {
266 panic(err)
267 }
268 }
269 {{- else }}
270 err := binary.Write(bw.wtrs[j], binary.LittleEndian, &v)
271 if err != nil {
272 panic(err)
273 }
274 {{- end }}
275 {{- end }}
276 }
277 }
278 }
279 }