@@ -1087,6 +1087,54 @@ a powerful way in which to encapsulate upsert logics for vertices and edges. As
1087
1087
continue through the following sections you will find more advanced features with
1088
1088
these steps.
1089
1089
1090
+ Using 'inject' with 'mergeV'
1091
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1092
+
1093
+ In the "<<addinject>>" section, we saw how data could be injected to a traversal
1094
+ stream to create multiple vertices at a time with 'addV' . A similar tactic could be
1095
+ used with 'addE' to create multiple edges at a time. The pattern with 'inject' also
1096
+ extends to 'mergeV' and 'mergeE' . In the most simple form, 'mergeV' will simply
1097
+ accept an incoming 'Map' traverser as its argument.
1098
+
1099
+ [source,groovy]
1100
+ ----
1101
+ m = [[code: 'IAD',country: 'US',desc: 'Washington Dulles International Airport', (T.label): 'airport'],
1102
+ [code: 'LTA',country: 'US',desc: 'Little Tiny Airport', (T.label): 'airport'],
1103
+ [code: 'DCA',country: 'US',desc: 'Ronald Reagan Washington National Airport', (T.label): 'airport']]
1104
+
1105
+ g.inject(m).unfold().mergeV().elementMap('code','country','desc')
1106
+
1107
+ [id:10,label:airport,country:US,code:IAD,desc:Washington Dulles International Airport]
1108
+ [id:41223,label:airport,country:US,code:LTA,desc:Little Tiny Airport]
1109
+ [id:7,label:airport,country:US,code:DCA,desc:Ronald Reagan Washington National Airport]
1110
+ ----
1111
+
1112
+ The prior example flattens the list of maps in "m", feeding them one at a time to
1113
+ 'mergeV' where it does a match on each of the keys returning those vertices it finds
1114
+ and creating those it doesn't. You could take a more advanced approach and perform
1115
+ different operations for the 'onCreate" and 'onMatch' options driven fully on the
1116
+ data supplied to 'inject' . In the next example, we form a list of maps, where each
1117
+ key refers to the search, create, or match 'Map' options that 'mergeV' expects. We
1118
+ can then use 'select' to grab the appropriate one and supply it as an argument to
1119
+ the step.
1120
+
1121
+ [source,groovy]
1122
+ ----
1123
+ m = [[search: [code: 'IAD', (T.label): 'airport'], create: [desc: 'Washington Dulles International Airport', country: 'US'], match: [elev: 313]],
1124
+ [search: [code: 'LTA',(T.label): 'airport'], create: [country: 'US',desc: 'Little Tiny Airport'], match: [elev: 555]],
1125
+ [search: [code: 'DCA', (T.label): 'airport'], create: [country: 'US',desc: 'Ronald Reagan Washington National Airport'], match: [elev: 14]]]
1126
+
1127
+ g.inject(m).unfold().
1128
+ mergeV(select('search')).
1129
+ option(onCreate, select('create')).
1130
+ option(onMatch, select('match')).
1131
+ elementMap('code','country','desc','elev')
1132
+
1133
+ [id:10,label:airport,country:US,code:IAD,elev:313,desc:Washington Dulles International Airport]
1134
+ id:82454,label:airport,country:US,code:LTA,desc:Little Tiny Airport]
1135
+ id:7,label:airport,country:US,code:DCA,elev:14,desc:Ronald Reagan Washington National Airport]
1136
+ ----
1137
+
1090
1138
Incorporating 'fail' with 'mergeV' or 'mergeE'
1091
1139
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1092
1140
0 commit comments