Skip to content

Commit f701bd2

Browse files
committed
multi label api change
1 parent f86cee7 commit f701bd2

File tree

5 files changed

+61
-59
lines changed

5 files changed

+61
-59
lines changed

client_test.go

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ func createGraph() {
1616
graph = GraphNew("social", conn)
1717

1818
// Create 2 nodes connect via a single edge.
19-
japan := NodeNew("Country", "j", nil)
20-
john := NodeNew("Person", "p", nil)
19+
japan := NodeNew([]string{"Country"}, "j", nil)
20+
john := NodeNew([]string{"Person"}, "p", nil)
2121
edge := EdgeNew("Visited", john, japan, nil)
2222

2323
// Set node properties.
@@ -91,9 +91,9 @@ func checkQueryResults(t *testing.T, res *QueryResult) {
9191
d, ok := r.GetByIndex(2).(*Node)
9292
assert.True(t, ok, "Third column should contain nodes.")
9393

94-
assert.Equal(t, s.Label, "Person", "Node should be of type 'Person'")
94+
assert.Equal(t, s.Labels[0], "Person", "Node should be of type 'Person'")
9595
assert.Equal(t, e.Relation, "Visited", "Edge should be of relation type 'Visited'")
96-
assert.Equal(t, d.Label, "Country", "Node should be of type 'Country'")
96+
assert.Equal(t, d.Labels[0], "Country", "Node should be of type 'Country'")
9797

9898
assert.Equal(t, len(s.Properties), 4, "Person node should have 4 properties")
9999

@@ -131,7 +131,7 @@ func TestCreateQuery(t *testing.T) {
131131
res.Next()
132132
r := res.Record()
133133
w := r.GetByIndex(0).(*Node)
134-
assert.Equal(t, w.Label, "WorkPlace", "Unexpected node label.")
134+
assert.Equal(t, w.Labels[0], "WorkPlace", "Unexpected node label.")
135135
}
136136

137137
func TestCreateROQueryFailure(t *testing.T) {
@@ -199,8 +199,8 @@ func TestArray(t *testing.T) {
199199
t.Error(err)
200200
}
201201

202-
a := NodeNew("person", "", nil)
203-
b := NodeNew("person", "", nil)
202+
a := NodeNew([]string{"person"}, "", nil)
203+
b := NodeNew([]string{"person"}, "", nil)
204204

205205
a.SetProperty("name", "a")
206206
a.SetProperty("age", 32)
@@ -288,9 +288,9 @@ func TestPath(t *testing.T) {
288288
e := p.GetEdge(0)
289289
d := p.LastNode()
290290

291-
assert.Equal(t, s.Label, "Person", "Node should be of type 'Person'")
291+
assert.Equal(t, s.Labels[0], "Person", "Node should be of type 'Person'")
292292
assert.Equal(t, e.Relation, "Visited", "Edge should be of relation type 'Visited'")
293-
assert.Equal(t, d.Label, "Country", "Node should be of type 'Country'")
293+
assert.Equal(t, d.Labels[0], "Country", "Node should be of type 'Country'")
294294

295295
assert.Equal(t, len(s.Properties), 4, "Person node should have 4 properties")
296296

@@ -308,12 +308,12 @@ func TestPath(t *testing.T) {
308308

309309
func TestParameterizedQuery(t *testing.T) {
310310
createGraph()
311-
params := []interface{}{1, 2.3, "str", true, false, nil, []interface {}{0, 1, 2}, []interface {}{"0", "1", "2"}}
311+
params := []interface{}{1, 2.3, "str", true, false, nil, []interface{}{0, 1, 2}, []interface{}{"0", "1", "2"}}
312312
q := "RETURN $param"
313313
params_map := make(map[string]interface{})
314314
for index, param := range params {
315315
params_map["param"] = param
316-
res, err := graph.ParameterizedQuery(q, params_map);
316+
res, err := graph.ParameterizedQuery(q, params_map)
317317
if err != nil {
318318
t.Error(err)
319319
}
@@ -348,24 +348,24 @@ func TestCreateIndex(t *testing.T) {
348348
func TestQueryStatistics(t *testing.T) {
349349
graph.Flush()
350350
err := graph.Delete()
351-
assert.Nil(t,err)
351+
assert.Nil(t, err)
352352

353353
q := "CREATE (:Person{name:'a',age:32,array:[0,1,2]})"
354354
res, err := graph.Query(q)
355-
assert.Nil(t,err)
355+
assert.Nil(t, err)
356356

357357
assert.Equal(t, 1, res.NodesCreated(), "Expecting 1 node created")
358358
assert.Equal(t, 0, res.NodesDeleted(), "Expecting 0 nodes deleted")
359-
assert.Greater(t, res.InternalExecutionTime(),0.0, "Expecting internal execution time not to be 0.0")
359+
assert.Greater(t, res.InternalExecutionTime(), 0.0, "Expecting internal execution time not to be 0.0")
360360
assert.Equal(t, true, res.Empty(), "Expecting empty resultset")
361361

362-
res,err = graph.Query("MATCH (n) DELETE n")
363-
assert.Nil(t,err)
362+
res, err = graph.Query("MATCH (n) DELETE n")
363+
assert.Nil(t, err)
364364
assert.Equal(t, 1, res.NodesDeleted(), "Expecting 1 nodes deleted")
365365

366366
// Create 2 nodes connect via a single edge.
367-
japan := NodeNew("Country", "j", nil)
368-
john := NodeNew("Person", "p", nil)
367+
japan := NodeNew([]string{"Country"}, "j", nil)
368+
john := NodeNew([]string{"Person"}, "p", nil)
369369
edge := EdgeNew("Visited", john, japan, nil)
370370

371371
// Set node properties.
@@ -386,21 +386,21 @@ func TestQueryStatistics(t *testing.T) {
386386

387387
// Flush graph to DB.
388388
res, err = graph.Commit()
389-
assert.Nil(t,err)
389+
assert.Nil(t, err)
390390
assert.Equal(t, 2, res.NodesCreated(), "Expecting 2 node created")
391391
assert.Equal(t, 0, res.NodesDeleted(), "Expecting 0 nodes deleted")
392392
assert.Equal(t, 7, res.PropertiesSet(), "Expecting 7 properties set")
393393
assert.Equal(t, 1, res.RelationshipsCreated(), "Expecting 1 relationships created")
394394
assert.Equal(t, 0, res.RelationshipsDeleted(), "Expecting 0 relationships deleted")
395-
assert.Greater(t, res.InternalExecutionTime(),0.0, "Expecting internal execution time not to be 0.0")
395+
assert.Greater(t, res.InternalExecutionTime(), 0.0, "Expecting internal execution time not to be 0.0")
396396
assert.Equal(t, true, res.Empty(), "Expecting empty resultset")
397397
q = "MATCH p = (:Person)-[:Visited]->(:Country) RETURN p"
398398
res, err = graph.Query(q)
399-
assert.Nil(t,err)
399+
assert.Nil(t, err)
400400
assert.Equal(t, len(res.results), 1, "expecting 1 result record")
401401
assert.Equal(t, false, res.Empty(), "Expecting resultset to have records")
402-
res,err = graph.Query("MATCH ()-[r]-() DELETE r")
403-
assert.Nil(t,err)
402+
res, err = graph.Query("MATCH ()-[r]-() DELETE r")
403+
assert.Nil(t, err)
404404
assert.Equal(t, 1, res.RelationshipsDeleted(), "Expecting 1 relationships deleted")
405405
}
406406

@@ -410,22 +410,22 @@ func TestUtils(t *testing.T) {
410410

411411
res = ToString("test_string")
412412
assert.Equal(t, res, "\"test_string\"")
413-
413+
414414
res = ToString(10)
415-
assert.Equal(t, res, "10")
415+
assert.Equal(t, res, "10")
416416

417417
res = ToString(1.2)
418418
assert.Equal(t, res, "1.2")
419419

420420
res = ToString(true)
421421
assert.Equal(t, res, "true")
422422

423-
var arr = []interface{}{1,2,3,"boom"}
423+
var arr = []interface{}{1, 2, 3, "boom"}
424424
res = ToString(arr)
425425
assert.Equal(t, res, "[1,2,3,\"boom\"]")
426-
426+
427427
jsonMap := make(map[string]interface{})
428-
jsonMap["object"] = map[string]interface{} {"foo": 1}
428+
jsonMap["object"] = map[string]interface{}{"foo": 1}
429429
res = ToString(jsonMap)
430430
assert.Equal(t, res, "{object: {foo: 1}}")
431431
}
@@ -436,13 +436,13 @@ func TestNodeMapDatatype(t *testing.T) {
436436
assert.Nil(t, err)
437437

438438
// Create 2 nodes connect via a single edge.
439-
japan := NodeNew("Country", "j",
439+
japan := NodeNew([]string{"Country"}, "j",
440440
map[string]interface{}{
441441
"name": "Japan",
442442
"population": 126800000,
443443
"states": []string{"Kanto", "Chugoku"},
444444
})
445-
john := NodeNew("Person", "p",
445+
john := NodeNew([]string{"Person"}, "p",
446446
map[string]interface{}{
447447
"name": "John Doe",
448448
"age": 33,
@@ -488,7 +488,7 @@ func TestTimeout(t *testing.T) {
488488

489489
params := make(map[string]interface{})
490490
params["ub"] = 1000000
491-
res, err = graph.ParameterizedQueryWithOptions("UNWIND range(0, $ub) AS v RETURN v", params, options);
491+
res, err = graph.ParameterizedQueryWithOptions("UNWIND range(0, $ub) AS v RETURN v", params, options)
492492
assert.Nil(t, res)
493493
assert.NotNil(t, err)
494494
}

example_graph_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import (
44
"crypto/tls"
55
"crypto/x509"
66
"fmt"
7-
"github.com/RedisGraph/redisgraph-go"
8-
"github.com/gomodule/redigo/redis"
97
"io/ioutil"
108
"log"
119
"os"
10+
11+
"github.com/RedisGraph/redisgraph-go"
12+
"github.com/gomodule/redigo/redis"
1213
)
1314

1415
func ExampleGraphNew() {
@@ -22,7 +23,7 @@ func ExampleGraphNew() {
2223
res.Next()
2324
r := res.Record()
2425
w := r.GetByIndex(0).(*redisgraph.Node)
25-
fmt.Println(w.Label)
26+
fmt.Println(w.Labels[0])
2627
// Output: WorkPlace
2728
}
2829

@@ -40,7 +41,7 @@ func ExampleGraphNew_pool() {
4041
res.Next()
4142
r := res.Record()
4243
w := r.GetByIndex(0).(*redisgraph.Node)
43-
fmt.Println(w.Label)
44+
fmt.Println(w.Labels[0])
4445
// Output: WorkPlace
4546

4647
}
@@ -103,7 +104,7 @@ func ExampleGraphNew_tls() {
103104
res.Next()
104105
r := res.Record()
105106
w := r.GetByIndex(0).(*redisgraph.Node)
106-
fmt.Println(w.Label)
107+
fmt.Println(w.Labels[0])
107108
}
108109

109110
func getConnectionDetails() (host string, password string) {

examples/redisgraph_tls_client/redisgraph_tls_client.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import (
55
"crypto/x509"
66
"flag"
77
"fmt"
8-
"github.com/RedisGraph/redisgraph-go"
9-
"github.com/gomodule/redigo/redis"
108
"io/ioutil"
119
"log"
1210
"os"
11+
12+
"github.com/RedisGraph/redisgraph-go"
13+
"github.com/gomodule/redigo/redis"
1314
)
1415

1516
var (
@@ -85,6 +86,6 @@ func main() {
8586
res.Next()
8687
r := res.Record()
8788
w := r.GetByIndex(0).(*redisgraph.Node)
88-
fmt.Println(w.Label)
89+
fmt.Println(w.Labels[0])
8990
// Output: WorkPlace
9091
}

node.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@ import (
88
// Node represents a node within a graph.
99
type Node struct {
1010
ID uint64
11-
Label string
11+
Labels []string
1212
Alias string
1313
Properties map[string]interface{}
1414
graph *Graph
1515
}
1616

17-
func NodeNew(label string, alias string, properties map[string]interface{}) *Node {
17+
func NodeNew(labels []string, alias string, properties map[string]interface{}) *Node {
1818

1919
p := properties
2020
if p == nil {
2121
p = make(map[string]interface{})
2222
}
2323

2424
return &Node{
25-
Label: label,
25+
Labels: labels,
2626
Alias: alias,
2727
Properties: p,
2828
graph: nil,
@@ -60,8 +60,8 @@ func (n Node) Encode() string {
6060
s = append(s, n.Alias)
6161
}
6262

63-
if n.Label != "" {
64-
s = append(s, ":", n.Label)
63+
for _, label := range n.Labels {
64+
s = append(s, ":", label)
6565
}
6666

6767
if len(n.Properties) > 0 {

query_result.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"strconv"
77
"strings"
8+
89
"github.com/gomodule/redigo/redis"
910
"github.com/olekukonko/tablewriter"
1011
)
@@ -16,10 +17,10 @@ const (
1617
RELATIONSHIPS_DELETED string = "Relationships deleted"
1718
PROPERTIES_SET string = "Properties set"
1819
RELATIONSHIPS_CREATED string = "Relationships created"
19-
INDICES_CREATED string = "Indices created"
20-
INDICES_DELETED string = "Indices deleted"
20+
INDICES_CREATED string = "Indices created"
21+
INDICES_DELETED string = "Indices deleted"
2122
INTERNAL_EXECUTION_TIME string = "Query internal execution time"
22-
CACHED_EXECUTION string = "Cached execution"
23+
CACHED_EXECUTION string = "Cached execution"
2324
)
2425

2526
type ResultSetColumnTypes int
@@ -54,11 +55,11 @@ type QueryResultHeader struct {
5455

5556
// QueryResult represents the results of a query.
5657
type QueryResult struct {
57-
graph *Graph
58-
header QueryResultHeader
59-
results []*Record
60-
statistics map[string]float64
61-
current_record_idx int
58+
graph *Graph
59+
header QueryResultHeader
60+
results []*Record
61+
statistics map[string]float64
62+
current_record_idx int
6263
}
6364

6465
func QueryResultNew(g *Graph, response interface{}) (*QueryResult, error) {
@@ -69,7 +70,7 @@ func QueryResultNew(g *Graph, response interface{}) (*QueryResult, error) {
6970
column_names: make([]string, 0),
7071
column_types: make([]ResultSetColumnTypes, 0),
7172
},
72-
graph: g,
73+
graph: g,
7374
current_record_idx: -1,
7475
}
7576

@@ -172,18 +173,18 @@ func (qr *QueryResult) parseNode(cell interface{}) *Node {
172173
// [label string offset (integer)],
173174
// [[name, value type, value] X N]
174175

175-
var label string
176176
c, _ := redis.Values(cell, nil)
177177
id, _ := redis.Uint64(c[0], nil)
178-
labels, _ := redis.Ints(c[1], nil)
179-
if len(labels) > 0 {
180-
label = qr.graph.getLabel(labels[0])
178+
label_ids, _ := redis.Ints(c[1], nil)
179+
labels := make([]string, len(label_ids))
180+
for i := 0; i < len(label_ids); i++ {
181+
labels[i] = qr.graph.getLabel(label_ids[i])
181182
}
182183

183184
rawProps, _ := redis.Values(c[2], nil)
184185
properties := qr.parseProperties(rawProps)
185186

186-
n := NodeNew(label, "", properties)
187+
n := NodeNew(labels, "", properties)
187188
n.ID = id
188189
return n
189190
}
@@ -386,4 +387,3 @@ func (qr *QueryResult) InternalExecutionTime() float64 {
386387
func (qr *QueryResult) CachedExecution() int {
387388
return int(qr.getStat(CACHED_EXECUTION))
388389
}
389-

0 commit comments

Comments
 (0)