-
Notifications
You must be signed in to change notification settings - Fork 0
/
j2n.go
280 lines (256 loc) · 6.95 KB
/
j2n.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package json2neo
import (
"fmt"
"github.com/johnnadratowski/golang-neo4j-bolt-driver"
"reflect"
"strings"
"sync"
"time"
)
/*
TODO::::: help haye LINT ro ejra konam
http://go-lint.appspot.com/github.com/HamiTrip/json2neo
TODO:: write tests and benchmarks
TODO:: support for struct!
TODO:: if wrote a method for get json instantly, must check json validity
TODO:: an update model that don't have to delete nodes and lost node IDs:
for example update each node with id! addition to normal update model
*/
/*
J2N is Json to Neo4j interface
*/
type J2N interface {
SetStubNode(nodeID int64) J2N
SetRootLabel(sl string) J2N
SetRootName(n string) J2N
SetConn(conn golangNeo4jBoltDriver.Conn) J2N
Insert(data interface{}) (id int64, count int)
Submit(data interface{}) (id int64, count int)
execCypher(cypherPart string) interface{}
cypherGenerator()
createNested(nodeKey interface{}, parentVar, parentType, nodeType, nodeVar string, properties interface{}, parentChan chan int)
}
type j2n struct {
sync.Mutex
sync.WaitGroup
totalNodes int
neoConn golangNeo4jBoltDriver.Conn
hasConn bool
data interface{}
dataType string
rootID int64
rootName string
rootLabel string
hasStub bool
stubCypher string
}
func (j2n *j2n) SetConn(conn golangNeo4jBoltDriver.Conn) J2N {
j2n.neoConn, j2n.hasConn = conn, true
return j2n
}
func (j2n *j2n) SetStubNode(nodeID int64) J2N {
j2n.stubCypher, j2n.hasStub = fmt.Sprintf("MATCH (%s) WHERE ID(%s) = %d\n", VarStub, VarStub, nodeID), true
return j2n
}
func (j2n *j2n) SetRootLabel(rl string) J2N {
if strings.Contains(rl, ":") {
panic("Only one lable are accepted!")
}
j2n.rootLabel = fmt.Sprintf(":%s", strings.ToUpper(rl))
return j2n
}
func (j2n *j2n) SetRootName(n string) J2N {
j2n.rootName = n
return j2n
}
func (j2n *j2n) Submit(data interface{}) (id int64, count int) {
return j2n.Insert(data)
}
func (j2n *j2n) Insert(data interface{}) (id int64, count int) {
if !j2n.hasConn {
panic("Neo4j connection not found!")
}
j2n.data = data
switch data.(type) {
case []interface{}:
j2n.dataType = TypeArray
case map[string]interface{}:
j2n.dataType = TypeObject
default:
panic(fmt.Sprintf("Only '[]interface{}' and 'map[string]interface{}' are accepted, given: '%T'", data))
}
j2n.cypherGenerator()
j2n.Wait()
return j2n.rootID, j2n.totalNodes
}
func (j2n *j2n) execCypher(cypherPart string) (res interface{}) {
if strings.TrimSpace(cypherPart) != "" {
j2n.Lock()
result, err := j2n.neoConn.QueryNeo(cypherPart, map[string]interface{}{}) // TODO::Neo.ClientError.Schema.ConstraintValidationFailed // recoveram nemishe!!!
if err == nil {
r, _, _ := result.All()
res = r[0][0]
result.Close()
j2n.totalNodes++
} else {
switch err.(type){
/*case *errors.Error:
if err.(*errors.Error).Inner().(messages.FailureMessage).Metadata["code"].(string) == "Neo.ClientError.Schema.ConstraintValidationFailed" {
}*/
default:
panic(err)
}
}
j2n.Unlock()
}
return
}
func (j2n *j2n) cypherGenerator() {
//TODO:: Pipeline!
var typeLabel string
switch j2n.dataType {
case TypeArray:
typeLabel = LabelArrProp
case TypeObject:
typeLabel = LabelObjProp
}
var sfc string
var c = make(chan int, 1)
var fc = j2n.getFieldsCypherPart(j2n.data, VarRoot, j2n.dataType, c)
if len(fc) > 0 {
sfc = "," + strings.Join(fc, ",")
}
var cypher = fmt.Sprintf(
"%s\n CREATE %s(%s%s:%s:%s:%s {%s%s}) RETURN ID(%s)\n",
j2n.stubCypher,
j2n.getStubCypherPart(),
VarRoot,
j2n.rootLabel,
typeLabel,
LabelRootNode,
DefaultLabel,
j2n.getRootNameCypherPart(),
sfc,
VarRoot,
)
var nodeID = j2n.execCypher(cypher)
switch {
case nodeID == nil:
panic("Cannot create root node!")
default:
c <- int(nodeID.(int64))
j2n.rootID = nodeID.(int64)
fmt.Println("root_node_id:", nodeID, time.Now().Unix())
}
}
func (j2n *j2n) getStubCypherPart() (c string) {
if j2n.hasStub {
c = fmt.Sprintf("(%s)-[%s {%s:'%s'}]->", VarStub, j2n.rootLabel, RootNameKey, j2n.rootName)
}
return
}
func (j2n *j2n) getRootNameCypherPart() (c string) {
if strings.TrimSpace(j2n.rootName) != "" {
c = fmt.Sprintf("%s:'%s'",
RootNameKey,
j2n.rootName)
}
return
}
func (j2n *j2n) createNested(nodeKey interface{}, parentVar, parentType, nodeType, nodeVar string, properties interface{}, parentChan chan int) {
var nodeTypeLabel string
switch nodeType {
case TypeArray: //TODO:: null ro skip konam
nodeTypeLabel = LabelArrProp
case TypeObject:
nodeTypeLabel = LabelObjProp
}
var c = make(chan int, 1)
var sfc = strings.Join(j2n.getFieldsCypherPart(properties, nodeVar, nodeType, c), ",")
var cName string
if parentType == TypeObject {
cName = fmt.Sprintf(",name:'%v'", nodeKey)
} else if parentType == TypeArray {
cName = fmt.Sprintf(",name:'%v'", nodeKey)
}
parentNodeID := <-parentChan
parentChan <- parentNodeID
var parentCypher = fmt.Sprintf("MATCH (%s) WHERE ID(%s) = %d\n", parentVar, parentVar, parentNodeID)
var cypher = fmt.Sprintf(
"%s \n CREATE (%s)-[:%s {type:'%s'%s}]->(%s:%s:%s {%s}) RETURN ID(%s)\n",
parentCypher,
parentVar,
LabelHasNested,
nodeType,
cName,
nodeVar,
nodeTypeLabel,
DefaultLabel,
sfc,
nodeVar,
)
var nodeID = j2n.execCypher(cypher)
switch {
case nodeID == nil:
panic("Cannot create node: " + cypher)
default:
c <- int(nodeID.(int64))
j2n.Done()
}
}
func (j2n *j2n) getFieldsCypherPart(data interface{}, nodeVar, nodeType string, nodeChan chan int) (cy []string) {
switch nodeType {
case TypeObject:
for k, v := range data.(map[string]interface{}) {
if f, ok := j2n.makeField(k, v, nodeVar, nodeType, nodeChan); ok {
cy = append(cy, f)
}
}
case TypeArray:
for i, v := range data.([]interface{}) {
if f, ok := j2n.makeField(i, v, nodeVar, nodeType, nodeChan); ok {
cy = append(cy, f)
}
}
default:
panic(fmt.Sprintf("Only '[]interface{}' and 'map[string]interface{}' are accepted in getFieldsCypherPart, given: '%T'", data))
}
return
}
func (j2n *j2n) makeField(k, v interface{}, parentVar, parentType string, parentChan chan int) (f string, ok bool) {
switch v.(type) {
case string:
v = strings.Replace(v.(string), "'", "\\'", -1)
}
var nodeVar = strings.Replace(fmt.Sprintf("%s_%v", parentVar, k), "-", "_", -1)
switch reflect.ValueOf(v).Kind() {
case reflect.Array, reflect.Slice:
j2n.Add(1)
go j2n.createNested(k, parentVar, parentType, TypeArray, nodeVar, v, parentChan)
case reflect.Map:
j2n.Add(1)
go j2n.createNested(k, parentVar, parentType, TypeObject, nodeVar, v, parentChan)
case reflect.Struct:
panic(fmt.Sprintf("Only '[]interface{}' and 'map[string]interface{}' are accepted in nested values, given: '%T'", v))
default:
if v == nil {
v = ""
}
var key string
switch k.(type) {
case string:
key = k.(string)
key = strings.Replace(key, "-", "_", -1)
case int:
key = fmt.Sprintf("k_%v", k)
}
return fmt.Sprintf("%v:'%v'", key, v), true
}
return
}
/*
NewJ2N is J2N factory method
*/
func NewJ2N(conn golangNeo4jBoltDriver.Conn) J2N {
return new(j2n).SetConn(conn)
}