-
Notifications
You must be signed in to change notification settings - Fork 2
/
condition.go
165 lines (133 loc) · 3.56 KB
/
condition.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
package pto3
import (
"net/http"
"strings"
"github.com/go-pg/pg/orm"
)
type Condition struct {
ID int
Name string
Feature string
Aspect string
}
func NewCondition(name string) *Condition {
out := new(Condition)
out.Name = name
if firstDot := strings.Index(name, "."); firstDot > -1 {
out.Feature = name[0:firstDot]
}
if lastDot := strings.LastIndex(name, "."); lastDot > -1 {
out.Aspect = name[0:lastDot]
}
return out
}
func NewConditionWithID(id int, name string) *Condition {
out := NewCondition(name)
out.ID = id
return out
}
// FIXME consider replacing this with a condition cache everywhere
func (c *Condition) InsertOnce(db orm.DB) error {
if c.ID == 0 {
_, err := db.Model(c).
Column("id").
Where("name=?name").
Returning("id").
SelectOrInsert()
if err != nil {
return PTOWrapError(err)
}
}
return nil
}
// FIXME consider replacing this with a condition cache everywhere
func (c *Condition) SelectByID(db orm.DB) error {
return db.Select(c)
}
// ConditionCache maps a condition name to a condition ID
type ConditionCache map[string]int
// FillConditionIDsInSet ensures all the conditions in a given observation set
// have valid IDs. It keeps the condition cache synchronized with the database
// if any new conditions have been added.
func (cache ConditionCache) FillConditionIDsInSet(db orm.DB, set *ObservationSet) error {
for _, c := range set.Conditions {
if err := cache.SetConditionID(db, &c); err != nil {
return err
}
}
return nil
}
// SetConditionID ensures the given condition has a valid ID. It keeps the
// condition cache synchronized with the database if the condition is new.
func (cache ConditionCache) SetConditionID(db orm.DB, c *Condition) error {
// check for a cache hit
id, ok := cache[c.Name]
if ok {
c.ID = id
return nil
}
// check to see if we need to insert or retrieve from DB
if c.ID == 0 {
if err := c.InsertOnce(db); err != nil {
return err
}
}
// now cache
cache[c.Name] = c.ID
return nil
}
func (cache ConditionCache) Reload(db orm.DB) error {
var conditions []Condition
if err := db.Model(&conditions).Select(); err != nil {
return PTOWrapError(err)
}
for _, c := range conditions {
cache[c.Name] = c.ID
}
return nil
}
func (cache ConditionCache) ConditionsByName(db orm.DB, conditionName string) ([]Condition, error) {
var out []Condition
if strings.HasSuffix(conditionName, ".*") {
// Wildcard. Reload cache and find everything that matches.
if err := cache.Reload(db); err != nil {
return nil, err
}
out = make([]Condition, 0)
for cachedName := range cache {
if strings.HasPrefix(cachedName, conditionName[:len(conditionName)-1]) {
out = append(out, *NewConditionWithID(cache[cachedName], cachedName))
}
}
} else {
// No wildcard, just look up by name.
if cache[conditionName] == 0 {
if err := cache.Reload(db); err != nil {
return nil, err
}
}
if cache[conditionName] == 0 {
return nil, PTOErrorf("unknown condition %s", conditionName).StatusIs(http.StatusBadRequest)
}
out = make([]Condition, 1)
out[0] = *NewConditionWithID(cache[conditionName], conditionName)
}
return out, nil
}
func (cache ConditionCache) Names() []string {
names := make([]string, len(cache))
i := 0
for k := range cache {
names[i] = k
i++
}
return names
}
// LoadConditionCache creates a new condition cache with all the conditions in a given database.
func LoadConditionCache(db orm.DB) (ConditionCache, error) {
cache := make(ConditionCache)
if err := cache.Reload(db); err != nil {
return nil, err
}
return cache, nil
}