use of org.janusgraph.core.VertexLabel in project janusgraph by JanusGraph.
the class JanusGraphIndexTest method testConditionalIndexing.
/**
* Tests conditional indexing and the different management features
*/
@Test
public void testConditionalIndexing() {
PropertyKey name = makeKey("name", String.class);
PropertyKey weight = makeKey("weight", Double.class);
PropertyKey text = makeKey("text", String.class);
VertexLabel person = mgmt.makeVertexLabel("person").make();
VertexLabel org = mgmt.makeVertexLabel("org").make();
JanusGraphIndex index1 = mgmt.buildIndex("index1", Vertex.class).addKey(name, getStringMapping()).buildMixedIndex(INDEX);
JanusGraphIndex index2 = mgmt.buildIndex("index2", Vertex.class).indexOnly(person).addKey(text, getTextMapping()).addKey(weight).buildMixedIndex(INDEX);
JanusGraphIndex index3 = mgmt.buildIndex("index3", Vertex.class).indexOnly(org).addKey(text, getTextMapping()).addKey(weight).buildMixedIndex(INDEX);
// ########### INSPECTION & FAILURE ##############
assertTrue(mgmt.containsGraphIndex("index1"));
assertFalse(mgmt.containsGraphIndex("index"));
assertCount(3, mgmt.getGraphIndexes(Vertex.class));
assertNull(mgmt.getGraphIndex("indexx"));
name = mgmt.getPropertyKey("name");
weight = mgmt.getPropertyKey("weight");
text = mgmt.getPropertyKey("text");
person = mgmt.getVertexLabel("person");
org = mgmt.getVertexLabel("org");
index1 = mgmt.getGraphIndex("index1");
index2 = mgmt.getGraphIndex("index2");
index3 = mgmt.getGraphIndex("index3");
assertTrue(Vertex.class.isAssignableFrom(index1.getIndexedElement()));
assertEquals("index2", index2.name());
assertEquals(INDEX, index3.getBackingIndex());
assertFalse(index2.isUnique());
assertEquals(2, index3.getFieldKeys().length);
assertEquals(1, index1.getFieldKeys().length);
assertEquals(3, index3.getParametersFor(text).length);
assertEquals(2, index3.getParametersFor(weight).length);
try {
// Already exists
mgmt.buildIndex("index2", Vertex.class).addKey(weight).buildMixedIndex(INDEX);
fail();
} catch (IllegalArgumentException ignored) {
}
try {
// Already exists
mgmt.buildIndex("index2", Vertex.class).addKey(weight).buildCompositeIndex();
fail();
} catch (IllegalArgumentException ignored) {
}
try {
// Key is already added
mgmt.addIndexKey(index2, weight);
fail();
} catch (IllegalArgumentException ignored) {
}
finishSchema();
clopen();
// ########### INSPECTION & FAILURE (copied from above) ##############
assertTrue(mgmt.containsGraphIndex("index1"));
assertFalse(mgmt.containsGraphIndex("index"));
assertCount(3, mgmt.getGraphIndexes(Vertex.class));
assertNull(mgmt.getGraphIndex("indexx"));
name = mgmt.getPropertyKey("name");
weight = mgmt.getPropertyKey("weight");
text = mgmt.getPropertyKey("text");
person = mgmt.getVertexLabel("person");
org = mgmt.getVertexLabel("org");
index1 = mgmt.getGraphIndex("index1");
index2 = mgmt.getGraphIndex("index2");
index3 = mgmt.getGraphIndex("index3");
assertTrue(Vertex.class.isAssignableFrom(index1.getIndexedElement()));
assertEquals("index2", index2.name());
assertEquals(INDEX, index3.getBackingIndex());
assertFalse(index2.isUnique());
assertEquals(2, index3.getFieldKeys().length);
assertEquals(1, index1.getFieldKeys().length);
assertEquals(3, index3.getParametersFor(text).length);
assertEquals(2, index3.getParametersFor(weight).length);
try {
// Already exists
mgmt.buildIndex("index2", Vertex.class).addKey(weight).buildMixedIndex(INDEX);
fail();
} catch (IllegalArgumentException ignored) {
}
try {
// Already exists
mgmt.buildIndex("index2", Vertex.class).addKey(weight).buildCompositeIndex();
fail();
} catch (IllegalArgumentException ignored) {
}
try {
// Key is already added
mgmt.addIndexKey(index2, weight);
fail();
} catch (IllegalArgumentException ignored) {
}
// ########### TRANSACTIONAL ##############
weight = tx.getPropertyKey("weight");
final int numV = 200;
String[] strings = { "houseboat", "humanoid", "differential", "extraordinary" };
String[] stringsTwo = new String[strings.length];
for (int i = 0; i < strings.length; i++) stringsTwo[i] = strings[i] + " " + strings[i];
final int modulo = 5;
assert numV % (modulo * strings.length * 2) == 0;
for (int i = 0; i < numV; i++) {
JanusGraphVertex v = tx.addVertex(i % 2 == 0 ? "person" : "org");
v.property("name", strings[i % strings.length]);
v.property("text", strings[i % strings.length]);
v.property("weight", (i % modulo) + 0.5);
}
// ########## QUERIES ################
evaluateQuery(tx.query().has("text", Text.CONTAINS, strings[0]).has(LABEL_NAME, Cmp.EQUAL, "person"), ElementCategory.VERTEX, numV / strings.length, new boolean[] { true, true }, index2.name());
evaluateQuery(tx.query().has("text", Text.CONTAINS, strings[0]).has(LABEL_NAME, Cmp.EQUAL, "person").orderBy("weight", decr), ElementCategory.VERTEX, numV / strings.length, new boolean[] { true, true }, weight, Order.DESC, index2.name());
evaluateQuery(tx.query().has("text", Text.CONTAINS, strings[3]).has(LABEL_NAME, Cmp.EQUAL, "org"), ElementCategory.VERTEX, numV / strings.length, new boolean[] { true, true }, index3.name());
evaluateQuery(tx.query().has("text", Text.CONTAINS, strings[1]).has(LABEL_NAME, Cmp.EQUAL, "org").orderBy("weight", decr), ElementCategory.VERTEX, numV / strings.length, new boolean[] { true, true }, weight, Order.DESC, index3.name());
evaluateQuery(tx.query().has("text", Text.CONTAINS, strings[0]).has("weight", Cmp.EQUAL, 2.5).has(LABEL_NAME, Cmp.EQUAL, "person"), ElementCategory.VERTEX, numV / (modulo * strings.length), new boolean[] { true, true }, index2.name());
evaluateQuery(tx.query().has("name", Cmp.EQUAL, strings[2]).has(LABEL_NAME, Cmp.EQUAL, "person"), ElementCategory.VERTEX, numV / strings.length, new boolean[] { false, true }, index1.name());
evaluateQuery(tx.query().has("name", Cmp.EQUAL, strings[3]).has(LABEL_NAME, Cmp.EQUAL, "person"), ElementCategory.VERTEX, 0, new boolean[] { false, true }, index1.name());
evaluateQuery(tx.query().has("name", Cmp.EQUAL, strings[0]), ElementCategory.VERTEX, numV / strings.length, new boolean[] { true, true }, index1.name());
evaluateQuery(tx.query().has("name", Cmp.EQUAL, strings[2]).has("text", Text.CONTAINS, strings[2]).has(LABEL_NAME, Cmp.EQUAL, "person"), ElementCategory.VERTEX, numV / strings.length, new boolean[] { true, true }, index1.name(), index2.name());
evaluateQuery(tx.query().has("name", Cmp.EQUAL, strings[0]).has("text", Text.CONTAINS, strings[0]).has(LABEL_NAME, Cmp.EQUAL, "person").orderBy("weight", incr), ElementCategory.VERTEX, numV / strings.length, new boolean[] { true, true }, weight, Order.ASC, index1.name(), index2.name());
evaluateQuery(tx.query().has("text", Text.CONTAINS, strings[0]), ElementCategory.VERTEX, numV / strings.length, new boolean[] { false, true });
evaluateQuery(tx.query().has("text", Text.CONTAINS, strings[0]).orderBy("weight", incr), ElementCategory.VERTEX, numV / strings.length, new boolean[] { false, false }, weight, Order.ASC);
clopen();
weight = tx.getPropertyKey("weight");
// ########## QUERIES (copied from above) ################
evaluateQuery(tx.query().has("text", Text.CONTAINS, strings[0]).has(LABEL_NAME, Cmp.EQUAL, "person"), ElementCategory.VERTEX, numV / strings.length, new boolean[] { true, true }, index2.name());
evaluateQuery(tx.query().has("text", Text.CONTAINS, strings[0]).has(LABEL_NAME, Cmp.EQUAL, "person").orderBy("weight", decr), ElementCategory.VERTEX, numV / strings.length, new boolean[] { true, true }, weight, Order.DESC, index2.name());
evaluateQuery(tx.query().has("text", Text.CONTAINS, strings[3]).has(LABEL_NAME, Cmp.EQUAL, "org"), ElementCategory.VERTEX, numV / strings.length, new boolean[] { true, true }, index3.name());
evaluateQuery(tx.query().has("text", Text.CONTAINS, strings[1]).has(LABEL_NAME, Cmp.EQUAL, "org").orderBy("weight", decr), ElementCategory.VERTEX, numV / strings.length, new boolean[] { true, true }, weight, Order.DESC, index3.name());
evaluateQuery(tx.query().has("text", Text.CONTAINS, strings[0]).has("weight", Cmp.EQUAL, 2.5).has(LABEL_NAME, Cmp.EQUAL, "person"), ElementCategory.VERTEX, numV / (modulo * strings.length), new boolean[] { true, true }, index2.name());
evaluateQuery(tx.query().has("name", Cmp.EQUAL, strings[2]).has(LABEL_NAME, Cmp.EQUAL, "person"), ElementCategory.VERTEX, numV / strings.length, new boolean[] { false, true }, index1.name());
evaluateQuery(tx.query().has("name", Cmp.EQUAL, strings[3]).has(LABEL_NAME, Cmp.EQUAL, "person"), ElementCategory.VERTEX, 0, new boolean[] { false, true }, index1.name());
evaluateQuery(tx.query().has("name", Cmp.EQUAL, strings[0]), ElementCategory.VERTEX, numV / strings.length, new boolean[] { true, true }, index1.name());
evaluateQuery(tx.query().has("name", Cmp.EQUAL, strings[2]).has("text", Text.CONTAINS, strings[2]).has(LABEL_NAME, Cmp.EQUAL, "person"), ElementCategory.VERTEX, numV / strings.length, new boolean[] { true, true }, index1.name(), index2.name());
evaluateQuery(tx.query().has("name", Cmp.EQUAL, strings[0]).has("text", Text.CONTAINS, strings[0]).has(LABEL_NAME, Cmp.EQUAL, "person").orderBy("weight", incr), ElementCategory.VERTEX, numV / strings.length, new boolean[] { true, true }, weight, Order.ASC, index1.name(), index2.name());
evaluateQuery(tx.query().has("text", Text.CONTAINS, strings[0]), ElementCategory.VERTEX, numV / strings.length, new boolean[] { false, true });
evaluateQuery(tx.query().has("text", Text.CONTAINS, strings[0]).orderBy("weight", incr), ElementCategory.VERTEX, numV / strings.length, new boolean[] { false, false }, weight, Order.ASC);
}
use of org.janusgraph.core.VertexLabel in project janusgraph by JanusGraph.
the class JanusGraphTest 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();
final JanusGraphIndex vertexIndex1 = mgmt.buildIndex("vindex1", Vertex.class).addKey(time).indexOnly(person).unique().buildCompositeIndex();
final JanusGraphIndex vertexIndex2 = mgmt.buildIndex("vindex2", Vertex.class).addKey(time).addKey(text).unique().buildCompositeIndex();
finishSchema();
// ================== VERTEX UNIQUENESS ====================
// I) Label uniqueness
// Ia) Uniqueness violation in same transaction
failTransactionOnCommit(tx -> {
final JanusGraphVertex v0 = tx.addVertex("person");
v0.property(VertexProperty.Cardinality.single, "time", 1);
final JanusGraphVertex v1 = tx.addVertex("person");
v1.property(VertexProperty.Cardinality.single, "time", 1);
});
// Ib) Uniqueness violation across transactions
JanusGraphVertex v0 = tx.addVertex("person");
v0.property(VertexProperty.Cardinality.single, "time", 1);
newTx();
failTransactionOnCommit(tx -> {
final JanusGraphVertex v1 = tx.addVertex("person");
v1.property(VertexProperty.Cardinality.single, "time", 1);
});
// Ic) However, this should work since the label is different
final JanusGraphVertex v1 = tx.addVertex("organization");
v1.property(VertexProperty.Cardinality.single, "time", 1);
newTx();
// II) Composite uniqueness
// IIa) Uniqueness violation in same transaction
failTransactionOnCommit(tx -> {
final JanusGraphVertex v01 = tx.addVertex("time", 2, "text", "hello");
final JanusGraphVertex v11 = tx.addVertex("time", 2, "text", "hello");
});
// IIb) Uniqueness violation across transactions
v0 = tx.addVertex("time", 2, "text", "hello");
newTx();
failTransactionOnCommit(tx -> {
final JanusGraphVertex v112 = tx.addVertex("time", 2, "text", "hello");
});
}
use of org.janusgraph.core.VertexLabel in project janusgraph by JanusGraph.
the class JanusGraphTest 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));
final JanusGraphIndex index1 = mgmt.buildIndex("index1", Vertex.class).addKey(name).buildCompositeIndex();
final JanusGraphIndex 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();
JanusGraphVertex 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 org.janusgraph.core.VertexLabel in project janusgraph by JanusGraph.
the class JanusGraphTest method testIndexQueryWithLabelsAndContainsIN.
@Test
public void testIndexQueryWithLabelsAndContainsIN() {
// This test is based on the steps to reproduce #882
String labelName = "labelName";
VertexLabel label = mgmt.makeVertexLabel(labelName).make();
PropertyKey uid = mgmt.makePropertyKey("uid").dataType(String.class).make();
JanusGraphIndex uidCompositeIndex = mgmt.buildIndex("uidIndex", Vertex.class).indexOnly(label).addKey(uid).unique().buildCompositeIndex();
mgmt.setConsistency(uidCompositeIndex, ConsistencyModifier.LOCK);
finishSchema();
JanusGraphVertex foo = graph.addVertex(labelName);
JanusGraphVertex bar = graph.addVertex(labelName);
foo.property("uid", "foo");
bar.property("uid", "bar");
graph.tx().commit();
Iterable<JanusGraphVertex> vertexes = graph.query().has("uid", Contain.IN, ImmutableList.of("foo", "bar")).has(LABEL_NAME, labelName).vertices();
assertEquals(2, Iterables.size(vertexes));
for (JanusGraphVertex v : vertexes) {
assertEquals(labelName, v.vertexLabel().name());
}
}
use of org.janusgraph.core.VertexLabel in project janusgraph by JanusGraph.
the class AbstractJanusGraphProvider method createIndices.
private void createIndices(final JanusGraph g, final LoadGraphWith.GraphData graphData) {
JanusGraphManagement management = g.openManagement();
if (graphData.equals(LoadGraphWith.GraphData.GRATEFUL)) {
VertexLabel artist = management.makeVertexLabel("artist").make();
VertexLabel song = management.makeVertexLabel("song").make();
PropertyKey name = management.makePropertyKey("name").cardinality(Cardinality.LIST).dataType(String.class).make();
PropertyKey songType = management.makePropertyKey("songType").cardinality(Cardinality.LIST).dataType(String.class).make();
PropertyKey performances = management.makePropertyKey("performances").cardinality(Cardinality.LIST).dataType(Integer.class).make();
management.buildIndex("artistByName", Vertex.class).addKey(name).indexOnly(artist).buildCompositeIndex();
management.buildIndex("songByName", Vertex.class).addKey(name).indexOnly(song).buildCompositeIndex();
management.buildIndex("songByType", Vertex.class).addKey(songType).indexOnly(song).buildCompositeIndex();
management.buildIndex("songByPerformances", Vertex.class).addKey(performances).indexOnly(song).buildCompositeIndex();
} else if (graphData.equals(LoadGraphWith.GraphData.MODERN)) {
VertexLabel person = management.makeVertexLabel("person").make();
VertexLabel software = management.makeVertexLabel("software").make();
PropertyKey name = management.makePropertyKey("name").cardinality(Cardinality.LIST).dataType(String.class).make();
PropertyKey lang = management.makePropertyKey("lang").cardinality(Cardinality.LIST).dataType(String.class).make();
PropertyKey age = management.makePropertyKey("age").cardinality(Cardinality.LIST).dataType(Integer.class).make();
management.buildIndex("personByName", Vertex.class).addKey(name).indexOnly(person).buildCompositeIndex();
management.buildIndex("softwareByName", Vertex.class).addKey(name).indexOnly(software).buildCompositeIndex();
management.buildIndex("personByAge", Vertex.class).addKey(age).indexOnly(person).buildCompositeIndex();
management.buildIndex("softwareByLang", Vertex.class).addKey(lang).indexOnly(software).buildCompositeIndex();
} else if (graphData.equals(LoadGraphWith.GraphData.CLASSIC)) {
PropertyKey name = management.makePropertyKey("name").cardinality(Cardinality.LIST).dataType(String.class).make();
PropertyKey lang = management.makePropertyKey("lang").cardinality(Cardinality.LIST).dataType(String.class).make();
PropertyKey age = management.makePropertyKey("age").cardinality(Cardinality.LIST).dataType(Integer.class).make();
management.buildIndex("byName", Vertex.class).addKey(name).buildCompositeIndex();
management.buildIndex("byAge", Vertex.class).addKey(age).buildCompositeIndex();
management.buildIndex("byLang", Vertex.class).addKey(lang).buildCompositeIndex();
} else {
// TODO: add CREW work here.
// TODO: add meta_property indices when meta_property graph is provided
// throw new RuntimeException("Could not load graph with " + graphData);
}
management.commit();
}
Aggregations