Skip to content

Commit

Permalink
Fixed issue #3163, added test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
lvca committed Dec 11, 2014
1 parent bb762e6 commit 0dbaf5f
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,6 @@
*/
package com.orientechnologies.orient.core.db.record.ridbag.embedded;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.UUID;
import java.util.WeakHashMap;

import com.orientechnologies.common.serialization.types.OIntegerSerializer;
import com.orientechnologies.common.util.OResettable;
import com.orientechnologies.common.util.OSizeable;
Expand All @@ -41,6 +31,16 @@
import com.orientechnologies.orient.core.record.ORecord;
import com.orientechnologies.orient.core.serialization.serializer.binary.impl.OLinkSerializer;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.UUID;
import java.util.WeakHashMap;

public class OEmbeddedRidBag implements ORidBagDelegate {
private byte[] serializedContent = null;

Expand Down Expand Up @@ -287,9 +287,12 @@ public String toString() {
for (final Iterator<OIdentifiable> it = this.iterator(); it.hasNext();) {
try {
OIdentifiable e = it.next();
sb.append(e.getIdentity());
if (it.hasNext())
sb.append(", ");
if (e != null) {
if (sb.length() > 1)
sb.append(", ");

sb.append(e.getIdentity());
}
} catch (NoSuchElementException ex) {
// IGNORE THIS
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
*/
package com.orientechnologies.orient.graph.sql;

import java.util.List;
import java.util.Map;

import com.orientechnologies.orient.core.command.OCommandDistributedReplicateRequest;
import com.orientechnologies.orient.core.command.OCommandRequest;
import com.orientechnologies.orient.core.command.OCommandRequestText;
Expand All @@ -40,16 +43,12 @@
import com.tinkerpop.blueprints.impls.orient.OrientVertex;
import com.tinkerpop.blueprints.impls.orient.OrientVertexType;

import java.util.List;
import java.util.Map;

/**
* SQL DELETE VERTEX command.
*
* @author Luca Garulli
*/
public class
OCommandExecutorSQLDeleteVertex extends OCommandExecutorSQLAbstract implements OCommandDistributedReplicateRequest,
public class OCommandExecutorSQLDeleteVertex extends OCommandExecutorSQLAbstract implements OCommandDistributedReplicateRequest,
OCommandResultListener {
public static final String NAME = "DELETE VERTEX";
private ORecordId rid;
Expand Down Expand Up @@ -177,7 +176,7 @@ public boolean result(final Object iRecord) {

@Override
public String getSyntax() {
return "DELETE VERTEX [FROM <query>]|<rid>|<[<class>] [WHERE <conditions>] [LIMIT <max-records>]>";
return "DELETE VERTEX <rid>|<class>|FROM <query> [WHERE <conditions>] [LIMIT <max-records>]>";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,44 @@
*/
package com.orientechnologies.orient.test.database.auto;

import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.exception.OCommandExecutionException;
import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.metadata.schema.OType;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientEdge;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
import com.tinkerpop.blueprints.impls.orient.OrientVertex;
import org.testng.Assert;
import org.testng.annotations.*;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Test
public class GraphDatabaseTest extends DocumentDBBaseTest {
private OrientGraph database;

@Parameters(value = "url")
public GraphDatabaseTest(@Optional String url) {
super(url);
}

@Parameters(value = "url")
public GraphDatabaseTest(@Optional String url) {
super(url);
}

@BeforeMethod
public void init() {
database = new OrientGraph(url);
database = new OrientGraph(url);
database.setUseLightweightEdges(false);
}

Expand All @@ -60,15 +63,15 @@ public void deinit() {

@Test
public void populate() {
OClass vehicleClass = database.createVertexType("GraphVehicle");
OClass vehicleClass = database.createVertexType("GraphVehicle");
database.createVertexType("GraphCar", vehicleClass);
database.createVertexType("GraphMotocycle", "GraphVehicle");

ODocument carNode = database.addVertex("class:GraphCar", "brand", "Hyundai", "model", "Coupe", "year", 2003).getRecord();
ODocument motoNode = database.addVertex("class:GraphMotocycle", "brand", "Yamaha", "model", "X-City 250", "year", 2009)
.getRecord();

database.commit();
database.commit();
database.addEdge(null, database.getVertex(carNode), database.getVertex(motoNode), "E").save();

List<ODocument> result = database.getRawGraph().query(new OSQLSynchQuery<ODocument>("select from GraphVehicle"));
Expand Down Expand Up @@ -107,7 +110,7 @@ public void populate() {

@Test(dependsOnMethods = "populate")
public void testSQLAgainstGraph() {
Vertex tom = database.addVertex(null, "name", "Tom");
Vertex tom = database.addVertex(null, "name", "Tom");
Vertex ferrari = database.addVertex("class:GraphCar", "brand", "Ferrari");
Vertex maserati = database.addVertex("class:GraphCar", "brand", "Maserati");
Vertex porsche = database.addVertex("class:GraphCar", "brand", "Porsche");
Expand All @@ -133,7 +136,7 @@ public void testSQLAgainstGraph() {
}

public void testNotDuplicatedIndexTxChanges() throws IOException {
database.setAutoStartTx(false);
database.setAutoStartTx(false);
database.commit();
OClass oc = database.getVertexType("vertexA");
if (oc == null)
Expand All @@ -157,7 +160,7 @@ public void testNotDuplicatedIndexTxChanges() throws IOException {
}

public void testNewVertexAndEdgesWithFieldsInOneShoot() throws IOException {
OrientVertex vertexA = database.addVertex(null, "field1", "value1", "field2", "value2");
OrientVertex vertexA = database.addVertex(null, "field1", "value1", "field2", "value2");

Map<String, Object> map = new HashMap<String, Object>();
map.put("field1", "value1");
Expand Down Expand Up @@ -214,19 +217,19 @@ public void sqlNestedQueries() {

@SuppressWarnings("unchecked")
public void nestedQuery() {
Vertex countryVertex1 = database.addVertex(null, "name", "UK", "area", "Europe", "code", "2");
Vertex countryVertex1 = database.addVertex(null, "name", "UK", "area", "Europe", "code", "2");
Vertex cityVertex1 = database.addVertex(null, "name", "leicester", "lat", "52.64640", "long", "-1.13159");
Vertex cityVertex2 = database.addVertex(null, "name", "manchester", "lat", "53.47497", "long", "-2.25769");

database.addEdge(null, countryVertex1, cityVertex1, "owns");
database.addEdge(null, countryVertex1, cityVertex2, "owns");

database.commit();
database.commit();
String subquery = "select out('owns') from V where name = 'UK'";
List<OIdentifiable> result = database.getRawGraph().query(new OSQLSynchQuery<ODocument>(subquery));

Assert.assertEquals(result.size(), 1);
Assert.assertEquals( ((Collection) ((ODocument) result.get(0)).field("out")).size(), 2);
Assert.assertEquals(((Collection) ((ODocument) result.get(0)).field("out")).size(), 2);

subquery = "select expand(out('owns')) from V where name = 'UK'";
result = database.getRawGraph().query(new OSQLSynchQuery<ODocument>(subquery));
Expand All @@ -247,4 +250,44 @@ public void nestedQuery() {
Assert.assertTrue(((ODocument) result.get(i).getRecord()).containsField("distance"));
}
}

public void testDeleteOfVerticesWithDeleteCommandMustFail() {
try {
database.command(new OCommandSQL("delete from GraphVehicle")).execute();
Assert.assertTrue(false);
} catch (OCommandExecutionException e) {
Assert.assertTrue(true);
}
}

public void testDeleteOfEdgesWithDeleteCommandMustFail() {
try {
database.command(new OCommandSQL("delete from E")).execute();
Assert.assertTrue(false);
} catch (OCommandExecutionException e) {
Assert.assertTrue(true);
}
}

public void testDeleteOfVerticesAndEdgesWithDeleteCommandAndUnsafe() {
Iterable<OIdentifiable> deletedVertices = database.command(
new OCommandSQL("delete from GraphVehicle return before limit 1 unsafe")).execute();
Assert.assertTrue(deletedVertices.iterator().hasNext());

OrientVertex v = (OrientVertex) deletedVertices.iterator().next();

Integer confirmDeleted = database.command(new OCommandSQL("delete from " + v.getIdentity() + " unsafe")).execute();
Assert.assertFalse(deletedVertices.iterator().hasNext());
Assert.assertEquals(confirmDeleted.intValue(), 0);

Iterable<Edge> edges = v.getEdges(Direction.BOTH);

for (Edge e : edges) {
Integer deletedEdges = database.command(new OCommandSQL("delete from " + ((OrientEdge) e).getIdentity() + " unsafe"))
.execute();
Assert.assertEquals(deletedEdges.intValue(), 1);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public void testDeleteFromTo() {
resultTwo = database.query(new OSQLSynchQuery<ODocument>("select expand(outE()) from " + result.get(1).getIdentity()));
Assert.assertEquals(resultTwo.size(), 1);

database.command(new OCommandSQL("DELETE FROM testFromToOneE")).execute();
database.command(new OCommandSQL("DELETE FROM testFromToTwoE")).execute();
database.command(new OCommandSQL("DELETE FROM testFromToV")).execute();
database.command(new OCommandSQL("DELETE FROM testFromToOneE unsafe")).execute();
database.command(new OCommandSQL("DELETE FROM testFromToTwoE unsafe")).execute();
database.command(new OCommandSQL("DELETE VERTEX testFromToV")).execute();
}

public void testDeleteFrom() {
Expand Down Expand Up @@ -79,9 +79,9 @@ public void testDeleteFrom() {
resultTwo = database.query(new OSQLSynchQuery<ODocument>("select expand(outE()) from " + result.get(1).getIdentity()));
Assert.assertEquals(resultTwo.size(), 1);

database.command(new OCommandSQL("DELETE FROM testFromOneE")).execute();
database.command(new OCommandSQL("DELETE FROM testFromTwoE")).execute();
database.command(new OCommandSQL("DELETE FROM testFromV")).execute();
database.command(new OCommandSQL("DELETE FROM testFromOneE unsafe")).execute();
database.command(new OCommandSQL("DELETE FROM testFromTwoE unsafe")).execute();
database.command(new OCommandSQL("DELETE VERTEX testFromV")).execute();
}

public void testDeleteTo() {
Expand Down Expand Up @@ -110,8 +110,8 @@ public void testDeleteTo() {
resultTwo = database.query(new OSQLSynchQuery<ODocument>("select expand(outE()) from " + result.get(1).getIdentity()));
Assert.assertEquals(resultTwo.size(), 1);

database.command(new OCommandSQL("DELETE FROM testToOneE")).execute();
database.command(new OCommandSQL("DELETE FROM testToTwoE")).execute();
database.command(new OCommandSQL("DELETE FROM testToV")).execute();
database.command(new OCommandSQL("DELETE FROM testToOneE unsafe")).execute();
database.command(new OCommandSQL("DELETE FROM testToTwoE unsafe")).execute();
database.command(new OCommandSQL("DELETE VERTEX testToV")).execute();
}
}
}

0 comments on commit 0dbaf5f

Please sign in to comment.