use of org.janusgraph.core.schema.JanusGraphIndex in project janusgraph by JanusGraph.
the class ManagementSystem method changeName.
@Override
public void changeName(JanusGraphSchemaElement element, String newName) {
Preconditions.checkArgument(StringUtils.isNotBlank(newName), "Invalid name: %s", newName);
JanusGraphSchemaVertex schemaVertex = getSchemaVertex(element);
String oldName = schemaVertex.name();
if (oldName.equals(newName))
return;
JanusGraphSchemaCategory schemaCategory = schemaVertex.valueOrNull(BaseKey.SchemaCategory);
Preconditions.checkArgument(schemaCategory.hasName(), "Invalid schema element: %s", element);
if (schemaVertex instanceof RelationType) {
InternalRelationType relType = (InternalRelationType) schemaVertex;
if (relType.getBaseType() != null) {
newName = composeRelationTypeIndexName(relType.getBaseType(), newName);
} else
assert !(element instanceof RelationTypeIndex);
JanusGraphSchemaCategory cat = relType.isEdgeLabel() ? JanusGraphSchemaCategory.EDGELABEL : JanusGraphSchemaCategory.PROPERTYKEY;
SystemTypeManager.throwIfSystemName(cat, newName);
} else if (element instanceof VertexLabel) {
SystemTypeManager.throwIfSystemName(JanusGraphSchemaCategory.VERTEXLABEL, newName);
} else if (element instanceof JanusGraphIndex) {
checkIndexName(newName);
}
transaction.addProperty(schemaVertex, BaseKey.SchemaName, schemaCategory.getSchemaName(newName));
updateConnectionEdgeConstraints(schemaVertex, oldName, newName);
updateSchemaVertex(schemaVertex);
schemaVertex.resetCache();
updatedTypes.add(schemaVertex);
}
use of org.janusgraph.core.schema.JanusGraphIndex in project janusgraph by JanusGraph.
the class JanusGraphTest method testGlobalGraphIndexingAndQueriesForInternalIndexes.
/* ==================================================================================
GLOBAL GRAPH QUERIES
==================================================================================*/
/**
* Tests index definitions and their correct application for internal indexes only
*/
@Test
public void testGlobalGraphIndexingAndQueriesForInternalIndexes() {
PropertyKey weight = makeKey("weight", Float.class);
PropertyKey time = makeKey("time", Long.class);
PropertyKey text = makeKey("text", String.class);
PropertyKey name = mgmt.makePropertyKey("name").dataType(String.class).cardinality(Cardinality.LIST).make();
EdgeLabel connect = mgmt.makeEdgeLabel("connect").signature(weight).make();
EdgeLabel related = mgmt.makeEdgeLabel("related").signature(time).make();
VertexLabel person = mgmt.makeVertexLabel("person").make();
VertexLabel organization = mgmt.makeVertexLabel("organization").make();
JanusGraphIndex edge1 = mgmt.buildIndex("edge1", Edge.class).addKey(time).addKey(weight).buildCompositeIndex();
JanusGraphIndex edge2 = mgmt.buildIndex("edge2", Edge.class).indexOnly(connect).addKey(text).buildCompositeIndex();
JanusGraphIndex prop1 = mgmt.buildIndex("prop1", JanusGraphVertexProperty.class).addKey(time).buildCompositeIndex();
JanusGraphIndex prop2 = mgmt.buildIndex("prop2", JanusGraphVertexProperty.class).addKey(weight).addKey(text).buildCompositeIndex();
JanusGraphIndex vertex1 = mgmt.buildIndex("vertex1", Vertex.class).addKey(time).indexOnly(person).unique().buildCompositeIndex();
JanusGraphIndex vertex12 = mgmt.buildIndex("vertex12", Vertex.class).addKey(text).indexOnly(person).buildCompositeIndex();
JanusGraphIndex vertex2 = mgmt.buildIndex("vertex2", Vertex.class).addKey(time).addKey(name).indexOnly(organization).buildCompositeIndex();
JanusGraphIndex vertex3 = mgmt.buildIndex("vertex3", Vertex.class).addKey(name).buildCompositeIndex();
// ########### INSPECTION & FAILURE ##############
assertTrue(mgmt.containsRelationType("name"));
assertTrue(mgmt.containsGraphIndex("prop1"));
assertFalse(mgmt.containsGraphIndex("prop3"));
assertEquals(2, Iterables.size(mgmt.getGraphIndexes(Edge.class)));
assertEquals(2, Iterables.size(mgmt.getGraphIndexes(JanusGraphVertexProperty.class)));
assertEquals(4, Iterables.size(mgmt.getGraphIndexes(Vertex.class)));
assertNull(mgmt.getGraphIndex("balblub"));
edge1 = mgmt.getGraphIndex("edge1");
edge2 = mgmt.getGraphIndex("edge2");
prop1 = mgmt.getGraphIndex("prop1");
prop2 = mgmt.getGraphIndex("prop2");
vertex1 = mgmt.getGraphIndex("vertex1");
vertex12 = mgmt.getGraphIndex("vertex12");
vertex2 = mgmt.getGraphIndex("vertex2");
vertex3 = mgmt.getGraphIndex("vertex3");
assertTrue(vertex1.isUnique());
assertFalse(edge2.isUnique());
assertEquals("prop1", prop1.name());
assertTrue(Vertex.class.isAssignableFrom(vertex3.getIndexedElement()));
assertTrue(JanusGraphVertexProperty.class.isAssignableFrom(prop1.getIndexedElement()));
assertTrue(Edge.class.isAssignableFrom(edge2.getIndexedElement()));
assertEquals(2, vertex2.getFieldKeys().length);
assertEquals(1, vertex1.getFieldKeys().length);
try {
// Parameters not supported
mgmt.buildIndex("blablub", Vertex.class).addKey(text, Mapping.TEXT.asParameter()).buildCompositeIndex();
fail();
} catch (IllegalArgumentException ignored) {
}
try {
// Name already in use
mgmt.buildIndex("edge1", Vertex.class).addKey(weight).buildCompositeIndex();
fail();
} catch (IllegalArgumentException ignored) {
}
try {
// ImplicitKeys not allowed
mgmt.buildIndex("jupdup", Vertex.class).addKey(ImplicitKey.ID).buildCompositeIndex();
fail();
} catch (IllegalArgumentException ignored) {
}
try {
// Unique is only allowed for vertex
mgmt.buildIndex("edgexyz", Edge.class).addKey(time).unique().buildCompositeIndex();
fail();
} catch (IllegalArgumentException ignored) {
}
// ########### END INSPECTION & FAILURE ##############
finishSchema();
clopen();
text = mgmt.getPropertyKey("text");
time = mgmt.getPropertyKey("time");
weight = mgmt.getPropertyKey("weight");
// ########### INSPECTION & FAILURE (copied from above) ##############
assertTrue(mgmt.containsRelationType("name"));
assertTrue(mgmt.containsGraphIndex("prop1"));
assertFalse(mgmt.containsGraphIndex("prop3"));
assertEquals(2, Iterables.size(mgmt.getGraphIndexes(Edge.class)));
assertEquals(2, Iterables.size(mgmt.getGraphIndexes(JanusGraphVertexProperty.class)));
assertEquals(4, Iterables.size(mgmt.getGraphIndexes(Vertex.class)));
assertNull(mgmt.getGraphIndex("balblub"));
edge1 = mgmt.getGraphIndex("edge1");
edge2 = mgmt.getGraphIndex("edge2");
prop1 = mgmt.getGraphIndex("prop1");
prop2 = mgmt.getGraphIndex("prop2");
vertex1 = mgmt.getGraphIndex("vertex1");
vertex12 = mgmt.getGraphIndex("vertex12");
vertex2 = mgmt.getGraphIndex("vertex2");
vertex3 = mgmt.getGraphIndex("vertex3");
assertTrue(vertex1.isUnique());
assertFalse(edge2.isUnique());
assertEquals("prop1", prop1.name());
assertTrue(Vertex.class.isAssignableFrom(vertex3.getIndexedElement()));
assertTrue(JanusGraphVertexProperty.class.isAssignableFrom(prop1.getIndexedElement()));
assertTrue(Edge.class.isAssignableFrom(edge2.getIndexedElement()));
assertEquals(2, vertex2.getFieldKeys().length);
assertEquals(1, vertex1.getFieldKeys().length);
try {
// Parameters not supported
mgmt.buildIndex("blablub", Vertex.class).addKey(text, Mapping.TEXT.asParameter()).buildCompositeIndex();
fail();
} catch (IllegalArgumentException ignored) {
}
try {
// Name already in use
mgmt.buildIndex("edge1", Vertex.class).addKey(weight).buildCompositeIndex();
fail();
} catch (IllegalArgumentException ignored) {
}
try {
// ImplicitKeys not allowed
mgmt.buildIndex("jupdup", Vertex.class).addKey(ImplicitKey.ID).buildCompositeIndex();
fail();
} catch (IllegalArgumentException ignored) {
}
try {
// Unique is only allowed for vertex
mgmt.buildIndex("edgexyz", Edge.class).addKey(time).unique().buildCompositeIndex();
fail();
} catch (IllegalArgumentException ignored) {
}
// ########### END INSPECTION & FAILURE ##############
final int numV = 100;
final boolean sorted = true;
JanusGraphVertex[] ns = new JanusGraphVertex[numV];
String[] strings = { "aaa", "bbb", "ccc", "ddd" };
for (int i = 0; i < numV; i++) {
ns[i] = tx.addVertex(i % 2 == 0 ? "person" : "organization");
VertexProperty p1 = ns[i].property("name", "v" + i);
VertexProperty p2 = ns[i].property("name", "u" + (i % 5));
double w = (i * 0.5) % 5;
String txt = strings[i % (strings.length)];
ns[i].property(VertexProperty.Cardinality.single, "weight", w);
ns[i].property(VertexProperty.Cardinality.single, "time", (long) i);
ns[i].property(VertexProperty.Cardinality.single, "text", txt);
for (VertexProperty p : new VertexProperty[] { p1, p2 }) {
p.property("weight", w);
p.property("time", (long) i);
p.property("text", txt);
}
// previous or self-loop
JanusGraphVertex u = ns[(i > 0 ? i - 1 : i)];
for (String label : new String[] { "connect", "related" }) {
Edge e = ns[i].addEdge(label, u, "weight", (w++) % 5, "time", (long) i, "text", txt);
}
}
// ########## QUERIES ################
evaluateQuery(tx.query().has("time", Cmp.EQUAL, 10).has("weight", Cmp.EQUAL, 0), ElementCategory.EDGE, 1, new boolean[] { true, sorted }, edge1.name());
evaluateQuery(tx.query().has("time", Contain.IN, ImmutableList.of(10, 20, 30)).has("weight", Cmp.EQUAL, 0), ElementCategory.EDGE, 3, new boolean[] { true, sorted }, edge1.name());
evaluateQuery(tx.query().has("time", Cmp.EQUAL, 10).has("weight", Cmp.EQUAL, 0).has("text", Cmp.EQUAL, strings[10 % strings.length]), ElementCategory.EDGE, 1, new boolean[] { false, sorted }, edge1.name());
evaluateQuery(tx.query().has("time", Cmp.EQUAL, 10).has("weight", Cmp.EQUAL, 1), ElementCategory.EDGE, 1, new boolean[] { true, sorted }, edge1.name());
evaluateQuery(tx.query().has("time", Cmp.EQUAL, 20).has("weight", Cmp.EQUAL, 0), ElementCategory.EDGE, 1, new boolean[] { true, sorted }, edge1.name());
evaluateQuery(tx.query().has("time", Cmp.EQUAL, 20).has("weight", Cmp.EQUAL, 3), ElementCategory.EDGE, 0, new boolean[] { true, sorted }, edge1.name());
evaluateQuery(tx.query().has("text", Cmp.EQUAL, strings[0]).has(LABEL_NAME, "connect"), ElementCategory.EDGE, numV / strings.length, new boolean[] { true, sorted }, edge2.name());
evaluateQuery(tx.query().has("text", Cmp.EQUAL, strings[0]).has(LABEL_NAME, "connect").limit(10), ElementCategory.EDGE, 10, new boolean[] { true, sorted }, edge2.name());
evaluateQuery(tx.query().has("text", Cmp.EQUAL, strings[0]), ElementCategory.EDGE, numV / strings.length * 2, new boolean[] { false, sorted });
evaluateQuery(tx.query().has("weight", Cmp.EQUAL, 1.5), ElementCategory.EDGE, numV / 10 * 2, new boolean[] { false, sorted });
evaluateQuery(tx.query().has("time", Cmp.EQUAL, 50), ElementCategory.PROPERTY, 2, new boolean[] { true, sorted }, prop1.name());
evaluateQuery(tx.query().has("weight", Cmp.EQUAL, 0.0).has("text", Cmp.EQUAL, strings[0]), ElementCategory.PROPERTY, 2 * numV / (4 * 5), new boolean[] { true, sorted }, prop2.name());
evaluateQuery(tx.query().has("weight", Cmp.EQUAL, 0.0).has("text", Cmp.EQUAL, strings[0]).has("time", Cmp.EQUAL, 0), ElementCategory.PROPERTY, 2, new boolean[] { true, sorted }, prop2.name(), prop1.name());
evaluateQuery(tx.query().has("weight", Cmp.EQUAL, 1.5), ElementCategory.PROPERTY, 2 * numV / 10, new boolean[] { false, sorted });
evaluateQuery(tx.query().has("time", Cmp.EQUAL, 50).has(LABEL_NAME, "person"), ElementCategory.VERTEX, 1, new boolean[] { true, sorted }, vertex1.name());
evaluateQuery(tx.query().has("text", Cmp.EQUAL, strings[2]).has(LABEL_NAME, "person"), ElementCategory.VERTEX, numV / strings.length, new boolean[] { true, sorted }, vertex12.name());
evaluateQuery(tx.query().has("text", Cmp.EQUAL, strings[3]).has(LABEL_NAME, "person"), ElementCategory.VERTEX, 0, new boolean[] { true, sorted }, vertex12.name());
evaluateQuery(tx.query().has("text", Cmp.EQUAL, strings[2]).has(LABEL_NAME, "person").has("time", Cmp.EQUAL, 2), ElementCategory.VERTEX, 1, new boolean[] { true, sorted }, vertex12.name(), vertex1.name());
evaluateQuery(tx.query().has("time", Cmp.EQUAL, 51).has("name", Cmp.EQUAL, "v51").has(LABEL_NAME, "organization"), ElementCategory.VERTEX, 1, new boolean[] { true, sorted }, vertex2.name());
evaluateQuery(tx.query().has("time", Cmp.EQUAL, 51).has("name", Cmp.EQUAL, "u1").has(LABEL_NAME, "organization"), ElementCategory.VERTEX, 1, new boolean[] { true, sorted }, vertex2.name());
evaluateQuery(tx.query().has("time", Contain.IN, ImmutableList.of(51, 61, 71, 31, 41)).has("name", Cmp.EQUAL, "u1").has(LABEL_NAME, "organization"), ElementCategory.VERTEX, 5, new boolean[] { true, sorted }, vertex2.name());
evaluateQuery(tx.query().has("time", Contain.IN, ImmutableList.of()), ElementCategory.VERTEX, 0, new boolean[] { true, false });
// this query is not fitted because NOT_IN must be filtered in-memory to satisfy has("time")
evaluateQuery(tx.query().has("text", Cmp.EQUAL, strings[2]).has(LABEL_NAME, "person").has("time", Contain.NOT_IN, ImmutableList.of()), ElementCategory.VERTEX, numV / strings.length, new boolean[] { false, sorted }, vertex12.name());
evaluateQuery(tx.query().has("time", Cmp.EQUAL, 51).has(LABEL_NAME, "organization"), ElementCategory.VERTEX, 1, new boolean[] { false, sorted });
evaluateQuery(tx.query().has("name", Cmp.EQUAL, "u1"), ElementCategory.VERTEX, numV / 5, new boolean[] { true, sorted }, vertex3.name());
evaluateQuery(tx.query().has("name", Cmp.EQUAL, "v1"), ElementCategory.VERTEX, 1, new boolean[] { true, sorted }, vertex3.name());
evaluateQuery(tx.query().has("name", Cmp.EQUAL, "v1").has(LABEL_NAME, "organization"), ElementCategory.VERTEX, 1, new boolean[] { false, sorted }, vertex3.name());
clopen();
// ########## QUERIES (copied from above) ################
evaluateQuery(tx.query().has("time", Cmp.EQUAL, 10).has("weight", Cmp.EQUAL, 0), ElementCategory.EDGE, 1, new boolean[] { true, sorted }, edge1.name());
evaluateQuery(tx.query().has("time", Contain.IN, ImmutableList.of(10, 20, 30)).has("weight", Cmp.EQUAL, 0), ElementCategory.EDGE, 3, new boolean[] { true, sorted }, edge1.name());
evaluateQuery(tx.query().has("time", Cmp.EQUAL, 10).has("weight", Cmp.EQUAL, 0).has("text", Cmp.EQUAL, strings[10 % strings.length]), ElementCategory.EDGE, 1, new boolean[] { false, sorted }, edge1.name());
evaluateQuery(tx.query().has("time", Cmp.EQUAL, 10).has("weight", Cmp.EQUAL, 1), ElementCategory.EDGE, 1, new boolean[] { true, sorted }, edge1.name());
evaluateQuery(tx.query().has("time", Cmp.EQUAL, 20).has("weight", Cmp.EQUAL, 0), ElementCategory.EDGE, 1, new boolean[] { true, sorted }, edge1.name());
evaluateQuery(tx.query().has("time", Cmp.EQUAL, 20).has("weight", Cmp.EQUAL, 3), ElementCategory.EDGE, 0, new boolean[] { true, sorted }, edge1.name());
evaluateQuery(tx.query().has("text", Cmp.EQUAL, strings[0]).has(LABEL_NAME, "connect"), ElementCategory.EDGE, numV / strings.length, new boolean[] { true, sorted }, edge2.name());
evaluateQuery(tx.query().has("text", Cmp.EQUAL, strings[0]).has(LABEL_NAME, "connect").limit(10), ElementCategory.EDGE, 10, new boolean[] { true, sorted }, edge2.name());
evaluateQuery(tx.query().has("text", Cmp.EQUAL, strings[0]), ElementCategory.EDGE, numV / strings.length * 2, new boolean[] { false, sorted });
evaluateQuery(tx.query().has("weight", Cmp.EQUAL, 1.5), ElementCategory.EDGE, numV / 10 * 2, new boolean[] { false, sorted });
evaluateQuery(tx.query().has("time", Cmp.EQUAL, 50), ElementCategory.PROPERTY, 2, new boolean[] { true, sorted }, prop1.name());
evaluateQuery(tx.query().has("weight", Cmp.EQUAL, 0.0).has("text", Cmp.EQUAL, strings[0]), ElementCategory.PROPERTY, 2 * numV / (4 * 5), new boolean[] { true, sorted }, prop2.name());
evaluateQuery(tx.query().has("weight", Cmp.EQUAL, 0.0).has("text", Cmp.EQUAL, strings[0]).has("time", Cmp.EQUAL, 0), ElementCategory.PROPERTY, 2, new boolean[] { true, sorted }, prop2.name(), prop1.name());
evaluateQuery(tx.query().has("weight", Cmp.EQUAL, 1.5), ElementCategory.PROPERTY, 2 * numV / 10, new boolean[] { false, sorted });
evaluateQuery(tx.query().has("time", Cmp.EQUAL, 50).has(LABEL_NAME, "person"), ElementCategory.VERTEX, 1, new boolean[] { true, sorted }, vertex1.name());
evaluateQuery(tx.query().has("text", Cmp.EQUAL, strings[2]).has(LABEL_NAME, "person"), ElementCategory.VERTEX, numV / strings.length, new boolean[] { true, sorted }, vertex12.name());
evaluateQuery(tx.query().has("text", Cmp.EQUAL, strings[3]).has(LABEL_NAME, "person"), ElementCategory.VERTEX, 0, new boolean[] { true, sorted }, vertex12.name());
evaluateQuery(tx.query().has("text", Cmp.EQUAL, strings[2]).has(LABEL_NAME, "person").has("time", Cmp.EQUAL, 2), ElementCategory.VERTEX, 1, new boolean[] { true, sorted }, vertex12.name(), vertex1.name());
evaluateQuery(tx.query().has("time", Cmp.EQUAL, 51).has("name", Cmp.EQUAL, "v51").has(LABEL_NAME, "organization"), ElementCategory.VERTEX, 1, new boolean[] { true, sorted }, vertex2.name());
evaluateQuery(tx.query().has("time", Cmp.EQUAL, 51).has("name", Cmp.EQUAL, "u1").has(LABEL_NAME, "organization"), ElementCategory.VERTEX, 1, new boolean[] { true, sorted }, vertex2.name());
evaluateQuery(tx.query().has("time", Contain.IN, ImmutableList.of(51, 61, 71, 31, 41)).has("name", Cmp.EQUAL, "u1").has(LABEL_NAME, "organization"), ElementCategory.VERTEX, 5, new boolean[] { true, sorted }, vertex2.name());
evaluateQuery(tx.query().has("time", Cmp.EQUAL, 51).has(LABEL_NAME, "organization"), ElementCategory.VERTEX, 1, new boolean[] { false, sorted });
evaluateQuery(tx.query().has("name", Cmp.EQUAL, "u1"), ElementCategory.VERTEX, numV / 5, new boolean[] { true, sorted }, vertex3.name());
evaluateQuery(tx.query().has("name", Cmp.EQUAL, "v1"), ElementCategory.VERTEX, 1, new boolean[] { true, sorted }, vertex3.name());
evaluateQuery(tx.query().has("name", Cmp.EQUAL, "v1").has(LABEL_NAME, "organization"), ElementCategory.VERTEX, 1, new boolean[] { false, sorted }, vertex3.name());
evaluateQuery(tx.query().has("time", Contain.IN, ImmutableList.of()), ElementCategory.VERTEX, 0, new boolean[] { true, false });
// this query is not fitted because NOT_IN must be filtered in-memory to satisfy has("time")
evaluateQuery(tx.query().has("text", Cmp.EQUAL, strings[2]).has(LABEL_NAME, "person").has("time", Contain.NOT_IN, ImmutableList.of()), ElementCategory.VERTEX, numV / strings.length, new boolean[] { false, sorted }, vertex12.name());
// Update in transaction
for (int i = 0; i < numV / 2; i++) {
JanusGraphVertex v = getV(tx, ns[i]);
v.remove();
}
ns = new JanusGraphVertex[numV * 3 / 2];
for (int i = numV; i < numV * 3 / 2; i++) {
ns[i] = tx.addVertex(i % 2 == 0 ? "person" : "organization");
VertexProperty p1 = ns[i].property("name", "v" + i);
VertexProperty p2 = ns[i].property("name", "u" + (i % 5));
double w = (i * 0.5) % 5;
String txt = strings[i % (strings.length)];
ns[i].property(VertexProperty.Cardinality.single, "weight", w);
ns[i].property(VertexProperty.Cardinality.single, "time", (long) i);
ns[i].property(VertexProperty.Cardinality.single, "text", txt);
for (VertexProperty p : new VertexProperty[] { p1, p2 }) {
p.property("weight", w);
p.property("time", (long) i);
p.property("text", txt);
}
// previous or self-loop
JanusGraphVertex u = ns[(i > numV ? i - 1 : i)];
for (String label : new String[] { "connect", "related" }) {
Edge e = ns[i].addEdge(label, u, "weight", (w++) % 5, "time", (long) i, "text", txt);
}
}
// ######### UPDATED QUERIES ##########
evaluateQuery(tx.query().has("time", Cmp.EQUAL, 10).has("weight", Cmp.EQUAL, 0), ElementCategory.EDGE, 0, new boolean[] { true, sorted }, edge1.name());
evaluateQuery(tx.query().has("time", Cmp.EQUAL, numV + 10).has("weight", Cmp.EQUAL, 0), ElementCategory.EDGE, 1, new boolean[] { true, sorted }, edge1.name());
evaluateQuery(tx.query().has("text", Cmp.EQUAL, strings[0]).has(LABEL_NAME, "connect").limit(10), ElementCategory.EDGE, 10, new boolean[] { true, sorted }, edge2.name());
evaluateQuery(tx.query().has("weight", Cmp.EQUAL, 1.5), ElementCategory.EDGE, numV / 10 * 2, new boolean[] { false, sorted });
evaluateQuery(tx.query().has("time", Cmp.EQUAL, 20), ElementCategory.PROPERTY, 0, new boolean[] { true, sorted }, prop1.name());
evaluateQuery(tx.query().has("time", Cmp.EQUAL, numV + 20), ElementCategory.PROPERTY, 2, new boolean[] { true, sorted }, prop1.name());
evaluateQuery(tx.query().has("time", Cmp.EQUAL, 30).has(LABEL_NAME, "person"), ElementCategory.VERTEX, 0, new boolean[] { true, sorted }, vertex1.name());
evaluateQuery(tx.query().has("time", Cmp.EQUAL, numV + 30).has(LABEL_NAME, "person"), ElementCategory.VERTEX, 1, new boolean[] { true, sorted }, vertex1.name());
evaluateQuery(tx.query().has("name", Cmp.EQUAL, "u1"), ElementCategory.VERTEX, numV / 5, new boolean[] { true, sorted }, vertex3.name());
// ######### END UPDATED QUERIES ##########
newTx();
// ######### UPDATED QUERIES (copied from above) ##########
evaluateQuery(tx.query().has("time", Cmp.EQUAL, 10).has("weight", Cmp.EQUAL, 0), ElementCategory.EDGE, 0, new boolean[] { true, sorted }, edge1.name());
evaluateQuery(tx.query().has("time", Cmp.EQUAL, numV + 10).has("weight", Cmp.EQUAL, 0), ElementCategory.EDGE, 1, new boolean[] { true, sorted }, edge1.name());
evaluateQuery(tx.query().has("text", Cmp.EQUAL, strings[0]).has(LABEL_NAME, "connect").limit(10), ElementCategory.EDGE, 10, new boolean[] { true, sorted }, edge2.name());
evaluateQuery(tx.query().has("weight", Cmp.EQUAL, 1.5), ElementCategory.EDGE, numV / 10 * 2, new boolean[] { false, sorted });
evaluateQuery(tx.query().has("time", Cmp.EQUAL, 20), ElementCategory.PROPERTY, 0, new boolean[] { true, sorted }, prop1.name());
evaluateQuery(tx.query().has("time", Cmp.EQUAL, numV + 20), ElementCategory.PROPERTY, 2, new boolean[] { true, sorted }, prop1.name());
evaluateQuery(tx.query().has("time", Cmp.EQUAL, 30).has(LABEL_NAME, "person"), ElementCategory.VERTEX, 0, new boolean[] { true, sorted }, vertex1.name());
evaluateQuery(tx.query().has("time", Cmp.EQUAL, numV + 30).has(LABEL_NAME, "person"), ElementCategory.VERTEX, 1, new boolean[] { true, sorted }, vertex1.name());
evaluateQuery(tx.query().has("name", Cmp.EQUAL, "u1"), ElementCategory.VERTEX, numV / 5, new boolean[] { true, sorted }, vertex3.name());
// *** INDIVIDUAL USE CASE TESTS ******
}
use of org.janusgraph.core.schema.JanusGraphIndex in project janusgraph by JanusGraph.
the class JanusGraphTest method testGraphCentricQueryProfilingWithLimitAdjusting.
@Test
public void testGraphCentricQueryProfilingWithLimitAdjusting() throws BackendException {
Runnable dataLoader = () -> {
final PropertyKey name = makeKey("name", String.class);
final JanusGraphIndex compositeNameIndex = mgmt.buildIndex("nameIdx", Vertex.class).addKey(name).buildCompositeIndex();
finishSchema();
newTx();
for (int i = 0; i < 3000; i++) {
tx.addVertex("name", "bob");
}
tx.commit();
};
clopen(option(ADJUST_LIMIT), false, option(HARD_MAX_LIMIT), 100000);
dataLoader.run();
newTx();
Metrics mCompSingle = tx.traversal().V().has("name", "bob").profile().next().getMetrics(0);
assertEquals(2, mCompSingle.getNested().size());
Metrics nested = (Metrics) mCompSingle.getNested().toArray()[1];
Map<String, String> nameIdxAnnotations = new HashMap() {
{
put("condition", "(name = bob)");
put("orders", "[]");
put("isFitted", "true");
put("isOrdered", "true");
// 100000 is HARD_MAX_LIMIT
put("query", "multiKSQ[1]@100000");
put("index", "nameIdx");
}
};
assertEquals(nameIdxAnnotations, nested.getAnnotations());
List<Metrics> backendQueryMetrics = nested.getNested().stream().map(m -> (Metrics) m).collect(Collectors.toList());
assertEquals(1, backendQueryMetrics.size());
Map<String, String> backendAnnotations = new HashMap() {
{
put("query", "nameIdx:multiKSQ[1]@100000");
put("limit", 100000);
}
};
assertEquals(backendAnnotations, backendQueryMetrics.get(0).getAnnotations());
assertTrue(backendQueryMetrics.get(0).getDuration(TimeUnit.MICROSECONDS) > 0);
close();
JanusGraphFactory.drop(graph);
clopen(option(ADJUST_LIMIT), false, option(HARD_MAX_LIMIT), Integer.MAX_VALUE);
dataLoader.run();
newTx();
mCompSingle = tx.traversal().V().has("name", "bob").profile().next().getMetrics(0);
assertEquals(2, mCompSingle.getNested().size());
nested = (Metrics) mCompSingle.getNested().toArray()[1];
nameIdxAnnotations = new HashMap() {
{
put("condition", "(name = bob)");
put("orders", "[]");
put("isFitted", "true");
put("isOrdered", "true");
put("query", "multiKSQ[1]");
put("index", "nameIdx");
}
};
assertEquals(nameIdxAnnotations, nested.getAnnotations());
backendQueryMetrics = nested.getNested().stream().map(m -> (Metrics) m).collect(Collectors.toList());
assertEquals(1, backendQueryMetrics.size());
backendAnnotations = new HashMap() {
{
put("query", "nameIdx:multiKSQ[1]");
}
};
assertEquals(backendAnnotations, backendQueryMetrics.get(0).getAnnotations());
assertTrue(backendQueryMetrics.get(0).getDuration(TimeUnit.MICROSECONDS) > 0);
close();
JanusGraphFactory.drop(graph);
clopen(option(ADJUST_LIMIT), true);
dataLoader.run();
newTx();
mCompSingle = tx.traversal().V().has("name", "bob").profile().next().getMetrics(0);
assertEquals("JanusGraphStep([],[name.eq(bob)])", mCompSingle.getName());
assertTrue(mCompSingle.getDuration(TimeUnit.MICROSECONDS) > 0);
assertEquals(2, mCompSingle.getNested().size());
nested = (Metrics) mCompSingle.getNested().toArray()[0];
assertEquals(QueryProfiler.CONSTRUCT_GRAPH_CENTRIC_QUERY, nested.getName());
assertTrue(nested.getDuration(TimeUnit.MICROSECONDS) > 0);
nested = (Metrics) mCompSingle.getNested().toArray()[1];
assertEquals(QueryProfiler.GRAPH_CENTRIC_QUERY, nested.getName());
assertTrue(nested.getDuration(TimeUnit.MICROSECONDS) > 0);
nameIdxAnnotations = new HashMap() {
{
put("condition", "(name = bob)");
put("orders", "[]");
put("isFitted", "true");
put("isOrdered", "true");
put("query", "multiKSQ[1]@4000");
put("index", "nameIdx");
}
};
assertEquals(nameIdxAnnotations, nested.getAnnotations());
backendQueryMetrics = nested.getNested().stream().map(m -> (Metrics) m).collect(Collectors.toList());
assertEquals(3, backendQueryMetrics.size());
int limit = 1000;
// due to LimitAdjustingIterator, there are three backend queries with limits 1000, 2000, and 4000, respectively.
for (Metrics backendQueryMetric : backendQueryMetrics) {
int queryLimit = limit;
backendAnnotations = new HashMap() {
{
put("query", "nameIdx:multiKSQ[1]@" + queryLimit);
put("limit", queryLimit);
}
};
assertEquals(backendAnnotations, backendQueryMetric.getAnnotations());
assertTrue(backendQueryMetric.getDuration(TimeUnit.MICROSECONDS) > 0);
limit = limit * 2;
}
}
use of org.janusgraph.core.schema.JanusGraphIndex in project janusgraph by JanusGraph.
the class JanusGraphTest method testGraphCentricQueryProfiling.
@Test
public void testGraphCentricQueryProfiling() {
final PropertyKey name = makeKey("name", String.class);
final PropertyKey weight = makeKey("weight", Integer.class);
final JanusGraphIndex compositeNameIndex = mgmt.buildIndex("nameIdx", Vertex.class).addKey(name).buildCompositeIndex();
final JanusGraphIndex compositeWeightIndex = mgmt.buildIndex("weightIdx", Vertex.class).addKey(weight).buildCompositeIndex();
final PropertyKey prop = makeKey("prop", Integer.class);
finishSchema();
tx.addVertex("name", "bob", "prop", 100, "weight", 100);
tx.addVertex("name", "alex", "prop", 100, "weight", 100);
tx.addVertex("name", "bob", "prop", 150, "weight", 120);
tx.commit();
// satisfied by a single composite index query
newTx();
Metrics mCompSingle = tx.traversal().V().has("name", "bob").profile().next().getMetrics(0);
assertEquals(2, tx.traversal().V().has("name", "bob").count().next());
assertEquals("JanusGraphStep([],[name.eq(bob)])", mCompSingle.getName());
assertTrue(mCompSingle.getDuration(TimeUnit.MICROSECONDS) > 0);
assertEquals(2, mCompSingle.getNested().size());
Metrics nested = (Metrics) mCompSingle.getNested().toArray()[0];
assertEquals(QueryProfiler.CONSTRUCT_GRAPH_CENTRIC_QUERY, nested.getName());
assertTrue(nested.getDuration(TimeUnit.MICROSECONDS) > 0);
nested = (Metrics) mCompSingle.getNested().toArray()[1];
assertEquals(QueryProfiler.GRAPH_CENTRIC_QUERY, nested.getName());
assertTrue(nested.getDuration(TimeUnit.MICROSECONDS) > 0);
Map<String, String> nameIdxAnnotations = new HashMap() {
{
put("condition", "(name = bob)");
put("orders", "[]");
put("isFitted", "true");
put("isOrdered", "true");
put("query", "multiKSQ[1]");
put("index", "nameIdx");
}
};
assertEquals(nameIdxAnnotations, nested.getAnnotations());
assertEquals(1, nested.getNested().size());
Metrics backendQueryMetrics = (Metrics) nested.getNested().toArray()[0];
assertTrue(backendQueryMetrics.getDuration(TimeUnit.MICROSECONDS) > 0);
// satisfied by unions of two separate graph-centric queries, each satisfied by a single composite index query
newTx();
Metrics mCompMultiOr = tx.traversal().V().or(__.has("name", "bob"), __.has("weight", 100)).profile().next().getMetrics(0);
assertEquals(3, tx.traversal().V().or(__.has("name", "bob"), __.has("weight", 100)).count().next());
assertEquals("Or(JanusGraphStep([],[name.eq(bob)]),JanusGraphStep([],[weight.eq(100)]))", mCompMultiOr.getName());
assertTrue(mCompMultiOr.getDuration(TimeUnit.MICROSECONDS) > 0);
assertEquals(5, mCompMultiOr.getNested().size());
nested = (Metrics) mCompMultiOr.getNested().toArray()[0];
assertEquals(QueryProfiler.CONSTRUCT_GRAPH_CENTRIC_QUERY, nested.getName());
assertTrue(nested.getDuration(TimeUnit.MICROSECONDS) > 0);
nested = (Metrics) mCompMultiOr.getNested().toArray()[1];
assertEquals(QueryProfiler.CONSTRUCT_GRAPH_CENTRIC_QUERY, nested.getName());
assertTrue(nested.getDuration(TimeUnit.MICROSECONDS) > 0);
nested = (Metrics) mCompMultiOr.getNested().toArray()[2];
assertEquals(QueryProfiler.GRAPH_CENTRIC_QUERY, nested.getName());
assertTrue(nested.getDuration(TimeUnit.MICROSECONDS) > 0);
assertEquals(nameIdxAnnotations, nested.getAnnotations());
assertEquals(1, nested.getNested().size());
backendQueryMetrics = (Metrics) nested.getNested().toArray()[0];
assertTrue(backendQueryMetrics.getDuration(TimeUnit.MICROSECONDS) > 0);
nested = (Metrics) mCompMultiOr.getNested().toArray()[3];
assertEquals(QueryProfiler.CONSTRUCT_GRAPH_CENTRIC_QUERY, nested.getName());
assertTrue(nested.getDuration(TimeUnit.MICROSECONDS) > 0);
nested = (Metrics) mCompMultiOr.getNested().toArray()[4];
assertEquals(QueryProfiler.GRAPH_CENTRIC_QUERY, nested.getName());
assertTrue(nested.getDuration(TimeUnit.MICROSECONDS) > 0);
Map<String, String> weightIdxAnnotations = new HashMap() {
{
put("condition", "(weight = 100)");
put("orders", "[]");
put("isFitted", "true");
put("isOrdered", "true");
put("query", "multiKSQ[1]");
put("index", "weightIdx");
}
};
assertEquals(weightIdxAnnotations, nested.getAnnotations());
assertEquals(1, nested.getNested().size());
backendQueryMetrics = (Metrics) nested.getNested().toArray()[0];
assertTrue(backendQueryMetrics.getDuration(TimeUnit.MICROSECONDS) > 0);
// satisfied by a single graph-centric query which satisfied by intersection of two composite index queries
newTx();
assertEquals(1, tx.traversal().V().and(__.has("name", "bob"), __.has("weight", 100)).count().next());
TraversalMetrics metrics = tx.traversal().V().and(__.has("name", "bob"), __.has("weight", 100)).profile().next();
Metrics mCompMultiAnd = metrics.getMetrics(0);
assertEquals("JanusGraphStep([],[name.eq(bob), weight.eq(100)])", mCompMultiAnd.getName());
assertTrue(mCompMultiAnd.getDuration(TimeUnit.MICROSECONDS) > 0);
assertEquals(2, mCompMultiAnd.getNested().size());
nested = (Metrics) mCompMultiAnd.getNested().toArray()[0];
assertEquals(QueryProfiler.CONSTRUCT_GRAPH_CENTRIC_QUERY, nested.getName());
assertTrue(nested.getDuration(TimeUnit.MICROSECONDS) > 0);
nested = (Metrics) mCompMultiAnd.getNested().toArray()[1];
assertEquals(QueryProfiler.GRAPH_CENTRIC_QUERY, nested.getName());
assertTrue(nested.getDuration(TimeUnit.MICROSECONDS) > 0);
assertEquals("(name = bob AND weight = 100)", nested.getAnnotation("condition"));
assertEquals(2, nested.getNested().size());
Metrics deeplyNested = (Metrics) nested.getNested().toArray()[0];
assertEquals("AND-query", deeplyNested.getName());
// FIXME: assertTrue(deeplyNested.getDuration(TimeUnit.MICROSECONDS) > 0);
assertEquals("multiKSQ[1]", deeplyNested.getAnnotation("query"));
deeplyNested = (Metrics) nested.getNested().toArray()[1];
assertEquals("AND-query", deeplyNested.getName());
// FIXME: assertTrue(deeplyNested.getDuration(TimeUnit.MICROSECONDS) > 0);
assertEquals("multiKSQ[1]", deeplyNested.getAnnotation("query"));
// satisfied by one graph-centric query, which satisfied by in-memory filtering after one composite index query
newTx();
assertEquals(1, tx.traversal().V().and(__.has("name", "bob"), __.has("prop", 100)).count().next());
Metrics mUnfittedMultiAnd = tx.traversal().V().and(__.has("name", "bob"), __.has("prop", 100)).profile().next().getMetrics(0);
assertEquals("JanusGraphStep([],[name.eq(bob), prop.eq(100)])", mUnfittedMultiAnd.getName());
assertTrue(mUnfittedMultiAnd.getDuration(TimeUnit.MICROSECONDS) > 0);
assertEquals(2, mUnfittedMultiAnd.getNested().size());
nested = (Metrics) mUnfittedMultiAnd.getNested().toArray()[0];
assertEquals(QueryProfiler.CONSTRUCT_GRAPH_CENTRIC_QUERY, nested.getName());
assertTrue(nested.getDuration(TimeUnit.MICROSECONDS) > 0);
assertTrue(nested.getDuration(TimeUnit.MICROSECONDS) > 0);
nested = (Metrics) mUnfittedMultiAnd.getNested().toArray()[1];
assertEquals(QueryProfiler.GRAPH_CENTRIC_QUERY, nested.getName());
assertTrue(nested.getDuration(TimeUnit.MICROSECONDS) > 0);
Map<String, String> annotations = new HashMap() {
{
put("condition", "(name = bob AND prop = 100)");
put("orders", "[]");
// not fitted because prop = 100 requires in-memory filtering
put("isFitted", "false");
put("isOrdered", "true");
put("query", "multiKSQ[1]");
put("index", "nameIdx");
}
};
assertEquals(annotations, nested.getAnnotations());
// satisfied by union of two separate graph-centric queries, one satisfied by a composite index query and the other requires full scan
newTx();
assertEquals(3, tx.traversal().V().or(__.has("name", "bob"), __.has("prop", 100)).count().next());
Metrics mUnfittedMultiOr = tx.traversal().V().or(__.has("name", "bob"), __.has("prop", 100)).profile().next().getMetrics(0);
assertEquals("Or(JanusGraphStep([],[name.eq(bob)]),JanusGraphStep([],[prop.eq(100)]))", mUnfittedMultiOr.getName());
assertTrue(mUnfittedMultiOr.getDuration(TimeUnit.MICROSECONDS) > 0);
assertEquals(5, mUnfittedMultiOr.getNested().size());
nested = (Metrics) mUnfittedMultiOr.getNested().toArray()[0];
assertEquals(QueryProfiler.CONSTRUCT_GRAPH_CENTRIC_QUERY, nested.getName());
assertTrue(nested.getDuration(TimeUnit.MICROSECONDS) > 0);
nested = (Metrics) mUnfittedMultiOr.getNested().toArray()[1];
assertEquals(QueryProfiler.CONSTRUCT_GRAPH_CENTRIC_QUERY, nested.getName());
assertTrue(nested.getDuration(TimeUnit.MICROSECONDS) > 0);
nested = (Metrics) mUnfittedMultiOr.getNested().toArray()[2];
assertEquals(QueryProfiler.GRAPH_CENTRIC_QUERY, nested.getName());
assertTrue(nested.getDuration(TimeUnit.MICROSECONDS) > 0);
assertEquals(nameIdxAnnotations, nested.getAnnotations());
nested = (Metrics) mUnfittedMultiOr.getNested().toArray()[3];
assertEquals(QueryProfiler.CONSTRUCT_GRAPH_CENTRIC_QUERY, nested.getName());
assertTrue(nested.getDuration(TimeUnit.MICROSECONDS) > 0);
nested = (Metrics) mUnfittedMultiOr.getNested().toArray()[4];
assertEquals(QueryProfiler.GRAPH_CENTRIC_QUERY, nested.getName());
assertTrue(nested.getDuration(TimeUnit.MICROSECONDS) > 0);
annotations = new HashMap() {
{
put("condition", "(prop = 100)");
put("orders", "[]");
put("isFitted", "false");
put("isOrdered", "true");
put("query", "[]");
}
};
assertEquals(annotations, nested.getAnnotations());
nested = (Metrics) nested.getNested().toArray()[0];
final Map<String, String> fullScanAnnotations = new HashMap() {
{
put("query", "[]");
put("fullscan", "true");
put("condition", "VERTEX");
}
};
assertEquals(fullScanAnnotations, nested.getAnnotations());
assertTrue(nested.getDuration(TimeUnit.MICROSECONDS) > 0);
}
use of org.janusgraph.core.schema.JanusGraphIndex in project janusgraph by JanusGraph.
the class JanusGraphTest method testGotGIndexRemoval.
@Test
public void testGotGIndexRemoval() throws InterruptedException, ExecutionException {
clopen(option(LOG_SEND_DELAY, MANAGEMENT_LOG), Duration.ZERO, option(KCVSLog.LOG_READ_LAG_TIME, MANAGEMENT_LOG), Duration.ofMillis(50), option(LOG_READ_INTERVAL, MANAGEMENT_LOG), Duration.ofMillis(250));
final String name = "name";
// Load Graph of the Gods
GraphOfTheGodsFactory.loadWithoutMixedIndex(graph, // True makes the index on names unique. Test fails when this is true.
true);
// Change to false and test will pass.
newTx();
finishSchema();
JanusGraphIndex graphIndex = mgmt.getGraphIndex(name);
// Sanity checks on the index that we assume GraphOfTheGodsFactory created
assertNotNull(graphIndex);
assertEquals(1, graphIndex.getFieldKeys().length);
assertEquals(name, graphIndex.getFieldKeys()[0].name());
assertEquals("internalindex", graphIndex.getBackingIndex());
assertEquals(SchemaStatus.ENABLED, graphIndex.getIndexStatus(graphIndex.getFieldKeys()[0]));
finishSchema();
// Disable name index
graphIndex = mgmt.getGraphIndex(name);
mgmt.updateIndex(graphIndex, SchemaAction.DISABLE_INDEX);
mgmt.commit();
tx.commit();
ManagementUtil.awaitGraphIndexUpdate(graph, name, 5, ChronoUnit.SECONDS);
finishSchema();
// Remove name index
graphIndex = mgmt.getGraphIndex(name);
mgmt.updateIndex(graphIndex, SchemaAction.REMOVE_INDEX);
ScanJobFuture graphMetrics = mgmt.getIndexJobStatus(graphIndex);
finishSchema();
// Should have deleted at least one record
assertNotEquals(0, graphMetrics.get().getCustom(IndexRemoveJob.DELETED_RECORDS_COUNT));
}
Aggregations