-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection_set.go
100 lines (78 loc) · 2.12 KB
/
connection_set.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
package ownmapdal
import (
"context"
"log"
"sync"
"github.com/jamesrr39/goutil/errorsx"
"github.com/jamesrr39/ownmap-app/ownmap"
"github.com/paulmach/osm"
)
type DataSourceConn interface {
// Info methods
Name() string
DatasetInfo() (*ownmap.DatasetInfo, errorsx.Error)
// Data fetch methods
GetInBounds(ctx context.Context, bounds osm.Bounds, filter *GetInBoundsFilter) (TagNodeMap, TagWayMap, TagRelationMap, errorsx.Error)
}
type DBConnSet struct {
conns []DataSourceConn
mu *sync.RWMutex
}
func NewDBConnSet(conns []DataSourceConn) *DBConnSet {
return &DBConnSet{conns, new(sync.RWMutex)}
}
func (dbcs *DBConnSet) GetConns() []DataSourceConn {
dbcs.mu.RLock()
defer dbcs.mu.RUnlock()
return dbcs.conns
}
func (dbcs *DBConnSet) AddDBConn(conn DataSourceConn) {
dbcs.mu.Lock()
defer dbcs.mu.Unlock()
dbcs.conns = append(dbcs.conns, conn)
}
type MatchLevel int
const (
MatchLevelNone MatchLevel = iota
MatchLevelPartial
MatchLevelFull
)
type ChosenConnForBounds struct {
MatchLevel MatchLevel
DataSourceConn
}
func getMatchLevel(conn DataSourceConn, bounds osm.Bounds) (MatchLevel, errorsx.Error) {
datasetInfo, err := conn.DatasetInfo()
if err != nil {
return 0, errorsx.Wrap(err)
}
dataSourceBounds := datasetInfo.Bounds.ToOSMBounds()
atLeastPartialMatch := ownmap.Overlaps(dataSourceBounds, bounds)
if !atLeastPartialMatch {
return MatchLevelNone, nil
}
isFullMatch := ownmap.IsTotallyInside(dataSourceBounds, bounds)
if isFullMatch {
return MatchLevelFull, nil
}
return MatchLevelPartial, nil
}
// GetConnForBounds selects a connection to use to provide data for a given bounds
func (dbcs *DBConnSet) GetConnsForBounds(bounds osm.Bounds) ([]*ChosenConnForBounds, errorsx.Error) {
var chosen []*ChosenConnForBounds
for _, conn := range dbcs.GetConns() {
matchLevel, err := getMatchLevel(conn, bounds)
if err != nil {
return nil, err
}
log.Printf("matchlevel: %v, file: %v\n", matchLevel, conn.Name())
if matchLevel == MatchLevelNone {
continue
}
chosen = append(chosen, &ChosenConnForBounds{
DataSourceConn: conn,
MatchLevel: matchLevel,
})
}
return chosen, nil
}