use of org.janusgraph.core.schema.JanusGraphIndex in project janusgraph by JanusGraph.
the class JanusGraphIndexTest method testOrderByWithRange.
/**
* This test builds a mixed index and tests index queries with order and range/limit.
* It also tests if index query cache is utilised correctly.
*/
@Test
public void testOrderByWithRange() {
final PropertyKey age = makeKey("age", Integer.class);
final JanusGraphIndex mixed = mgmt.buildIndex("mixed", Vertex.class).addKey(age).buildMixedIndex(INDEX);
finishSchema();
for (int i = 0; i < 100; i++) {
tx.addVertex("age", i);
}
tx.commit();
Supplier<GraphTraversal> common = () -> graph.traversal().V().has("age", P.gte(0)).order();
Supplier<GraphTraversal> traversal;
// traverse with limit 30 (cache cold miss)
traversal = () -> common.get().by(ORDER_AGE_ASC).limit(30).values("age");
assertBackendHit((TraversalMetrics) traversal.get().profile().next());
assertIntRange(traversal.get(), 0, 30);
traversal = () -> common.get().by(ORDER_AGE_DESC).limit(30).values("age");
assertBackendHit((TraversalMetrics) traversal.get().profile().next());
assertIntRange(traversal.get(), 99, 69);
// traverse with limit 30 (cache hit)
traversal = () -> common.get().by(ORDER_AGE_ASC).limit(30).values("age");
assertNoBackendHit((TraversalMetrics) traversal.get().profile().next());
assertIntRange(traversal.get(), 0, 30);
// traverse with limit followed by orderBy
traversal = () -> graph.traversal().V().has("age", P.gte(0)).limit(30).order().by(ORDER_AGE_ASC).values("age");
assertBackendHit((TraversalMetrics) traversal.get().profile().next());
assertNoBackendHit((TraversalMetrics) traversal.get().profile().next());
// traverse with range(10, 20) (cache hit)
traversal = () -> common.get().by(ORDER_AGE_DESC).range(10, 20).values("age");
assertNoBackendHit((TraversalMetrics) traversal.get().profile().next());
assertIntRange(traversal.get(), 89, 79);
}
use of org.janusgraph.core.schema.JanusGraphIndex in project janusgraph by JanusGraph.
the class JanusGraphIndexTest method testIndexSelectStrategy.
@Test
public void testIndexSelectStrategy() {
final PropertyKey name = makeKey("name", String.class);
final JanusGraphIndex compositeNameIndex = mgmt.buildIndex("composite", Vertex.class).addKey(name).buildCompositeIndex();
compositeNameIndex.name();
final PropertyKey prop = makeKey("prop", String.class);
final JanusGraphIndex mixedIndex = mgmt.buildIndex("mixed", Vertex.class).addKey(name, Mapping.STRING.asParameter()).addKey(prop, Mapping.STRING.asParameter()).buildMixedIndex(INDEX);
mixedIndex.name();
finishSchema();
// best combination is to pick up only 1 index (mixed index), however, greedy based approximate algorithm
// picks up 2 indexes (composite index + mixed index)
// use default config
assertEquals(1, getIndexSelectResultNum());
// use full class name
assertEquals(1, getIndexSelectResultNum(option(INDEX_SELECT_STRATEGY), ThresholdBasedIndexSelectionStrategy.class.getName()));
assertEquals(1, getIndexSelectResultNum(option(INDEX_SELECT_BRUTE_FORCE_THRESHOLD), 10, option(INDEX_SELECT_STRATEGY), ThresholdBasedIndexSelectionStrategy.NAME));
assertEquals(2, getIndexSelectResultNum(option(INDEX_SELECT_BRUTE_FORCE_THRESHOLD), 0, option(INDEX_SELECT_STRATEGY), ThresholdBasedIndexSelectionStrategy.NAME));
assertEquals(1, getIndexSelectResultNum(option(INDEX_SELECT_STRATEGY), BruteForceIndexSelectionStrategy.NAME));
assertEquals(2, getIndexSelectResultNum(option(INDEX_SELECT_STRATEGY), ApproximateIndexSelectionStrategy.NAME));
}
use of org.janusgraph.core.schema.JanusGraphIndex 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 (final IllegalArgumentException ignored) {
}
try {
// Already exists
mgmt.buildIndex("index2", Vertex.class).addKey(weight).buildCompositeIndex();
fail();
} catch (final IllegalArgumentException ignored) {
}
try {
// Key is already added
mgmt.addIndexKey(index2, weight);
fail();
} catch (final 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 (final IllegalArgumentException ignored) {
}
try {
// Already exists
mgmt.buildIndex("index2", Vertex.class).addKey(weight).buildCompositeIndex();
fail();
} catch (final IllegalArgumentException ignored) {
}
try {
// Key is already added
mgmt.addIndexKey(index2, weight);
fail();
} catch (final IllegalArgumentException ignored) {
}
// ########### TRANSACTIONAL ##############
weight = tx.getPropertyKey("weight");
final int numV = 200;
final String[] strings = { "houseboat", "humanoid", "differential", "extraordinary" };
final String[] stringsTwo = new String[strings.length];
for (int i = 0; i < strings.length; i++) stringsTwo[i] = strings[i] + " " + strings[i];
final int modulo = 5;
assertEquals(0, numV % (modulo * strings.length * 2));
for (int i = 0; i < numV; i++) {
final 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", desc), 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", desc), 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", asc), 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", asc), 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", desc), 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", desc), 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", asc), 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", asc), ElementCategory.VERTEX, numV / strings.length, new boolean[] { false, false }, weight, Order.ASC);
}
use of org.janusgraph.core.schema.JanusGraphIndex 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();
assertEquals("person", mgmt.getIndexOnlyConstraint("vindex1").name());
assertNull(mgmt.getIndexOnlyConstraint("vindex2"));
assertThrows(IllegalArgumentException.class, () -> mgmt.getIndexOnlyConstraint(null));
assertThrows(IllegalArgumentException.class, () -> mgmt.getIndexOnlyConstraint("invalidName"));
// ================== 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.schema.JanusGraphIndex in project janusgraph by JanusGraph.
the class ManagementSystem method setTypeModifier.
private void setTypeModifier(final JanusGraphSchemaElement element, final ModifierType modifierType, final Object value) {
Preconditions.checkArgument(element != null, "null schema element");
TypeDefinitionCategory cat = modifierType.getCategory();
if (cat.hasDataType() && null != value) {
Preconditions.checkArgument(cat.getDataType().equals(value.getClass()), "modifier value is not of expected type %s", cat.getDataType());
}
JanusGraphSchemaVertex typeVertex;
if (element instanceof JanusGraphSchemaVertex) {
typeVertex = (JanusGraphSchemaVertex) element;
} else if (element instanceof JanusGraphIndex) {
IndexType index = ((JanusGraphIndexWrapper) element).getBaseIndex();
assert index instanceof IndexTypeWrapper;
SchemaSource base = ((IndexTypeWrapper) index).getSchemaBase();
typeVertex = (JanusGraphSchemaVertex) base;
} else
throw new IllegalArgumentException("Invalid schema element: " + element);
// remove any pre-existing value for the modifier, or return if an identical value has already been set
for (JanusGraphEdge e : typeVertex.getEdges(TypeDefinitionCategory.TYPE_MODIFIER, Direction.OUT)) {
JanusGraphSchemaVertex v = (JanusGraphSchemaVertex) e.vertex(Direction.IN);
TypeDefinitionMap def = v.getDefinition();
Object existingValue = def.getValue(modifierType.getCategory());
if (null != existingValue) {
if (existingValue.equals(value)) {
// Already has the right value, don't need to do anything
return;
} else {
e.remove();
v.remove();
}
}
}
if (null != value) {
TypeDefinitionMap def = new TypeDefinitionMap();
def.setValue(cat, value);
JanusGraphSchemaVertex cVertex = transaction.makeSchemaVertex(JanusGraphSchemaCategory.TYPE_MODIFIER, null, def);
addSchemaEdge(typeVertex, cVertex, TypeDefinitionCategory.TYPE_MODIFIER, null);
}
updateSchemaVertex(typeVertex);
updatedTypes.add(typeVertex);
}
Aggregations