-
Notifications
You must be signed in to change notification settings - Fork 490
/
Create.java
197 lines (173 loc) · 9.52 KB
/
Create.java
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
package apoc.create;
import org.neo4j.procedure.*;
import apoc.get.Get;
import apoc.result.*;
import apoc.util.Util;
import org.neo4j.graphdb.*;
import org.neo4j.kernel.internal.GraphDatabaseAPI;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.LongStream;
import java.util.stream.Stream;
public class Create {
@Context
public GraphDatabaseService db;
@Procedure(mode = Mode.WRITE)
@Description("apoc.create.node(['Label'], {key:value,...}) - create node with dynamic labels")
public Stream<NodeResult> node(@Name("label") List<String> labelNames, @Name("props") Map<String, Object> props) {
return Stream.of(new NodeResult(setProperties(db.createNode(Util.labels(labelNames)),props)));
}
@Procedure(mode = Mode.WRITE)
@Description("apoc.create.addLabels( [node,id,ids,nodes], ['Label',...]) - adds the given labels to the node or nodes")
public Stream<NodeResult> addLabels(@Name("nodes") Object nodes, @Name("label") List<String> labelNames) {
Label[] labels = Util.labels(labelNames);
return new Get((GraphDatabaseAPI) db).nodes(nodes).map((r) -> {
Node node = r.node;
for (Label label : labels) {
node.addLabel(label);
}
return r;
});
}
@Procedure(mode = Mode.WRITE)
@Description("apoc.create.setProperty( [node,id,ids,nodes], key, value) - sets the given property on the node(s)")
public Stream<NodeResult> setProperty(@Name("nodes") Object nodes, @Name("key") String key, @Name("value") Object value) {
return new Get((GraphDatabaseAPI) db).nodes(nodes).map((r) -> {
r.node.setProperty(key,value);
return r;
});
}
@Procedure(mode = Mode.WRITE)
@Description("apoc.create.setRelProperty( [rel,id,ids,rels], key, value) - sets the given property on the relationship(s)")
public Stream<RelationshipResult> setRelProperty(@Name("relationships") Object rels, @Name("key") String key, @Name("value") Object value) {
return new Get((GraphDatabaseAPI) db).rels(rels).map((r) -> {
r.rel.setProperty(key,value);
return r;
});
}
@Procedure(mode = Mode.WRITE)
@Description("apoc.create.setProperties( [node,id,ids,nodes], [keys], [values]) - sets the given property on the nodes(s)")
public Stream<NodeResult> setProperties(@Name("nodes") Object nodes, @Name("keys") List<String> keys, @Name("values") List<Object> values) {
return new Get((GraphDatabaseAPI) db).nodes(nodes).map((r) -> {
setProperties(r.node, Util.mapFromLists(keys,values));
return r;
});
}
@Procedure(mode = Mode.WRITE)
@Description("apoc.create.setRelProperties( [rel,id,ids,rels], [keys], [values]) - sets the given property on the relationship(s)")
public Stream<RelationshipResult> setRelProperties(@Name("rels") Object rels, @Name("keys") List<String> keys, @Name("values") List<Object> values) {
return new Get((GraphDatabaseAPI) db).rels(rels).map((r) -> {
setProperties(r.rel, Util.mapFromLists(keys,values));
return r;
});
}
@Procedure(mode = Mode.WRITE)
@Description("apoc.create.setLabels( [node,id,ids,nodes], ['Label',...]) - sets the given labels, non matching labels are removed on the node or nodes")
public Stream<NodeResult> setLabels(@Name("nodes") Object nodes, @Name("label") List<String> labelNames) {
Label[] labels = Util.labels(labelNames);
return new Get((GraphDatabaseAPI) db).nodes(nodes).map((r) -> {
Node node = r.node;
for (Label label : node.getLabels()) {
if (labelNames.contains(label.name())) continue;
node.removeLabel(label);
}
for (Label label : labels) {
if (node.hasLabel(label)) continue;
node.addLabel(label);
}
return r;
});
}
@Procedure(mode = Mode.WRITE)
@Description("apoc.create.removeLabels( [node,id,ids,nodes], ['Label',...]) - removes the given labels from the node or nodes")
public Stream<NodeResult> removeLabels(@Name("nodes") Object nodes, @Name("label") List<String> labelNames) {
Label[] labels = Util.labels(labelNames);
return new Get((GraphDatabaseAPI) db).nodes(nodes).map((r) -> {
Node node = r.node;
for (Label label : labels) {
node.removeLabel(label);
}
return r;
});
}
@Procedure(mode = Mode.WRITE)
@Description("apoc.create.nodes(['Label'], [{key:value,...}]) create multiple nodes with dynamic labels")
public Stream<NodeResult> nodes(@Name("label") List<String> labelNames, @Name("props") List<Map<String, Object>> props) {
Label[] labels = Util.labels(labelNames);
return props.stream().map(p -> new NodeResult(setProperties(db.createNode(labels), p)));
}
@Procedure(mode = Mode.WRITE)
@Description("apoc.create.relationship(person1,'KNOWS',{key:value,...}, person2) create relationship with dynamic rel-type")
public Stream<RelationshipResult> relationship(@Name("from") Node from,
@Name("relType") String relType, @Name("props") Map<String, Object> props,
@Name("to") Node to) {
return Stream.of(new RelationshipResult(setProperties(from.createRelationshipTo(to,RelationshipType.withName(relType)),props)));
}
@Procedure
@Description("apoc.create.vNode(['Label'], {key:value,...}) returns a virtual node")
public Stream<NodeResult> vNode(@Name("label") List<String> labelNames, @Name("props") Map<String, Object> props) {
Label[] labels = Util.labels(labelNames);
return Stream.of(new NodeResult(new VirtualNode(labels, props, db)));
}
@Procedure
@Description("apoc.create.vNodes(['Label'], [{key:value,...}]) returns virtual nodes")
public Stream<NodeResult> vNodes(@Name("label") List<String> labelNames, @Name("props") List<Map<String, Object>> props) {
Label[] labels = Util.labels(labelNames);
return props.stream().map(p -> new NodeResult(new VirtualNode(labels, p, db)));
}
@Procedure
@Description("apoc.create.vRelationship(nodeFrom,'KNOWS',{key:value,...}, nodeTo) returns a virtual relationship")
public Stream<RelationshipResult> vRelationship(@Name("from") Node from, @Name("relType") String relType, @Name("props") Map<String, Object> props, @Name("to") Node to) {
RelationshipType type = RelationshipType.withName(relType);
return Stream.of(new RelationshipResult(new VirtualRelationship(from,to,type).withProperties(props)));
}
@Procedure
@Description("apoc.create.vPattern({_labels:['LabelA'],key:value},'KNOWS',{key:value,...}, {_labels:['LabelB'],key:value}) returns a virtual pattern")
public Stream<VirtualPathResult> vPattern(@Name("from") Map<String,Object> n,
@Name("relType") String relType, @Name("props") Map<String, Object> props,
@Name("to") Map<String,Object> m) {
n = new LinkedHashMap<>(n); m=new LinkedHashMap<>(m);
RelationshipType type = RelationshipType.withName(relType);
VirtualNode from = new VirtualNode(Util.labels(n.remove("_labels")), n, db);
VirtualNode to = new VirtualNode(Util.labels(m.remove("_labels")), m, db);
Relationship rel = new VirtualRelationship(from, to, RelationshipType.withName(relType)).withProperties(props);
return Stream.of(new VirtualPathResult(from, rel, to));
}
@Procedure
@Description("apoc.create.vPatternFull(['LabelA'],{key:value},'KNOWS',{key:value,...},['LabelB'],{key:value}) returns a virtual pattern")
public Stream<VirtualPathResult> vPatternFull(@Name("labelsN") List<String> labelsN, @Name("n") Map<String,Object> n,
@Name("relType") String relType, @Name("props") Map<String, Object> props,
@Name("labelsM") List<String> labelsM, @Name("m") Map<String,Object> m) {
RelationshipType type = RelationshipType.withName(relType);
VirtualNode from = new VirtualNode(Util.labels(labelsN), n, db);
VirtualNode to = new VirtualNode(Util.labels(labelsM), m, db);
Relationship rel = new VirtualRelationship(from, to, type).withProperties(props);
return Stream.of(new VirtualPathResult(from,rel,to));
}
private <T extends PropertyContainer> T setProperties(T pc, Map<String, Object> p) {
if (p == null) return pc;
for (Map.Entry<String, Object> entry : p.entrySet()) pc.setProperty(entry.getKey(), entry.getValue());
return pc;
}
@Procedure
@Description("apoc.create.uuid yield uuid - creates an UUID")
public Stream<UUIDResult> uuid() {
return Stream.of(new UUIDResult(0));
}
@Procedure
@Description("apoc.create.uuids(count) yield uuid - creates 'count' UUIDs ")
public Stream<UUIDResult> uuids(@Name("count") long count) {
return LongStream.range(0,count).mapToObj(UUIDResult::new);
}
public static class UUIDResult {
public final long row;
public final String uuid;
public UUIDResult(long row) {
this.row = row;
this.uuid = UUID.randomUUID().toString();
// TODO Long.toHexString(uuid.getMostSignificantBits())+Long.toHexString(uuid.getLeastSignificantBits());
}
}
}