Skip to content

Commit 3e4ab76

Browse files
committed
Add Query endpoints with QueryOptions support
1 parent f906451 commit 3e4ab76

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

client_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,3 +473,19 @@ func TestNodeMapDatatype(t *testing.T) {
473473
assert.Nil(t, err)
474474
assert.Equal(t, 1, res.RelationshipsDeleted(), "Expecting 1 relationships deleted")
475475
}
476+
477+
func TestTimeout(t *testing.T) {
478+
// Instantiate a new QueryOptions struct with a 1-second timeout
479+
options := QueryOptionsNew(1)
480+
481+
// Issue a long-running query with a 1-millisecond timeout.
482+
res, err := graph.QueryWithOptions("UNWIND range(0, 1000000) AS v RETURN v", options)
483+
assert.Nil(t, res)
484+
assert.NotNil(t, err)
485+
486+
params := make(map[string]interface{})
487+
params["ub"] = 1000000
488+
res, err = graph.ParameterizedQueryWithOptions("UNWIND range(0, $ub) AS v RETURN v", params, options);
489+
assert.Nil(t, res)
490+
assert.NotNil(t, err)
491+
}

graph.go

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import (
88
"github.com/gomodule/redigo/redis"
99
)
1010

11+
// QueryOptions are a set of additional arguments to be emitted with a query.
12+
type QueryOptions struct {
13+
timeout int
14+
}
15+
1116
// Graph represents a graph, which is a collection of nodes and edges.
1217
type Graph struct {
1318
Id string
@@ -97,9 +102,16 @@ func (g *Graph) Commit() (*QueryResult, error) {
97102
return g.Query(q)
98103
}
99104

105+
// QueryOptionsNew instantiates a new QueryOptions struct.
106+
func QueryOptionsNew(timeout int) QueryOptions {
107+
options := QueryOptions{
108+
timeout: timeout,
109+
}
110+
return options
111+
}
112+
100113
// Query executes a query against the graph.
101114
func (g *Graph) Query(q string) (*QueryResult, error) {
102-
103115
r, err := g.Conn.Do("GRAPH.QUERY", g.Id, q, "--compact")
104116
if err != nil {
105117
return nil, err
@@ -126,6 +138,46 @@ func (g *Graph) ParameterizedQuery(q string, params map[string]interface{}) (*Qu
126138
return g.Query(q);
127139
}
128140

141+
// QueryWithOptions issues a query with the given timeout
142+
func (g *Graph) QueryWithOptions(q string, options QueryOptions) (*QueryResult, error) {
143+
var r interface{}
144+
var err error
145+
if(options.timeout >= 0) {
146+
r, err = g.Conn.Do("GRAPH.QUERY", g.Id, q, "--compact", "timeout", options.timeout)
147+
} else {
148+
r, err = g.Conn.Do("GRAPH.QUERY", g.Id, q, "--compact")
149+
}
150+
if err != nil {
151+
return nil, err
152+
}
153+
154+
return QueryResultNew(g, r)
155+
}
156+
157+
// ParameterizedQueryWithOptions issues a parameterized query with the given timeout
158+
func (g *Graph) ParameterizedQueryWithOptions(q string, params map[string]interface{}, options QueryOptions) (*QueryResult, error) {
159+
if(params != nil){
160+
q = BuildParamsHeader(params) + q
161+
}
162+
return g.QueryWithOptions(q, options);
163+
}
164+
165+
// ROQueryWithOptions issues a read-only query with the given timeout
166+
func (g *Graph) ROQueryWithOptions(q string, options QueryOptions) (*QueryResult, error) {
167+
var r interface{}
168+
var err error
169+
if(options.timeout >= 0) {
170+
r, err = g.Conn.Do("GRAPH.RO_QUERY", g.Id, q, "--compact", "timeout", options.timeout)
171+
} else {
172+
r, err = g.Conn.Do("GRAPH.RO_QUERY", g.Id, q, "--compact")
173+
}
174+
if err != nil {
175+
return nil, err
176+
}
177+
178+
return QueryResultNew(g, r)
179+
}
180+
129181
// Merge pattern
130182
func (g *Graph) Merge(p string) (*QueryResult, error) {
131183
q := fmt.Sprintf("MERGE %s", p)

0 commit comments

Comments
 (0)