-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgraph.go
109 lines (88 loc) · 2.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
package falkordb
import (
"fmt"
"strings"
"github.com/redis/go-redis/v9"
)
// 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
Conn *redis.Client
schema GraphSchema
}
// New creates a new graph.
func graphNew(Id string, conn *redis.Client) *Graph {
g := new(Graph)
g.Id = Id
g.Conn = conn
g.schema = GraphSchemaNew(g)
return g
}
// ExecutionPlan gets the execution plan for given query.
func (g *Graph) ExecutionPlan(query string) (string, error) {
return g.Conn.Do(ctx, "GRAPH.EXPLAIN", g.Id, query).Text()
}
// Delete removes the graph.
func (g *Graph) Delete() error {
err := g.Conn.Do(ctx, "GRAPH.DELETE", g.Id).Err()
// clear internal mappings
g.schema.clear()
return err
}
// 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
}
func (g *Graph) query(command string, query string, params map[string]interface{}, options *QueryOptions) (*QueryResult, error) {
if params != nil {
query = BuildParamsHeader(params) + query
}
var r interface{}
var err error
if options != nil && options.timeout >= 0 {
r, err = g.Conn.Do(ctx, command, g.Id, query, "--compact", "timeout", options.timeout).Result()
} else {
r, err = g.Conn.Do(ctx, command, g.Id, query, "--compact").Result()
}
if err != nil {
return nil, err
}
return QueryResultNew(g, r)
}
// Query executes a query against the graph.
func (g *Graph) Query(query string, params map[string]interface{}, options *QueryOptions) (*QueryResult, error) {
return g.query("GRAPH.QUERY", query, params, options)
}
// ROQuery executes a read only query against the graph.
func (g *Graph) ROQuery(query string, params map[string]interface{}, options *QueryOptions) (*QueryResult, error) {
return g.query("GRAPH.RO_QUERY", query, params, options)
}
// Procedures
// CallProcedure invokes procedure.
func (g *Graph) CallProcedure(procedure string, yield []string, args ...interface{}) (*QueryResult, error) {
query := fmt.Sprintf("CALL %s(", procedure)
tmp := make([]string, 0, len(args))
for arg := range args {
tmp = append(tmp, ToString(arg))
}
query += fmt.Sprintf("%s)", strings.Join(tmp, ","))
if len(yield) > 0 {
query += fmt.Sprintf(" YIELD %s", strings.Join(yield, ","))
}
return g.Query(query, nil, nil)
}