-
Notifications
You must be signed in to change notification settings - Fork 111
/
connections.go
109 lines (88 loc) · 2.62 KB
/
connections.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
package runtime
import (
"context"
"fmt"
"github.com/rilldata/rill/runtime/drivers"
"github.com/rilldata/rill/runtime/services/catalog"
)
func (r *Runtime) Registry() drivers.RegistryStore {
registry, ok := r.metastore.AsRegistry()
if !ok {
// Verified as registry in New, so this should never happen
panic("metastore is not a registry")
}
return registry
}
func (r *Runtime) Repo(ctx context.Context, instanceID string) (drivers.RepoStore, error) {
inst, err := r.FindInstance(ctx, instanceID)
if err != nil {
return nil, err
}
conn, err := r.connCache.get(ctx, instanceID, inst.RepoDriver, inst.RepoDSN)
if err != nil {
return nil, err
}
repo, ok := conn.AsRepoStore()
if !ok {
// Verified as repo when instance is created, so this should never happen
return nil, fmt.Errorf("connection for instance '%s' is not a repo", instanceID)
}
return repo, nil
}
func (r *Runtime) OLAP(ctx context.Context, instanceID string) (drivers.OLAPStore, error) {
inst, err := r.FindInstance(ctx, instanceID)
if err != nil {
return nil, err
}
conn, err := r.connCache.get(ctx, instanceID, inst.OLAPDriver, inst.OLAPDSN)
if err != nil {
return nil, err
}
olap, ok := conn.AsOLAP()
if !ok {
// Verified as OLAP when instance is created, so this should never happen
return nil, fmt.Errorf("connection for instance '%s' is not an olap", instanceID)
}
return olap, nil
}
func (r *Runtime) Catalog(ctx context.Context, instanceID string) (drivers.CatalogStore, error) {
inst, err := r.FindInstance(ctx, instanceID)
if err != nil {
return nil, err
}
if inst.EmbedCatalog {
conn, err := r.connCache.get(ctx, inst.ID, inst.OLAPDriver, inst.OLAPDSN)
if err != nil {
return nil, err
}
store, ok := conn.AsCatalogStore()
if !ok {
// Verified as CatalogStore when instance is created, so this should never happen
return nil, fmt.Errorf("instance cannot embed catalog")
}
return store, nil
}
store, ok := r.metastore.AsCatalogStore()
if !ok {
return nil, fmt.Errorf("metastore cannot serve as catalog")
}
return store, nil
}
func (r *Runtime) NewCatalogService(ctx context.Context, instanceID string) (*catalog.Service, error) {
// get all stores
olapStore, err := r.OLAP(ctx, instanceID)
if err != nil {
return nil, err
}
catalogStore, err := r.Catalog(ctx, instanceID)
if err != nil {
return nil, err
}
repoStore, err := r.Repo(ctx, instanceID)
if err != nil {
return nil, err
}
registry := r.Registry()
migrationMetadata := r.migrationMetaCache.get(instanceID)
return catalog.NewService(catalogStore, repoStore, olapStore, registry, instanceID, r.logger, migrationMetadata), nil
}