use of com.thinkaurelius.titan.core.TitanVertex in project titan by thinkaurelius.
the class TitanIndexTest method assertGraphOfTheGods.
public static void assertGraphOfTheGods(TitanGraph gotg) {
assertCount(12, gotg.query().vertices());
assertCount(3, gotg.query().has(LABEL_NAME, "god").vertices());
TitanVertex h = getOnlyVertex(gotg.query().has("name", "hercules"));
assertEquals(30, h.<Integer>value("age").intValue());
assertEquals("demigod", h.label());
assertCount(5, h.query().direction(Direction.BOTH).edges());
gotg.tx().commit();
}
use of com.thinkaurelius.titan.core.TitanVertex in project titan by thinkaurelius.
the class TitanIndexTest method testIndexing.
@Test
public void testIndexing() {
PropertyKey text = makeKey("text", String.class);
createExternalVertexIndex(text, INDEX);
createExternalEdgeIndex(text, INDEX);
PropertyKey location = makeKey("location", Geoshape.class);
createExternalVertexIndex(location, INDEX);
createExternalEdgeIndex(location, INDEX);
PropertyKey time = makeKey("time", Long.class);
createExternalVertexIndex(time, INDEX);
createExternalEdgeIndex(time, INDEX);
PropertyKey category = makeKey("category", Integer.class);
mgmt.buildIndex("vcategory", Vertex.class).addKey(category).buildCompositeIndex();
mgmt.buildIndex("ecategory", Edge.class).addKey(category).buildCompositeIndex();
PropertyKey group = makeKey("group", Byte.class);
createExternalVertexIndex(group, INDEX);
createExternalEdgeIndex(group, INDEX);
PropertyKey id = makeVertexIndexedKey("uid", Integer.class);
EdgeLabel knows = ((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("knows")).sortKey(time).signature(location).make();
finishSchema();
clopen();
String[] words = { "world", "aurelius", "titan", "graph" };
int numCategories = 5;
int numGroups = 10;
double distance, offset;
int numV = 100;
final int originalNumV = numV;
for (int i = 0; i < numV; i++) {
TitanVertex v = tx.addVertex();
v.property(VertexProperty.Cardinality.single, "uid", i);
v.property(VertexProperty.Cardinality.single, "category", i % numCategories);
v.property(VertexProperty.Cardinality.single, "group", i % numGroups);
v.property(VertexProperty.Cardinality.single, "text", "Vertex " + words[i % words.length]);
v.property(VertexProperty.Cardinality.single, "time", i);
offset = (i % 2 == 0 ? 1 : -1) * (i * 50.0 / numV);
v.property(VertexProperty.Cardinality.single, "location", Geoshape.point(0.0 + offset, 0.0 + offset));
Edge e = v.addEdge("knows", getVertex("uid", Math.max(0, i - 1)));
e.property("text", "Vertex " + words[i % words.length]);
e.property("time", i);
e.property("category", i % numCategories);
e.property("group", i % numGroups);
e.property("location", Geoshape.point(0.0 + offset, 0.0 + offset));
}
for (int i = 0; i < words.length; i++) {
int expectedSize = numV / words.length;
assertCount(expectedSize, tx.query().has("text", Text.CONTAINS, words[i]).vertices());
assertCount(expectedSize, tx.query().has("text", Text.CONTAINS, words[i]).edges());
//Test ordering
for (String orderKey : new String[] { "time", "category" }) {
for (Order order : Order.values()) {
for (TitanGraphQuery traversal : ImmutableList.of(tx.query().has("text", Text.CONTAINS, words[i]).orderBy(orderKey, order.getTP()), tx.query().has("text", Text.CONTAINS, words[i]).orderBy(orderKey, order.getTP()))) {
verifyElementOrder(traversal.vertices(), orderKey, order, expectedSize);
}
}
}
}
assertCount(3, tx.query().has("group", 3).orderBy("time", incr).limit(3).vertices());
assertCount(3, tx.query().has("group", 3).orderBy("time", decr).limit(3).edges());
for (int i = 0; i < numV / 2; i += numV / 10) {
assertCount(i, tx.query().has("time", Cmp.GREATER_THAN_EQUAL, i).has("time", Cmp.LESS_THAN, i + i).vertices());
assertCount(i, tx.query().has("time", Cmp.GREATER_THAN_EQUAL, i).has("time", Cmp.LESS_THAN, i + i).edges());
}
for (int i = 0; i < numV; i += 10) {
offset = (i * 50.0 / originalNumV);
distance = Geoshape.point(0.0, 0.0).getPoint().distance(Geoshape.point(offset, offset).getPoint()) + 20;
assertCount(i + 1, tx.query().has("location", Geo.WITHIN, Geoshape.circle(0.0, 0.0, distance)).vertices());
assertCount(i + 1, tx.query().has("location", Geo.WITHIN, Geoshape.circle(0.0, 0.0, distance)).edges());
}
//Queries combining mixed and composite indexes
assertCount(4, tx.query().has("category", 1).interval("time", 10, 28).vertices());
assertCount(4, tx.query().has("category", 1).interval("time", 10, 28).edges());
assertCount(5, tx.query().has("time", Cmp.GREATER_THAN_EQUAL, 10).has("time", Cmp.LESS_THAN, 30).has("text", Text.CONTAINS, words[0]).vertices());
offset = (19 * 50.0 / originalNumV);
distance = Geoshape.point(0.0, 0.0).getPoint().distance(Geoshape.point(offset, offset).getPoint()) + 20;
assertCount(5, tx.query().has("location", Geo.INTERSECT, Geoshape.circle(0.0, 0.0, distance)).has("text", Text.CONTAINS, words[0]).vertices());
assertCount(numV, tx.query().vertices());
assertCount(numV, tx.query().edges());
//--------------
clopen();
for (int i = 0; i < words.length; i++) {
int expectedSize = numV / words.length;
assertCount(expectedSize, tx.query().has("text", Text.CONTAINS, words[i]).vertices());
assertCount(expectedSize, tx.query().has("text", Text.CONTAINS, words[i]).edges());
//Test ordering
for (String orderKey : new String[] { "time", "category" }) {
for (Order order : Order.values()) {
for (TitanGraphQuery traversal : ImmutableList.of(tx.query().has("text", Text.CONTAINS, words[i]).orderBy(orderKey, order.getTP()), tx.query().has("text", Text.CONTAINS, words[i]).orderBy(orderKey, order.getTP()))) {
verifyElementOrder(traversal.vertices(), orderKey, order, expectedSize);
}
}
}
}
assertCount(3, tx.query().has("group", 3).orderBy("time", incr).limit(3).vertices());
assertCount(3, tx.query().has("group", 3).orderBy("time", decr).limit(3).edges());
for (int i = 0; i < numV / 2; i += numV / 10) {
assertCount(i, tx.query().has("time", Cmp.GREATER_THAN_EQUAL, i).has("time", Cmp.LESS_THAN, i + i).vertices());
assertCount(i, tx.query().has("time", Cmp.GREATER_THAN_EQUAL, i).has("time", Cmp.LESS_THAN, i + i).edges());
}
for (int i = 0; i < numV; i += 10) {
offset = (i * 50.0 / originalNumV);
distance = Geoshape.point(0.0, 0.0).getPoint().distance(Geoshape.point(offset, offset).getPoint()) + 20;
assertCount(i + 1, tx.query().has("location", Geo.WITHIN, Geoshape.circle(0.0, 0.0, distance)).vertices());
assertCount(i + 1, tx.query().has("location", Geo.WITHIN, Geoshape.circle(0.0, 0.0, distance)).edges());
}
//Queries combining mixed and composite indexes
assertCount(4, tx.query().has("category", 1).interval("time", 10, 28).vertices());
assertCount(4, tx.query().has("category", 1).interval("time", 10, 28).edges());
assertCount(5, tx.query().has("time", Cmp.GREATER_THAN_EQUAL, 10).has("time", Cmp.LESS_THAN, 30).has("text", Text.CONTAINS, words[0]).vertices());
offset = (19 * 50.0 / originalNumV);
distance = Geoshape.point(0.0, 0.0).getPoint().distance(Geoshape.point(offset, offset).getPoint()) + 20;
assertCount(5, tx.query().has("location", Geo.INTERSECT, Geoshape.circle(0.0, 0.0, distance)).has("text", Text.CONTAINS, words[0]).vertices());
assertCount(numV, tx.query().vertices());
assertCount(numV, tx.query().edges());
newTx();
int numDelete = 12;
for (int i = numV - numDelete; i < numV; i++) {
getVertex("uid", i).remove();
}
numV = numV - numDelete;
//Copied from above
for (int i = 0; i < words.length; i++) {
assertCount(numV / words.length, tx.query().has("text", Text.CONTAINS, words[i]).vertices());
assertCount(numV / words.length, tx.query().has("text", Text.CONTAINS, words[i]).edges());
}
for (int i = 0; i < numV / 2; i += numV / 10) {
assertCount(i, tx.query().has("time", Cmp.GREATER_THAN_EQUAL, i).has("time", Cmp.LESS_THAN, i + i).vertices());
assertCount(i, tx.query().has("time", Cmp.GREATER_THAN_EQUAL, i).has("time", Cmp.LESS_THAN, i + i).edges());
}
for (int i = 0; i < numV; i += 10) {
offset = (i * 50.0 / originalNumV);
distance = Geoshape.point(0.0, 0.0).getPoint().distance(Geoshape.point(offset, offset).getPoint()) + 20;
assertCount(i + 1, tx.query().has("location", Geo.WITHIN, Geoshape.circle(0.0, 0.0, distance)).vertices());
assertCount(i + 1, tx.query().has("location", Geo.WITHIN, Geoshape.circle(0.0, 0.0, distance)).edges());
}
assertCount(5, tx.query().has("time", Cmp.GREATER_THAN_EQUAL, 10).has("time", Cmp.LESS_THAN, 30).has("text", Text.CONTAINS, words[0]).vertices());
offset = (19 * 50.0 / originalNumV);
distance = Geoshape.point(0.0, 0.0).getPoint().distance(Geoshape.point(offset, offset).getPoint()) + 20;
assertCount(5, tx.query().has("location", Geo.INTERSECT, Geoshape.circle(0.0, 0.0, distance)).has("text", Text.CONTAINS, words[0]).vertices());
assertCount(numV, tx.query().vertices());
assertCount(numV, tx.query().edges());
}
use of com.thinkaurelius.titan.core.TitanVertex in project titan by thinkaurelius.
the class TitanIndexTest method testEdgeTTLWithMixedIndices.
@Test
public void testEdgeTTLWithMixedIndices() throws Exception {
if (!features.hasCellTTL() || !indexFeatures.supportsDocumentTTL()) {
return;
}
PropertyKey name = mgmt.makePropertyKey("name").dataType(String.class).make();
PropertyKey text = mgmt.makePropertyKey("text").dataType(String.class).make();
PropertyKey time = makeKey("time", Long.class);
EdgeLabel label = mgmt.makeEdgeLabel("likes").make();
final int likesTTLSeconds = (int) TestGraphConfigs.getTTL(TimeUnit.SECONDS);
mgmt.setTTL(label, Duration.ofSeconds(likesTTLSeconds));
mgmt.buildIndex("index1", Edge.class).addKey(name, getStringMapping()).addKey(time).buildMixedIndex(INDEX);
mgmt.buildIndex("index2", Edge.class).indexOnly(label).addKey(text, getTextMapping()).buildMixedIndex(INDEX);
assertEquals(Duration.ZERO, mgmt.getTTL(name));
assertEquals(Duration.ofSeconds(likesTTLSeconds), mgmt.getTTL(label));
finishSchema();
TitanVertex v1 = tx.addVertex(), v2 = tx.addVertex(), v3 = tx.addVertex();
Edge e1 = v1.addEdge("likes", v2, "name", "v1 likes v2", "text", "this will help to identify the edge");
long time1 = System.currentTimeMillis();
e1.property("time", time1);
Edge e2 = v2.addEdge("likes", v3, "name", "v2 likes v3", "text", "this won't match anything");
long time2 = time1 + 1;
e2.property("time", time2);
tx.commit();
clopen();
Object e1Id = e1.id();
Object e2Id = e2.id();
evaluateQuery(tx.query().has("text", Text.CONTAINS, "help").has(LABEL_NAME, "likes"), ElementCategory.EDGE, 1, new boolean[] { true, true }, "index2");
evaluateQuery(tx.query().has("name", "v2 likes v3").orderBy("time", decr), ElementCategory.EDGE, 1, new boolean[] { true, true }, tx.getPropertyKey("time"), Order.DESC, "index1");
v1 = getV(tx, v1.id());
v2 = getV(tx, v2.id());
v3 = getV(tx, v3.id());
e1 = getE(tx, e1Id);
e2 = getE(tx, e1Id);
assertNotNull(v1);
assertNotNull(v2);
assertNotNull(v3);
assertNotNull(e1);
assertNotNull(e2);
assertNotEmpty(v1.query().direction(Direction.OUT).edges());
assertNotEmpty(v2.query().direction(Direction.OUT).edges());
Thread.sleep(TimeUnit.MILLISECONDS.convert((long) Math.ceil(likesTTLSeconds * 1.25), TimeUnit.SECONDS));
clopen();
// ...indexes have expired
evaluateQuery(tx.query().has("text", Text.CONTAINS, "help").has(LABEL_NAME, "likes"), ElementCategory.EDGE, 0, new boolean[] { true, true }, "index2");
evaluateQuery(tx.query().has("name", "v2 likes v3").orderBy("time", decr), ElementCategory.EDGE, 0, new boolean[] { true, true }, tx.getPropertyKey("time"), Order.DESC, "index1");
v1 = getV(tx, v1.id());
v2 = getV(tx, v2.id());
v3 = getV(tx, v3.id());
e1 = getE(tx, e1Id);
e2 = getE(tx, e1Id);
assertNotNull(v1);
assertNotNull(v2);
assertNotNull(v3);
// edges have expired from the graph...
assertNull(e1);
assertNull(e2);
assertEmpty(v1.query().direction(Direction.OUT).edges());
assertEmpty(v2.query().direction(Direction.OUT).edges());
}
use of com.thinkaurelius.titan.core.TitanVertex in project titan by thinkaurelius.
the class TitanGraphTest method testConsistencyEnforcement.
/* ==================================================================================
CONSISTENCY
==================================================================================*/
/**
* Tests the correct application of ConsistencyModifiers across transactional boundaries
*/
@Test
public void testConsistencyEnforcement() {
PropertyKey uid = makeVertexIndexedUniqueKey("uid", Integer.class);
PropertyKey name = makeKey("name", String.class);
mgmt.setConsistency(uid, ConsistencyModifier.LOCK);
mgmt.setConsistency(name, ConsistencyModifier.LOCK);
mgmt.setConsistency(mgmt.getGraphIndex("uid"), ConsistencyModifier.LOCK);
EdgeLabel knows = mgmt.makeEdgeLabel("knows").multiplicity(Multiplicity.SIMPLE).make();
EdgeLabel spouse = mgmt.makeEdgeLabel("spouse").multiplicity(Multiplicity.ONE2ONE).make();
EdgeLabel connect = mgmt.makeEdgeLabel("connect").multiplicity(Multiplicity.MULTI).make();
EdgeLabel related = mgmt.makeEdgeLabel("related").multiplicity(Multiplicity.MULTI).make();
mgmt.setConsistency(knows, ConsistencyModifier.LOCK);
mgmt.setConsistency(spouse, ConsistencyModifier.LOCK);
mgmt.setConsistency(related, ConsistencyModifier.FORK);
finishSchema();
name = tx.getPropertyKey("name");
connect = tx.getEdgeLabel("connect");
related = tx.getEdgeLabel("related");
TitanVertex v1 = tx.addVertex("uid", 1);
TitanVertex v2 = tx.addVertex("uid", 2);
TitanVertex v3 = tx.addVertex("uid", 3);
Edge e1 = v1.addEdge(connect.name(), v2, name.name(), "e1");
Edge e2 = v1.addEdge(related.name(), v2, name.name(), "e2");
newTx();
v1 = getV(tx, v1);
/*
==== check fork, no fork behavior
*/
long e1id = getId(e1);
long e2id = getId(e2);
e1 = getOnlyElement(v1.query().direction(Direction.OUT).labels("connect").edges());
assertEquals("e1", e1.value("name"));
assertEquals(e1id, getId(e1));
e2 = getOnlyElement(v1.query().direction(Direction.OUT).labels("related").edges());
assertEquals("e2", e2.value("name"));
assertEquals(e2id, getId(e2));
//Update edges - one should simply update, the other fork
e1.property("name", "e1.2");
e2.property("name", "e2.2");
newTx();
v1 = getV(tx, v1);
e1 = getOnlyElement(v1.query().direction(Direction.OUT).labels("connect").edges());
assertEquals("e1.2", e1.value("name"));
//should have same id
assertEquals(e1id, getId(e1));
e2 = getOnlyElement(v1.query().direction(Direction.OUT).labels("related").edges());
assertEquals("e2.2", e2.value("name"));
//should have different id since forked
assertNotEquals(e2id, getId(e2));
clopen();
/*
=== check cross transaction
*/
final Random random = new Random();
final long[] vids = { getId(v1), getId(v2), getId(v3) };
//1) Index uniqueness
executeLockConflictingTransactionJobs(graph, new TransactionJob() {
private int pos = 0;
@Override
public void run(TitanTransaction tx) {
TitanVertex u = getV(tx, vids[pos++]);
u.property(VertexProperty.Cardinality.single, "uid", 5);
}
});
//2) Property out-uniqueness
executeLockConflictingTransactionJobs(graph, new TransactionJob() {
@Override
public void run(TitanTransaction tx) {
TitanVertex u = getV(tx, vids[0]);
u.property(VertexProperty.Cardinality.single, "name", "v" + random.nextInt(10));
}
});
//3) knows simpleness
executeLockConflictingTransactionJobs(graph, new TransactionJob() {
@Override
public void run(TitanTransaction tx) {
TitanVertex u1 = getV(tx, vids[0]), u2 = getV(tx, vids[1]);
u1.addEdge("knows", u2);
}
});
//4) knows one2one (in 2 separate configurations)
executeLockConflictingTransactionJobs(graph, new TransactionJob() {
private int pos = 1;
@Override
public void run(TitanTransaction tx) {
TitanVertex u1 = getV(tx, vids[0]), u2 = getV(tx, vids[pos++]);
u1.addEdge("spouse", u2);
}
});
executeLockConflictingTransactionJobs(graph, new TransactionJob() {
private int pos = 1;
@Override
public void run(TitanTransaction tx) {
TitanVertex u1 = getV(tx, vids[pos++]), u2 = getV(tx, vids[0]);
u1.addEdge("spouse", u2);
}
});
//######### TRY INVALID CONSISTENCY
try {
//Fork does not apply to constrained types
mgmt.setConsistency(mgmt.getPropertyKey("name"), ConsistencyModifier.FORK);
fail();
} catch (IllegalArgumentException e) {
}
}
use of com.thinkaurelius.titan.core.TitanVertex in project titan by thinkaurelius.
the class TitanGraphTest method testWithoutIndex.
@Test
public void testWithoutIndex() {
PropertyKey kid = mgmt.makePropertyKey("kid").dataType(Long.class).make();
mgmt.makePropertyKey("name").dataType(String.class).make();
mgmt.makeEdgeLabel("knows").signature(kid).make();
finishSchema();
Random random = new Random();
int numV = 1000;
TitanVertex previous = null;
for (int i = 0; i < numV; i++) {
TitanVertex v = graph.addVertex("kid", random.nextInt(numV), "name", "v" + i);
if (previous != null) {
Edge e = v.addEdge("knows", previous, "kid", random.nextInt(numV / 2));
}
previous = v;
}
verifyElementOrder(graph.query().orderBy("kid", incr).limit(500).vertices(), "kid", Order.ASC, 500);
verifyElementOrder(graph.query().orderBy("kid", incr).limit(300).edges(), "kid", Order.ASC, 300);
verifyElementOrder(graph.query().orderBy("kid", decr).limit(400).vertices(), "kid", Order.DESC, 400);
verifyElementOrder(graph.query().orderBy("kid", decr).limit(200).edges(), "kid", Order.DESC, 200);
clopen();
//Copied from above
verifyElementOrder(graph.query().orderBy("kid", incr).limit(500).vertices(), "kid", Order.ASC, 500);
verifyElementOrder(graph.query().orderBy("kid", incr).limit(300).edges(), "kid", Order.ASC, 300);
verifyElementOrder(graph.query().orderBy("kid", decr).limit(400).vertices(), "kid", Order.DESC, 400);
verifyElementOrder(graph.query().orderBy("kid", decr).limit(200).edges(), "kid", Order.DESC, 200);
}
Aggregations