use of com.thinkaurelius.titan.core.TitanVertex in project titan by thinkaurelius.
the class TitanGraphTest method testIndexUniqueness.
@Test
public void testIndexUniqueness() {
PropertyKey time = makeKey("time", Long.class);
PropertyKey text = makeKey("text", String.class);
VertexLabel person = mgmt.makeVertexLabel("person").make();
VertexLabel org = mgmt.makeVertexLabel("organization").make();
TitanGraphIndex vindex1 = mgmt.buildIndex("vindex1", Vertex.class).addKey(time).indexOnly(person).unique().buildCompositeIndex();
TitanGraphIndex vindex2 = mgmt.buildIndex("vindex2", Vertex.class).addKey(time).addKey(text).unique().buildCompositeIndex();
finishSchema();
//================== VERTEX UNIQUENESS ====================
//I) Label uniqueness
//Ia) Uniqueness violation in same transaction
failTransactionOnCommit(new TransactionJob() {
@Override
public void run(TitanTransaction tx) {
TitanVertex v0 = tx.addVertex("person");
v0.property(VertexProperty.Cardinality.single, "time", 1);
TitanVertex v1 = tx.addVertex("person");
v1.property(VertexProperty.Cardinality.single, "time", 1);
}
});
//Ib) Uniqueness violation across transactions
TitanVertex v0 = tx.addVertex("person");
v0.property(VertexProperty.Cardinality.single, "time", 1);
newTx();
failTransactionOnCommit(new TransactionJob() {
@Override
public void run(TitanTransaction tx) {
TitanVertex v1 = tx.addVertex("person");
v1.property(VertexProperty.Cardinality.single, "time", 1);
}
});
//Ic) However, this should work since the label is different
TitanVertex v1 = tx.addVertex("organization");
v1.property(VertexProperty.Cardinality.single, "time", 1);
newTx();
//II) Composite uniqueness
//IIa) Uniqueness violation in same transaction
failTransactionOnCommit(new TransactionJob() {
@Override
public void run(TitanTransaction tx) {
TitanVertex v0 = tx.addVertex("time", 2, "text", "hello");
TitanVertex v1 = tx.addVertex("time", 2, "text", "hello");
}
});
//IIb) Uniqueness violation across transactions
v0 = tx.addVertex("time", 2, "text", "hello");
newTx();
failTransactionOnCommit(new TransactionJob() {
@Override
public void run(TitanTransaction tx) {
TitanVertex v1 = tx.addVertex("time", 2, "text", "hello");
}
});
}
use of com.thinkaurelius.titan.core.TitanVertex in project titan by thinkaurelius.
the class TitanGraphTest method testThreadBoundTx.
/**
* Tests that elements can be accessed beyond their transactional boundaries if they
* are bound to single-threaded graph transactions
*/
@Test
@SuppressWarnings("deprecation")
public void testThreadBoundTx() {
PropertyKey t = mgmt.makePropertyKey("type").dataType(Integer.class).make();
mgmt.buildIndex("etype", Edge.class).addKey(t).buildCompositeIndex();
((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("friend")).sortKey(t).make();
finishSchema();
TitanVertex v1 = graph.addVertex("name", "Vertex1", "age", 35);
TitanVertex v2 = graph.addVertex("name", "Vertex2", "age", 45);
TitanVertex v3 = graph.addVertex("name", "Vertex3", "age", 55);
Edge e1 = v1.addEdge("knows", v2, "time", 5);
Edge e2 = v2.addEdge("knows", v3, "time", 15);
Edge e3 = v3.addEdge("knows", v1, "time", 25);
Edge e4 = v2.addEdge("friend", v2, "type", 1);
for (TitanVertex v : new TitanVertex[] { v1, v2, v3 }) {
assertCount(2, v.query().direction(Direction.BOTH).labels("knows").edges());
assertCount(1, v.query().direction(Direction.OUT).labels("knows").edges());
TitanEdge tmpE = getOnlyElement(v.query().direction(Direction.OUT).labels("knows").edges());
assertEquals(5, tmpE.<Integer>value("time") % 10);
}
e3.property("time", 35);
assertEquals(35, e3.<Integer>value("time").intValue());
v1.addEdge("friend", v2, "type", 0);
graph.tx().commit();
e4.property("type", 2);
TitanEdge ef = getOnlyElement(v1.query().direction(Direction.OUT).labels("friend").edges());
assertEquals(ef, (Edge) getOnlyElement(graph.query().has("type", 0).edges()));
ef.property("type", 1);
graph.tx().commit();
assertEquals(35, e3.<Integer>value("time").intValue());
e3 = getE(graph, e3);
e3.property("time", 45);
assertEquals(45, e3.<Integer>value("time").intValue());
assertEquals(15, e2.<Integer>value("time").intValue());
e2.property("time", 25);
assertEquals(25, e2.<Integer>value("time").intValue());
assertEquals(35, v1.<Integer>value("age").intValue());
assertEquals(55, v3.<Integer>value("age").intValue());
v3.property("age", 65);
assertEquals(65, v3.<Integer>value("age").intValue());
e1 = getE(graph, e1);
for (TitanVertex v : new TitanVertex[] { v1, v2, v3 }) {
assertCount(2, v.query().direction(Direction.BOTH).labels("knows").edges());
assertCount(1, v.query().direction(Direction.OUT).labels("knows").edges());
assertEquals(5, getOnlyElement(v.query().direction(Direction.OUT).labels("knows").edges()).<Integer>value("time").intValue() % 10);
}
graph.tx().commit();
VertexProperty prop = v1.properties().next();
assertTrue(getId(prop) > 0);
prop = (VertexProperty) ((Iterable) graph.multiQuery((TitanVertex) v1).properties().values().iterator().next()).iterator().next();
assertTrue(getId(prop) > 0);
assertEquals(45, e3.<Integer>value("time").intValue());
assertEquals(5, e1.<Integer>value("time").intValue());
assertEquals(35, v1.<Integer>value("age").intValue());
assertEquals(65, v3.<Integer>value("age").intValue());
for (TitanVertex v : new TitanVertex[] { v1, v2, v3 }) {
assertCount(2, v.query().direction(Direction.BOTH).labels("knows").edges());
assertCount(1, v.query().direction(Direction.OUT).labels("knows").edges());
assertEquals(5, getOnlyElement(v.query().direction(Direction.OUT).labels("knows").edges()).<Integer>value("time").intValue() % 10);
}
graph.tx().commit();
v1 = graph.addVertex();
v2 = graph.addVertex();
v1.addEdge("knows", v2);
graph.tx().commit();
v3 = graph.addVertex();
Edge e = v1.addEdge("knows", v3);
assertFalse(e.property("age").isPresent());
}
use of com.thinkaurelius.titan.core.TitanVertex in project titan by thinkaurelius.
the class TitanGraphTest method testVertexRemoval.
/**
* Adding a removing a vertex with index
*/
@Test
public void testVertexRemoval() {
final String namen = "name";
makeVertexIndexedUniqueKey(namen, String.class);
finishSchema();
TitanVertex v1 = graph.addVertex(namen, "v1");
TitanVertex v2 = graph.addVertex(namen, "v2");
v1.addEdge("knows", v2);
assertCount(2, graph.query().vertices());
assertCount(1, graph.query().has(namen, "v2").vertices());
clopen();
v1 = getV(graph, v1);
v2 = getV(graph, v2);
assertCount(1, v1.query().direction(BOTH).edges());
assertCount(1, v2.query().direction(Direction.BOTH).edges());
v2.remove();
assertCount(0, v1.query().direction(Direction.BOTH).edges());
try {
assertCount(0, v2.query().direction(Direction.BOTH).edges());
fail();
} catch (IllegalStateException ex) {
}
assertCount(1, graph.query().vertices());
assertCount(1, graph.query().has(namen, "v1").vertices());
assertCount(0, graph.query().has(namen, "v2").vertices());
graph.tx().commit();
assertMissing(graph, v2);
assertCount(1, graph.query().vertices());
assertCount(1, graph.query().has(namen, "v1").vertices());
assertCount(0, graph.query().has(namen, "v2").vertices());
}
use of com.thinkaurelius.titan.core.TitanVertex in project titan by thinkaurelius.
the class TitanGraphTest method testPropertyTTLTiming.
@Category({ BrittleTests.class })
@Test
public void testPropertyTTLTiming() throws Exception {
if (!features.hasCellTTL()) {
return;
}
PropertyKey name = mgmt.makePropertyKey("name").dataType(String.class).make();
PropertyKey place = mgmt.makePropertyKey("place").dataType(String.class).make();
mgmt.setTTL(name, Duration.ofSeconds(42));
mgmt.setTTL(place, Duration.ofSeconds(1));
TitanGraphIndex index1 = mgmt.buildIndex("index1", Vertex.class).addKey(name).buildCompositeIndex();
TitanGraphIndex index2 = mgmt.buildIndex("index2", Vertex.class).addKey(name).addKey(place).buildCompositeIndex();
VertexLabel label1 = mgmt.makeVertexLabel("event").setStatic().make();
mgmt.setTTL(label1, Duration.ofSeconds(2));
assertEquals(Duration.ofSeconds(42), mgmt.getTTL(name));
assertEquals(Duration.ofSeconds(1), mgmt.getTTL(place));
assertEquals(Duration.ofSeconds(2), mgmt.getTTL(label1));
mgmt.commit();
TitanVertex v1 = tx.addVertex(T.label, "event", "name", "some event", "place", "somewhere");
tx.commit();
Object id = v1.id();
v1 = getV(graph, id);
assertNotNull(v1);
assertNotEmpty(graph.query().has("name", "some event").has("place", "somewhere").vertices());
assertNotEmpty(graph.query().has("name", "some event").vertices());
Thread.sleep(1001);
graph.tx().rollback();
// short-lived property expires first
v1 = getV(graph, id);
assertNotNull(v1);
assertEmpty(graph.query().has("name", "some event").has("place", "somewhere").vertices());
assertNotEmpty(graph.query().has("name", "some event").vertices());
Thread.sleep(1001);
graph.tx().rollback();
// vertex expires before defined TTL of the long-lived property
assertEmpty(graph.query().has("name", "some event").has("place", "somewhere").vertices());
assertEmpty(graph.query().has("name", "some event").vertices());
v1 = getV(graph, id);
assertNull(v1);
}
use of com.thinkaurelius.titan.core.TitanVertex in project titan by thinkaurelius.
the class TitanGraphTest method testSchemaNameChange.
@Test
public void testSchemaNameChange() {
PropertyKey time = mgmt.makePropertyKey("time").dataType(Integer.class).cardinality(Cardinality.SINGLE).make();
EdgeLabel knows = mgmt.makeEdgeLabel("knows").multiplicity(Multiplicity.MULTI).make();
mgmt.buildEdgeIndex(knows, "byTime", Direction.BOTH, time);
mgmt.buildIndex("timeIndex", Vertex.class).addKey(time).buildCompositeIndex();
mgmt.makeVertexLabel("people").make();
finishSchema();
//CREATE SMALL GRAPH
TitanVertex v = tx.addVertex("people");
v.property(VertexProperty.Cardinality.single, "time", 5);
v.addEdge("knows", v, "time", 11);
newTx();
v = getOnlyElement(tx.query().has("time", 5).vertices());
assertNotNull(v);
assertEquals("people", v.label());
assertEquals(5, v.<Integer>value("time").intValue());
assertCount(1, v.query().direction(Direction.IN).labels("knows").edges());
assertCount(1, v.query().direction(Direction.OUT).labels("knows").has("time", 11).edges());
newTx();
//UPDATE SCHEMA NAMES
assertTrue(mgmt.containsRelationType("knows"));
knows = mgmt.getEdgeLabel("knows");
mgmt.changeName(knows, "know");
assertEquals("know", knows.name());
assertTrue(mgmt.containsRelationIndex(knows, "byTime"));
RelationTypeIndex rindex = mgmt.getRelationIndex(knows, "byTime");
assertEquals("byTime", rindex.name());
mgmt.changeName(rindex, "overTime");
assertEquals("overTime", rindex.name());
assertTrue(mgmt.containsVertexLabel("people"));
VertexLabel vl = mgmt.getVertexLabel("people");
mgmt.changeName(vl, "person");
assertEquals("person", vl.name());
assertTrue(mgmt.containsGraphIndex("timeIndex"));
TitanGraphIndex gindex = mgmt.getGraphIndex("timeIndex");
mgmt.changeName(gindex, "byTime");
assertEquals("byTime", gindex.name());
finishSchema();
//VERIFY UPDATES IN MGMT SYSTEM
assertTrue(mgmt.containsRelationType("know"));
assertFalse(mgmt.containsRelationType("knows"));
knows = mgmt.getEdgeLabel("know");
assertTrue(mgmt.containsRelationIndex(knows, "overTime"));
assertFalse(mgmt.containsRelationIndex(knows, "byTime"));
assertTrue(mgmt.containsVertexLabel("person"));
assertFalse(mgmt.containsVertexLabel("people"));
assertTrue(mgmt.containsGraphIndex("byTime"));
assertFalse(mgmt.containsGraphIndex("timeIndex"));
//VERIFY UPDATES IN TRANSACTION
newTx();
v = getOnlyElement(tx.query().has("time", 5).vertices());
assertNotNull(v);
assertEquals("person", v.label());
assertEquals(5, v.<Integer>value("time").intValue());
assertCount(1, v.query().direction(Direction.IN).labels("know").edges());
assertCount(0, v.query().direction(Direction.IN).labels("knows").edges());
assertCount(1, v.query().direction(Direction.OUT).labels("know").has("time", 11).edges());
}
Aggregations