use of org.janusgraph.core.RelationType in project janusgraph by JanusGraph.
the class RelationIdentifier method findRelation.
JanusGraphRelation findRelation(JanusGraphTransaction tx) {
JanusGraphVertex v = ((StandardJanusGraphTx) tx).getInternalVertex(outVertexId);
if (v == null || v.isRemoved())
return null;
JanusGraphVertex typeVertex = tx.getVertex(typeId);
if (typeVertex == null)
return null;
if (!(typeVertex instanceof RelationType))
throw new IllegalArgumentException("Invalid RelationIdentifier: typeID does not reference a type");
RelationType type = (RelationType) typeVertex;
Iterable<? extends JanusGraphRelation> relations;
if (((RelationType) typeVertex).isEdgeLabel()) {
Direction dir = Direction.OUT;
JanusGraphVertex other = ((StandardJanusGraphTx) tx).getInternalVertex(inVertexId);
if (other == null || other.isRemoved())
return null;
if (((StandardJanusGraphTx) tx).isPartitionedVertex(v) && !((StandardJanusGraphTx) tx).isPartitionedVertex(other)) {
// Swap for likely better performance
JanusGraphVertex tmp = other;
other = v;
v = tmp;
dir = Direction.IN;
}
relations = ((VertexCentricQueryBuilder) v.query()).noPartitionRestriction().types((EdgeLabel) type).direction(dir).adjacent(other).edges();
} else {
relations = ((VertexCentricQueryBuilder) v.query()).noPartitionRestriction().types((PropertyKey) type).properties();
}
for (JanusGraphRelation r : relations) {
// Find current or previous relation
if (r.longId() == relationId || ((r instanceof StandardRelation) && ((StandardRelation) r).getPreviousID() == relationId))
return r;
}
return null;
}
use of org.janusgraph.core.RelationType in project janusgraph by JanusGraph.
the class StandardTransactionLogProcessor method restoreExternalIndexes.
private void restoreExternalIndexes(Predicate<String> isFailedIndex, TransactionLogHeader.Entry entry) {
// 1) Collect all elements (vertices and relations) and the indexes for which they need to be restored
SetMultimap<String, IndexRestore> indexRestores = HashMultimap.create();
BackendOperation.execute(() -> {
final StandardJanusGraphTx tx = (StandardJanusGraphTx) graph.newTransaction();
try {
entry.getContentAsModifications(serializer).stream().map(m -> ModificationDeserializer.parseRelation(m, tx)).forEach(rel -> {
// Collect affected vertex indexes
for (final MixedIndexType index : getMixedIndexes(rel.getType())) {
if (index.getElement() == ElementCategory.VERTEX && isFailedIndex.apply(index.getBackingIndexName())) {
assert rel.isProperty();
indexRestores.put(index.getBackingIndexName(), new IndexRestore(rel.getVertex(0).longId(), ElementCategory.VERTEX, getIndexId(index)));
}
}
// See if relation itself is affected
for (final RelationType relType : rel.getPropertyKeysDirect()) {
for (final MixedIndexType index : getMixedIndexes(relType)) {
if (index.getElement().isInstance(rel) && isFailedIndex.apply(index.getBackingIndexName())) {
assert rel.id() instanceof RelationIdentifier;
indexRestores.put(index.getBackingIndexName(), new IndexRestore(rel.id(), ElementCategory.getByClazz(rel.getClass()), getIndexId(index)));
}
}
}
});
} finally {
if (tx.isOpen())
tx.rollback();
}
return true;
}, readTime);
// 2) Restore elements per backing index
for (final String indexName : indexRestores.keySet()) {
final StandardJanusGraphTx tx = (StandardJanusGraphTx) graph.newTransaction();
try {
BackendTransaction btx = tx.getTxHandle();
final IndexTransaction indexTx = btx.getIndexTransaction(indexName);
BackendOperation.execute(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
Map<String, Map<String, List<IndexEntry>>> restoredDocs = Maps.newHashMap();
indexRestores.get(indexName).forEach(restore -> {
JanusGraphSchemaVertex indexV = (JanusGraphSchemaVertex) tx.getVertex(restore.indexId);
MixedIndexType index = (MixedIndexType) indexV.asIndexType();
JanusGraphElement element = restore.retrieve(tx);
if (element != null) {
graph.getIndexSerializer().reindexElement(element, index, restoredDocs);
} else {
// Element is deleted
graph.getIndexSerializer().removeElement(restore.elementId, index, restoredDocs);
}
});
indexTx.restore(restoredDocs);
indexTx.commit();
return true;
}
@Override
public String toString() {
return "IndexMutation";
}
}, persistenceTime);
} finally {
if (tx.isOpen())
tx.rollback();
}
}
}
use of org.janusgraph.core.RelationType in project janusgraph by JanusGraph.
the class QueryUtil method constraints2QNF.
/**
* Prepares the constraints from the query builder into a QNF compliant condition.
* If the condition is invalid or trivially false, it returns null.
*
* @param tx
* @param constraints
* @param <E>
* @return
* @see #isQueryNormalForm(org.janusgraph.graphdb.query.condition.Condition)
*/
public static <E extends JanusGraphElement> And<E> constraints2QNF(StandardJanusGraphTx tx, List<PredicateCondition<String, E>> constraints) {
final And<E> conditions = new And<>(constraints.size() + 4);
for (final PredicateCondition<String, E> atom : constraints) {
final RelationType type = getType(tx, atom.getKey());
if (type == null) {
if (atom.getPredicate() == Cmp.EQUAL && atom.getValue() == null)
// Ignore condition, its trivially satisfied
continue;
return null;
}
final Object value = atom.getValue();
final JanusGraphPredicate predicate = atom.getPredicate();
if (type.isPropertyKey()) {
final PropertyKey key = (PropertyKey) type;
assert predicate.isValidCondition(value);
Preconditions.checkArgument(key.dataType() == Object.class || predicate.isValidValueType(key.dataType()), "Data type of key is not compatible with condition");
} else {
// its a label
Preconditions.checkArgument(((EdgeLabel) type).isUnidirected());
Preconditions.checkArgument(predicate.isValidValueType(JanusGraphVertex.class), "Data type of key is not compatible with condition");
}
if (predicate instanceof Contain) {
// Rewrite contains conditions
final Collection values = (Collection) value;
if (predicate == Contain.NOT_IN) {
// Simply ignore since trivially satisfied
if (values.isEmpty())
continue;
for (final Object inValue : values) addConstraint(type, Cmp.NOT_EQUAL, inValue, conditions, tx);
} else {
Preconditions.checkArgument(predicate == Contain.IN);
if (values.isEmpty()) {
// Cannot be satisfied
return null;
}
if (values.size() == 1) {
addConstraint(type, Cmp.EQUAL, values.iterator().next(), conditions, tx);
} else {
final Or<E> nested = new Or<>(values.size());
for (final Object invalue : values) addConstraint(type, Cmp.EQUAL, invalue, nested, tx);
conditions.add(nested);
}
}
} else if (predicate instanceof AndJanusPredicate) {
if (addConstraint(type, (AndJanusPredicate) (predicate), (List<Object>) (value), conditions, tx) == null) {
return null;
}
} else if (predicate instanceof OrJanusPredicate) {
final List<Object> values = (List<Object>) (value);
// FIXME: this might generate a non QNF-compliant form, e.g. nested = PredicateCondition OR (PredicateCondition AND PredicateCondition)
final Or<E> nested = addConstraint(type, (OrJanusPredicate) predicate, values, new Or<>(values.size()), tx);
if (nested == null) {
return null;
}
conditions.add(nested);
} else {
addConstraint(type, predicate, value, conditions, tx);
}
}
return conditions;
}
use of org.janusgraph.core.RelationType in project janusgraph by JanusGraph.
the class QueryUtil method extractOrCondition.
public static Map.Entry<RelationType, Collection> extractOrCondition(Or<JanusGraphRelation> condition) {
RelationType masterType = null;
final List<Object> values = new ArrayList<>();
for (final Condition c : condition.getChildren()) {
if (!(c instanceof PredicateCondition))
return null;
final PredicateCondition<RelationType, JanusGraphRelation> atom = (PredicateCondition) c;
if (atom.getPredicate() != Cmp.EQUAL)
return null;
final Object value = atom.getValue();
if (value == null)
return null;
final RelationType type = atom.getKey();
if (masterType == null)
masterType = type;
else if (!masterType.equals(type))
return null;
values.add(value);
}
if (masterType == null)
return null;
assert !values.isEmpty();
return new AbstractMap.SimpleImmutableEntry(masterType, values);
}
use of org.janusgraph.core.RelationType in project janusgraph by JanusGraph.
the class JanusGraphTest method performReindexAndVerifyEdgeCount.
public void performReindexAndVerifyEdgeCount(String indexName, String edgeLabel, String propKey, Vertex[] vertices, int[] propValues, Direction[] dirs, int[] resultsBeforeReindex) throws InterruptedException, ExecutionException {
assert propValues.length == dirs.length;
assert propValues.length == resultsBeforeReindex.length;
RelationType t = mgmt.getRelationType(edgeLabel);
RelationTypeIndex relationIndex = mgmt.getRelationIndex(t, indexName);
assertEquals(SchemaStatus.ENABLED, relationIndex.getIndexStatus());
GraphTraversalSource g = graph.traversal();
// asserting before reindex
for (int i = 0; i < propValues.length; i++) {
final int expectedCount = resultsBeforeReindex[i];
final Vertex v = vertices[i];
long count = 0;
if (OUT.equals(dirs[i])) {
count = g.V(v).outE(edgeLabel).has(propKey, propValues[i]).count().next();
} else if (IN.equals(dirs[i])) {
count = g.V(v).inE(edgeLabel).has(propKey, propValues[i]).count().next();
} else {
count = g.V(v).bothE(edgeLabel).has(propKey, propValues[i]).count().next();
}
assertEquals(expectedCount, count, String.format("v = %s, index = %s, direction = %s, prop value = %d", g.V(v).properties("vtName").next().value(), indexName, dirs[i], propValues[i]));
}
// Reindexing
mgmt.updateIndex(relationIndex, SchemaAction.REINDEX, 1).get();
finishSchema();
relationIndex = mgmt.getRelationIndex(t, indexName);
assertEquals(SchemaStatus.ENABLED, relationIndex.getIndexStatus());
final int[] expectedResultsAfterReindex = new int[] { 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1 };
for (int i = 0; i < propValues.length; i++) {
final int expectedCount = expectedResultsAfterReindex[i];
final Vertex v = vertices[i];
long count = 0;
if (OUT.equals(dirs[i])) {
count = g.V(v).outE(edgeLabel).has(propKey, propValues[i]).count().next();
} else if (IN.equals(dirs[i])) {
count = g.V(v).inE(edgeLabel).has(propKey, propValues[i]).count().next();
} else {
count = g.V(v).bothE(edgeLabel).has(propKey, propValues[i]).count().next();
}
assertEquals(expectedCount, count, String.format("v = %s, index = %s, direction = %s, prop value = %d", g.V(v).properties("vtName").next().value(), indexName, dirs[i], propValues[i]));
}
}
Aggregations