Debian Codegraph @

dstream/bcols.go

raw symbols SCIP
1 package dstream
2
3 import (
4 "bufio"
5 "compress/gzip"
6 "fmt"
7 "io"
8 "os"
9 "path"
10 "sort"
11
12 "github.com/golang/snappy"
13 )
14
15 type bcols struct {
16 bpath string
17
18 dtypes map[string]string
19
20 // Data types for all variables in the source directory, including
21 // those not included in the Dstream when reading.
22 dtypesAll map[string]string
23
24 names []string
25
26 bdata []interface{}
27
28 rdrs []*bufio.Reader
29
30 // io.Readers, etc. that need closing
31 toclose []io.Closer
32
33 chunksize int
34
35 namepos map[string]int
36
37 done bool
38 doneInit bool
39
40 nobsKnown bool
41 nobs int
42
43 // Names to include or exclude. If include is empty it is ignored.
44 include []string
45 exclude []string
46 }
47
48 // NewBCols takes data stored in a column-wise compressed format under
49 // the given directory path, and returns it as a Dstream. 'path' is
50 // the directory containing the data, 'chunksize' is the number of
51 // values included in each chunk, 'include' and 'exclude' are lists of
52 // variable names to include (exclude) respectively.
53 //
54 // The underlying bcols format is simple. Each column of data is
55 // stored in its own file, in binary native format, compressed using
56 // either gzip or snappy compression. A file called 'dtypes.json'
57 // contains a dictionary mapping variable names to data types.
58 func NewBCols(path string, chunksize int) *bcols {
59
60 b := &bcols{
61 bpath: path,
62 chunksize: chunksize,
63 }
64
65 return b
66 }
67
68 // Include specifies variables that are included when reading the data from BCols
69 // storage into a Dstream. If Include is not called, all variables not listed in
70 // a call to Exclude are read.
71 func (bc *bcols) Include(vars ...string) *bcols {
72 bc.include = vars
73 return bc
74 }
75
76 // Exclude specifies variables that are not read from the Bcols storage.
77 func (bc *bcols) Exclude(vars ...string) *bcols {
78 bc.exclude = vars
79 return bc
80 }
81
82 // Done is called to signal the conclusion of configuring the BCols reader or
83 // writer.
84 func (bc *bcols) Done() Dstream {
85 bc.init()
86 return bc
87 }
88
89 // usenames returns the variable names to include in the Dstream.
90 func (bc *bcols) usenames() []string {
91
92 inc := make(map[string]bool)
93 for _, v := range bc.include {
94 inc[v] = true
95
96 _, ok := bc.dtypesAll[v]
97 if !ok {
98 msg := fmt.Sprintf("Variable '%s' does not exist\n", v)
99 panic(msg)
100 }
101 }
102
103 // If no variables are included, default is to set include to
104 // equal all variable names.
105 if len(bc.include) == 0 {
106 for k := range bc.dtypesAll {
107 inc[k] = true
108 }
109 }
110
111 exc := make(map[string]bool)
112 for _, v := range bc.exclude {
113 exc[v] = true
114
115 _, ok := bc.dtypesAll[v]
116 if !ok {
117 msg := fmt.Sprintf("Variable '%s' does not exist\n", v)
118 panic(msg)
119 }
120 }
121
122 var use []string
123 for k := range inc {
124 if !exc[k] {
125 use = append(use, k)
126 }
127 }
128
129 sort.StringSlice(use).Sort()
130
131 return use
132 }
133
134 func (bc *bcols) Names() []string {
135 return bc.names
136 }
137
138 func (bc *bcols) Close() {
139 for _, x := range bc.toclose {
140 x.Close()
141 }
142 }
143
144 func (bc *bcols) Reset() {
145 bc.Close()
146 bc.toclose = bc.toclose[0:0]
147 bc.rdrs = bc.rdrs[0:0]
148 bc.init()
149 bc.nobsKnown = false
150 bc.nobs = 0
151 bc.done = false
152 }
153
154 func (bc *bcols) NumVar() int {
155 return len(bc.names)
156 }
157
158 func (bc *bcols) NumObs() int {
159 if !bc.nobsKnown {
160 return -1
161 }
162 return bc.nobs
163 }
164
165 func (bc *bcols) GetPos(j int) interface{} {
166 return bc.bdata[j]
167 }
168
169 func (bc *bcols) Get(na string) interface{} {
170 pos, ok := bc.namepos[na]
171 if !ok {
172 msg := fmt.Sprintf("Variable '%s' not found", na)
173 panic(msg)
174 }
175 return bc.bdata[pos]
176 }
177
178 // BColsWriter writes a dstream to disk in bcols format.
179 type BColsWriter struct {
180
181 // The source data to be written
182 stream Dstream
183
184 // The directory where the results will be written
185 path string
186
187 // either "sz" (snappy), "gz" (gzip), or empty string
188 cmpr string
189
190 // A writer for each variable
191 wtrs []io.Writer
192
193 // All io values needing closing
194 needsClosing []io.Closer
195 }
196
197 // NewBColsWriter creates a new BColsWriter that writes the given
198 // dstream.
199 func NewBColsWriter(d Dstream) *BColsWriter {
200
201 return &BColsWriter{
202 stream: d,
203 }
204 }
205
206 // Path sets the location (a directory path) to which the data are
207 // written.
208 func (bw *BColsWriter) Path(p string) *BColsWriter {
209
210 bw.path = p
211
212 err := os.MkdirAll(p, 0770)
213 if err != nil {
214 panic(err)
215 }
216
217 return bw
218 }
219
220 func (bw *BColsWriter) init() {
221
222 if bw.path == "" {
223 msg := "Path value not set"
224 panic(msg)
225 }
226
227 // Default compression type
228 if bw.cmpr == "" {
229 bw.cmpr = "gz"
230 }
231
232 names := bw.stream.Names()
233
234 bw.writeDtypes()
235
236 for _, na := range names {
237
238 na += ".bin"
239 if bw.cmpr == "gz" {
240 na += ".gz"
241 } else if bw.cmpr == "sz" {
242 na += ".sz"
243 } else {
244 msg := fmt.Sprintf("Compression type %s not recognized", bw.cmpr)
245 panic(msg)
246 }
247
248 // Create the file
249 fn := path.Join(bw.path, na)
250 f, err := os.Create(fn)
251 if err != nil {
252 panic(err)
253 }
254 bw.needsClosing = append(bw.needsClosing, f)
255
256 // Wrap it in a compressor if needed
257 switch bw.cmpr {
258 case "gz":
259 g := gzip.NewWriter(f)
260 bw.needsClosing = append(bw.needsClosing, g)
261 bw.wtrs = append(bw.wtrs, g)
262 case "sz":
263 g := snappy.NewBufferedWriter(f)
264 bw.needsClosing = append(bw.needsClosing, g)
265 bw.wtrs = append(bw.wtrs, g)
266 default:
267 bw.wtrs = append(bw.wtrs, f)
268 }
269 }
270 }
271
272 // Done flushes the data to disk.
273 func (bw *BColsWriter) Done() {
274
275 bw.init()
276 bw.write()
277
278 // Need to process in reverse order so that nested writers are
279 // closed inside-out.
280 for j := len(bw.needsClosing) - 1; j >= 0; j-- {
281 f := bw.needsClosing[j]
282 f.Close()
283 }
284 }