-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathgraph.go
316 lines (262 loc) · 7.74 KB
/
graph.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package redisgraph
import (
"fmt"
"strings"
"sync"
"github.com/gomodule/redigo/redis"
)
// QueryOptions are a set of additional arguments to be emitted with a query.
type QueryOptions struct {
timeout int
}
// Graph represents a graph, which is a collection of nodes and edges.
type Graph struct {
Id string
Nodes map[string]*Node
Edges []*Edge
Conn redis.Conn
labels []string // List of node labels.
relationshipTypes []string // List of relation types.
properties []string // List of properties.
mutex sync.Mutex // Lock, used for updating internal state.
}
// New creates a new graph.
func GraphNew(Id string, conn redis.Conn) Graph {
return Graph{
Id: Id,
Nodes: make(map[string]*Node, 0),
Edges: make([]*Edge, 0),
Conn: conn,
labels: make([]string, 0),
relationshipTypes: make([]string, 0),
properties: make([]string, 0),
}
}
// AddNode adds a node to the graph.
func (g *Graph) AddNode(n *Node) {
if n.Alias == "" {
n.Alias = RandomString(10)
}
n.graph = g
g.Nodes[n.Alias] = n
}
// AddEdge adds an edge to the graph.
func (g *Graph) AddEdge(e *Edge) error {
// Verify that the edge has source and destination
if e.Source == nil || e.Destination == nil {
return fmt.Errorf("Both source and destination nodes should be defined")
}
// Verify that the edge's nodes have been previously added to the graph
if _, ok := g.Nodes[e.Source.Alias]; !ok {
return fmt.Errorf("Source node neeeds to be added to the graph first")
}
if _, ok := g.Nodes[e.Destination.Alias]; !ok {
return fmt.Errorf("Destination node neeeds to be added to the graph first")
}
e.graph = g
g.Edges = append(g.Edges, e)
return nil
}
// ExecutionPlan gets the execution plan for given query.
func (g *Graph) ExecutionPlan(q string) (string, error) {
return redis.String(g.Conn.Do("GRAPH.EXPLAIN", g.Id, q))
}
// Delete removes the graph.
func (g *Graph) Delete() error {
_, err := g.Conn.Do("GRAPH.DELETE", g.Id)
// clear internal mappings
g.labels = g.labels[:0]
g.properties = g.properties[:0]
g.relationshipTypes = g.relationshipTypes[:0]
return err
}
// Flush will create the graph and clear it
func (g *Graph) Flush() (*QueryResult, error) {
res, err := g.Commit()
if err == nil {
g.Nodes = make(map[string]*Node)
g.Edges = make([]*Edge, 0)
}
return res, err
}
// Commit creates the entire graph, but will re-add nodes if called again.
func (g *Graph) Commit() (*QueryResult, error) {
items := make([]string, 0, len(g.Nodes)+len(g.Edges))
for _, n := range g.Nodes {
items = append(items, n.Encode())
}
for _, e := range g.Edges {
items = append(items, e.Encode())
}
q := "CREATE " + strings.Join(items, ",")
return g.Query(q)
}
// NewQueryOptions instantiates a new QueryOptions struct.
func NewQueryOptions() *QueryOptions {
return &QueryOptions{
timeout: -1,
}
}
// SetTimeout sets the timeout member of the QueryOptions struct
func (options *QueryOptions) SetTimeout(timeout int) *QueryOptions {
options.timeout = timeout
return options
}
// GetTimeout retrieves the timeout of the QueryOptions struct
func (options *QueryOptions) GetTimeout() int {
return options.timeout
}
// Query executes a query against the graph.
func (g *Graph) Query(q string) (*QueryResult, error) {
r, err := g.Conn.Do("GRAPH.QUERY", g.Id, q, "--compact")
if err != nil {
return nil, err
}
return QueryResultNew(g, r)
}
// ROQuery executes a read only query against the graph.
func (g *Graph) ROQuery(q string) (*QueryResult, error) {
r, err := g.Conn.Do("GRAPH.RO_QUERY", g.Id, q, "--compact")
if err != nil {
return nil, err
}
return QueryResultNew(g, r)
}
func (g *Graph) ParameterizedQuery(q string, params map[string]interface{}) (*QueryResult, error) {
if(params != nil){
q = BuildParamsHeader(params) + q
}
return g.Query(q);
}
// QueryWithOptions issues a query with the given timeout
func (g *Graph) QueryWithOptions(q string, options *QueryOptions) (*QueryResult, error) {
var r interface{}
var err error
if(options.timeout >= 0) {
r, err = g.Conn.Do("GRAPH.QUERY", g.Id, q, "--compact", "timeout", options.timeout)
} else {
r, err = g.Conn.Do("GRAPH.QUERY", g.Id, q, "--compact")
}
if err != nil {
return nil, err
}
return QueryResultNew(g, r)
}
// ParameterizedQueryWithOptions issues a parameterized query with the given timeout
func (g *Graph) ParameterizedQueryWithOptions(q string, params map[string]interface{}, options *QueryOptions) (*QueryResult, error) {
if(params != nil){
q = BuildParamsHeader(params) + q
}
return g.QueryWithOptions(q, options);
}
// ROQueryWithOptions issues a read-only query with the given timeout
func (g *Graph) ROQueryWithOptions(q string, options *QueryOptions) (*QueryResult, error) {
var r interface{}
var err error
if(options.timeout >= 0) {
r, err = g.Conn.Do("GRAPH.RO_QUERY", g.Id, q, "--compact", "timeout", options.timeout)
} else {
r, err = g.Conn.Do("GRAPH.RO_QUERY", g.Id, q, "--compact")
}
if err != nil {
return nil, err
}
return QueryResultNew(g, r)
}
// Merge pattern
func (g *Graph) Merge(p string) (*QueryResult, error) {
q := fmt.Sprintf("MERGE %s", p)
return g.Query(q)
}
func (g *Graph) getLabel(lblIdx int) string {
if lblIdx >= len(g.labels) {
// Missing label, refresh label mapping table.
g.mutex.Lock()
// Recheck now that we've got the lock.
if lblIdx >= len(g.labels) {
g.labels = g.Labels()
// Retry.
if lblIdx >= len(g.labels) {
// Error!
panic("Unknown label index.")
}
}
g.mutex.Unlock()
}
return g.labels[lblIdx]
}
func (g *Graph) getRelation(relIdx int) string {
if relIdx >= len(g.relationshipTypes) {
// Missing relation type, refresh relation type mapping table.
g.mutex.Lock()
// Recheck now that we've got the lock.
if relIdx >= len(g.relationshipTypes) {
g.relationshipTypes = g.RelationshipTypes()
// Retry.
if relIdx >= len(g.relationshipTypes) {
// Error!
panic("Unknown relation type index.")
}
}
g.mutex.Unlock()
}
return g.relationshipTypes[relIdx]
}
func (g *Graph) getProperty(propIdx int) string {
if propIdx >= len(g.properties) {
// Missing property, refresh property mapping table.
g.mutex.Lock()
// Recheck now that we've got the lock.
if propIdx >= len(g.properties) {
g.properties = g.PropertyKeys()
// Retry.
if propIdx >= len(g.properties) {
// Error!
panic("Unknown property index.")
}
}
g.mutex.Unlock()
}
return g.properties[propIdx]
}
// Procedures
// CallProcedure invokes procedure.
func (g *Graph) CallProcedure(procedure string, yield []string, args ...interface{}) (*QueryResult, error) {
q := fmt.Sprintf("CALL %s(", procedure)
tmp := make([]string, 0, len(args))
for arg := range args {
tmp = append(tmp, ToString(arg))
}
q += fmt.Sprintf("%s)", strings.Join(tmp, ","))
if len(yield) > 0 {
q += fmt.Sprintf(" YIELD %s", strings.Join(yield, ","))
}
return g.Query(q)
}
// Labels, retrieves all node labels.
func (g *Graph) Labels() []string {
qr, _ := g.CallProcedure("db.labels", nil)
l := make([]string, len(qr.results))
for idx, r := range qr.results {
l[idx] = r.GetByIndex(0).(string)
}
return l
}
// RelationshipTypes, retrieves all edge relationship types.
func (g *Graph) RelationshipTypes() []string {
qr, _ := g.CallProcedure("db.relationshipTypes", nil)
rt := make([]string, len(qr.results))
for idx, r := range qr.results {
rt[idx] = r.GetByIndex(0).(string)
}
return rt
}
// PropertyKeys, retrieves all properties names.
func (g *Graph) PropertyKeys() []string {
qr, _ := g.CallProcedure("db.propertyKeys", nil)
p := make([]string, len(qr.results))
for idx, r := range qr.results {
p[idx] = r.GetByIndex(0).(string)
}
return p
}