/
data_holdings.go
87 lines (69 loc) · 1.66 KB
/
data_holdings.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
package main
import (
"bytes"
"github.com/GeoNet/fdsn/internal/holdings"
"github.com/lib/pq"
)
// http://www.postgresql.org/docs/9.4/static/errcodes-appendix.html
const (
errorUniqueViolation pq.ErrorCode = "23505"
)
type holding struct {
holdings.Holding
key string // the S3 bucket key
errorData bool // the miniSEED file has errors
errorMsg string // the cause of the errors
}
func (h *holding) save() error {
r, err := h.saveHoldings()
switch {
case err != nil:
return err
case r == 1:
return nil
}
_, err = h.saveStream()
if err != nil {
return err
}
_, err = h.saveHoldings()
if err != nil {
return err
}
return nil
}
func (h *holding) saveHoldings() (int64, error) {
r, err := saveHoldings.Exec(h.Network, h.Station, h.Channel, h.Location, h.Start, h.NumSamples, h.key, h.errorData, h.errorMsg)
if err != nil {
return 0, err
}
return r.RowsAffected()
}
func (h *holding) saveStream() (int64, error) {
r, err := db.Exec(`INSERT INTO fdsn.stream (network, station, channel, location) VALUES($1, $2, $3, $4)`,
h.Network, h.Station, h.Channel, h.Location)
if err != nil {
if u, ok := err.(*pq.Error); ok && u.Code == errorUniqueViolation {
return 1, nil
} else {
return 0, err
}
}
return r.RowsAffected()
}
func (h *holding) delete() error {
_, err := db.Exec(`DELETE FROM fdsn.holdings WHERE key = $1`, h.key)
return err
}
func holdingS3(bucket, key string) (holding, error) {
buf := &bytes.Buffer{}
err := s3Client.Get(bucket, key, "", buf)
if err != nil {
return holding{}, err
}
h, err := holdings.SingleStream(buf)
if err != nil {
return holding{}, err
}
return holding{key: key, Holding: h}, nil
}