-
Notifications
You must be signed in to change notification settings - Fork 2
/
propertygraph2go.go
190 lines (166 loc) · 3.9 KB
/
propertygraph2go.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
package propertygraph2go
import (
"encoding/json"
"errors"
"fmt"
)
type Key interface{}
var ErrKeyNotFound = errors.New("Key not found")
type VertexWalker func(v Vertex)
type EdgeWalker func(e Edge)
type Graph interface {
CreateVertex(key Key) (v Vertex, err error)
GetVertex(key Key) (v Vertex, err error)
WalkVertices(VertexWalker) (err error)
RemoveVertex(key Key) (err error)
CreateEdge(k Key, label string, head, tail Key) (e Edge, err error)
GetEdge(k Key) (e Edge, err error)
WalkEdges(EdgeWalker) (err error)
RemoveEdge(k Key) (err error)
WithProperties
}
type Edge interface {
Key() (k Key)
Label() (label string)
Head() (v Vertex)
Tail() (v Vertex)
WithProperties
}
type Vertex interface {
Key() (k Key)
Incoming() (incoming []Edge)
Outgoing() (Outgoing []Edge)
WithProperties
}
func StringVertex(v Vertex) (s string) {
s = fmt.Sprintf("Vertex (%s)", v.Key())
s = fmt.Sprintf("%s\n\tProperties:", s)
props, _ := v.Properties()
for n, v := range props {
s = fmt.Sprintf("%s {%s: %v}", s, n, v)
}
s = fmt.Sprintf("%s\n\tIncoming:", s)
for _, e := range v.Incoming() {
s = fmt.Sprintf("%s {%s %s: %s -> %s}", s, e.Label(), e.Key(), e.Tail().Key(), e.Head().Key())
}
s = fmt.Sprintf("%s\n\tOutgoing:", s)
for _, e := range v.Outgoing() {
s = fmt.Sprintf("%s {%s %s: %s -> %s}", s, e.Label(), e.Key(), e.Tail().Key(), e.Head().Key())
}
return
}
func PrintVertex(v Vertex) {
fmt.Println(StringVertex(v))
}
func PrintVertices(vs ...Vertex) {
for _, v := range vs {
PrintVertex(v)
}
}
func PrintAllVertices(g Graph) {
g.WalkVertices(PrintVertex)
}
type EncodingFunc func(interface{}) ([]byte, error)
type DecodingFunc func([]byte, interface{}) error
type EncodedVertex struct {
Key Key
Properties map[string]interface{}
}
type EncodedEdge struct {
Key Key
Label string
Properties map[string]interface{}
Head Key
Tail Key
}
func EncodeVertexJSON(e Vertex) (data []byte, err error) { return EncodeVertex(json.Marshal, e) }
func EncodeVertex(enc EncodingFunc, v Vertex) (data []byte, err error) {
p, err := v.Properties()
if err != nil {
return
}
ev := EncodedVertex{
Key: v.Key(),
Properties: p,
}
return enc(ev)
}
func DecodeVertexJSON(g Graph, data []byte) (v Vertex, err error) {
return DecodeVertex(json.Unmarshal, g, data)
}
func DecodeVertex(dec DecodingFunc, g Graph, data []byte) (v Vertex, err error) {
var ev EncodedVertex
err = dec(data, &ev)
if err != nil {
return
}
v, err = g.CreateVertex(ev.Key)
if err != nil {
return
}
for k, val := range ev.Properties {
err = v.SetProperty(k, val)
if err != nil {
return
}
}
return
}
func EncodeEdgeJSON(e Edge) (data []byte, err error) { return EncodeEdge(json.Marshal, e) }
func EncodeEdge(enc EncodingFunc, e Edge) (data []byte, err error) {
p, err := e.Properties()
if err != nil {
return
}
ee := EncodedEdge{
Key: e.Key(),
Label: e.Label(),
Properties: p,
Head: e.Head().Key(),
Tail: e.Tail().Key(),
}
return enc(ee)
}
func DecodeEdgeJSON(g Graph, data []byte) (e Edge, err error) {
return DecodeEdge(json.Unmarshal, g, data)
}
func DecodeEdge(dec DecodingFunc, g Graph, data []byte) (e Edge, err error) {
var ee EncodedEdge
err = dec(data, &ee)
if err != nil {
return
}
e, err = g.CreateEdge(ee.Key, ee.Label, ee.Head, ee.Tail)
if err != nil {
return
}
for k, v := range ee.Properties {
err = e.SetProperty(k, v)
if err != nil {
return
}
}
return
}
type KeyToPath func(Key) string
func EncodeGraph(enc EncodingFunc, g Graph) (vs map[Key][]byte, es map[Key][]byte) {
vs = map[Key][]byte{}
walkVertices := func(v Vertex) {
sv, err := EncodeVertex(enc, v)
if err != nil {
panic(err)
}
vs[v.Key()] = sv
}
es = map[Key][]byte{}
walkEdges := func(e Edge) {
se, err := EncodeEdge(enc, e)
if err != nil {
panic(err)
}
es[e.Key()] = se
}
g.WalkVertices(walkVertices)
g.WalkEdges(walkEdges)
return
}