@Test public void testRelationshipCreateAndDelete() { Node node1 = getGraphDb().createNode(); Node node2 = getGraphDb().createNode(); Relationship relationship = node1.createRelationshipTo(node2, MyRelTypes.TEST); Relationship relArray1[] = getRelationshipArray(node1.getRelationships()); Relationship relArray2[] = getRelationshipArray(node2.getRelationships()); assertEquals(1, relArray1.length); assertEquals(relationship, relArray1[0]); assertEquals(1, relArray2.length); assertEquals(relationship, relArray2[0]); relArray1 = getRelationshipArray(node1.getRelationships(MyRelTypes.TEST)); assertEquals(1, relArray1.length); assertEquals(relationship, relArray1[0]); relArray2 = getRelationshipArray(node2.getRelationships(MyRelTypes.TEST)); assertEquals(1, relArray2.length); assertEquals(relationship, relArray2[0]); relArray1 = getRelationshipArray(node1.getRelationships(MyRelTypes.TEST, Direction.OUTGOING)); assertEquals(1, relArray1.length); relArray2 = getRelationshipArray(node2.getRelationships(MyRelTypes.TEST, Direction.INCOMING)); assertEquals(1, relArray2.length); relArray1 = getRelationshipArray(node1.getRelationships(MyRelTypes.TEST, Direction.INCOMING)); assertEquals(0, relArray1.length); relArray2 = getRelationshipArray(node2.getRelationships(MyRelTypes.TEST, Direction.OUTGOING)); assertEquals(0, relArray2.length); relationship.delete(); node2.delete(); node1.delete(); }
@Test public void testRelationshipChangeProperty() { Integer int1 = new Integer(1); Integer int2 = new Integer(2); String string1 = new String("1"); String string2 = new String("2"); Node node1 = getGraphDb().createNode(); Node node2 = getGraphDb().createNode(); Relationship rel1 = node1.createRelationshipTo(node2, MyRelTypes.TEST); Relationship rel2 = node2.createRelationshipTo(node1, MyRelTypes.TEST); rel1.setProperty(key1, int1); rel2.setProperty(key1, string1); rel1.setProperty(key2, string2); rel2.setProperty(key2, int2); try { rel1.setProperty(null, null); fail("Null argument should result in exception."); } catch (IllegalArgumentException e) { } catch (NotFoundException e) { fail("wrong exception"); } // test type change of exsisting property // cannot test this for now because of exceptions in PL rel2.setProperty(key1, int1); rel1.delete(); rel2.delete(); node2.delete(); node1.delete(); }
@Test public void testRelationshipChangeProperty2() { Integer int1 = new Integer(1); Integer int2 = new Integer(2); String string1 = new String("1"); String string2 = new String("2"); Boolean bool1 = new Boolean(true); Boolean bool2 = new Boolean(false); Node node1 = getGraphDb().createNode(); Node node2 = getGraphDb().createNode(); Relationship rel1 = node1.createRelationshipTo(node2, MyRelTypes.TEST); rel1.setProperty(key1, int1); rel1.setProperty(key1, int2); assertEquals(int2, rel1.getProperty(key1)); rel1.removeProperty(key1); rel1.setProperty(key1, string1); rel1.setProperty(key1, string2); assertEquals(string2, rel1.getProperty(key1)); rel1.removeProperty(key1); rel1.setProperty(key1, bool1); rel1.setProperty(key1, bool2); assertEquals(bool2, rel1.getProperty(key1)); rel1.removeProperty(key1); rel1.delete(); node2.delete(); node1.delete(); }
@Test public void testRelationshipCahinIterator() { Node node1 = getGraphDb().createNode(); Node node2 = getGraphDb().createNode(); Relationship rels[] = new Relationship[100]; for (int i = 0; i < rels.length; i++) { if (i < 50) { rels[i] = node1.createRelationshipTo(node2, MyRelTypes.TEST); } else { rels[i] = node2.createRelationshipTo(node1, MyRelTypes.TEST); } } newTransaction(); getNodeManager().clearCache(); Iterable<Relationship> relIterable = node1.getRelationships(); for (Relationship rel : rels) { rel.delete(); } newTransaction(); for (Relationship rel : relIterable) { System.out.println(rel); } node1.delete(); node2.delete(); }
@Test public void testRelMultiRemoveProperty() { Node node1 = getGraphDb().createNode(); Node node2 = getGraphDb().createNode(); Relationship rel = node1.createRelationshipTo(node2, MyRelTypes.TEST); rel.setProperty("key0", "0"); rel.setProperty("key1", "1"); rel.setProperty("key2", "2"); rel.setProperty("key3", "3"); rel.setProperty("key4", "4"); newTransaction(); rel.removeProperty("key3"); rel.removeProperty("key2"); rel.removeProperty("key3"); newTransaction(); getNodeManager().clearCache(); assertEquals("0", rel.getProperty("key0")); assertEquals("1", rel.getProperty("key1")); assertEquals("4", rel.getProperty("key4")); assertTrue(!rel.hasProperty("key2")); assertTrue(!rel.hasProperty("key3")); rel.delete(); node1.delete(); node2.delete(); }
@Test public void testSimple3() { Node node1 = getGraphDb().createNode(); Node node2 = getGraphDb().createNode(); for (int i = 0; i < 1; i++) { node1.createRelationshipTo(node2, MyRelTypes.TEST); node1.createRelationshipTo(node2, MyRelTypes.TEST_TRAVERSAL); node1.createRelationshipTo(node2, MyRelTypes.TEST2); } allGetRelationshipMethods2(node1, Direction.OUTGOING); allGetRelationshipMethods2(node2, Direction.INCOMING); newTransaction(); allGetRelationshipMethods2(node1, Direction.OUTGOING); allGetRelationshipMethods2(node2, Direction.INCOMING); node1.getRelationships(MyRelTypes.TEST, Direction.OUTGOING).iterator().next().delete(); node1 .getRelationships(MyRelTypes.TEST_TRAVERSAL, Direction.OUTGOING) .iterator() .next() .delete(); node1.getRelationships(MyRelTypes.TEST2, Direction.OUTGOING).iterator().next().delete(); node1.createRelationshipTo(node2, MyRelTypes.TEST); node1.createRelationshipTo(node2, MyRelTypes.TEST_TRAVERSAL); node1.createRelationshipTo(node2, MyRelTypes.TEST2); allGetRelationshipMethods2(node1, Direction.OUTGOING); allGetRelationshipMethods2(node2, Direction.INCOMING); newTransaction(); allGetRelationshipMethods2(node1, Direction.OUTGOING); allGetRelationshipMethods2(node2, Direction.INCOMING); for (Relationship rel : node1.getRelationships()) { rel.delete(); } node1.delete(); node2.delete(); }
@After public void deleteTestingGraph() { Node node1 = getGraphDb().getNodeById(node1Id); Node node2 = getGraphDb().getNodeById(node2Id); node1.getSingleRelationship(MyRelTypes.TEST, Direction.BOTH).delete(); node1.delete(); node2.delete(); }
@Test public void testRelCountInSameTx() { Node node1 = getGraphDb().createNode(); Node node2 = getGraphDb().createNode(); Relationship rel = node1.createRelationshipTo(node2, MyRelTypes.TEST); assertEquals(1, getRelationshipArray(node1.getRelationships()).length); assertEquals(1, getRelationshipArray(node2.getRelationships()).length); rel.delete(); assertEquals(0, getRelationshipArray(node1.getRelationships()).length); assertEquals(0, getRelationshipArray(node2.getRelationships()).length); node1.delete(); node2.delete(); }
@Test public void testAddPropertyThenDelete() { Node node1 = getGraphDb().createNode(); Node node2 = getGraphDb().createNode(); Relationship rel = node1.createRelationshipTo(node2, MyRelTypes.TEST); rel.setProperty("test", "test"); newTransaction(); rel.setProperty("test2", "test2"); rel.delete(); node1.delete(); node2.delete(); newTransaction(); }
@Test public void testRelationshipRemoveProperty() { Integer int1 = new Integer(1); Integer int2 = new Integer(2); String string1 = new String("1"); String string2 = new String("2"); Node node1 = getGraphDb().createNode(); Node node2 = getGraphDb().createNode(); Relationship rel1 = node1.createRelationshipTo(node2, MyRelTypes.TEST); Relationship rel2 = node2.createRelationshipTo(node1, MyRelTypes.TEST); // verify that we can rely on PL to reomve non existing properties try { if (rel1.removeProperty(key1) != null) { fail("Remove of non existing property should return null"); } } catch (NotFoundException e) { } try { rel1.removeProperty(null); fail("Remove null property should throw exception."); } catch (IllegalArgumentException e) { } rel1.setProperty(key1, int1); rel2.setProperty(key1, string1); rel1.setProperty(key2, string2); rel2.setProperty(key2, int2); try { rel1.removeProperty(null); fail("Null argument should result in exception."); } catch (IllegalArgumentException e) { } // test remove property assertEquals(int1, rel1.removeProperty(key1)); assertEquals(string1, rel2.removeProperty(key1)); // test remove of non exsisting property try { if (rel2.removeProperty(key1) != null) { fail("Remove of non existing property should return null"); } } catch (NotFoundException e) { // have to set rollback only here getTransaction().failure(); } rel1.delete(); rel2.delete(); node1.delete(); node2.delete(); }
@Test public void testDeletedRelationship() { Node node1 = getGraphDb().createNode(); Node node2 = getGraphDb().createNode(); Relationship relationship = node1.createRelationshipTo(node2, MyRelTypes.TEST); relationship.delete(); try { relationship.setProperty("key1", new Integer(1)); fail("Adding property to deleted rel should throw exception."); } catch (Exception e) { // good } node1.delete(); node2.delete(); }
@Test public void testRollbackDeleteRelationship() { Node node1 = getGraphDb().createNode(); Node node2 = getGraphDb().createNode(); Relationship rel1 = node1.createRelationshipTo(node2, MyRelTypes.TEST); newTransaction(); node1.delete(); rel1.delete(); getTransaction().failure(); getTransaction().finish(); setTransaction(getGraphDb().beginTx()); node1.delete(); node2.delete(); rel1.delete(); }
@Test public void deletingNodeWithLabelsShouldHaveRemovalReflectedInLabelScans() throws Exception { // GIVEN Transaction tx = db.beginTx(); Label label = label("labello"); Node node = db.createNode(label); tx.success(); tx.finish(); // AND GIVEN I DELETE IT tx = db.beginTx(); node.delete(); tx.success(); tx.finish(); // WHEN tx = db.beginTx(); Statement statement = statementContextProvider.instance(); int labelId = statement.readOperations().labelGetForName(label.name()); PrimitiveLongIterator nodes = statement.readOperations().nodesGetForLabel(labelId); Set<Long> nodeSet = asSet(nodes); tx.success(); tx.finish(); // THEN assertThat(nodeSet, equalTo(Collections.<Long>emptySet())); }
public void removeAll() throws ApplicationException { log.info("Removing all nodes and references."); Transaction tx = graphDb.beginTx(); try { GlobalGraphOperations ops = GlobalGraphOperations.at(graphDb); for (Relationship relationship : ops.getAllRelationships()) { relationship.delete(); } for (Node node : ops.getAllNodes()) { node.delete(); } tx.success(); log.info("Deleted all relationships and nodes"); } catch (Exception e) { log.error(e.toString()); tx.failure(); throw new ApplicationException(e); } finally { tx.finish(); } }
@Override /** * Delete a record from the database. * * @param table The name of the table * @param key The record key of the record to delete. * @return Zero on success, a non-zero error code on error. See this class's description for a * discussion of error codes. */ public int delete(String table, String key) { Transaction tx = gds.beginTx(); try { IndexHits<Node> hits = index().get("_id", key); Node node = hits.getSingle(); index().remove(node); node.delete(); tx.success(); return 0; } catch (Exception e) { e.printStackTrace(); return 1; } finally { tx.finish(); } }
@Test public void shouldRecoverTransactionWhereNodeIsDeletedInTheFuture() throws Exception { // GIVEN Node node; try (Transaction tx = db.beginTx()) { node = db.createNode(label); node.setProperty("key", "value"); tx.success(); } rotateLog(); try (Transaction tx = db.beginTx()) { node.setProperty("other-key", 1); tx.success(); } try (Transaction tx = db.beginTx()) { node.delete(); tx.success(); } flushAll(); // WHEN FileSystemAbstraction uncleanFs = fsRule.snapshot(shutdownDb(db)); db = (GraphDatabaseAPI) new TestGraphDatabaseFactory().setFileSystem(uncleanFs).newImpermanentDatabase(); // THEN // -- really the problem was that recovery threw exception, so mostly assert that. try (Transaction tx = db.beginTx()) { node = db.getNodeById(node.getId()); fail("Should not exist"); } catch (NotFoundException e) { assertEquals("Node " + node.getId() + " not found", e.getMessage()); } }
public void delete(ConstructionZoneNode zone) { Node n = zone.getNode(); for (Relationship rel : n.getRelationships()) { rel.delete(); } n.delete(); }
@Test public void removeNodesWithARelation() { int originalNodeCount = countNodesOf(graphDb); int removedNodeCount = 0; Transaction tx = graphDb.beginTx(); try { Set<Node> toRemove = new HashSet<Node>(); for (Node node : graphDb.getAllNodes()) { for (Relationship relationship : node.getRelationships(RelationType.ON)) { toRemove.add(relationship.getStartNode()); toRemove.add(relationship.getEndNode()); relationship.delete(); } } for (Node node : toRemove) { node.delete(); removedNodeCount++; } tx.success(); } finally { tx.finish(); } int finalNodeCount = countNodesOf(graphDb); assertEquals( Integer.valueOf(originalNodeCount), Integer.valueOf(finalNodeCount + removedNodeCount)); }
private void createSomeTransactions(GraphDatabaseService db) { Transaction tx = db.beginTx(); Node node1 = db.createNode(); Node node2 = db.createNode(); node1.createRelationshipTo(node2, DynamicRelationshipType.withName("relType1")); tx.success(); tx.finish(); tx = db.beginTx(); node1.delete(); tx.success(); try { // Will throw exception, causing the tx to be rolledback. tx.finish(); } catch (Exception nothingToSeeHereMoveAlong) { // InvalidRecordException coming, node1 has rels } /* * The damage has already been done. The following just makes sure * the corrupting tx is flushed to disk, since we will exit * uncleanly. */ tx = db.beginTx(); node1.setProperty("foo", "bar"); tx.success(); tx.finish(); }
/** * Removes all nodes with non-valid resource classes from the graph. * * @param docGraph * @param validResourceClasses */ private void preProcessGraph(DocGraph docGraph, Set<String> validResourceClasses) { log.info(String.format("Preprocessing DocGraph[%d]", docGraph.getId())); Node n; int cnt = 0; try (Transaction tx = graphDB.beginTx()) { for (Long nodeId : docGraph.getNodes()) { n = graphDB.getNodeById(nodeId); // node's class is resource class and it's not in the valid set if (n.hasProperty(Constants.NODE_SUPER_CLASS_KEY) && n.getProperty(Constants.NODE_SUPER_CLASS_KEY) .equals(Constants.NODE_SUPER_CLASS_RESOURCE_VALUE) && !validResourceClasses.contains(getNodeClass(n))) { try (Transaction innerTx = graphDB.beginTx()) { log.info("Deleting " + n); for (Relationship e : n.getRelationships()) { e.delete(); } n.delete(); innerTx.success(); cnt++; } } } tx.success(); } log.info( String.format("Preprocessing removed %d nodes from DocGraph[%d]", cnt, docGraph.getId())); }
/* * It will remove a property from an embedded node if it exists. * After deleting the property, if the node does not have any more properties and relationships (except for an incoming one), * it will delete the embedded node as well. */ private void removePropertyForEmbedded(Node embeddedNode, String[] embeddedColumnSplit, int i) { if (i == embeddedColumnSplit.length - 1) { // Property String property = embeddedColumnSplit[embeddedColumnSplit.length - 1]; if (embeddedNode.hasProperty(property)) { embeddedNode.removeProperty(property); } } else { Iterator<Relationship> iterator = embeddedNode .getRelationships(Direction.OUTGOING, withName(embeddedColumnSplit[i])) .iterator(); if (iterator.hasNext()) { removePropertyForEmbedded(iterator.next().getEndNode(), embeddedColumnSplit, i + 1); } } if (!embeddedNode.getPropertyKeys().iterator().hasNext()) { // Node without properties Iterator<Relationship> iterator = embeddedNode.getRelationships().iterator(); if (iterator.hasNext()) { Relationship relationship = iterator.next(); if (!iterator.hasNext()) { // Node with only one relationship and no properties, // we can remove it: // It means we have removed all the properties from the embedded node // and it is NOT an intermediate node like // (entity) --> (embedded1) --> (embedded2) relationship.delete(); embeddedNode.delete(); } } } }
public void testIt() throws Exception { String key = "mykey"; String value = "myvalue"; Collection<Node> nodes = new ArrayList<Node>(); for (int i = 0; i < 20000; i++) { Node node = graphDb().createNode(); indexService.index(node, key, value); nodes.add(node); if (i == 2000) { Iterable<Node> itr = indexService.getNodes(key, value); assertCollection(asCollection(itr), nodes.toArray(new Node[0])); } if (i % 10000 == 0) { restartTx(); } } restartTx(); Node[] nodeArray = nodes.toArray(new Node[0]); long total = 0; long totalTotal = 0; int counter = 0; for (int i = 0; i < 10; i++) { // So that it'll get the nodes in the synchronous way ((LuceneIndexService) indexService).setLazySearchResultThreshold(nodes.size() + 10); long time = System.currentTimeMillis(); Iterable<Node> itr = indexService.getNodes(key, value); long syncTime = System.currentTimeMillis() - time; assertCollection(asCollection(itr), nodeArray); long syncTotalTime = System.currentTimeMillis() - time; // So that it'll get the nodes in the lazy way ((LuceneIndexService) indexService).setLazySearchResultThreshold(nodes.size() - 10); time = System.currentTimeMillis(); itr = indexService.getNodes(key, value); long lazyTime = System.currentTimeMillis() - time; assertCollection(asCollection(itr), nodeArray); long lazyTotalTime = System.currentTimeMillis() - time; // System.out.println( "lazy:" + lazyTime + " (" + lazyTotalTime + // "), sync:" + syncTime + " (" + syncTotalTime + ")" ); if (i > 0) { total += syncTime; totalTotal += syncTotalTime; counter++; } // At the very least assertTrue(lazyTime < syncTime / 3); } // System.out.println( "avg:" + ( total / counter ) + ", " + // ( totalTotal / counter ) ); for (Node node : nodes) { node.delete(); } }
@Test public void testCreateRelationshipWithCommitts() // throws NotFoundException { Node n1 = getGraphDb().createNode(); newTransaction(); clearCache(); n1 = getGraphDb().getNodeById((int) n1.getId()); Node n2 = getGraphDb().createNode(); n1.createRelationshipTo(n2, MyRelTypes.TEST); newTransaction(); Relationship[] relArray = getRelationshipArray(n1.getRelationships()); assertEquals(1, relArray.length); relArray = getRelationshipArray(n1.getRelationships()); relArray[0].delete(); n1.delete(); n2.delete(); }
@Override protected void doSomething() throws Throwable { Pair<Integer, Node> pair = getNode(random, true); int index = pair.first(); Node node = pair.other(); node.delete(); setNode(index, db.createNode()); }
@Test public void testDeleteWithRelationship() { // do some evil stuff Node node1 = getGraphDb().createNode(); Node node2 = getGraphDb().createNode(); Relationship relationship = node1.createRelationshipTo(node2, MyRelTypes.TEST); node1.delete(); node2.delete(); try { getTransaction().success(); getTransaction().finish(); fail("deleting node with relaitonship should not commit."); } catch (Exception e) { // good } setTransaction(getGraphDb().beginTx()); }
public void removeVertex(final Vertex vertex) { this.autoStartTransaction(); final Node node = ((Neo4jVertex) vertex).getRawVertex(); for (final Relationship relationship : node.getRelationships(org.neo4j.graphdb.Direction.BOTH)) { relationship.delete(); } node.delete(); }
@Test public void testRelationshipIsType() { Node node1 = getGraphDb().createNode(); Node node2 = getGraphDb().createNode(); Relationship rel = node1.createRelationshipTo(node2, MyRelTypes.TEST); assertTrue(rel.isType(MyRelTypes.TEST)); assertTrue( rel.isType( new RelationshipType() { public String name() { return MyRelTypes.TEST.name(); } })); assertFalse(rel.isType(MyRelTypes.TEST_TRAVERSAL)); rel.delete(); node1.delete(); node2.delete(); }
@Test public void testTxCacheLoadIsolation() throws Exception { Node node = getGraphDb().createNode(); node.setProperty("someproptest", "testing"); Node node1 = getGraphDb().createNode(); node1.setProperty("someotherproptest", 2); commit(); EmbeddedGraphDatabase graphDb = (EmbeddedGraphDatabase) getGraphDb(); TransactionManager txManager = graphDb.getConfig().getTxModule().getTxManager(); NodeManager nodeManager = graphDb.getConfig().getGraphDbModule().getNodeManager(); txManager.begin(); node.setProperty("someotherproptest", "testing2"); Relationship rel = node.createRelationshipTo(node1, MyRelTypes.TEST); javax.transaction.Transaction txA = txManager.suspend(); txManager.begin(); assertEquals("testing", node.getProperty("someproptest")); assertTrue(!node.hasProperty("someotherproptest")); assertTrue(!node.hasRelationship()); nodeManager.clearCache(); assertEquals("testing", node.getProperty("someproptest")); assertTrue(!node.hasProperty("someotherproptest")); javax.transaction.Transaction txB = txManager.suspend(); txManager.resume(txA); assertEquals("testing", node.getProperty("someproptest")); assertTrue(node.hasProperty("someotherproptest")); assertTrue(node.hasRelationship()); nodeManager.clearCache(); assertEquals("testing", node.getProperty("someproptest")); assertTrue(node.hasProperty("someotherproptest")); assertTrue(node.hasRelationship()); txManager.suspend(); txManager.resume(txB); assertEquals("testing", node.getProperty("someproptest")); assertTrue(!node.hasProperty("someotherproptest")); assertTrue(!node.hasRelationship()); txManager.rollback(); txManager.resume(txA); node.delete(); node1.delete(); rel.delete(); txManager.commit(); newTransaction(); }
@Test public void testIndexedSet() throws Exception { Node rootNode = graphDb().createNode(); Collection<AnItem> collection = new SortedNodeCollection<AnItem>(rootNode, new AnItemComparator(), AnItem.class); List<String> strings = new ArrayList<String>( Arrays.asList( "Mattias", "Persson", "Went", "And", "Implemented", "An", "Indexed", "GraphDb", "Set", "And", "How", "About", "That")); for (String string : strings) { assertTrue(collection.add(new AnItem(string))); } // Compare the indexed node collection against a natural sorting order Collections.sort(strings); assertCollectionSame(strings, collection); String toRemove = "Persson"; AnItem toRemoveItem = findItem(collection, toRemove); assertTrue(collection.remove(toRemoveItem)); assertTrue(strings.remove(toRemove)); assertCollectionSame(strings, collection); collection.clear(); ((SortedNodeCollection<AnItem>) collection).delete(); rootNode.delete(); for (Node node : AnItem.createdNodes) { node.delete(); } }
@Test(expected = org.neo4j.graphdb.NotFoundException.class) public void testDeleteNode() { final Transaction tx = restAPI.beginTx(); Node n1 = restAPI.createNode(map("name", "node1")); n1.delete(); Node n2 = restAPI.createNode(map("name", "node2")); tx.success(); tx.finish(); loadRealNode(n1); }