use of org.janusgraph.core.PropertyKey in project janusgraph by JanusGraph.
the class JanusGraphIndexTest method setupChainGraph.
private void setupChainGraph(int numV, String[] strings, boolean sameNameMapping) {
clopen(option(INDEX_NAME_MAPPING, INDEX), sameNameMapping);
JanusGraphIndex vindex = getExternalIndex(Vertex.class, INDEX);
JanusGraphIndex eindex = getExternalIndex(Edge.class, INDEX);
JanusGraphIndex pindex = getExternalIndex(JanusGraphVertexProperty.class, INDEX);
PropertyKey name = makeKey("name", String.class);
mgmt.addIndexKey(vindex, name, getStringMapping());
mgmt.addIndexKey(eindex, name, getStringMapping());
mgmt.addIndexKey(pindex, name, getStringMapping(), Parameter.of("mapped-name", "xstr"));
PropertyKey text = makeKey("text", String.class);
mgmt.addIndexKey(vindex, text, getTextMapping(), Parameter.of("mapped-name", "xtext"));
mgmt.addIndexKey(eindex, text, getTextMapping());
mgmt.addIndexKey(pindex, text, getTextMapping());
mgmt.makeEdgeLabel("knows").signature(name).make();
mgmt.makePropertyKey("uid").dataType(String.class).signature(text).make();
finishSchema();
JanusGraphVertex previous = null;
for (int i = 0; i < numV; i++) {
JanusGraphVertex v = graph.addVertex("name", strings[i % strings.length], "text", strings[i % strings.length]);
v.addEdge("knows", previous == null ? v : previous, "name", strings[i % strings.length], "text", strings[i % strings.length]);
v.property("uid", "v" + i, "name", strings[i % strings.length], "text", strings[i % strings.length]);
previous = v;
}
}
use of org.janusgraph.core.PropertyKey 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.PropertyKey in project janusgraph by JanusGraph.
the class JanusGraphTest method evaluateQuery.
public static void evaluateQuery(JanusGraphVertexQuery query, RelationCategory resultType, int expectedResults, int numSubQueries, boolean[] subQuerySpecs, Map<PropertyKey, Order> orderMap) {
SimpleQueryProfiler profiler = new SimpleQueryProfiler();
((BasicVertexCentricQueryBuilder) query).profiler(profiler);
Iterable<? extends JanusGraphElement> result;
switch(resultType) {
case PROPERTY:
result = query.properties();
break;
case EDGE:
result = query.edges();
break;
case RELATION:
result = query.relations();
break;
default:
throw new AssertionError();
}
OrderList orders = profiler.getAnnotation(QueryProfiler.ORDERS_ANNOTATION);
// Check elements and that they are returned in the correct order
int no = 0;
JanusGraphElement previous = null;
for (JanusGraphElement e : result) {
assertNotNull(e);
no++;
if (previous != null && !orders.isEmpty()) {
assertTrue(orders.compare(previous, e) <= 0);
}
previous = e;
}
assertEquals(expectedResults, no);
// Check OrderList of query
assertNotNull(orders);
assertEquals(orderMap.size(), orders.size());
for (int i = 0; i < orders.size(); i++) {
assertEquals(orderMap.get(orders.getKey(i)), orders.getOrder(i));
}
for (PropertyKey key : orderMap.keySet()) assertTrue(orders.containsKey(key));
// Check subqueries
assertEquals(Integer.valueOf(1), profiler.getAnnotation(QueryProfiler.NUMVERTICES_ANNOTATION));
int subQueryCounter = 0;
for (SimpleQueryProfiler subProfiler : profiler) {
assertNotNull(subProfiler);
if (subProfiler.getGroupName().equals(QueryProfiler.OPTIMIZATION))
continue;
if (subQuerySpecs.length == 2) {
// 0=>fitted, 1=>ordered
assertEquals(subQuerySpecs[0], subProfiler.getAnnotation(QueryProfiler.FITTED_ANNOTATION));
assertEquals(subQuerySpecs[1], subProfiler.getAnnotation(QueryProfiler.ORDERED_ANNOTATION));
}
// assertEquals(1,Iterables.size(subProfiler)); This only applies if a disk call is necessary
subQueryCounter++;
}
assertEquals(numSubQueries, subQueryCounter);
}
use of org.janusgraph.core.PropertyKey in project janusgraph by JanusGraph.
the class JanusGraphTest method testConsistencyEnforcement.
/* ==================================================================================
CONSISTENCY
==================================================================================*/
/**
* Tests the correct application of ConsistencyModifiers across transactional boundaries
*/
@Test
public void testConsistencyEnforcement() {
PropertyKey uid = makeVertexIndexedUniqueKey("uid", Integer.class);
PropertyKey name = makeKey("name", String.class);
mgmt.setConsistency(uid, ConsistencyModifier.LOCK);
mgmt.setConsistency(name, ConsistencyModifier.LOCK);
mgmt.setConsistency(mgmt.getGraphIndex("uid"), ConsistencyModifier.LOCK);
EdgeLabel knows = mgmt.makeEdgeLabel("knows").multiplicity(Multiplicity.SIMPLE).make();
EdgeLabel spouse = mgmt.makeEdgeLabel("spouse").multiplicity(Multiplicity.ONE2ONE).make();
EdgeLabel connect = mgmt.makeEdgeLabel("connect").multiplicity(Multiplicity.MULTI).make();
EdgeLabel related = mgmt.makeEdgeLabel("related").multiplicity(Multiplicity.MULTI).make();
mgmt.setConsistency(knows, ConsistencyModifier.LOCK);
mgmt.setConsistency(spouse, ConsistencyModifier.LOCK);
mgmt.setConsistency(related, ConsistencyModifier.FORK);
finishSchema();
name = tx.getPropertyKey("name");
connect = tx.getEdgeLabel("connect");
related = tx.getEdgeLabel("related");
JanusGraphVertex v1 = tx.addVertex("uid", 1);
JanusGraphVertex v2 = tx.addVertex("uid", 2);
JanusGraphVertex v3 = tx.addVertex("uid", 3);
Edge e1 = v1.addEdge(connect.name(), v2, name.name(), "e1");
Edge e2 = v1.addEdge(related.name(), v2, name.name(), "e2");
newTx();
v1 = getV(tx, v1);
/*
==== check fork, no fork behavior
*/
long e1id = getId(e1);
long e2id = getId(e2);
e1 = Iterables.getOnlyElement(v1.query().direction(Direction.OUT).labels("connect").edges());
assertEquals("e1", e1.value("name"));
assertEquals(e1id, getId(e1));
e2 = Iterables.getOnlyElement(v1.query().direction(Direction.OUT).labels("related").edges());
assertEquals("e2", e2.value("name"));
assertEquals(e2id, getId(e2));
// Update edges - one should simply update, the other fork
e1.property("name", "e1.2");
e2.property("name", "e2.2");
newTx();
v1 = getV(tx, v1);
e1 = Iterables.getOnlyElement(v1.query().direction(Direction.OUT).labels("connect").edges());
assertEquals("e1.2", e1.value("name"));
// should have same id
assertEquals(e1id, getId(e1));
e2 = Iterables.getOnlyElement(v1.query().direction(Direction.OUT).labels("related").edges());
assertEquals("e2.2", e2.value("name"));
// should have different id since forked
assertNotEquals(e2id, getId(e2));
clopen();
/*
=== check cross transaction
*/
final Random random = new Random();
final long[] vertexIds = { getId(v1), getId(v2), getId(v3) };
// 1) Index uniqueness
executeLockConflictingTransactionJobs(graph, new TransactionJob() {
private int pos = 0;
@Override
public void run(JanusGraphTransaction tx) {
JanusGraphVertex u = getV(tx, vertexIds[pos++]);
u.property(VertexProperty.Cardinality.single, "uid", 5);
}
});
// 2) Property out-uniqueness
executeLockConflictingTransactionJobs(graph, tx -> {
final JanusGraphVertex u = getV(tx, vertexIds[0]);
u.property(VertexProperty.Cardinality.single, "name", "v" + random.nextInt(10));
});
// 3) knows simpleness
executeLockConflictingTransactionJobs(graph, tx -> {
final JanusGraphVertex u1 = getV(tx, vertexIds[0]), u2 = getV(tx, vertexIds[1]);
u1.addEdge("knows", u2);
});
// 4) knows one2one (in 2 separate configurations)
executeLockConflictingTransactionJobs(graph, new TransactionJob() {
private int pos = 1;
@Override
public void run(JanusGraphTransaction tx) {
final JanusGraphVertex u1 = getV(tx, vertexIds[0]), u2 = getV(tx, vertexIds[pos++]);
u1.addEdge("spouse", u2);
}
});
executeLockConflictingTransactionJobs(graph, new TransactionJob() {
private int pos = 1;
@Override
public void run(JanusGraphTransaction tx) {
final JanusGraphVertex u1 = getV(tx, vertexIds[pos++]), u2 = getV(tx, vertexIds[0]);
u1.addEdge("spouse", u2);
}
});
// ######### TRY INVALID CONSISTENCY
try {
// Fork does not apply to constrained types
mgmt.setConsistency(mgmt.getPropertyKey("name"), ConsistencyModifier.FORK);
fail();
} catch (IllegalArgumentException ignored) {
}
}
use of org.janusgraph.core.PropertyKey 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");
});
}
Aggregations