-
Notifications
You must be signed in to change notification settings - Fork 2
/
store.go
443 lines (377 loc) · 8.66 KB
/
store.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// Copyright (c) 2013 Ostap Cherkashin, Julius Chrobak. You can use this
// source code under the terms of the MIT License found in the LICENSE file.
package main
import (
"bufio"
"encoding/csv"
"encoding/json"
"encoding/xml"
"fmt"
"io"
"log"
"math"
"path"
"regexp"
"runtime"
"strconv"
"strings"
"time"
)
type Body chan Value
type Store struct {
types map[string]Type
values map[string]Value
}
type Stats struct {
Total int
Found int
}
type line struct {
lineNo int
rec []string
}
type LineReader interface {
Read() (rec []string, err error)
}
type TabLineReader struct {
reader *bufio.Reader
}
func (r *TabLineReader) Read() ([]string, error) {
b := r.reader
line, err := b.ReadString('\n')
if err != nil {
return nil, err
}
return strings.Split(line[:len(line)-1], "\t"), nil
}
func (s Store) IsDef(name string) bool {
return s.types[name] != nil
}
func (s Store) Add(fileName string, r io.Reader) error {
name := path.Base(fileName)
if dot := strings.Index(name, "."); dot > 0 {
name = name[:dot]
}
if !IsIdent(name) {
return fmt.Errorf("invalid file name: '%v' cannot be used as an identifier (ignoring)", name)
}
var t Type
var v Value
var err error
switch path.Ext(fileName) {
case ".json":
t, v, err = readJSON(r)
case ".xml":
t, v, err = readXML(r)
case ".csv":
t, v, err = readText(csvReader(r), fileName)
case ".txt":
t, v, err = readText(tsvReader(r), fileName)
default:
err = fmt.Errorf("unknown content type %v (use one of json, xml, csv, txt)", path.Ext(fileName))
}
if err != nil {
return fmt.Errorf("failed to load %v: %v", fileName, err)
}
s.types[name] = t
s.values[name] = v
return nil
}
func (s Store) PrintSymbols() {
log.Printf("available symbols:")
for n, v := range s.values {
info := fmt.Sprintf(" %v (%v", n, s.types[n].Name())
switch value := v.(type) {
case List:
info = fmt.Sprintf("%v, %v elements", info, len(value))
case Object:
info = fmt.Sprintf("%v, %v fields", info, len(value))
}
log.Printf("%v)", info)
}
}
func (s Store) Decls() *Decls {
decls := NewDecls()
for k, v := range s.values {
decls.Declare(k, v, s.types[k])
}
decls.AddFunc(FuncTrunc())
decls.AddFunc(FuncDist())
decls.AddFunc(FuncTrim())
decls.AddFunc(FuncLower())
decls.AddFunc(FuncUpper())
decls.AddFunc(FuncFuzzy())
decls.AddFunc(FuncReplace())
return decls
}
func IsIdent(s string) bool {
ident, _ := regexp.MatchString("^\\w+$", s)
return ident
}
func toScalar(s string) interface{} {
num, err := strconv.ParseFloat(s, 64)
if err != nil || math.IsNaN(num) || math.IsInf(num, 0) {
return s
} else {
return num
}
}
func name(prefix string, n xml.Name) string {
if n.Space == "" {
return prefix + n.Local
}
return prefix + n.Space + ":" + n.Local
}
func alloc() map[string]interface{} {
res := make(map[string]interface{})
res["text()"] = ""
return res
}
func readXML(r io.Reader) (Type, Value, error) {
dec := xml.NewDecoder(r)
value := alloc() /* root element */
stack := append(make([]map[string]interface{}, 0), value)
names := append(make([]string, 0), "")
top := 1
for {
tok, err := dec.RawToken()
if err == io.EOF && top == 1 {
break
}
if err != nil {
return nil, nil, err
}
switch t := tok.(type) {
case xml.StartElement:
val := alloc()
n := name("", t.Name)
for _, v := range t.Attr {
val[name("@", v.Name)] = toScalar(v.Value)
}
parent := stack[top-1]
if prev, ok := parent[n]; ok {
switch e := prev.(type) {
case []interface{}:
parent[n] = append(e, val)
default:
parent[n] = []interface{}{e, val}
}
} else {
parent[n] = val
}
if top >= len(stack) {
stack = append(stack, val)
names = append(names, n)
} else {
stack[top] = val
names[top] = n
}
top++
case xml.EndElement:
exp := names[top-1]
got := name("", t.Name)
if exp != got {
return nil, nil, fmt.Errorf("XML syntax error: element <%v> closed by </%v>", exp, got)
}
top--
case xml.CharData:
parent := stack[top-1]
parent["text()"] = parent["text()"].(string) + string(t)
default:
/* ignoring the following token types
xml.Comment
xml.ProcInst
xml.Directive
*/
}
}
return traverse(nil, value)
}
func traverse(h Type, v interface{}) (Type, Value, error) {
switch v.(type) {
case nil:
return ScalarType(0), Bool(false), nil
case map[string]interface{}:
elems := v.(map[string]interface{})
val := make(Object, len(elems))
var head ObjectType
switch h.(type) {
case ObjectType:
head = h.(ObjectType)
if len(head) != len(elems) { /* very strict */
return nil, nil, fmt.Errorf("invalid object type, expected %v got %v", head, elems)
}
case nil:
head = make(ObjectType, len(elems))
default:
return nil, nil, fmt.Errorf("expected object, got %v (%v)", h.Name(), v)
}
idx := 0
for name, value := range elems {
t, v, e := traverse(head.Type(name), value)
if e != nil {
return nil, nil, e
}
if h == nil {
head[idx].Name = name
head[idx].Type = t
}
i := head.Pos(name)
if i < 0 {
return nil, nil, fmt.Errorf("cannot find field %v in %v (%v)", name, head, v)
}
val[i] = v
idx++
}
return head, val, nil
case []interface{}:
elems := v.([]interface{})
val := make(List, len(elems))
var head ListType
switch h.(type) {
case ListType:
head = h.(ListType)
case nil:
head = ListType{}
default:
return nil, nil, fmt.Errorf("expected list, got %v (%v)", h.Name(), v)
}
for idx, value := range elems {
t, v, e := traverse(head.Elem, value)
if e != nil {
return nil, nil, e
}
head.Elem = t
val[idx] = v
}
return head, val, nil
case bool:
switch h.(type) {
case nil, ScalarType:
return ScalarType(0), Bool(v.(bool)), nil
default:
return nil, nil, fmt.Errorf("expected bool, got %v (%v)", h.Name(), v)
}
case float64:
switch h.(type) {
case nil, ScalarType:
return ScalarType(0), Number(v.(float64)), nil
default:
return nil, nil, fmt.Errorf("expected number, got %v (%v)", h.Name(), v)
}
default:
switch h.(type) {
case nil, ScalarType:
return ScalarType(0), String(v.(string)), nil
default:
return nil, nil, fmt.Errorf("expected string, got %v (%v)", h.Name(), v)
}
}
}
func readJSON(r io.Reader) (Type, Value, error) {
dec := json.NewDecoder(r)
var data interface{}
err := dec.Decode(&data) /* reading a single valid JSON value */
if err != nil {
return nil, nil, err
}
return traverse(nil, data)
}
func csvReader(in io.Reader) LineReader {
r := csv.NewReader(in)
r.Comma = ','
r.LazyQuotes = true
r.TrailingComma = true
r.FieldsPerRecord = -1
return r
}
func tsvReader(in io.Reader) LineReader {
return &TabLineReader{reader: bufio.NewReader(in)}
}
func readText(r LineReader, fileName string) (Type, Value, error) {
rec, err := r.Read()
if err != nil {
return nil, nil, err
}
head := make(ObjectType, len(rec))
for i, f := range rec {
head[i].Name = strings.Trim(f, " \r\n")
head[i].Type = ScalarType(0)
}
t := ListType{Elem: head}
return t, readBody(t, fileName, r), nil
}
func readBody(t ListType, fileName string, r LineReader) List {
lines := make(chan line, 1024)
go func() {
for lineNo := 0; ; lineNo++ {
rec, err := r.Read()
if err == io.EOF {
break
} else if err != nil {
log.Printf("failed to parse %v, %v", fileName, err)
break
}
lines <- line{lineNo, rec}
}
close(lines)
}()
tuples := make(Body, 1024)
ctl := make(chan int)
ot := t.Elem.(ObjectType)
for i := 0; i < runtime.NumCPU(); i++ {
go processLine(i, ot, lines, tuples, ctl)
}
go func() {
for i := 0; i < runtime.NumCPU(); i++ {
<-ctl
}
close(tuples)
}()
ticker := time.NewTicker(3 * time.Second)
list := make(List, 0)
count := 0
stop := false
for !stop {
select {
case <-ticker.C:
log.Printf("loading %v (%d tuples)", fileName, count)
case t, ok := <-tuples:
if !ok {
stop = true
break
}
list = append(list, t)
count++
}
}
ticker.Stop()
return list
}
func processLine(id int, ot ObjectType, in chan line, out Body, ctl chan int) {
count := 0
for l := range in {
fields := l.rec
if len(fields) > len(ot) {
log.Printf("line %d: truncating object (-%d fields)", l.lineNo, len(fields)-len(ot))
fields = fields[:len(ot)]
} else if len(fields) < len(ot) {
log.Printf("line %d: missing fields, appending blank strings", l.lineNo)
for len(fields) < len(ot) {
fields = append(fields, "")
}
}
obj := make(Object, len(ot))
for i, s := range fields {
num, err := strconv.ParseFloat(s, 64)
if err != nil || math.IsNaN(num) || math.IsInf(num, 0) {
obj[i] = String(s)
} else {
obj[i] = Number(num)
count++
}
}
out <- obj
}
ctl <- 1
}