-
Notifications
You must be signed in to change notification settings - Fork 0
/
link.go
294 lines (281 loc) · 9.87 KB
/
link.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package neo4j
import (
"errors"
"fmt"
"github.com/cloudprivacylabs/lpg/v2"
"github.com/cloudprivacylabs/lsa/pkg/ls"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
)
type linkSpec struct {
targetEntity string
// The foreign key fields
fkFields []string
// The label of the link
label string
// If true, the link is from this entity to the target. If false,
// the link is from the target to this.
forward bool
// If the schema node is not a reference node, then this is the node
// that should receive the link
linkNode string
// If true, the reference can have more than one links
multi bool
node *lpg.Node
}
func getLinkSpec(docNode *lpg.Node) *linkSpec {
if docNode == nil {
return nil
}
ref := ls.AsPropertyValue(docNode.GetProperty(ls.ReferenceFKFor)).AsString()
if len(ref) == 0 {
return nil
}
ret := linkSpec{
targetEntity: ref,
label: ls.AsPropertyValue(docNode.GetProperty(ls.ReferenceLabelTerm)).AsString(),
multi: ls.AsPropertyValue(docNode.GetProperty(ls.ReferenceMultiTerm)).AsString() != "false",
node: docNode,
fkFields: ls.AsPropertyValue(docNode.GetProperty(ls.ReferenceFKTerm)).MustStringSlice(),
}
if len(ret.label) == 0 {
ret.label = ls.HasTerm
}
ret.linkNode = ls.AsPropertyValue(docNode.GetProperty(ls.ReferenceLinkNodeTerm)).AsString()
switch ls.AsPropertyValue(docNode.GetProperty(ls.ReferenceDirectionTerm)).AsString() {
case "to", "toTarget", "":
ret.forward = true
case "from", "fromTarget":
ret.forward = false
}
return &ret
}
func (spec linkSpec) getForeignKeys(entityRoot *lpg.Node) ([][]string, error) {
if len(spec.fkFields) == 0 {
v, _ := ls.GetRawNodeValue(spec.node)
return [][]string{{v}}, nil
}
// There can be multiple instances of a foreign key in an
// entity. ForeignKeyNdoes[i] keeps all the nodes for spec.FK[i]
foreignKeyNodes := make([][]*lpg.Node, len(spec.fkFields))
ls.IterateDescendants(entityRoot, func(n *lpg.Node) bool {
attrId := ls.AsPropertyValue(n.GetProperty(ls.SchemaNodeIDTerm)).AsString()
if len(attrId) == 0 {
return true
}
for i := range spec.fkFields {
if spec.fkFields[i] == attrId {
foreignKeyNodes[i] = append(foreignKeyNodes[i], n)
}
}
return true
}, ls.OnlyDocumentNodes, false)
// All foreign key elements must have the same number of elements, and no index must be skipped
var numKeys int
for index := 0; index < len(foreignKeyNodes); index++ {
if index == 0 {
numKeys = len(foreignKeyNodes[index])
} else {
if len(foreignKeyNodes[index]) != numKeys {
return nil, ls.ErrInvalidForeignKeys{Msg: "Inconsistent foreign keys"}
}
}
}
// foreignKeyNodes is organized as:
//
// 0 1 2
// fk0_key0 fk0_key1 fk0_key2 --> foreign key 1
// fk1_key0 fk1_key1 fk1_key2 --> foreign key 2
fks := make([][]string, numKeys)
for i := 0; i < numKeys; i++ {
for key := 0; key < len(spec.fkFields); key++ {
v, _ := ls.GetRawNodeValue(foreignKeyNodes[i][key])
fks[i] = append(fks[i], v)
}
}
return fks, nil
}
func LinkNodesForNewEntity(ctx *ls.Context, tx neo4j.ExplicitTransaction, session *Session, config Config, entityRoot *lpg.Node, nodeMap map[*lpg.Node]string) error {
if err := LinkEntitiesByForeignKeys(ctx, tx, session, config, entityRoot, nodeMap); err != nil {
return err
}
return AddLinksToThisEntity(ctx, tx, session, config, entityRoot, nodeMap)
}
func LinkEntitiesByForeignKeys(ctx *ls.Context, tx neo4j.ExplicitTransaction, session *Session, config Config, entityRoot *lpg.Node, nodeMap map[*lpg.Node]string) error {
links := make([]linkSpec, 0)
// Does the entity have any outstanding links we need to work on?
var itrErr error
ls.IterateDescendants(entityRoot, func(node *lpg.Node) bool {
if !node.GetLabels().Has(ls.DocumentNodeTerm) {
return true
}
spec := getLinkSpec(node)
if spec != nil {
links = append(links, *spec)
}
return true
}, func(edge *lpg.Edge) ls.EdgeFuncResult {
to := edge.GetTo()
// Edge must go to a document node
if !to.GetLabels().Has(ls.DocumentNodeTerm) {
return ls.SkipEdgeResult
}
// If edge goes to a different entity with ID, we should stop here
if _, ok := to.GetProperty(ls.EntitySchemaTerm); ok {
if _, ok := to.GetProperty(ls.EntityIDTerm); ok {
return ls.SkipEdgeResult
}
}
return ls.FollowEdgeResult
}, false)
if itrErr != nil {
return itrErr
}
for _, link := range links {
if err := linkEntities(ctx, tx, session, config, entityRoot, link, nodeMap); err != nil {
return err
}
}
return nil
}
func linkEntities(ctx *ls.Context, tx neo4j.ExplicitTransaction, session *Session, config Config, entityRoot *lpg.Node, spec linkSpec, nodeMap map[*lpg.Node]string) error {
foreignKeys, err := spec.getForeignKeys(entityRoot)
if err != nil {
return err
}
var linkToNode *lpg.Node
if len(spec.linkNode) > 0 {
ls.WalkNodesInEntity(entityRoot, func(n *lpg.Node) bool {
if ls.IsInstanceOf(n, spec.linkNode) {
linkToNode = n
return false
}
return true
})
}
if linkToNode == nil {
linkToNode = entityRoot
}
for _, fk := range foreignKeys {
var query string
params := make(map[string]interface{})
nodeLabelsClause := config.MakeLabels([]string{spec.targetEntity})
var fkVal *ls.PropertyValue
if len(fk) == 1 {
fkVal = ls.StringPropertyValue(ls.EntityIDTerm, fk[0])
} else {
fkVal = ls.StringSlicePropertyValue(ls.EntityIDTerm, fk)
}
nodePropertiesClause := config.MakeProperties(mapWithProperty(map[string]interface{}{
ls.EntityIDTerm: fkVal,
}), params)
if spec.forward {
query = fmt.Sprintf(`match (target %s %s) with target match (source) where %s create (source)-[%s]->(target)`, nodeLabelsClause, nodePropertiesClause, session.IDEqValueFunc("source", nodeMap[linkToNode]), config.MakeLabels([]string{spec.label}))
} else {
query = fmt.Sprintf(`match (source %s %s) with source match (target) where %s create (source)-[%s]->(target)`, nodeLabelsClause, nodePropertiesClause, session.IDEqValueFunc("target", nodeMap[linkToNode]), config.MakeLabels([]string{spec.label}))
}
ctx.GetLogger().Debug(map[string]interface{}{"linkEntity": query, "params": params})
_, err := tx.Run(ctx, query, params)
if err != nil {
return err
}
}
return nil
}
// ID is entity id of entity root
func AddLinksToThisEntity(ctx *ls.Context, tx neo4j.ExplicitTransaction, session *Session, config Config, entityRoot *lpg.Node, nodeMap map[*lpg.Node]string) error {
// Are there any links pointing to this entity in the DB?
idTerm, ok := entityRoot.GetProperty(ls.EntityIDTerm)
if !ok {
return nil
}
ID := ls.AsPropertyValue(idTerm, ok).MustStringSlice()
if len(ID) == 0 {
return nil
}
vars := make(map[string]interface{})
_, ok = entityRoot.GetProperty(ls.EntitySchemaTerm)
if !ok {
return errors.New("invalid schema node, given entity root must contain schema node id property")
}
var fkNodesRec neo4j.ResultWithContext
var err error
entityLabels := ls.FilterNonLayerTypes(entityRoot.GetLabels().Slice())
if len(ID) > 1 {
fkNodesRec, err = tx.Run(ctx, fmt.Sprintf(
"MATCH (n) WHERE n.`%s` IN $labels MATCH (n) WHERE ALL(cmp IN $ids WHERE cmp IN SPLIT(n.`%s`, %s)) RETURN n",
config.Shorten(ls.ReferenceFKFor), config.Shorten(ls.ReferenceFK), quoteStringLiteral(",")),
map[string]interface{}{"ids": ID, "labels": entityLabels},
)
} else {
fkNodesRec, err = tx.Run(ctx, fmt.Sprintf(
"MATCH (n) WHERE n.`%s` IN $labels AND n.`%s` = $ids RETURN n",
config.Shorten(ls.ReferenceFKFor), config.Shorten(ls.ReferenceFK)),
map[string]interface{}{"ids": ID[0], "labels": entityLabels},
)
}
if err != nil {
return err
}
for fkNodesRec.Next(ctx) {
rec := fkNodesRec.Record()
fkNode := rec.Values[0].(neo4j.Node)
dirTo := false
if fkNode.Props[ls.ReferenceDirectionTerm] == "to" || fkNode.Props[ls.ReferenceDirectionTerm] == "toTarget" {
dirTo = true
}
// if fkNode is entity root, connect directly to new node
if _, ok := fkNode.Props[ls.EntitySchemaTerm]; ok {
if dirTo {
// MATCH (n), (m) WHERE ID(n) = %d AND m.`%s` = ID CREATE (n)-[%s]->(m)
_, err := tx.Run(ctx, fmt.Sprintf("MATCH (n) MATCH (m) WHERE %s AND %s CREATE (n)-[%s]->(m)", session.IDEqValueFunc("n", fkNode.ElementId), session.IDEqValueFunc("m", nodeMap[entityRoot]), config.MakeLabels([]string{ls.HasTerm})), vars)
if err != nil {
return err
}
} else {
_, err := tx.Run(ctx, fmt.Sprintf("MATCH (n) MATCH (m) WHERE %s AND %s CREATE (n)<-[%s]-(m)", session.IDEqValueFunc("n", fkNode.ElementId), session.IDEqValueFunc("m", nodeMap[entityRoot]), config.MakeLabels([]string{ls.HasTerm})), vars)
if err != nil {
return err
}
}
} else {
// otherwise find nearest entity node
const MAX_DEPTH = 10
var depth int = 1
for {
if depth >= MAX_DEPTH {
return errors.New("cannot find entity node")
}
eNodesRec, err := tx.Run(ctx, fmt.Sprintf("MATCH (n)<-[*%d]-(m) WHERE %s AND m.`%s` IS NOT NULL RETURN m", depth, session.IDEqValueFunc("n", fkNode.ElementId), ls.EntitySchemaTerm), vars)
if err != nil {
return err
}
singleRec, err := eNodesRec.Collect(ctx)
if err != nil {
return err
}
if len(singleRec) == 0 {
depth++
continue
}
if len(singleRec) > 1 {
return errors.New("mulitple entity nodes found")
}
eNode := singleRec[0].Values[0].(neo4j.Node)
// connect found entity root to new node
if dirTo {
_, err := tx.Run(ctx, fmt.Sprintf("MATCH (n) MATCH (m) WHERE %s AND %s CREATE (n)-[%s]->(m)", session.IDEqValueFunc("n", eNode.ElementId), session.IDEqValueFunc("m", nodeMap[entityRoot]), config.MakeLabels([]string{ls.HasTerm})), vars)
if err != nil {
return err
}
} else {
_, err := tx.Run(ctx, fmt.Sprintf("MATCH (n) MATCH (m) WHERE %s AND %s CREATE (n)<-[%s]-(m)", session.IDEqValueFunc("n", eNode.ElementId), session.IDEqValueFunc("m", nodeMap[entityRoot]), config.MakeLabels([]string{ls.HasTerm})), vars)
if err != nil {
return err
}
}
break
}
}
}
return nil
}