-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgraph_schema.go
112 lines (92 loc) · 2.24 KB
/
graph_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
package falkordb
import "errors"
type GraphSchema struct {
graph *Graph
version int
labels []string
relationships []string
properties []string
}
func GraphSchemaNew(graph *Graph) GraphSchema {
return GraphSchema{
graph: graph,
version: 0,
labels: []string{},
relationships: []string{},
properties: []string{},
}
}
func (gs *GraphSchema) clear() {
gs.labels = []string{}
gs.relationships = []string{}
gs.properties = []string{}
}
func (gs *GraphSchema) refresh_labels() error {
qr, err := gs.graph.CallProcedure("db.labels", nil)
if err != nil {
return err
}
gs.labels = make([]string, len(qr.results))
for idx, r := range qr.results {
gs.labels[idx] = r.GetByIndex(0).(string)
}
return nil
}
func (gs *GraphSchema) refresh_relationships() error {
qr, err := gs.graph.CallProcedure("db.relationshipTypes", nil)
if err != nil {
return err
}
gs.relationships = make([]string, len(qr.results))
for idx, r := range qr.results {
gs.relationships[idx] = r.GetByIndex(0).(string)
}
return nil
}
func (gs *GraphSchema) refresh_properties() error {
qr, err := gs.graph.CallProcedure("db.propertyKeys", nil)
if err != nil {
return err
}
gs.properties = make([]string, len(qr.results))
for idx, r := range qr.results {
gs.properties[idx] = r.GetByIndex(0).(string)
}
return nil
}
func (gs *GraphSchema) getLabel(lblIdx int) (string, error) {
if lblIdx >= len(gs.labels) {
err := gs.refresh_labels()
if err != nil {
return "", err
}
if lblIdx >= len(gs.labels) {
return "", errors.New("Unknown label index.")
}
}
return gs.labels[lblIdx], nil
}
func (gs *GraphSchema) getRelation(relIdx int) (string, error) {
if relIdx >= len(gs.relationships) {
err := gs.refresh_relationships()
if err != nil {
return "", err
}
if relIdx >= len(gs.relationships) {
return "", errors.New("Unknown label index.")
}
}
return gs.relationships[relIdx], nil
}
func (gs *GraphSchema) getProperty(propIdx int) (string, error) {
if propIdx >= len(gs.properties) {
err := gs.refresh_properties()
if err != nil {
return "", err
}
if propIdx >= len(gs.properties) {
return "", errors.New("Unknown property index.")
}
}
return gs.properties[propIdx], nil
}