/
connection.go
365 lines (338 loc) · 11.1 KB
/
connection.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
package types
import (
"errors"
"fmt"
"github.com/gbl08ma/sqalx"
sq "github.com/Masterminds/squirrel"
)
// Connection connects two stations in a single direction
type Connection struct {
From *Station
To *Station
// TypicalWaitingSeconds: time in seconds it usually takes to catch a train at the From station when moving towards To
TypicalWaitingSeconds int
// TypicalStopSeconds: time in seconds the train usually stops at the From station when moving towards To
TypicalStopSeconds int
// TypicalSeconds: the time in seconds it usually takes for the train to move from From to To
TypicalSeconds int
// WorldLength: the physical length of this connection in meters
WorldLength int
FromPlatform string
ToPlatform string
isCompatFake bool
compatOrigFirst *Connection
compatOrigSecond *Connection
}
// GetConnections returns a slice with all registered connections
func GetConnections(node sqalx.Node, closedCompat bool) ([]*Connection, error) {
if value, present := node.Load(getCacheKey("connections", closedCompat)); present {
return value.([]*Connection), nil
}
connections, err := getConnectionsWithSelect(node, sdb.Select(), closedCompat)
if err != nil {
return connections, err
}
node.Store(getCacheKey("connections", closedCompat), connections)
return connections, nil
}
func getConnectionsWithSelect(node sqalx.Node, sbuilder sq.SelectBuilder, closedCompat bool) ([]*Connection, error) {
connections := []*Connection{}
tx, err := node.Beginx()
if err != nil {
return connections, err
}
defer tx.Commit() // read-only tx
rows, err := sbuilder.Columns("from_station", "to_station", "typ_wait_time",
"typ_stop_time", "typ_time", "world_length",
"from_platform", "to_platform").
From("connection").
RunWith(tx).Query()
if err != nil {
return connections, fmt.Errorf("getConnectionsWithSelect: %s", err)
}
defer rows.Close()
var fromIDs []string
var toIDs []string
for rows.Next() {
var connection Connection
var fromID string
var toID string
err := rows.Scan(
&fromID,
&toID,
&connection.TypicalWaitingSeconds,
&connection.TypicalStopSeconds,
&connection.TypicalSeconds,
&connection.WorldLength,
&connection.FromPlatform,
&connection.ToPlatform)
if err != nil {
return connections, fmt.Errorf("getConnectionsWithSelect: %s", err)
}
if err != nil {
return connections, fmt.Errorf("getConnectionsWithSelect: %s", err)
}
connections = append(connections, &connection)
fromIDs = append(fromIDs, fromID)
toIDs = append(toIDs, toID)
}
if err := rows.Err(); err != nil {
return connections, fmt.Errorf("getConnectionsWithSelect: %s", err)
}
for i := range connections {
connections[i].From, err = GetStation(tx, fromIDs[i])
if err != nil {
return connections, fmt.Errorf("getConnectionsWithSelect: %s", err)
}
connections[i].To, err = GetStation(tx, toIDs[i])
if err != nil {
return connections, fmt.Errorf("getConnectionsWithSelect: %s", err)
}
}
if closedCompat {
connections, err = provideClosedCompat(tx, connections)
if err != nil {
return connections, err
}
}
return connections, nil
}
// GetConnection returns the Connection with the given ID
func GetConnection(node sqalx.Node, from string, to string, closedCompat bool) (*Connection, error) {
if value, present := node.Load(getCacheKey("connection", from, to, closedCompat)); present {
return value.(*Connection), nil
}
connections := []*Connection{}
if closedCompat {
cs, err := GetConnections(node, true)
if err != nil {
return nil, err
}
for _, c := range cs {
if c.From.ID == from && c.To.ID == to {
connections = []*Connection{c}
break
}
}
} else {
var err error
s := sdb.Select().
Where(sq.Eq{"from_station": from}).
Where(sq.Eq{"to_station": to})
connections, err = getConnectionsWithSelect(node, s, closedCompat)
if err != nil {
return nil, err
}
}
if len(connections) == 0 {
return nil, errors.New("Connection not found")
}
node.Store(getCacheKey("connection", from, to, closedCompat), connections[0])
return connections[0], nil
}
// GetConnectionsTo returns the Connections that point to the given ID
func GetConnectionsTo(node sqalx.Node, to string, closedCompat bool) ([]*Connection, error) {
if value, present := node.Load(getCacheKey("connections-to", to, closedCompat)); present {
return value.([]*Connection), nil
}
connections := []*Connection{}
if closedCompat {
cs, err := GetConnections(node, true)
if err != nil {
return nil, err
}
for _, c := range cs {
if c.To.ID == to {
connections = append(connections, c)
}
}
} else {
var err error
s := sdb.Select().
Where(sq.Eq{"to_station": to})
connections, err = getConnectionsWithSelect(node, s, closedCompat)
if err != nil {
return nil, err
}
}
node.Store(getCacheKey("connections-to", to, closedCompat), connections)
return connections, nil
}
// GetConnectionsFrom returns the Connections that point out of the given ID
func GetConnectionsFrom(node sqalx.Node, from string, closedCompat bool) ([]*Connection, error) {
if value, present := node.Load(getCacheKey("connections-from", from, closedCompat)); present {
return value.([]*Connection), nil
}
connections := []*Connection{}
if closedCompat {
cs, err := GetConnections(node, true)
if err != nil {
return nil, err
}
for _, c := range cs {
if c.From.ID == from {
connections = append(connections, c)
}
}
} else {
var err error
s := sdb.Select().
Where(sq.Eq{"from_station": from})
connections, err = getConnectionsWithSelect(node, s, closedCompat)
if err != nil {
return nil, err
}
}
node.Store(getCacheKey("connections-from", from, closedCompat), connections)
return connections, nil
}
// this function provides a compatibility mode that emulates the old way of
// dealing with closed stations, where closed stations retain only their
// outbound edges and replacement edges are created to connect the two nearest
// stations in the same line)
func provideClosedCompat(node sqalx.Node, origConnections []*Connection) ([]*Connection, error) {
tx, err := node.Beginx()
if err != nil {
return nil, err
}
defer tx.Commit() // read-only tx
connections := []*Connection{}
for _, connection := range origConnections {
closed, err := connection.To.Closed(tx)
if err != nil {
return connections, err
}
if closed {
connFromClosed, err := GetConnectionsFrom(tx, connection.To.ID, false)
if err != nil {
return connections, err
}
fromLines, err := connection.From.Lines(tx)
if err != nil {
return connections, err
}
candidates := []*Connection{}
for _, c := range connFromClosed {
if c.To.ID == connection.From.ID {
continue
}
cToLines, err := c.To.Lines(tx)
if err != nil {
return connections, err
}
func() {
for _, fromLine := range fromLines {
for _, toLine := range cToLines {
if fromLine.ID == toLine.ID {
candidates = append(candidates, c)
return
}
}
}
}()
}
for _, c := range candidates {
connections = append(connections, &Connection{
From: connection.From,
To: c.To,
TypicalWaitingSeconds: connection.TypicalWaitingSeconds,
TypicalStopSeconds: connection.TypicalStopSeconds,
TypicalSeconds: connection.TypicalSeconds + c.TypicalSeconds,
WorldLength: connection.WorldLength + c.WorldLength,
FromPlatform: connection.FromPlatform,
ToPlatform: connection.ToPlatform,
isCompatFake: true,
compatOrigFirst: connection,
compatOrigSecond: c,
})
}
} else {
connections = append(connections, connection)
}
}
return connections, nil
}
// GetConnectionsToPlatform returns the Connections that end on the given platform ID
func GetConnectionsToPlatform(node sqalx.Node, toPlatform string) ([]*Connection, error) {
if value, present := node.Load(getCacheKey("connections-toplatform", toPlatform)); present {
return value.([]*Connection), nil
}
s := sdb.Select().
Where(sq.Eq{"to_platform": toPlatform})
connections, err := getConnectionsWithSelect(node, s, false)
if err != nil {
return nil, err
}
node.Store(getCacheKey("connections-toplatform", toPlatform), connections)
return connections, nil
}
// GetConnectionsFromPlatform returns the Connections that start on the given platform ID
func GetConnectionsFromPlatform(node sqalx.Node, fromPlatform string) ([]*Connection, error) {
if value, present := node.Load(getCacheKey("connections-fromplatform", fromPlatform)); present {
return value.([]*Connection), nil
}
s := sdb.Select().
Where(sq.Eq{"from_platform": fromPlatform})
connections, err := getConnectionsWithSelect(node, s, false)
if err != nil {
return nil, err
}
node.Store(getCacheKey("connections-fromplatform", fromPlatform), connections)
return connections, nil
}
// Update adds or updates the connection
func (connection *Connection) Update(node sqalx.Node) error {
tx, err := node.Beginx()
if err != nil {
return err
}
defer tx.Rollback()
if connection.isCompatFake {
connection.compatOrigFirst.TypicalSeconds = connection.TypicalSeconds / 2
connection.compatOrigSecond.TypicalSeconds = connection.TypicalSeconds / 2
for connection.compatOrigFirst.TypicalSeconds+connection.compatOrigSecond.TypicalSeconds < connection.TypicalSeconds {
// make up lost in integer division
connection.compatOrigSecond.TypicalSeconds++
}
// other ones only apply to the start of the edge, i.e. the original first
connection.compatOrigFirst.TypicalStopSeconds = connection.TypicalStopSeconds
connection.compatOrigFirst.TypicalWaitingSeconds = connection.TypicalWaitingSeconds
err = connection.compatOrigFirst.Update(tx)
if err != nil {
return err
}
return connection.compatOrigSecond.Update(tx)
}
_, err = sdb.Insert("connection").
Columns("from_station", "to_station", "typ_wait_time",
"typ_stop_time", "typ_time", "world_length",
"from_platform", "to_platform").
Values(connection.From.ID, connection.To.ID, connection.TypicalWaitingSeconds,
connection.TypicalStopSeconds, connection.TypicalSeconds, connection.WorldLength,
connection.FromPlatform, connection.ToPlatform).
Suffix("ON CONFLICT (from_station, to_station) DO UPDATE SET typ_wait_time = ?, typ_stop_time = ?, typ_time = ?, world_length = ?, from_platform = ?, to_platform = ?",
connection.TypicalWaitingSeconds, connection.TypicalStopSeconds, connection.TypicalSeconds, connection.WorldLength, connection.FromPlatform, connection.ToPlatform).
RunWith(tx).Exec()
if err != nil {
return errors.New("AddConnection: " + err.Error())
}
return tx.Commit()
}
// Delete deletes the connection
func (connection *Connection) Delete(node sqalx.Node) error {
tx, err := node.Beginx()
if err != nil {
return err
}
defer tx.Rollback()
_, err = sdb.Delete("connection").
Where(sq.Eq{"from_station": connection.From.ID}).
Where(sq.Eq{"to_station": connection.To.ID}).
RunWith(tx).Exec()
if err != nil {
return fmt.Errorf("RemoveConnection: %s", err)
}
tx.Delete(getCacheKey("connection", connection.From.ID, connection.To.ID, true))
tx.Delete(getCacheKey("connection", connection.From.ID, connection.To.ID, false))
return tx.Commit()
}