Skip to content

Commit 588264a

Browse files
committed
Refactoring
1 parent 17d2c17 commit 588264a

3 files changed

Lines changed: 17 additions & 329 deletions

File tree

neo4j-json/src/main/java/org/neo4j/helpers/json/document/impl/DocumentRelationBuilderHasTypeArrayKey.java

Lines changed: 3 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -19,156 +19,20 @@
1919

2020
package org.neo4j.helpers.json.document.impl;
2121

22-
import java.util.HashSet;
23-
import java.util.List;
24-
import java.util.Set;
25-
import java.util.stream.Collectors;
26-
import java.util.stream.StreamSupport;
27-
28-
import org.apache.commons.lang3.ArrayUtils;
29-
import org.neo4j.graphdb.Direction;
30-
import org.neo4j.graphdb.GraphDatabaseService;
3122
import org.neo4j.graphdb.Node;
32-
import org.neo4j.graphdb.Relationship;
33-
import org.neo4j.graphdb.RelationshipType;
34-
import org.neo4j.graphdb.Result;
35-
import org.neo4j.helpers.json.document.DocumentRelationBuilder;
3623
import org.neo4j.helpers.json.document.context.DocumentRelationContext;
37-
import org.neo4j.logging.Log;
3824

3925
/**
4026
* Build relationship with name "HAS_"+the type of destination node
4127
* ES: HAS_TRACKS
4228
* @author Omar Rampado
43-
* FIXME merge code with DocumentRelationBuilderTypeArrayKey
4429
*/
45-
public class DocumentRelationBuilderHasTypeArrayKey implements DocumentRelationBuilder {
46-
47-
private static final String DOC_KEYS = "docKeys";
48-
49-
private GraphDatabaseService db;
50-
51-
private Log log;
30+
public class DocumentRelationBuilderHasTypeArrayKey extends DocumentRelationBuilderTypeArrayKey {
5231

53-
/**
54-
* @param db the db to set
55-
*/
56-
public void setDb(GraphDatabaseService db) {
57-
this.db = db;
58-
}
59-
60-
/**
61-
* @param log the log to set
62-
*/
63-
public void setLog(Log log) {
64-
this.log = log;
65-
}
66-
67-
/* (non-Javadoc)
68-
* @see org.neo4j.helpers.json.document.DocumentRelationBuilder#buildRelation(org.neo4j.graphdb.Node, org.neo4j.graphdb.Node, org.neo4j.helpers.json.document.DocumentRelationContext)
69-
*/
7032
@Override
71-
public Relationship buildRelation(Node parent, Node child, DocumentRelationContext context) {
33+
protected String buildRelationName(Node parent, Node child, DocumentRelationContext context) {
7234
String childType = (String) child.getProperty("type");
73-
String relationName = "HAS_"+childType.toUpperCase();
74-
75-
RelationshipType type = RelationshipType.withName( relationName );
76-
77-
//check if already exists
78-
Iterable<Relationship> relationships = child.getRelationships(Direction.INCOMING,type);
79-
80-
Relationship relationship;
81-
82-
//find only relation between parent and child node
83-
List<Relationship> rels = StreamSupport.stream(relationships.spliterator(), false)
84-
.filter(rel -> rel.getStartNode().getId() == parent.getId())
85-
.collect(Collectors.toList());
86-
87-
if(rels.isEmpty())
88-
{
89-
relationship = parent.createRelationshipTo(child, type);
90-
if(log.isDebugEnabled())
91-
log.debug("Create new Relation "+relationship);
92-
}else
93-
{
94-
relationship = rels.get(0);
95-
if(log.isDebugEnabled())
96-
log.debug("Update Relation "+relationship);
97-
}
98-
99-
//manage array of keys
100-
101-
String[] keys = new String[0];
102-
103-
//create property if doesn't exists (new relation)
104-
if(relationship.getAllProperties().containsKey(DOC_KEYS))
105-
{
106-
keys = (String[]) relationship.getProperty(DOC_KEYS);
107-
}
108-
109-
//set document key into property
110-
String documentKey = context.getDocumentKey();
111-
if(! ArrayUtils.contains(keys, documentKey))
112-
{
113-
keys = ArrayUtils.add(keys, documentKey);
114-
relationship.setProperty(DOC_KEYS, keys);
115-
}
116-
117-
118-
return relationship;
119-
}
120-
121-
/* (non-Javadoc)
122-
* @see org.neo4j.helpers.json.document.DocumentRelationBuilder#deleteRelations(org.neo4j.helpers.json.document.DocumentRelationContext)
123-
*/
124-
@Override
125-
public Set<Node> deleteRelations(DocumentRelationContext context) {
126-
Set<Node> orphans = new HashSet<>();
127-
128-
Result result = db.execute("MATCH (p)-[r]->(c) WHERE \""+context.getDocumentKey()+"\" IN r."+DOC_KEYS+" RETURN r");
129-
result.forEachRemaining((res)->{
130-
Relationship rel = (Relationship) res.get("r");
131-
String[] keys = (String[]) rel.getProperty(DOC_KEYS);
132-
if(keys.length == 1)
133-
{
134-
Node parent = rel.getStartNode();
135-
Node child = rel.getEndNode();
136-
rel.delete();
137-
if(log.isDebugEnabled())
138-
{
139-
log.debug("Delete relation "+rel);
140-
}
141-
142-
updateOrphans(orphans, parent);
143-
updateOrphans(orphans, child);
144-
145-
}else
146-
{
147-
//if other document pass through this relationship
148-
keys = ArrayUtils.removeElement(keys, context.getDocumentKey());
149-
rel.setProperty(DOC_KEYS, keys);
150-
}
151-
});
152-
153-
return orphans;
35+
return "HAS_"+childType.toUpperCase();
15436
}
15537

156-
/**
157-
* Update orphans collection with node
158-
* @param orphans
159-
* @param node
160-
* @return true if node is orphan
161-
*/
162-
private boolean updateOrphans(Set<Node> orphans, Node node)
163-
{
164-
boolean orphan = ! node.getRelationships().iterator().hasNext();
165-
166-
if(orphan)
167-
{
168-
orphans.add(node);
169-
}
170-
171-
return orphan;
172-
}
173-
17438
}

neo4j-json/src/main/java/org/neo4j/helpers/json/document/impl/DocumentRelationBuilderTypeArrayKey.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,25 @@ public void setLog(Log log) {
6464
this.log = log;
6565
}
6666

67+
/**
68+
* Compose the type of the relationship
69+
* @param parent
70+
* @param child
71+
* @param context
72+
* @return
73+
*/
74+
protected String buildRelationName(Node parent, Node child, DocumentRelationContext context){
75+
String parentType = (String) parent.getProperty("type");
76+
String childType = (String) child.getProperty("type");
77+
return parentType+"_"+childType;
78+
}
79+
6780
/* (non-Javadoc)
6881
* @see org.neo4j.helpers.json.document.DocumentRelationBuilder#buildRelation(org.neo4j.graphdb.Node, org.neo4j.graphdb.Node, org.neo4j.helpers.json.document.DocumentRelationContext)
6982
*/
7083
@Override
7184
public Relationship buildRelation(Node parent, Node child, DocumentRelationContext context) {
72-
String parentType = (String) parent.getProperty("type");
73-
String childType = (String) child.getProperty("type");
74-
String relationName = parentType+"_"+childType;
85+
String relationName = buildRelationName(parent, child, context);
7586

7687
RelationshipType type = RelationshipType.withName( relationName );
7788

neo4j-json/src/test/java/org/neo4j/helpers/json/document/impl/DocumentRelationBuilderHasTypeArrayKeyTest.java

Lines changed: 0 additions & 187 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,13 @@
2020
package org.neo4j.helpers.json.document.impl;
2121

2222
import java.io.File;
23-
import java.util.Set;
24-
import java.util.stream.StreamSupport;
2523

2624
import org.junit.After;
2725
import org.junit.AfterClass;
2826
import org.junit.Assert;
2927
import org.junit.Before;
3028
import org.junit.BeforeClass;
3129
import org.junit.Test;
32-
import org.neo4j.graphdb.Direction;
3330
import org.neo4j.graphdb.GraphDatabaseService;
3431
import org.neo4j.graphdb.Node;
3532
import org.neo4j.graphdb.Relationship;
@@ -108,188 +105,4 @@ public void shuldCreateRelation() {
108105
Assert.assertEquals(1, keys.length);
109106
Assert.assertEquals("key", keys[0]);
110107
}
111-
112-
@Test
113-
public void shuldUpdateRelation() {
114-
context.setDocumentKey("key1");
115-
116-
Node parent = db.createNode();
117-
parent.setProperty("type", "album");
118-
119-
Node child = db.createNode();
120-
child.setProperty("type", "artist");
121-
122-
Relationship rel1 = this.docrel.buildRelation(parent, child, context);
123-
124-
context.setDocumentKey("key2");
125-
Relationship rel2 = this.docrel.buildRelation(parent, child, context);
126-
127-
Assert.assertEquals(rel1.getId(), rel2.getId());
128-
129-
String[] keys = (String[]) rel2.getProperty("docKeys");
130-
131-
Assert.assertEquals(2, keys.length);
132-
Assert.assertEquals("key1", keys[0]);
133-
Assert.assertEquals("key2", keys[1]);
134-
}
135-
136-
@Test
137-
public void shuldDoNothing() {
138-
context.setDocumentKey("key");
139-
140-
Node parent = db.createNode();
141-
parent.setProperty("type", "album");
142-
143-
Node child = db.createNode();
144-
child.setProperty("type", "artist");
145-
146-
Relationship rel1 = this.docrel.buildRelation(parent, child, context);
147-
Relationship rel2 = this.docrel.buildRelation(parent, child, context);
148-
149-
Assert.assertEquals(rel1.getId(), rel2.getId());
150-
151-
String[] keys = (String[]) rel2.getProperty("docKeys");
152-
153-
Assert.assertEquals(1, keys.length);
154-
Assert.assertEquals("key", keys[0]);
155-
}
156-
157-
@Test
158-
public void shouldDeleteNothing()
159-
{
160-
context.setDocumentKey("key");
161-
162-
Node parent = db.createNode();
163-
parent.setProperty("type", "album");
164-
165-
Node child = db.createNode();
166-
child.setProperty("type", "artist");
167-
168-
this.docrel.buildRelation(parent, child, context);
169-
170-
context.setDocumentKey("another_key");
171-
Set<Node> orphans = this.docrel.deleteRelations(context);
172-
173-
Assert.assertEquals(0, orphans.size());
174-
Assert.assertEquals(1, StreamSupport.stream(parent.getRelationships().spliterator(), false).count());
175-
}
176-
177-
@Test
178-
public void shouldDeleteNodeWithoutRelation()
179-
{
180-
context.setDocumentKey("key");
181-
182-
Node parent = db.createNode();
183-
parent.setProperty("type", "album");
184-
185-
Node child = db.createNode();
186-
child.setProperty("type", "artist");
187-
188-
this.docrel.buildRelation(parent, child, context);
189-
190-
context.setDocumentKey("another_key");
191-
Set<Node> orphans = this.docrel.deleteRelations(context);
192-
193-
Assert.assertEquals(0, orphans.size());
194-
Assert.assertEquals(1, StreamSupport.stream(parent.getRelationships().spliterator(), false).count());
195-
}
196-
197-
@Test
198-
public void shouldDeleteRelation()
199-
{
200-
context.setDocumentKey("key");
201-
202-
Node parent = db.createNode();
203-
parent.setProperty("type", "album");
204-
205-
Node child = db.createNode();
206-
child.setProperty("type", "artist");
207-
208-
this.docrel.buildRelation(parent, child, context);
209-
210-
Set<Node> orphans = this.docrel.deleteRelations(context);
211-
212-
Assert.assertEquals(2, orphans.size());
213-
Assert.assertEquals(0, StreamSupport.stream(parent.getRelationships().spliterator(), false).count());
214-
}
215-
216-
@Test
217-
public void shouldDeleteRelations()
218-
{
219-
context.setDocumentKey("key");
220-
221-
Node parent = db.createNode();
222-
parent.setProperty("type", "album");
223-
224-
Node child = db.createNode();
225-
child.setProperty("type", "artist");
226-
227-
Node track = db.createNode();
228-
track.setProperty("type", "track");
229-
230-
this.docrel.buildRelation(parent, child, context);
231-
this.docrel.buildRelation(child, track, context);
232-
233-
Set<Node> orphans = this.docrel.deleteRelations(context);
234-
235-
Assert.assertEquals(3, orphans.size());
236-
Assert.assertEquals(0, StreamSupport.stream(parent.getRelationships().spliterator(), false).count());
237-
Assert.assertEquals(0, StreamSupport.stream(child.getRelationships().spliterator(), false).count());
238-
}
239-
240-
@Test
241-
public void shouldDeleteRelationsOfKey()
242-
{
243-
context.setDocumentKey("key");
244-
245-
Node parent = db.createNode();
246-
parent.setProperty("type", "album");
247-
248-
Node child = db.createNode();
249-
child.setProperty("type", "artist");
250-
251-
Node track = db.createNode();
252-
track.setProperty("type", "track");
253-
254-
this.docrel.buildRelation(parent, child, context);
255-
context.setDocumentKey("another_key");
256-
this.docrel.buildRelation(child, track, context);
257-
258-
Set<Node> orphans = this.docrel.deleteRelations(context);
259-
260-
Assert.assertEquals(1, orphans.size());
261-
Assert.assertEquals(1, StreamSupport.stream(parent.getRelationships().spliterator(), false).count());
262-
Assert.assertEquals(0, StreamSupport.stream(child.getRelationships(Direction.OUTGOING).spliterator(), false).count());
263-
}
264-
265-
@Test
266-
public void shouldDeleteOnlyKey()
267-
{
268-
context.setDocumentKey("key");
269-
270-
Node parent = db.createNode();
271-
parent.setProperty("type", "album");
272-
273-
Node child = db.createNode();
274-
child.setProperty("type", "artist");
275-
276-
Node track = db.createNode();
277-
track.setProperty("type", "track");
278-
279-
this.docrel.buildRelation(parent, child, context);
280-
this.docrel.buildRelation(child, track, context);
281-
282-
context.setDocumentKey("another_key");
283-
Relationship rel = this.docrel.buildRelation(child, track, context);
284-
285-
Set<Node> orphans = this.docrel.deleteRelations(context);
286-
287-
Assert.assertEquals(0, orphans.size());
288-
Assert.assertEquals(1, StreamSupport.stream(parent.getRelationships().spliterator(), false).count());
289-
Assert.assertEquals(1, StreamSupport.stream(child.getRelationships(Direction.OUTGOING).spliterator(), false).count());
290-
291-
String[] keys = (String[]) rel.getProperty("docKeys");
292-
Assert.assertEquals(1, keys.length);
293-
Assert.assertEquals("key", keys[0]);
294-
}
295108
}

0 commit comments

Comments
 (0)