-
Notifications
You must be signed in to change notification settings - Fork 117
/
catalog.go
171 lines (146 loc) · 3.78 KB
/
catalog.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
package runtime
import (
"context"
"errors"
"fmt"
runtimev1 "github.com/rilldata/rill/proto/gen/rill/runtime/v1"
"github.com/rilldata/rill/runtime/drivers"
"github.com/rilldata/rill/runtime/services/catalog"
"google.golang.org/protobuf/proto"
)
func (r *Runtime) ListCatalogEntries(ctx context.Context, instanceID string, t drivers.ObjectType) ([]*drivers.CatalogEntry, error) {
cat, err := r.Catalog(ctx, instanceID)
if err != nil {
return nil, err
}
return cat.FindEntries(ctx, t), nil
}
func (r *Runtime) GetCatalogEntry(ctx context.Context, instanceID, name string) (*drivers.CatalogEntry, error) {
cat, err := r.Catalog(ctx, instanceID)
if err != nil {
return nil, err
}
e, ok := cat.FindEntry(ctx, name)
if !ok {
return nil, fmt.Errorf("entry not found")
}
return e, nil
}
func (r *Runtime) Reconcile(ctx context.Context, instanceID string, changedPaths, forcedPaths []string, dry, strict bool) (*catalog.ReconcileResult, error) {
cat, err := r.Catalog(ctx, instanceID)
if err != nil {
return nil, err
}
resp, err := cat.Reconcile(ctx, catalog.ReconcileConfig{
DryRun: dry,
Strict: strict,
ChangedPaths: changedPaths,
ForcedPaths: forcedPaths,
})
if err != nil {
return nil, err
}
return resp, nil
}
func (r *Runtime) RefreshSource(ctx context.Context, instanceID, name string) error {
cat, err := r.Catalog(ctx, instanceID)
if err != nil {
return err
}
path, ok := cat.NameToPath[name]
if !ok {
return fmt.Errorf("artifact not found for source")
}
resp, err := cat.Reconcile(ctx, catalog.ReconcileConfig{
ChangedPaths: []string{path},
ForcedPaths: []string{path},
Strict: true,
})
if err != nil {
return err
}
if len(resp.Errors) > 0 {
return errors.New(resp.Errors[0].Message)
}
return nil
}
func (r *Runtime) SyncExistingTables(ctx context.Context, instanceID string) error {
// TODO: move to using reconcile
// Get OLAP
olap, err := r.OLAP(ctx, instanceID)
if err != nil {
return err
}
// Get catalog
cat, err := r.Catalog(ctx, instanceID)
if err != nil {
return err
}
// Get full catalog
objs := cat.FindEntries(ctx, drivers.ObjectTypeUnspecified)
// Get information schema
tables, err := olap.InformationSchema().All(ctx)
if err != nil {
return err
}
// Index objects for lookup
objMap := make(map[string]*drivers.CatalogEntry)
objSeen := make(map[string]bool)
for _, obj := range objs {
objMap[obj.Name] = obj
objSeen[obj.Name] = false
}
// Process tables in information schema
added := 0
updated := 0
for _, t := range tables {
obj, ok := objMap[t.Name]
// Track that the object still exists
if ok {
objSeen[t.Name] = true
}
// Create or update in catalog if relevant
if ok && obj.Type == drivers.ObjectTypeTable && !obj.GetTable().Managed {
// If the table has already been synced, update the schema if it has changed
tbl := obj.GetTable()
if !proto.Equal(t.Schema, tbl.Schema) {
tbl.Schema = t.Schema
err := cat.Catalog.UpdateEntry(ctx, instanceID, obj)
if err != nil {
return err
}
updated++
}
} else if !ok {
// If we haven't seen this table before, add it
err := cat.Catalog.CreateEntry(ctx, instanceID, &drivers.CatalogEntry{
Name: t.Name,
Type: drivers.ObjectTypeTable,
Object: &runtimev1.Table{
Name: t.Name,
Schema: t.Schema,
Managed: false,
},
})
if err != nil {
return err
}
added++
}
// Defensively do nothing in all other cases
}
// Remove non-managed tables not found in information schema
removed := 0
for name, seen := range objSeen {
obj := objMap[name]
if !seen && obj.Type == drivers.ObjectTypeTable && !obj.GetTable().Managed {
err := cat.Catalog.DeleteEntry(ctx, instanceID, name)
if err != nil {
return err
}
removed++
}
}
// Done
return nil
}