-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmetagraphs.go
192 lines (175 loc) · 5.59 KB
/
metagraphs.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
package server
import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/bmeg/grip/config"
"github.com/bmeg/grip/gripper"
"github.com/bmeg/grip/gripql"
"github.com/bmeg/grip/log"
"github.com/bmeg/grip/util/rpc"
"github.com/bmeg/jsonschema/v5"
"github.com/bmeg/jsonschemagraph/compile"
"github.com/bmeg/jsonschemagraph/graph"
)
var schemaSuffix = "__schema__"
var mappingSuffix = "__mapping__"
func isSchema(graphName string) bool {
return strings.HasSuffix(graphName, schemaSuffix)
}
func isMapping(graphName string) bool {
return strings.HasSuffix(graphName, mappingSuffix)
}
func (server *GripServer) getGraph(graph string) (*gripql.Graph, error) {
conn, err := gripql.Connect(rpc.ConfigWithDefaults(server.conf.Server.RPCAddress()), true)
if err != nil {
return nil, fmt.Errorf("failed to load existing schema: %v", err)
}
res, err := conn.Traversal(context.Background(), &gripql.GraphQuery{Graph: graph, Query: gripql.NewQuery().V().Statements})
if err != nil {
return nil, fmt.Errorf("failed to load existing schema: %v", err)
}
vertices := []*gripql.Vertex{}
for row := range res {
vertices = append(vertices, row.GetVertex())
}
res, err = conn.Traversal(context.Background(), &gripql.GraphQuery{Graph: graph, Query: gripql.NewQuery().E().Statements})
if err != nil {
return nil, fmt.Errorf("failed to load existing schema: %v", err)
}
edges := []*gripql.Edge{}
for row := range res {
edges = append(edges, row.GetEdge())
}
return &gripql.Graph{Graph: graph, Vertices: vertices, Edges: edges}, nil
}
func (server *GripServer) buildSchemas(ctx context.Context) {
for _, gdb := range server.dbs {
for _, name := range gdb.ListGraphs() {
select {
case <-ctx.Done():
return
default:
if isSchema(name) {
continue
}
if _, ok := server.schemas[name]; ok {
log.WithFields(log.Fields{"graph": name}).Debug("skipping build; cached schema found")
continue
}
log.WithFields(log.Fields{"graph": name}).Debug("building graph schema")
schema, err := gdb.BuildSchema(ctx, name, server.conf.Server.SchemaInspectN, server.conf.Server.SchemaRandomSample)
if err == nil {
log.WithFields(log.Fields{"graph": name}).Debug("cached graph schema")
err := server.addFullGraph(ctx, fmt.Sprintf(schema.Graph, schemaSuffix), schema)
if err != nil {
log.WithFields(log.Fields{"graph": name, "error": err}).Error("failed to store graph schema")
}
server.schemas[name] = schema
} else {
log.WithFields(log.Fields{"graph": name, "error": err}).Error("failed to build graph schema")
}
}
}
}
}
// cacheSchemas calls GetSchema on each graph and caches the schemas in memory
func (server *GripServer) cacheSchemas(ctx context.Context) {
if time.Duration(server.conf.Server.SchemaRefreshInterval) == 0 {
server.buildSchemas(ctx)
return
}
ticker := time.NewTicker(time.Duration(server.conf.Server.SchemaRefreshInterval))
server.buildSchemas(ctx)
for {
select {
case <-ctx.Done():
ticker.Stop()
return
case <-ticker.C:
server.buildSchemas(ctx)
}
}
}
func (server *GripServer) updateGraphMap() {
o := map[string]string{}
for k, v := range server.conf.Graphs {
o[k] = v
}
for n, dbs := range server.dbs {
for _, g := range dbs.ListGraphs() {
o[g] = n
if strings.HasSuffix(g, "__mapping__") {
graph, err := server.getGraph(g)
if err == nil {
log.Infof("Reading config for a gripper driver %s", g)
mapping, _ := gripper.GraphToConfig(graph)
graphName := strings.TrimSuffix(g, mappingSuffix)
gdb, err := StartDriver(config.DriverConfig{Gripper: &gripper.Config{Graph: graphName, Mapping: mapping}}, server.sources)
if err == nil {
driverName := fmt.Sprintf("%s__driver__", graphName)
server.dbs[driverName] = gdb
o[graphName] = driverName
} else {
log.Errorf("Failed to start gripper: %s", graphName)
}
} else {
log.Errorf("Failed to get graph mapping: %s", err)
}
}
}
}
server.graphMap = o
}
func (server *GripServer) addFullGraph(ctx context.Context, graphName string, schema *gripql.Graph) error {
if graphName == "" {
return fmt.Errorf("graph name is an empty string")
}
if !server.graphExists(graphName) {
_, err := server.AddGraph(ctx, &gripql.GraphID{Graph: graphName})
if err != nil {
return fmt.Errorf("error creating graph '%s': %v", graphName, err)
}
}
for _, v := range schema.Vertices {
_, err := server.addVertex(ctx, &gripql.GraphElement{Graph: graphName, Vertex: v})
if err != nil {
return fmt.Errorf("error adding vertex to graph '%s': %v", graphName, err)
}
}
for _, e := range schema.Edges {
_, err := server.addEdge(ctx, &gripql.GraphElement{Graph: graphName, Edge: e})
if err != nil {
return fmt.Errorf("error adding edge to graph '%s': %v", graphName, err)
}
}
return nil
}
func (server *GripServer) LoadSchemas(sch *gripql.Graph, out *graph.GraphSchema) (*graph.GraphSchema, error) {
schcompiler := jsonschema.NewCompiler()
schcompiler.ExtractAnnotations = true
schcompiler.RegisterExtension(compile.GraphExtensionTag, compile.GraphExtMeta, compile.GraphExtCompiler{})
for _, v := range sch.Vertices {
jsonData, err := json.Marshal(v.Data)
if err != nil {
return nil, err
}
err = schcompiler.AddResource(v.Id, strings.NewReader(string(jsonData)))
if err != nil {
log.Error("schcompiler.AddResource err: ", err)
return nil, err
}
}
for _, v := range sch.Vertices {
sch, err := schcompiler.Compile(v.Id)
if err != nil {
log.Error("schcompiler.Compile err: ", err)
return nil, err
}
out.Classes[v.Label] = sch
}
out.Compiler = schcompiler
return out, nil
}