| 1 |
package dstream |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/gob" |
| 5 |
"time" |
| 6 |
"io" |
| 7 |
) |
| 8 |
|
| 9 |
// Save saves the dstream to a file. It can subsequently be reloaded with Load. |
| 10 |
func Save(ds Dstream, w io.Writer) { |
| 11 |
|
| 12 |
ds.Reset() |
| 13 |
|
| 14 |
enc := gob.NewEncoder(w) |
| 15 |
|
| 16 |
err := enc.Encode(ds.Names()) |
| 17 |
if err != nil { |
| 18 |
panic(err) |
| 19 |
} |
| 20 |
|
| 21 |
first := true |
| 22 |
for ds.Next() { |
| 23 |
|
| 24 |
if first { |
| 25 |
var dtypes []Dtype |
| 26 |
for j := 0; j < ds.NumVar(); j++ { |
| 27 |
switch ds.GetPos(j).(type) { |
| 28 |
{{- range . }} |
| 29 |
case []{{ .Type }}: |
| 30 |
dtypes = append(dtypes, {{ .Utype }}) |
| 31 |
{{- end }} |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
err := enc.Encode(&dtypes) |
| 36 |
if err != nil { |
| 37 |
panic(err) |
| 38 |
} |
| 39 |
|
| 40 |
first = false |
| 41 |
} |
| 42 |
|
| 43 |
for j := 0; j < ds.NumVar(); j++ { |
| 44 |
switch x := ds.GetPos(j).(type) { |
| 45 |
{{- range . }} |
| 46 |
case []{{ .Type }}: |
| 47 |
err := enc.Encode(&x) |
| 48 |
if err != nil { |
| 49 |
panic(err) |
| 50 |
} |
| 51 |
{{- end }} |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
// load is a dstream that reads its data from a file that has been constructed using Save. |
| 58 |
type load struct { |
| 59 |
|
| 60 |
// done becomes true when the whole data source has been read |
| 61 |
done bool |
| 62 |
|
| 63 |
// The variable names |
| 64 |
names []string |
| 65 |
|
| 66 |
// The number of observations, not relevant until reading is complete. |
| 67 |
nobs int |
| 68 |
|
| 69 |
// The data types of the variables |
| 70 |
dtypes []Dtype |
| 71 |
|
| 72 |
// Use this to decode gob data from the file. |
| 73 |
dec *gob.Decoder |
| 74 |
|
| 75 |
// namespos maps variable names to their column positions |
| 76 |
namespos map[string]int |
| 77 |
|
| 78 |
// This holds the data for the current chunk |
| 79 |
data []interface{} |
| 80 |
} |
| 81 |
|
| 82 |
// NewLoad returns a dstream that loads data from the given file. The file must |
| 83 |
// have been created using the Save function. |
| 84 |
func NewLoad(r io.Reader) Dstream { |
| 85 |
|
| 86 |
var ld load |
| 87 |
ld.init(r) |
| 88 |
|
| 89 |
return &ld |
| 90 |
} |
| 91 |
|
| 92 |
func (ld *load) init(r io.Reader) { |
| 93 |
|
| 94 |
ld.dec = gob.NewDecoder(r) |
| 95 |
|
| 96 |
err := ld.dec.Decode(&ld.names) |
| 97 |
if err != nil { |
| 98 |
panic(err) |
| 99 |
} |
| 100 |
|
| 101 |
ld.namespos = make(map[string]int) |
| 102 |
for k, na := range ld.names { |
| 103 |
ld.namespos[na] = k |
| 104 |
} |
| 105 |
|
| 106 |
err = ld.dec.Decode(&ld.dtypes) |
| 107 |
if err != nil { |
| 108 |
panic(err) |
| 109 |
} |
| 110 |
|
| 111 |
ld.data = make([]interface{}, len(ld.names)) |
| 112 |
} |
| 113 |
|
| 114 |
// Reset panics since this concrete type of Dstream cannot be reset. |
| 115 |
func (ld *load) Reset() { |
| 116 |
panic("A dstream created using NewLoad cannot be reset.\n") |
| 117 |
} |
| 118 |
|
| 119 |
// GetPos returns the data for the variable at the given position in the current chunk. |
| 120 |
func (ld *load) GetPos(j int) interface{} { |
| 121 |
return ld.data[j] |
| 122 |
} |
| 123 |
|
| 124 |
// Names returns the variable names. |
| 125 |
func (ld *load) Names() []string { |
| 126 |
return ld.names |
| 127 |
} |
| 128 |
|
| 129 |
// NumObs returns the number of observations (data rows) in the dstream. A -1 is |
| 130 |
// returned if the source has not been completely read. |
| 131 |
func (ld *load) NumObs() int { |
| 132 |
if !ld.done { |
| 133 |
return -1 |
| 134 |
} |
| 135 |
return ld.nobs |
| 136 |
} |
| 137 |
|
| 138 |
// Get returns the data for the given named variable in the current chunk. |
| 139 |
func (ld *load) Get(na string) interface{} { |
| 140 |
return ld.GetPos(ld.namespos[na]) |
| 141 |
} |
| 142 |
|
| 143 |
// Close does nothing, it is here to satisfy the Dstream interface. |
| 144 |
func (ld *load) Close() { |
| 145 |
} |
| 146 |
|
| 147 |
// NumVar returns the number of variables in the dstream. |
| 148 |
func (ld *load) NumVar() int { |
| 149 |
return len(ld.names) |
| 150 |
} |
| 151 |
|
| 152 |
// Next advances to the next chunk and returns true, or returns false if |
| 153 |
// all chunks have been read. |
| 154 |
func (ld *load) Next() bool { |
| 155 |
|
| 156 |
// Loop over variables within chunks |
| 157 |
for j := 0; j < ld.NumVar(); j++ { |
| 158 |
switch ld.dtypes[j] { |
| 159 |
{{- range . }} |
| 160 |
case {{ .Utype }}: |
| 161 |
var x []{{ .Type }} |
| 162 |
err := ld.dec.Decode(&x) |
| 163 |
if err == io.EOF { |
| 164 |
if j != 0 { |
| 165 |
panic("File is corrupt") |
| 166 |
} |
| 167 |
ld.done = true |
| 168 |
return false |
| 169 |
} else if err != nil { |
| 170 |
panic(err) |
| 171 |
} |
| 172 |
ld.data[j] = x |
| 173 |
if j == 0 { |
| 174 |
ld.nobs += len(x) |
| 175 |
} |
| 176 |
{{- end }} |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
return true |
| 181 |
} |