Permalink
Browse files

added property removal as alternative to full entity deletion

  • Loading branch information...
1 parent fffa7c9 commit 97d6b3a6030d8ee6f745f697812847c3ec46e6d8 @technige technige committed Jan 31, 2012
@@ -21,15 +21,11 @@
import org.neo4j.geoff.except.RuleApplicationException;
-import org.neo4j.geoff.store.EntityStore;
-import org.neo4j.geoff.store.IndexToken;
-import org.neo4j.geoff.store.NodeToken;
-import org.neo4j.geoff.store.RelationshipToken;
+import org.neo4j.geoff.store.*;
import org.neo4j.graphdb.*;
import org.neo4j.graphdb.index.Index;
import org.neo4j.graphdb.index.IndexHits;
-import java.lang.reflect.Array;
import java.util.*;
/**
@@ -318,16 +314,47 @@ public void apply(Iterable<Rule> rules)
}
}
+ private void removeProperties(Set<? extends PropertyContainer> entities, Map<String, Object> properties) {
+ if (properties.isEmpty()) {
+ // for each entity, remove all properties
+ for (PropertyContainer entity : entities) {
+ for (String key : entity.getPropertyKeys()) {
+ entity.removeProperty(key);
+ }
+ }
+ } else {
+ // for each entity, remove only matching (or null) properties
+ for (Map.Entry<String, Object> entry : properties.entrySet()) {
+ for (PropertyContainer entity : entities) {
+ String key = entry.getKey();
+ if (entity.hasProperty(key)) {
+ Object value = entry.getValue();
+ if (value == null || value.equals(entity.getProperty(key))) {
+ entity.removeProperty(key);
+ }
+ }
+ }
+ }
+ }
+
+ }
+
/**
* Delete specific node
*
* @param a node token
- * @param properties
+ * @param properties properties to remove or null to delete node
*/
public void deleteNodes(NodeToken a, Map<String, Object> properties)
{
- for (Node node : nodeStore.remove(a)) {
- node.delete();
+ if (nodeStore.contains(a)) {
+ if (properties == null) {
+ for (Node node : nodeStore.remove(a)) {
+ node.delete();
+ }
+ } else {
+ removeProperties(nodeStore.get(a), properties);
+ }
}
}
@@ -337,27 +364,35 @@ public void deleteNodes(NodeToken a, Map<String, Object> properties)
* @param a start node token
* @param r relationship token
* @param b end node token
- * @param properties
+ * @param properties properties to remove or null to delete relationship
*/
public void deleteRelationships(NodeToken a, RelationshipToken r, NodeToken b, Map<String, Object> properties) {
Set<Relationship> relationships;
if (relationshipStore.contains(r)) {
- relationships = relationshipStore.remove(r);
+ if (properties == null) {
+ relationships = relationshipStore.remove(r);
+ } else {
+ relationships = relationshipStore.get(r);
+ }
} else if (r.hasType()) {
relationships = match(a, b, DynamicRelationshipType.withName(r.getType()));
} else {
relationships = match(a, b);
}
- for (Relationship relationship : relationships) {
- relationship.delete();
+ if (properties == null) {
+ for (Relationship relationship : relationships) {
+ relationship.delete();
+ }
+ } else {
+ removeProperties(relationships, properties);
}
}
/**
* Delete specific relationship
*
* @param r relationship token
- * @param properties
+ * @param properties properties to remove or null to delete relationship
*/
public void deleteRelationships(RelationshipToken r, Map<String, Object> properties)
{
@@ -694,7 +729,7 @@ private void setProperties(PropertyContainer entity, Map<String, Object> propert
throw new IllegalArgumentException("Illegal property type: " + value.getClass().getName());
}
} catch (ClassCastException ex) {
- throw new IllegalArgumentException("Illegal combination of list item types");
+ throw new IllegalArgumentException("Illegal combination of list item types", ex);
}
}
entity.setProperty(key, value);
@@ -58,7 +58,7 @@ public void testNodeExclusionRule() throws Exception {
}
@Test
- public void testLoadingNodeExclusionRule() throws Exception {
+ public void canDeleteNode() throws Exception {
// perform call to add and remove node
String source =
"(A) {\"name\": \"Alice\"}\n" +
@@ -71,6 +71,68 @@ public void testLoadingNodeExclusionRule() throws Exception {
}
@Test
+ public void canRemoveAllPropertiesFromNode() throws Exception {
+ String source =
+ "(A) {\"name\": \"Alice\", \"age\": 900}\n" +
+ "!(A) {}\n" +
+ "";
+ Map<String, PropertyContainer> out = Geoff.loadIntoNeo4j(new StringReader(source), db, null);
+ // check results
+ assertNotNull(out);
+ Node a = (Node) out.get("(A)");
+ for (String key : a.getPropertyKeys()) {
+ assertFalse(true);
+ }
+ assertEquals(1, out.size());
+ }
+
+ @Test
+ public void canRemoveNamedPropertiesFromNode() throws Exception {
+ String source =
+ "(A) {\"name\": \"Alice\", \"age\": 900}\n" +
+ "!(A) {\"name\": null}\n" +
+ "";
+ Map<String, PropertyContainer> out = Geoff.loadIntoNeo4j(new StringReader(source), db, null);
+ // check results
+ assertNotNull(out);
+ Node a = (Node) out.get("(A)");
+ for (String key : a.getPropertyKeys()) {
+ assertFalse("name".equals(key));
+ }
+ assertEquals(1, out.size());
+ }
+
+ @Test
+ public void canRemoveMatchingPropertiesFromNode() throws Exception {
+ String source =
+ "(A) {\"name\": \"Alice\", \"age\": 900}\n" +
+ "!(A) {\"name\": \"Alice\"}\n" +
+ "";
+ Map<String, PropertyContainer> out = Geoff.loadIntoNeo4j(new StringReader(source), db, null);
+ // check results
+ assertNotNull(out);
+ Node a = (Node) out.get("(A)");
+ for (String key : a.getPropertyKeys()) {
+ assertFalse("name".equals(key));
+ }
+ assertEquals(1, out.size());
+ }
+
+ @Test
+ public void willNotRemoveNonMatchingPropertiesFromNode() throws Exception {
+ String source =
+ "(A) {\"name\": \"Alice\", \"age\": 900}\n" +
+ "!(A) {\"name\": \"Ada\"}\n" +
+ "";
+ Map<String, PropertyContainer> out = Geoff.loadIntoNeo4j(new StringReader(source), db, null);
+ // check results
+ assertNotNull(out);
+ Node a = (Node) out.get("(A)");
+ assertTrue(a.hasProperty("name"));
+ assertEquals(1, out.size());
+ }
+
+ @Test
public void testLoadingNodeInclusionRuleWithLoadParameter() throws Exception {
// perform first call to inject node
String source = "(A) {\"name\": \"Alice\"}";

0 comments on commit 97d6b3a

Please sign in to comment.