use of org.janusgraph.core.EdgeLabel in project janusgraph by JanusGraph.
the class JanusGraphTest method testVertexCentricEdgeIndexOnSimpleMultiplicityShouldWork.
@Test
public void testVertexCentricEdgeIndexOnSimpleMultiplicityShouldWork() {
clopen(option(LOG_SEND_DELAY, MANAGEMENT_LOG), Duration.ofMillis(0), option(KCVSLog.LOG_READ_LAG_TIME, MANAGEMENT_LOG), Duration.ofMillis(50), option(LOG_READ_INTERVAL, MANAGEMENT_LOG), Duration.ofMillis(250));
PropertyKey time = mgmt.makePropertyKey("time").dataType(Integer.class).cardinality(Cardinality.SINGLE).make();
EdgeLabel friend = mgmt.makeEdgeLabel("friend").multiplicity(Multiplicity.SIMPLE).make();
mgmt.buildEdgeIndex(friend, "byTime", Direction.OUT, decr, time);
finishSchema();
assertEquals(SchemaStatus.ENABLED, mgmt.getRelationIndex(mgmt.getRelationType("friend"), "byTime").getIndexStatus());
JanusGraphVertex v = tx.addVertex();
v = getV(tx, v);
for (int i = 200; i < 210; i++) {
JanusGraphVertex o = tx.addVertex();
v.addEdge("friend", o, "time", i);
}
evaluateQuery(v.query().labels("friend").direction(OUT).interval("time", 199, 210).orderBy("time", decr), EDGE, 10, 1, new boolean[] { true, true }, tx.getPropertyKey("time"), Order.DESC);
tx.commit();
finishSchema();
}
use of org.janusgraph.core.EdgeLabel in project janusgraph by JanusGraph.
the class JanusGraphIndexTest 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();
JanusGraphVertex 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();
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 org.janusgraph.core.EdgeLabel in project janusgraph by JanusGraph.
the class JanusGraphTest 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
JanusGraphVertex v = tx.addVertex("people");
v.property(VertexProperty.Cardinality.single, "time", 5);
v.addEdge("knows", v, "time", 11);
newTx();
v = Iterables.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 byTimeIndex = mgmt.getRelationIndex(knows, "byTime");
assertEquals("byTime", byTimeIndex.name());
mgmt.changeName(byTimeIndex, "overTime");
assertEquals("overTime", byTimeIndex.name());
assertTrue(mgmt.containsVertexLabel("people"));
VertexLabel vl = mgmt.getVertexLabel("people");
mgmt.changeName(vl, "person");
assertEquals("person", vl.name());
assertTrue(mgmt.containsGraphIndex("timeIndex"));
JanusGraphIndex graphIndex = mgmt.getGraphIndex("timeIndex");
mgmt.changeName(graphIndex, "byTime");
assertEquals("byTime", graphIndex.name());
finishSchema();
// VERIFY UPDATES IN MANAGEMENT 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 = Iterables.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());
}
use of org.janusgraph.core.EdgeLabel in project janusgraph by JanusGraph.
the class JanusGraphTest method testSchemaTypes.
/* ==================================================================================
SCHEMA TESTS
==================================================================================*/
/**
* Test the definition and inspection of various schema types and ensure their correct interpretation
* within the graph
*/
@SuppressWarnings("unchecked")
@Test
public void testSchemaTypes() {
// ---------- PROPERTY KEYS ----------------
// Normal single-valued property key
PropertyKey weight = makeKey("weight", Float.class);
// Indexed unique property key
PropertyKey uid = makeVertexIndexedUniqueKey("uid", String.class);
// Indexed but not unique
PropertyKey someId = makeVertexIndexedKey("someid", Object.class);
// Set-valued property key
PropertyKey name = mgmt.makePropertyKey("name").dataType(String.class).cardinality(Cardinality.SET).make();
// List-valued property key with signature
PropertyKey value = mgmt.makePropertyKey("value").dataType(Double.class).signature(weight).cardinality(Cardinality.LIST).make();
// ---------- EDGE LABELS ----------------
// Standard edge label
EdgeLabel friend = mgmt.makeEdgeLabel("friend").make();
// Unidirected
EdgeLabel link = mgmt.makeEdgeLabel("link").unidirected().multiplicity(Multiplicity.MANY2ONE).make();
// Signature label
EdgeLabel connect = mgmt.makeEdgeLabel("connect").signature(uid).multiplicity(Multiplicity.SIMPLE).make();
// Edge labels with different cardinalities
EdgeLabel parent = mgmt.makeEdgeLabel("parent").multiplicity(Multiplicity.MANY2ONE).make();
EdgeLabel child = mgmt.makeEdgeLabel("child").multiplicity(Multiplicity.ONE2MANY).make();
EdgeLabel spouse = mgmt.makeEdgeLabel("spouse").multiplicity(Multiplicity.ONE2ONE).make();
// ---------- VERTEX LABELS ----------------
VertexLabel person = mgmt.makeVertexLabel("person").make();
VertexLabel tag = mgmt.makeVertexLabel("tag").make();
VertexLabel tweet = mgmt.makeVertexLabel("tweet").setStatic().make();
long[] sig;
// ######### INSPECTION & FAILURE ############
assertTrue(mgmt.isOpen());
assertEquals("weight", weight.toString());
assertTrue(mgmt.containsRelationType("weight"));
assertTrue(mgmt.containsPropertyKey("weight"));
assertFalse(mgmt.containsEdgeLabel("weight"));
assertTrue(mgmt.containsEdgeLabel("connect"));
assertFalse(mgmt.containsPropertyKey("connect"));
assertFalse(mgmt.containsRelationType("bla"));
assertNull(mgmt.getPropertyKey("bla"));
assertNull(mgmt.getEdgeLabel("bla"));
assertNotNull(mgmt.getPropertyKey("weight"));
assertNotNull(mgmt.getEdgeLabel("connect"));
assertTrue(weight.isPropertyKey());
assertFalse(weight.isEdgeLabel());
assertEquals(Cardinality.SINGLE, weight.cardinality());
assertEquals(Cardinality.SINGLE, someId.cardinality());
assertEquals(Cardinality.SET, name.cardinality());
assertEquals(Cardinality.LIST, value.cardinality());
assertEquals(Object.class, someId.dataType());
assertEquals(Float.class, weight.dataType());
sig = ((InternalRelationType) value).getSignature();
assertEquals(1, sig.length);
assertEquals(weight.longId(), sig[0]);
assertTrue(mgmt.getGraphIndex(uid.name()).isUnique());
assertFalse(mgmt.getGraphIndex(someId.name()).isUnique());
assertEquals("friend", friend.name());
assertTrue(friend.isEdgeLabel());
assertFalse(friend.isPropertyKey());
assertEquals(Multiplicity.ONE2ONE, spouse.multiplicity());
assertEquals(Multiplicity.ONE2MANY, child.multiplicity());
assertEquals(Multiplicity.MANY2ONE, parent.multiplicity());
assertEquals(Multiplicity.MULTI, friend.multiplicity());
assertEquals(Multiplicity.SIMPLE, connect.multiplicity());
assertTrue(link.isUnidirected());
assertFalse(link.isDirected());
assertFalse(child.isUnidirected());
assertTrue(spouse.isDirected());
assertFalse(((InternalRelationType) friend).isInvisibleType());
assertTrue(((InternalRelationType) friend).isInvisible());
assertEquals(0, ((InternalRelationType) friend).getSignature().length);
sig = ((InternalRelationType) connect).getSignature();
assertEquals(1, sig.length);
assertEquals(uid.longId(), sig[0]);
assertEquals(0, ((InternalRelationType) friend).getSortKey().length);
assertEquals(Order.DEFAULT, ((InternalRelationType) friend).getSortOrder());
assertEquals(SchemaStatus.ENABLED, ((InternalRelationType) friend).getStatus());
assertEquals(5, Iterables.size(mgmt.getRelationTypes(PropertyKey.class)));
assertEquals(6, Iterables.size(mgmt.getRelationTypes(EdgeLabel.class)));
assertEquals(11, Iterables.size(mgmt.getRelationTypes(RelationType.class)));
assertEquals(3, Iterables.size(mgmt.getVertexLabels()));
assertEquals("tweet", tweet.name());
assertTrue(mgmt.containsVertexLabel("person"));
assertFalse(mgmt.containsVertexLabel("bla"));
assertFalse(person.isPartitioned());
assertFalse(person.isStatic());
assertFalse(tag.isPartitioned());
assertTrue(tweet.isStatic());
// Failures
try {
// No data type
mgmt.makePropertyKey("fid").make();
fail();
} catch (IllegalArgumentException ignored) {
}
try {
// Already exists
mgmt.makeEdgeLabel("link").unidirected().make();
fail();
} catch (SchemaViolationException ignored) {
}
try {
// signature and sort-key collide
((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("other")).sortKey(someId, weight).signature(someId).make();
fail();
} catch (IllegalArgumentException ignored) {
}
// } catch (IllegalArgumentException e) {}
try {
// sort key requires the label to be non-constrained
((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("other")).multiplicity(Multiplicity.SIMPLE).sortKey(weight).make();
fail();
} catch (IllegalArgumentException ignored) {
}
try {
// sort key requires the label to be non-constrained
((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("other")).multiplicity(Multiplicity.MANY2ONE).sortKey(weight).make();
fail();
} catch (IllegalArgumentException ignored) {
}
try {
// Already exists
mgmt.makeVertexLabel("tweet").make();
fail();
} catch (SchemaViolationException ignored) {
}
try {
// signature key must have non-generic data type
mgmt.makeEdgeLabel("test").signature(someId).make();
fail();
} catch (IllegalArgumentException ignored) {
}
// ######### END INSPECTION ############
finishSchema();
clopen();
// Load schema types into current transaction
weight = mgmt.getPropertyKey("weight");
uid = mgmt.getPropertyKey("uid");
someId = mgmt.getPropertyKey("someid");
name = mgmt.getPropertyKey("name");
value = mgmt.getPropertyKey("value");
friend = mgmt.getEdgeLabel("friend");
link = mgmt.getEdgeLabel("link");
connect = mgmt.getEdgeLabel("connect");
parent = mgmt.getEdgeLabel("parent");
child = mgmt.getEdgeLabel("child");
spouse = mgmt.getEdgeLabel("spouse");
person = mgmt.getVertexLabel("person");
tag = mgmt.getVertexLabel("tag");
tweet = mgmt.getVertexLabel("tweet");
// ######### INSPECTION & FAILURE (COPIED FROM ABOVE) ############
assertTrue(mgmt.isOpen());
assertEquals("weight", weight.toString());
assertTrue(mgmt.containsRelationType("weight"));
assertTrue(mgmt.containsPropertyKey("weight"));
assertFalse(mgmt.containsEdgeLabel("weight"));
assertTrue(mgmt.containsEdgeLabel("connect"));
assertFalse(mgmt.containsPropertyKey("connect"));
assertFalse(mgmt.containsRelationType("bla"));
assertNull(mgmt.getPropertyKey("bla"));
assertNull(mgmt.getEdgeLabel("bla"));
assertNotNull(mgmt.getPropertyKey("weight"));
assertNotNull(mgmt.getEdgeLabel("connect"));
assertTrue(weight.isPropertyKey());
assertFalse(weight.isEdgeLabel());
assertEquals(Cardinality.SINGLE, weight.cardinality());
assertEquals(Cardinality.SINGLE, someId.cardinality());
assertEquals(Cardinality.SET, name.cardinality());
assertEquals(Cardinality.LIST, value.cardinality());
assertEquals(Object.class, someId.dataType());
assertEquals(Float.class, weight.dataType());
sig = ((InternalRelationType) value).getSignature();
assertEquals(1, sig.length);
assertEquals(weight.longId(), sig[0]);
assertTrue(mgmt.getGraphIndex(uid.name()).isUnique());
assertFalse(mgmt.getGraphIndex(someId.name()).isUnique());
assertEquals("friend", friend.name());
assertTrue(friend.isEdgeLabel());
assertFalse(friend.isPropertyKey());
assertEquals(Multiplicity.ONE2ONE, spouse.multiplicity());
assertEquals(Multiplicity.ONE2MANY, child.multiplicity());
assertEquals(Multiplicity.MANY2ONE, parent.multiplicity());
assertEquals(Multiplicity.MULTI, friend.multiplicity());
assertEquals(Multiplicity.SIMPLE, connect.multiplicity());
assertTrue(link.isUnidirected());
assertFalse(link.isDirected());
assertFalse(child.isUnidirected());
assertTrue(spouse.isDirected());
assertFalse(((InternalRelationType) friend).isInvisibleType());
assertTrue(((InternalRelationType) friend).isInvisible());
assertEquals(0, ((InternalRelationType) friend).getSignature().length);
sig = ((InternalRelationType) connect).getSignature();
assertEquals(1, sig.length);
assertEquals(uid.longId(), sig[0]);
assertEquals(0, ((InternalRelationType) friend).getSortKey().length);
assertEquals(Order.DEFAULT, ((InternalRelationType) friend).getSortOrder());
assertEquals(SchemaStatus.ENABLED, ((InternalRelationType) friend).getStatus());
assertEquals(5, Iterables.size(mgmt.getRelationTypes(PropertyKey.class)));
assertEquals(6, Iterables.size(mgmt.getRelationTypes(EdgeLabel.class)));
assertEquals(11, Iterables.size(mgmt.getRelationTypes(RelationType.class)));
assertEquals(3, Iterables.size(mgmt.getVertexLabels()));
assertEquals("tweet", tweet.name());
assertTrue(mgmt.containsVertexLabel("person"));
assertFalse(mgmt.containsVertexLabel("bla"));
assertFalse(person.isPartitioned());
assertFalse(person.isStatic());
assertFalse(tag.isPartitioned());
assertTrue(tweet.isStatic());
// Failures
try {
// No data type
mgmt.makePropertyKey("fid").make();
fail();
} catch (IllegalArgumentException ignored) {
}
try {
// Already exists
mgmt.makeEdgeLabel("link").unidirected().make();
fail();
} catch (SchemaViolationException ignored) {
}
try {
// signature and sort-key collide
((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("other")).sortKey(someId, weight).signature(someId).make();
fail();
} catch (IllegalArgumentException ignored) {
}
// } catch (IllegalArgumentException e) {}
try {
// sort key requires the label to be non-constrained
((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("other")).multiplicity(Multiplicity.SIMPLE).sortKey(weight).make();
fail();
} catch (IllegalArgumentException ignored) {
}
try {
// sort key requires the label to be non-constrained
((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("other")).multiplicity(Multiplicity.MANY2ONE).sortKey(weight).make();
fail();
} catch (IllegalArgumentException ignored) {
}
try {
// Already exists
mgmt.makeVertexLabel("tweet").make();
fail();
} catch (SchemaViolationException ignored) {
}
try {
// signature key must have non-generic data type
mgmt.makeEdgeLabel("test").signature(someId).make();
fail();
} catch (IllegalArgumentException ignored) {
}
// ######### END INSPECTION ############
/*
####### Make sure schema semantics are honored in transactions ######
*/
clopen();
JanusGraphTransaction tx2;
// shouldn't exist
assertEmpty(tx.query().has("uid", "v1").vertices());
JanusGraphVertex v = tx.addVertex();
// test property keys
v.property("uid", "v1");
v.property("weight", 1.5);
v.property("someid", "Hello");
v.property("name", "Bob");
v.property("name", "John");
VertexProperty p = v.property("value", 11);
p.property("weight", 22);
v.property("value", 33.3, "weight", 66.6);
// same values are supported for list-properties
v.property("value", 11, "weight", 22);
// test edges
JanusGraphVertex v12 = tx.addVertex("person"), v13 = tx.addVertex("person");
v12.property("uid", "v12");
v13.property("uid", "v13");
v12.addEdge("parent", v, "weight", 4.5);
v13.addEdge("parent", v, "weight", 4.5);
v.addEdge("child", v12);
v.addEdge("child", v13);
v.addEdge("spouse", v12);
v.addEdge("friend", v12);
// supports multi edges
v.addEdge("friend", v12);
v.addEdge("connect", v12, "uid", "e1");
v.addEdge("link", v13);
JanusGraphVertex v2 = tx.addVertex("tweet");
v2.addEdge("link", v13);
v12.addEdge("connect", v2);
JanusGraphEdge edge;
// ######### INSPECTION & FAILURE ############
assertEquals(v, getOnlyElement(tx.query().has("uid", Cmp.EQUAL, "v1").vertices()));
v = getOnlyVertex(tx.query().has("uid", Cmp.EQUAL, "v1"));
v12 = getOnlyVertex(tx.query().has("uid", Cmp.EQUAL, "v12"));
v13 = getOnlyVertex(tx.query().has("uid", Cmp.EQUAL, "v13"));
try {
// Invalid data type
v.property("weight", "x");
fail();
} catch (SchemaViolationException ignored) {
}
try {
// Only one "John" should be allowed
v.property(VertexProperty.Cardinality.list, "name", "John");
fail();
} catch (SchemaViolationException ignored) {
}
try {
// Cannot set a property as edge
v.property("link", v);
fail();
} catch (IllegalArgumentException ignored) {
}
// Only one property for weight allowed
v.property(single, "weight", 1.0);
assertCount(1, v.properties("weight"));
v.property(VertexProperty.Cardinality.single, "weight", 0.5);
assertEquals(0.5, v.<Float>value("weight").doubleValue(), 0.00001);
assertEquals("v1", v.value("uid"));
assertCount(2, v.properties("name"));
for (Object prop : v.query().labels("name").properties()) {
String nameString = ((JanusGraphVertexProperty<String>) prop).value();
assertTrue(nameString.equals("Bob") || nameString.equals("John"));
}
assertTrue(Iterators.size(v.properties("value")) >= 3);
for (Object o : v.query().labels("value").properties()) {
JanusGraphVertexProperty<Double> prop = (JanusGraphVertexProperty<Double>) o;
double prec = prop.value();
assertEquals(prec * 2, prop.<Number>value("weight").doubleValue(), 0.00001);
}
// Ensure we can add additional values
p = v.property("value", 44.4, "weight", 88.8);
assertEquals(v, getOnlyElement(tx.query().has("someid", Cmp.EQUAL, "Hello").vertices()));
// ------- EDGES -------
try {
// multiplicity violation
v12.addEdge("parent", v13);
fail();
} catch (SchemaViolationException ignored) {
}
try {
// multiplicity violation
v13.addEdge("child", v12);
fail();
} catch (SchemaViolationException ignored) {
}
try {
// multiplicity violation
v13.addEdge("spouse", v12);
fail();
} catch (SchemaViolationException ignored) {
}
try {
// multiplicity violation
v.addEdge("spouse", v13);
fail();
} catch (SchemaViolationException ignored) {
}
assertCount(2, v.query().direction(Direction.IN).labels("parent").edges());
assertCount(1, v12.query().direction(Direction.OUT).labels("parent").has("weight").edges());
assertCount(1, v13.query().direction(Direction.OUT).labels("parent").has("weight").edges());
assertEquals(v12, getOnlyElement(v.query().direction(Direction.OUT).labels("spouse").vertices()));
edge = Iterables.getOnlyElement(v.query().direction(Direction.BOTH).labels("connect").edges());
assertEquals(1, edge.keys().size());
assertEquals("e1", edge.value("uid"));
try {
// connect is simple
v.addEdge("connect", v12);
fail();
} catch (SchemaViolationException ignored) {
}
// Make sure "link" is unidirected
assertCount(1, v.query().direction(Direction.BOTH).labels("link").edges());
assertCount(0, v13.query().direction(Direction.BOTH).labels("link").edges());
// Assert we can add more friendships
v.addEdge("friend", v12);
v2 = Iterables.getOnlyElement(v12.query().direction(Direction.OUT).labels("connect").vertices());
assertEquals(v13, getOnlyElement(v2.query().direction(Direction.OUT).labels("link").vertices()));
assertEquals(BaseVertexLabel.DEFAULT_VERTEXLABEL.name(), v.label());
assertEquals("person", v12.label());
assertEquals("person", v13.label());
assertCount(4, tx.query().vertices());
// ######### END INSPECTION & FAILURE ############
clopen();
// ######### INSPECTION & FAILURE (copied from above) ############
assertEquals(v, getOnlyVertex(tx.query().has("uid", Cmp.EQUAL, "v1")));
v = getOnlyVertex(tx.query().has("uid", Cmp.EQUAL, "v1"));
v12 = getOnlyVertex(tx.query().has("uid", Cmp.EQUAL, "v12"));
v13 = getOnlyVertex(tx.query().has("uid", Cmp.EQUAL, "v13"));
try {
// Invalid data type
v.property("weight", "x");
fail();
} catch (SchemaViolationException ignored) {
}
try {
// Only one "John" should be allowed
v.property(VertexProperty.Cardinality.list, "name", "John");
fail();
} catch (SchemaViolationException ignored) {
}
try {
// Cannot set a property as edge
v.property("link", v);
fail();
} catch (IllegalArgumentException ignored) {
}
// Only one property for weight allowed
v.property(VertexProperty.Cardinality.single, "weight", 1.0);
assertCount(1, v.properties("weight"));
v.property(VertexProperty.Cardinality.single, "weight", 0.5);
assertEquals(0.5, v.<Float>value("weight").doubleValue(), 0.00001);
assertEquals("v1", v.value("uid"));
assertCount(2, v.properties("name"));
for (Object o : v.query().labels("name").properties()) {
JanusGraphVertexProperty<String> prop = (JanusGraphVertexProperty<String>) o;
String nameString = prop.value();
assertTrue(nameString.equals("Bob") || nameString.equals("John"));
}
assertTrue(Iterables.size(v.query().labels("value").properties()) >= 3);
for (Object o : v.query().labels("value").properties()) {
JanusGraphVertexProperty<Double> prop = (JanusGraphVertexProperty<Double>) o;
double prec = prop.value();
assertEquals(prec * 2, prop.<Number>value("weight").doubleValue(), 0.00001);
}
// Ensure we can add additional values
p = v.property("value", 44.4, "weight", 88.8);
assertEquals(v, getOnlyElement(tx.query().has("someid", Cmp.EQUAL, "Hello").vertices()));
// ------- EDGES -------
try {
// multiplicity violation
v12.addEdge("parent", v13);
fail();
} catch (SchemaViolationException ignored) {
}
try {
// multiplicity violation
v13.addEdge("child", v12);
fail();
} catch (SchemaViolationException ignored) {
}
try {
// multiplicity violation
v13.addEdge("spouse", v12);
fail();
} catch (SchemaViolationException ignored) {
}
try {
// multiplicity violation
v.addEdge("spouse", v13);
fail();
} catch (SchemaViolationException ignored) {
}
assertCount(2, v.query().direction(Direction.IN).labels("parent").edges());
assertCount(1, v12.query().direction(Direction.OUT).labels("parent").has("weight").edges());
assertCount(1, v13.query().direction(Direction.OUT).labels("parent").has("weight").edges());
assertEquals(v12, getOnlyElement(v.query().direction(Direction.OUT).labels("spouse").vertices()));
edge = Iterables.getOnlyElement(v.query().direction(Direction.BOTH).labels("connect").edges());
assertEquals(1, edge.keys().size());
assertEquals("e1", edge.value("uid"));
try {
// connect is simple
v.addEdge("connect", v12);
fail();
} catch (SchemaViolationException ignored) {
}
// Make sure "link" is unidirected
assertCount(1, v.query().direction(Direction.BOTH).labels("link").edges());
assertCount(0, v13.query().direction(Direction.BOTH).labels("link").edges());
// Assert we can add more friendships
v.addEdge("friend", v12);
v2 = Iterables.getOnlyElement(v12.query().direction(Direction.OUT).labels("connect").vertices());
assertEquals(v13, getOnlyElement(v2.query().direction(Direction.OUT).labels("link").vertices()));
assertEquals(BaseVertexLabel.DEFAULT_VERTEXLABEL.name(), v.label());
assertEquals("person", v12.label());
assertEquals("person", v13.label());
assertCount(4, tx.query().vertices());
// ######### END INSPECTION & FAILURE ############
// Ensure index uniqueness enforcement
tx2 = graph.newTransaction();
try {
JanusGraphVertex vx = tx2.addVertex();
try {
// property is unique
vx.property(VertexProperty.Cardinality.single, "uid", "v1");
fail();
} catch (SchemaViolationException ignored) {
}
vx.property(VertexProperty.Cardinality.single, "uid", "unique");
JanusGraphVertex vx2 = tx2.addVertex();
try {
// property unique
vx2.property(VertexProperty.Cardinality.single, "uid", "unique");
fail();
} catch (SchemaViolationException ignored) {
}
} finally {
tx2.rollback();
}
// Ensure that v2 is really static
v2 = getV(tx, v2);
assertEquals("tweet", v2.label());
try {
v2.property(VertexProperty.Cardinality.single, "weight", 11);
fail();
} catch (SchemaViolationException ignored) {
}
try {
v2.addEdge("friend", v12);
fail();
} catch (SchemaViolationException ignored) {
}
// Ensure that unidirected edges keep pointing to deleted vertices
getV(tx, v13).remove();
assertCount(1, v.query().direction(Direction.BOTH).labels("link").edges());
// Finally, test the schema container
SchemaContainer schemaContainer = new SchemaContainer(graph);
assertTrue(schemaContainer.containsRelationType("weight"));
assertTrue(schemaContainer.containsRelationType("friend"));
assertTrue(schemaContainer.containsVertexLabel("person"));
VertexLabelDefinition vld = schemaContainer.getVertexLabel("tag");
assertFalse(vld.isPartitioned());
assertFalse(vld.isStatic());
PropertyKeyDefinition pkd = schemaContainer.getPropertyKey("name");
assertEquals(Cardinality.SET, pkd.getCardinality());
assertEquals(String.class, pkd.getDataType());
EdgeLabelDefinition eld = schemaContainer.getEdgeLabel("child");
assertEquals("child", eld.getName());
assertEquals(child.longId(), eld.getLongId());
assertEquals(Multiplicity.ONE2MANY, eld.getMultiplicity());
assertFalse(eld.isUnidirected());
}
use of org.janusgraph.core.EdgeLabel in project janusgraph by JanusGraph.
the class JanusGraphTest method testTinkerPopOptimizationStrategies.
@Test
public void testTinkerPopOptimizationStrategies() {
PropertyKey id = mgmt.makePropertyKey("id").cardinality(Cardinality.SINGLE).dataType(Integer.class).make();
PropertyKey weight = mgmt.makePropertyKey("weight").cardinality(Cardinality.SINGLE).dataType(Integer.class).make();
mgmt.buildIndex("byId", Vertex.class).addKey(id).buildCompositeIndex();
mgmt.buildIndex("byWeight", Vertex.class).addKey(weight).buildCompositeIndex();
mgmt.buildIndex("byIdWeight", Vertex.class).addKey(id).addKey(weight).buildCompositeIndex();
EdgeLabel knows = mgmt.makeEdgeLabel("knows").make();
mgmt.buildEdgeIndex(knows, "byWeightDecr", Direction.OUT, decr, weight);
mgmt.buildEdgeIndex(knows, "byWeightIncr", Direction.OUT, incr, weight);
PropertyKey names = mgmt.makePropertyKey("names").cardinality(Cardinality.LIST).dataType(String.class).make();
mgmt.buildPropertyIndex(names, "namesByWeight", decr, weight);
finishSchema();
int numV = 100;
JanusGraphVertex[] vs = new JanusGraphVertex[numV];
for (int i = 0; i < numV; i++) {
vs[i] = graph.addVertex("id", i, "weight", i % 5);
}
int superV = 10;
int sid = -1;
JanusGraphVertex[] sv = new JanusGraphVertex[superV];
for (int i = 0; i < superV; i++) {
sv[i] = graph.addVertex("id", sid);
for (int j = 0; j < numV; j++) {
sv[i].addEdge("knows", vs[j], "weight", j % 5);
sv[i].property(VertexProperty.Cardinality.list, "names", "n" + j, "weight", j % 5);
}
}
Traversal t;
TraversalMetrics metrics;
GraphTraversalSource gts = graph.traversal();
// Edge
assertNumStep(numV / 5, 1, gts.V(sv[0]).outE("knows").has("weight", 1), JanusGraphVertexStep.class);
assertNumStep(numV, 1, gts.V(sv[0]).outE("knows"), JanusGraphVertexStep.class);
assertNumStep(numV, 1, gts.V(sv[0]).out("knows"), JanusGraphVertexStep.class);
assertNumStep(10, 1, gts.V(sv[0]).local(__.outE("knows").limit(10)), JanusGraphVertexStep.class);
assertNumStep(10, 1, gts.V(sv[0]).local(__.outE("knows").range(10, 20)), LocalStep.class);
assertNumStep(numV, 2, gts.V(sv[0]).outE("knows").order().by("weight", decr), JanusGraphVertexStep.class, OrderGlobalStep.class);
// Ensure the LocalStep is dropped because the Order can be folded in the JanusGraphVertexStep which in turn
// will allow JanusGraphLocalQueryOptimizationStrategy to drop the LocalStep as the local ordering will be
// provided by the single JanusGraphVertex step
assertNumStep(10, 0, gts.V(sv[0]).local(__.outE("knows").order().by("weight", decr).limit(10)), LocalStep.class);
assertNumStep(numV / 5, 2, gts.V(sv[0]).outE("knows").has("weight", 1).order().by("weight", incr), JanusGraphVertexStep.class, OrderGlobalStep.class);
assertNumStep(10, 0, gts.V(sv[0]).local(__.outE("knows").has("weight", 1).order().by("weight", incr).limit(10)), LocalStep.class);
// Note that for this test, the upper offset of the range will be folded into the JanusGraphVertexStep
// by JanusGraphLocalQueryOptimizationStrategy, but not the lower offset. The RangeGlobalStep will in turn be kept
// to enforce this lower bound and the LocalStep will be left as is as the local behavior will have not been
// entirely subsumed by the JanusGraphVertexStep
assertNumStep(5, 1, gts.V(sv[0]).local(__.outE("knows").has("weight", 1).has("weight", 1).order().by("weight", incr).range(10, 15)), LocalStep.class);
assertNumStep(1, 1, gts.V(sv[0]).outE("knows").filter(__.inV().is(vs[50])), JanusGraphVertexStep.class);
assertNumStep(1, 1, gts.V(sv[0]).outE("knows").filter(__.otherV().is(vs[50])), JanusGraphVertexStep.class);
assertNumStep(1, 1, gts.V(sv[0]).bothE("knows").filter(__.otherV().is(vs[50])), JanusGraphVertexStep.class);
assertNumStep(1, 2, gts.V(sv[0]).bothE("knows").filter(__.inV().is(vs[50])), JanusGraphVertexStep.class, TraversalFilterStep.class);
// Property
assertNumStep(numV / 5, 1, gts.V(sv[0]).properties("names").has("weight", 1), JanusGraphPropertiesStep.class);
assertNumStep(numV, 1, gts.V(sv[0]).properties("names"), JanusGraphPropertiesStep.class);
assertNumStep(10, 0, gts.V(sv[0]).local(__.properties("names").order().by("weight", decr).limit(10)), LocalStep.class);
assertNumStep(numV, 2, gts.V(sv[0]).outE("knows").values("weight"), JanusGraphVertexStep.class, JanusGraphPropertiesStep.class);
// Global graph queries
assertNumStep(1, 1, gts.V().has("id", numV / 5), JanusGraphStep.class);
assertNumStep(1, 1, gts.V().has("id", numV / 5).has("weight", (numV / 5) % 5), JanusGraphStep.class);
assertNumStep(numV / 5, 1, gts.V().has("weight", 1), JanusGraphStep.class);
assertNumStep(10, 1, gts.V().has("weight", 1).range(0, 10), JanusGraphStep.class);
assertNumStep(superV, 1, gts.V().has("id", sid), JanusGraphStep.class);
// Ensure that as steps don't interfere
assertNumStep(1, 1, gts.V().has("id", numV / 5).as("x"), JanusGraphStep.class);
assertNumStep(1, 1, gts.V().has("id", numV / 5).has("weight", (numV / 5) % 5).as("x"), JanusGraphStep.class);
assertNumStep(superV * (numV / 5), 2, gts.V().has("id", sid).outE("knows").has("weight", 1), JanusGraphStep.class, JanusGraphVertexStep.class);
assertNumStep(superV * (numV / 5 * 2), 2, gts.V().has("id", sid).outE("knows").has("weight", P.gte(1)).has("weight", P.lt(3)), JanusGraphStep.class, JanusGraphVertexStep.class);
assertNumStep(superV * (numV / 5 * 2), 2, gts.V().has("id", sid).outE("knows").has("weight", P.between(1, 3)), JanusGraphStep.class, JanusGraphVertexStep.class);
assertNumStep(superV * 10, 2, gts.V().has("id", sid).local(__.outE("knows").has("weight", P.gte(1)).has("weight", P.lt(3)).limit(10)), JanusGraphStep.class, JanusGraphVertexStep.class);
assertNumStep(superV * 10, 1, gts.V().has("id", sid).local(__.outE("knows").has("weight", P.between(1, 3)).order().by("weight", decr).limit(10)), JanusGraphStep.class);
assertNumStep(superV * 10, 0, gts.V().has("id", sid).local(__.outE("knows").has("weight", P.between(1, 3)).order().by("weight", decr).limit(10)), LocalStep.class);
clopen(option(USE_MULTIQUERY), true);
gts = graph.traversal();
assertNumStep(superV * (numV / 5), 2, gts.V().has("id", sid).outE("knows").has("weight", 1), JanusGraphStep.class, JanusGraphVertexStep.class);
assertNumStep(superV * (numV / 5 * 2), 2, gts.V().has("id", sid).outE("knows").has("weight", P.between(1, 3)), JanusGraphStep.class, JanusGraphVertexStep.class);
assertNumStep(superV * 10, 2, gts.V().has("id", sid).local(__.outE("knows").has("weight", P.gte(1)).has("weight", P.lt(3)).limit(10)), JanusGraphStep.class, JanusGraphVertexStep.class);
assertNumStep(superV * 10, 1, gts.V().has("id", sid).local(__.outE("knows").has("weight", P.between(1, 3)).order().by("weight", decr).limit(10)), JanusGraphStep.class);
assertNumStep(superV * 10, 0, gts.V().has("id", sid).local(__.outE("knows").has("weight", P.between(1, 3)).order().by("weight", decr).limit(10)), LocalStep.class);
assertNumStep(superV * numV, 2, gts.V().has("id", sid).values("names"), JanusGraphStep.class, JanusGraphPropertiesStep.class);
// Verify traversal metrics when all reads are from cache (i.e. no backend queries)
t = gts.V().has("id", sid).local(__.outE("knows").has("weight", P.between(1, 3)).order().by("weight", decr).limit(10)).profile("~metrics");
assertCount(superV * 10, t);
metrics = t.asAdmin().getSideEffects().get("~metrics");
// Verify that properties also use multi query
t = gts.V().has("id", sid).values("names").profile("~metrics");
assertCount(superV * numV, t);
metrics = t.asAdmin().getSideEffects().get("~metrics");
clopen(option(USE_MULTIQUERY), true);
gts = graph.traversal();
// Verify traversal metrics when having to read from backend [same query as above]
t = gts.V().has("id", sid).local(__.outE("knows").has("weight", P.gte(1)).has("weight", P.lt(3)).order().by("weight", decr).limit(10)).profile("~metrics");
assertCount(superV * 10, t);
metrics = t.asAdmin().getSideEffects().get("~metrics");
// Verify that properties also use multi query [same query as above]
t = gts.V().has("id", sid).values("names").profile("~metrics");
assertCount(superV * numV, t);
metrics = t.asAdmin().getSideEffects().get("~metrics");
}
Aggregations