-
Notifications
You must be signed in to change notification settings - Fork 0
/
valueset.go
395 lines (378 loc) · 10.9 KB
/
valueset.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package neo4j
import (
"context"
"fmt"
"io"
"reflect"
"sort"
"strings"
"github.com/cloudprivacylabs/lpg/v2"
"github.com/cloudprivacylabs/lsa/pkg/ls"
"github.com/fatih/structs"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
)
type NodesetInput interface {
ColumnNames() []string
Next() ([]string, error)
Reset() error
}
type Nodeset struct {
ID string
Labels []string
Properties map[string]interface{}
// Data[x] gives NodesetData with Nodeset.ID = x
Data map[string]NodesetData
}
type NodesetData struct {
ID string
Labels []string
Properties map[string]interface{}
}
func ParseNodesetData(cfg Config, input NodesetInput) (map[string]Nodeset, error) {
processed := false
ret := make(map[string]Nodeset)
getColumn := func(row []string, header string) string {
for cIdx, c := range input.ColumnNames() {
if c == header && cIdx < len(row) {
return row[cIdx]
}
}
return ""
}
for {
if err := input.Reset(); err != nil {
return nil, err
}
var currNodesetID string
var nodeset Nodeset
for {
row, err := input.Next()
if err == io.EOF {
if currNodesetID != "" {
ret[currNodesetID] = nodeset
} else {
processed = false
}
currNodesetID = ""
break
}
nodesetId := getColumn(row, "nodeset_id")
if nodesetId == "" {
continue
}
if _, seen := ret[nodesetId]; seen {
continue
}
currNodesetID = nodesetId
nodeset = Nodeset{
ID: currNodesetID,
Labels: []string{"NODESET"},
Properties: cfg.ShortenProperties(map[string]interface{}{cfg.Shorten(ls.EntityIDTerm): currNodesetID}),
Data: make(map[string]NodesetData),
}
// Process a new nodeset, parse row and add properties nodeset
parseRow := func(row []string) error {
cid := getColumn(row, "concept_id")
if len(cid) == 0 {
return nil
}
data := NodesetData{
ID: cid,
Properties: map[string]interface{}{cfg.Shorten(ls.EntityIDTerm): cid},
}
labels := lpg.NewStringSet(strings.Fields(getColumn(row, "node_labels"))...)
if labels.Len() == 0 {
labels.Add("CONCEPT")
}
data.Labels = labels.Slice()
for idx := 0; idx < len(input.ColumnNames()); idx++ {
header := input.ColumnNames()[idx]
if header != "node_labels" && header != "concept_id" && header != "nodeset_id" {
data.Properties[cfg.Shorten(input.ColumnNames()[idx])] = row[idx]
}
}
nodeset.Data[cid] = data
return nil
}
if err := parseRow(row); err != nil {
return nil, err
}
processed = true
for {
r, err := input.Next()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
// continue scanning with only with the same nodesetID
nodesetId = getColumn(r, "nodeset_id")
if currNodesetID != "" && nodesetId != currNodesetID {
continue
}
if err := parseRow(r); err != nil {
return nil, err
}
}
}
if !processed {
break
}
}
return ret, nil
}
type rootOpType int
const (
rootOpInsert rootOpType = iota
rootOpUpdate
rootOpDelete
)
// oldNodeset is nodeset pulled from DB
func NodesetDiff(oldNodeset, newNodeset Nodeset) (rootOp rootOpType, insertions []NodesetData, deletions []NodesetData, updates []NodesetData) {
// if DB nodeset does not exists, insert new nodeset
if oldNodeset.ID == "" {
rootOp = rootOpInsert
} else {
// delete DB nodeset if newNodeset is empty
if newNodeset.ID == "" {
rootOp = rootOpDelete
} else {
// update to use newNodeset if oldNodeset is not equal
rootOp = rootOpUpdate
}
}
for oid, oldNsData := range oldNodeset.Data {
newNsData, exists := newNodeset.Data[oid]
if exists {
update := NodesetData{
ID: oid,
Labels: make([]string, 0),
Properties: make(map[string]interface{}),
}
changed := false
if !lpg.NewStringSet(oldNsData.Labels...).IsEqual(lpg.NewStringSet(newNsData.Labels...)) {
// update - setting labels to those in newNodeset
update.Labels = newNsData.Labels
changed = true
} else {
update.Labels = oldNsData.Labels
}
// if props are not equal, update to use newNodeset properties
if !reflect.DeepEqual(oldNsData.Properties, newNsData.Properties) {
// Keep the old, add the new
for k, v := range oldNsData.Properties {
update.Properties[k] = v
}
for k, v := range newNsData.Properties {
update.Properties[k] = v
}
changed = true
}
if changed {
updates = append(updates, update)
}
} else {
deletions = append(deletions, oldNsData)
}
}
for nid, newNsData := range newNodeset.Data {
_, exists := oldNodeset.Data[nid]
if !exists {
insertions = append(insertions, newNsData)
}
}
return rootOp, insertions, deletions, updates
}
func LoadNodeset(ctx context.Context, cfg Config, tx neo4j.ExplicitTransaction, nodesetId string) (Nodeset, error) {
query := fmt.Sprintf("MATCH (root:`NODESET` {`%s`: %s})-[:%s]->(m) return root,m", cfg.Shorten(ls.EntityIDTerm), quoteStringLiteral(nodesetId), nodesetId)
idrec, err := tx.Run(ctx, query, map[string]interface{}{})
if err != nil {
return Nodeset{}, err
}
ns := Nodeset{
Labels: make([]string, 0),
Data: make(map[string]NodesetData),
Properties: make(map[string]any),
}
first := true
for idrec.Next(ctx) {
rec := idrec.Record()
if first {
first = false
nsCenter := rec.Values[0].(neo4j.Node)
for k, v := range nsCenter.Props {
if k == cfg.Shorten(ls.EntityIDTerm) {
ns.ID = v.(string)
ns.Properties[k] = ns.ID
}
}
ns.Labels = nsCenter.Labels
}
nsLeaf := rec.Values[1].(neo4j.Node)
var nsLeafEntityId string
for k, v := range nsLeaf.Props {
if k == cfg.Shorten(ls.EntityIDTerm) {
nsLeafEntityId = v.(string)
}
}
nsData := NodesetData{
ID: nsLeafEntityId,
Labels: nsLeaf.Labels,
Properties: make(map[string]any),
}
for k, v := range nsLeaf.Props {
nsData.Properties[k] = v
}
ns.Data[nsLeafEntityId] = nsData
}
return ns, nil
}
func buildProps(props map[string]interface{}, vars map[string]interface{}) string {
out := strings.Builder{}
first := true
for k, v := range props {
if v == nil {
continue
}
if first {
out.WriteRune('{')
first = false
} else {
out.WriteRune(',')
}
out.WriteString(quoteBacktick(k))
out.WriteRune(':')
out.WriteRune('$')
tname := fmt.Sprintf("p%d", len(vars))
out.WriteString(tname)
_, ok := v.(string)
if !ok {
vars[tname] = v.([]string)
} else {
vars[tname] = v.(string)
}
}
if !first {
out.WriteRune('}')
}
return out.String()
}
func Execute(ctx *ls.Context, tx neo4j.ExplicitTransaction, cfg Config, oldNodeset, newNodeset Nodeset, rootOp rootOpType, inserts, updates, deletes []NodesetData) error {
type groupedNodesetData struct {
labelExpr string
data []NodesetData
}
groupByLabel := func(nodesetData []NodesetData) []groupedNodesetData {
ret := make([]groupedNodesetData, 0)
hm := make(map[string][]NodesetData)
for _, nsD := range nodesetData {
nsD.Labels = sort.StringSlice(nsD.Labels)
labelExpr := cfg.MakeLabels(nsD.Labels)
hm[labelExpr] = append(hm[labelExpr], nsD)
}
for expr, nsData := range hm {
ret = append(ret, groupedNodesetData{labelExpr: expr, data: nsData})
}
return ret
}
mapNodesetData := func(nodesetData []NodesetData) []map[string]interface{} {
ans := make([]map[string]interface{}, len(nodesetData))
for ix, item := range nodesetData {
ans[ix] = structs.Map(item)
}
return ans
}
updateNodes := func() error {
if len(updates) > 0 {
for _, update := range groupByLabel(updates) {
unwindData := map[string]interface{}{"nodes": mapNodesetData(update.data)}
query := fmt.Sprintf("unwind $nodes as node MATCH (n%s) WHERE n.`%s` = node.ID SET n=node.Properties, n%s", update.labelExpr, cfg.Shorten(ls.EntityIDTerm), update.labelExpr)
if _, err := tx.Run(ctx, query, unwindData); err != nil {
return err
}
}
}
return nil
}
insertNodes := func() error {
if len(inserts) > 0 {
for _, insert := range groupByLabel(inserts) {
unwindData := map[string]interface{}{"nodes": mapNodesetData(insert.data)}
query := fmt.Sprintf("UNWIND $nodes AS node MERGE (n%s {`%s`: node.ID}) SET n=node.Properties",
insert.labelExpr, cfg.Shorten(ls.EntityIDTerm))
if _, err := tx.Run(ctx, query, unwindData); err != nil {
return err
}
}
}
return nil
}
deleteNodes := func() error {
if len(deletes) > 0 {
if _, err := tx.Run(ctx, fmt.Sprintf("MATCH (n %s) WHERE n.`%s` IN $deletes DETACH DELETE n", cfg.MakeLabels(oldNodeset.Labels), cfg.Shorten(ls.EntityIDTerm)), map[string]interface{}{"deletes": mapNodesetData(deletes)}); err != nil {
return err
}
}
return nil
}
insertEdges := func() error {
newNodesetData := []NodesetData{}
for _, n := range newNodeset.Data {
newNodesetData = append(newNodesetData, NodesetData{
ID: n.ID,
Labels: n.Labels,
Properties: n.Properties,
})
}
// create edges, merge with valueset root
for _, n := range groupByLabel(newNodesetData) {
unwindData := map[string]interface{}{"nodes": mapNodesetData(n.data)}
query := fmt.Sprintf("UNWIND $nodes AS node MERGE (n%s {`%s`: node.ID}) WITH n MATCH(root:`NODESET` {`%s`: %s}) MERGE(root)-[:%s]->(n) MERGE(n)-[:%s]->(root)",
n.labelExpr, cfg.Shorten(ls.EntityIDTerm), cfg.Shorten(ls.EntityIDTerm), quoteStringLiteral(newNodeset.ID), newNodeset.ID, newNodeset.ID)
if _, err := tx.Run(ctx, query, unwindData); err != nil {
return err
}
}
return nil
}
if rootOp == rootOpDelete {
q := fmt.Sprintf("MATCH (n %s {`%s`: $eid})-[r:%s]->(), (n %s {`%s`: $eid})<-[x:%s]-() DELETE r, x",
cfg.MakeLabels(oldNodeset.Labels), cfg.Shorten(ls.EntityIDTerm), oldNodeset.ID, cfg.MakeLabels(oldNodeset.Labels), cfg.Shorten(ls.EntityIDTerm), oldNodeset.ID)
if _, err := tx.Run(ctx, q, map[string]interface{}{"eid": oldNodeset.ID}); err != nil {
return err
}
q = fmt.Sprintf("MATCH (n %s {`%s`: $eid}) DETACH DELETE n",
cfg.MakeLabels(oldNodeset.Labels), cfg.Shorten(ls.EntityIDTerm))
if _, err := tx.Run(ctx, q, map[string]interface{}{"eid": oldNodeset.ID}); err != nil {
return err
}
if err := deleteNodes(); err != nil {
return err
}
return updateNodes()
}
switch rootOp {
case rootOpInsert:
vars := make(map[string]interface{})
if _, err := tx.Run(ctx, fmt.Sprintf("CREATE (n %s %s)", cfg.MakeLabels(newNodeset.Labels), buildProps(newNodeset.Properties, vars)), vars); err != nil {
return err
}
case rootOpUpdate:
stmt := fmt.Sprintf("MATCH (n %s) WHERE n.`%s` = $eid SET n=$props, n%s", cfg.MakeLabels(oldNodeset.Labels), cfg.Shorten(ls.EntityIDTerm), cfg.MakeLabels(newNodeset.Labels))
if _, err := tx.Run(ctx, stmt, map[string]interface{}{
"eid": oldNodeset.ID, "props": newNodeset.Properties}); err != nil {
return err
}
}
if err := insertNodes(); err != nil {
return err
}
if err := updateNodes(); err != nil {
return err
}
if err := deleteNodes(); err != nil {
return err
}
return insertEdges()
}