-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathschema.go
142 lines (121 loc) · 3.24 KB
/
schema.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
package sqlite
import (
"context"
"fmt"
"github.com/bmeg/grip/gripql"
"github.com/bmeg/grip/log"
"github.com/bmeg/grip/psql"
"github.com/bmeg/grip/util"
"golang.org/x/sync/errgroup"
"google.golang.org/protobuf/types/known/structpb"
)
// BuildSchema returns the schema of a specific graph in the database
func (db *GraphDB) BuildSchema(ctx context.Context, graphID string, sampleN uint32, random bool) (*gripql.Graph, error) {
var g errgroup.Group
gi, err := db.Graph(graphID)
if err != nil {
return nil, err
}
graph := gi.(*Graph)
vSchemaChan := make(chan *gripql.Vertex)
eSchemaChan := make(chan *gripql.Edge)
vLabels, err := graph.ListVertexLabels()
if err != nil {
return nil, err
}
for _, label := range vLabels {
label := label
if label == "" {
continue
}
g.Go(func() error {
q := fmt.Sprintf("SELECT * FROM %s WHERE label='%s'", graph.v, label)
rows, err := graph.db.QueryxContext(ctx, q)
if err != nil {
log.WithFields(log.Fields{"error": err}).Error("BuildSchema: QueryxContext")
return err
}
defer rows.Close()
schema := make(map[string]interface{})
for rows.Next() {
vrow := &psql.Row{}
if err := rows.StructScan(vrow); err != nil {
log.WithFields(log.Fields{"error": err}).Error("BuildSchema: StructScan")
continue
}
v, err := psql.ConvertVertexRow(vrow, true)
if err != nil {
log.WithFields(log.Fields{"error": err}).Error("BuildSchema: convertVertexRow")
continue
}
util.MergeMaps(schema, v.Data)
}
sSchema, _ := structpb.NewStruct(schema)
vSchema := &gripql.Vertex{Id: label, Label: "Vertex", Data: sSchema}
vSchemaChan <- vSchema
return nil
})
}
eLabels, err := graph.ListEdgeLabels()
if err != nil {
return nil, err
}
for _, label := range eLabels {
label := label
if label == "" {
continue
}
g.Go(func() error {
q := fmt.Sprintf(
`SELECT a.label, b.label, c.label, b.data FROM %s as a INNER JOIN %s as b ON b."to"=a.id INNER JOIN %s as c on b."from" = c.id WHERE b.label = '%s' limit %d`,
graph.v, graph.e, graph.v,
label, sampleN,
)
//fmt.Printf("Query: %s\n", q)
rows, err := graph.db.QueryxContext(ctx, q)
if err != nil {
log.WithFields(log.Fields{"error": err}).Error("BuildSchema: QueryxContext")
return err
}
defer rows.Close()
//schema := make(map[string]interface{})
for rows.Next() {
if row, err := rows.SliceScan(); err != nil {
log.WithFields(log.Fields{"error": err}).Error("BuildSchema: SliceScan")
continue
} else {
eSchema := &gripql.Edge{
Id: fmt.Sprintf("(%s)--%s->(%s)", row[0], row[1], row[2]),
Label: label,
From: row[0].(string),
To: row[2].(string),
}
eSchemaChan <- eSchema
//fmt.Printf("Found: %s\n", row)
}
}
return nil
})
}
wg := errgroup.Group{}
vSchema := []*gripql.Vertex{}
eSchema := []*gripql.Edge{}
wg.Go(func() error {
for s := range vSchemaChan {
vSchema = append(vSchema, s)
}
return nil
})
wg.Go(func() error {
for s := range eSchemaChan {
eSchema = append(eSchema, s)
}
return nil
})
g.Wait()
close(vSchemaChan)
close(eSchemaChan)
wg.Wait()
schema := &gripql.Graph{Graph: graphID, Vertices: vSchema, Edges: eSchema}
return schema, nil
}