-
Notifications
You must be signed in to change notification settings - Fork 0
/
ntuple.go
295 lines (268 loc) · 6.83 KB
/
ntuple.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Copyright 2016 The go-hep Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package hbook
import (
"database/sql"
"errors"
"fmt"
"go/ast"
"io"
"math"
"reflect"
"strings"
)
var (
ErrNotExist = errors.New("hbook: ntuple does not exist")
ErrMissingColDef = errors.New("hbook: expected at least one column definition")
)
// NTuple provides read/write access to row-wise data.
type NTuple struct {
db *sql.DB
name string
schema []Descriptor
}
// OpenNTuple inspects the given database handle and tries to return
// an NTuple connected to a table with the given name.
// OpenNTuple returns ErrNotExist if no such table exists.
// If name is "", OpenNTuple will connect to the one-and-only table in the db.
//
// e.g.:
// db, err := sql.Open("csv", "file.csv")
// nt, err := hbook.OpenNTuple(db, "ntup")
func OpenNTuple(db *sql.DB, name string) (*NTuple, error) {
nt := &NTuple{
db: db,
name: name,
}
// FIXME(sbinet) test whether the table 'name' actually exists
// FIXME(sbinet) retrieve underlying schema from db
return nt, nil
}
// CreateNTuple creates a new ntuple with the given name inside the given database handle.
// The n-tuple schema is inferred from the cols argument. cols can be:
// - a single struct value (columns are inferred from the names+types of the exported fields)
// - a list of builtin values (the columns names are varX where X=[1-len(cols)])
// - a list of hbook.Descriptors
//
// e.g.:
// nt, err := hbook.CreateNTuple(db, "nt", struct{X float64 `hbook:"x"`}{})
// nt, err := hbook.CreateNTuple(db, "nt", int64(0), float64(0))
func CreateNTuple(db *sql.DB, name string, cols ...interface{}) (*NTuple, error) {
var err error
nt := &NTuple{
db: db,
name: name,
}
var schema []Descriptor
switch len(cols) {
case 0:
return nil, ErrMissingColDef
case 1:
rv := reflect.Indirect(reflect.ValueOf(cols[0]))
rt := rv.Type()
switch rt.Kind() {
case reflect.Struct:
schema, err = schemaFromStruct(rt)
default:
schema, err = schemaFrom(cols...)
}
default:
schema, err = schemaFrom(cols...)
}
if err != nil {
return nil, err
}
nt.schema = schema
return nt, err
}
// Name returns the name of this n-tuple.
func (nt *NTuple) Name() string {
return nt.name
}
// Cols returns the columns' descriptors of this n-tuple.
// Modifying it directly leads to undefined behaviour.
func (nt *NTuple) Cols() []Descriptor {
return nt.schema
}
// Descriptor describes a column
type Descriptor interface {
Name() string // the column name
Type() reflect.Type // the column type
}
type columnDescr struct {
name string
typ reflect.Type
}
func (col *columnDescr) Name() string {
return col.name
}
func (col *columnDescr) Type() reflect.Type {
return col.typ
}
func schemaFromStruct(rt reflect.Type) ([]Descriptor, error) {
var schema []Descriptor
var err error
for i := 0; i < rt.NumField(); i++ {
f := rt.Field(i)
if !ast.IsExported(f.Name) {
continue
}
ft := f.Type
switch ft.Kind() {
case reflect.Chan:
return nil, fmt.Errorf("hbook: chans not supported")
case reflect.Interface:
return nil, fmt.Errorf("hbook: interfaces not supported")
case reflect.Map:
return nil, fmt.Errorf("hbook: maps not supported")
case reflect.Slice:
return nil, fmt.Errorf("hbook: nested slices not supported")
case reflect.Struct:
return nil, fmt.Errorf("hbook: nested structs not supported")
}
fname := getTag(f.Tag, "hbook", "rio", "db")
if fname == "" {
fname = f.Name
}
schema = append(schema, &columnDescr{fname, ft})
}
return schema, err
}
func schemaFrom(src ...interface{}) ([]Descriptor, error) {
var schema []Descriptor
var err error
for i, col := range src {
rt := reflect.TypeOf(col)
switch rt.Kind() {
case reflect.Chan:
return nil, fmt.Errorf("hbook: chans not supported")
case reflect.Interface:
return nil, fmt.Errorf("hbook: interfaces not supported")
case reflect.Map:
return nil, fmt.Errorf("hbook: maps not supported")
case reflect.Slice:
return nil, fmt.Errorf("hbook: slices not supported")
case reflect.Struct:
return nil, fmt.Errorf("hbook: structs not supported")
}
schema = append(schema, &columnDescr{fmt.Sprintf("var%d", i+1), rt})
}
return schema, err
}
func getTag(tag reflect.StructTag, keys ...string) string {
for _, k := range keys {
v := tag.Get(k)
if v != "" && v != "-" {
return v
}
}
return ""
}
// Scan executes a query against the ntuple and runs the function f against that context.
//
// e.g.
// err = nt.Scan("x,y where z>10", func(x,y float64) error {
// h1.Fill(x, 1)
// h2.Fill(y, 1)
// return nil
// })
func (nt *NTuple) Scan(query string, f interface{}) error {
rv := reflect.ValueOf(f)
rt := rv.Type()
if rt.Kind() != reflect.Func {
return fmt.Errorf("hbook: expected a func, got %T", f)
}
if rt.NumOut() != 1 || rt.Out(0) != reflect.TypeOf((*error)(nil)).Elem() {
return fmt.Errorf("hbook: expected a func returning an error. got %T", f)
}
vargs := make([]reflect.Value, rt.NumIn())
args := make([]interface{}, rt.NumIn())
for i := range args {
ptr := reflect.New(rt.In(i))
args[i] = ptr.Interface()
vargs[i] = ptr.Elem()
}
query, err := nt.massageQuery(query)
if err != nil {
return err
}
rows, err := nt.db.Query(query)
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
err = rows.Scan(args...)
if err != nil {
return err
}
out := rv.Call(vargs)[0].Interface()
if out != nil {
return out.(error)
}
}
err = rows.Err()
if err == io.EOF {
err = nil
}
return err
}
// ScanH1D executes a query against the ntuple and fills the histogram with
// the results of the query.
// If h is nil, a (100-bins, xmin, xmax) histogram is created,
// where xmin and xmax are inferred from the content of the underlying database.
func (nt *NTuple) ScanH1D(query string, h *H1D) (*H1D, error) {
var data []float64
var (
xmin = +math.MaxFloat64
xmax = -math.MaxFloat64
)
err := nt.Scan(query, func(x float64) error {
data = append(data, x)
if xmin > x {
xmin = x
}
if xmax < x {
xmax = x
}
return nil
})
if err != nil {
return nil, err
}
if h == nil {
h = NewH1D(100, xmin, xmax)
}
for _, x := range data {
h.Fill(x, 1)
}
return h, err
}
/*
// Scatter1D
func Scatter1D(db *sql.DB, query string) error {
var err error
return err
}
*/
func (nt *NTuple) massageQuery(q string) (string, error) {
const (
tokWHERE = " WHERE "
tokWhere = " where "
)
vars := q
where := ""
switch {
case strings.Contains(q, tokWHERE):
toks := strings.Split(q, tokWHERE)
vars = toks[0]
where = " where " + toks[1]
case strings.Contains(q, tokWhere):
toks := strings.Split(q, tokWhere)
vars = toks[0]
where = " where " + toks[1]
}
// FIXME(sbinet) this is vulnerable to SQL injections...
return "select " + vars + " from " + nt.name + where, nil
}