| 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 |
|
| 21 |
|
| 22 |
dtypesAll map[string]string |
| 23 |
|
| 24 |
names []string |
| 25 |
|
| 26 |
bdata []interface{} |
| 27 |
|
| 28 |
rdrs []*bufio.Reader |
| 29 |
|
| 30 |
|
| 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 |
|
| 44 |
include []string |
| 45 |
exclude []string |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 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 |
|
| 69 |
|
| 70 |
|
| 71 |
func (bc *bcols) Include(vars ...string) *bcols { |
| 72 |
bc.include = vars |
| 73 |
return bc |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
func (bc *bcols) Exclude(vars ...string) *bcols { |
| 78 |
bc.exclude = vars |
| 79 |
return bc |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
func (bc *bcols) Done() Dstream { |
| 85 |
bc.init() |
| 86 |
return bc |
| 87 |
} |
| 88 |
|
| 89 |
|
| 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 |
|
| 104 |
|
| 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 |
|
| 179 |
type BColsWriter struct { |
| 180 |
|
| 181 |
|
| 182 |
stream Dstream |
| 183 |
|
| 184 |
|
| 185 |
path string |
| 186 |
|
| 187 |
|
| 188 |
cmpr string |
| 189 |
|
| 190 |
|
| 191 |
wtrs []io.Writer |
| 192 |
|
| 193 |
|
| 194 |
needsClosing []io.Closer |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
func NewBColsWriter(d Dstream) *BColsWriter { |
| 200 |
|
| 201 |
return &BColsWriter{ |
| 202 |
stream: d, |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 273 |
func (bw *BColsWriter) Done() { |
| 274 |
|
| 275 |
bw.init() |
| 276 |
bw.write() |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
for j := len(bw.needsClosing) - 1; j >= 0; j-- { |
| 281 |
f := bw.needsClosing[j] |
| 282 |
f.Close() |
| 283 |
} |
| 284 |
} |