Search in sources :

Example 21 with RelationType

use of org.janusgraph.core.RelationType in project janusgraph by JanusGraph.

the class PredicateCondition method evaluate.

@Override
public boolean evaluate(E element) {
    RelationType type;
    if (key instanceof String) {
        type = ((InternalElement) element).tx().getRelationType((String) key);
        if (type == null)
            return satisfiesCondition(null);
    } else {
        type = (RelationType) key;
    }
    Preconditions.checkNotNull(type);
    if (type.isPropertyKey()) {
        Iterator<Object> iterator = ElementHelper.getValues(element, (PropertyKey) type).iterator();
        if (iterator.hasNext()) {
            while (iterator.hasNext()) {
                if (satisfiesCondition(iterator.next()))
                    return true;
            }
            return false;
        }
        return satisfiesCondition(null);
    } else {
        assert ((InternalRelationType) type).multiplicity().isUnique(Direction.OUT);
        return satisfiesCondition(element.value(type.name()));
    }
}
Also used : RelationType(org.janusgraph.core.RelationType) InternalRelationType(org.janusgraph.graphdb.internal.InternalRelationType) InternalElement(org.janusgraph.graphdb.internal.InternalElement) PropertyKey(org.janusgraph.core.PropertyKey)

Example 22 with RelationType

use of org.janusgraph.core.RelationType in project janusgraph by JanusGraph.

the class ManagementSystem method setConsistency.

/**
 * Sets the consistency level for those schema elements that support it (types and internal indexes)
 * <p>
 * Note, that it is possible to have a race condition here if two threads simultaneously try to change the
 * consistency level. However, this is resolved when the consistency level is being read by taking the
 * first one and deleting all existing attached consistency levels upon modification.
 *
 * @param element
 * @param consistency
 */
@Override
public void setConsistency(JanusGraphSchemaElement element, ConsistencyModifier consistency) {
    if (element instanceof RelationType) {
        RelationTypeVertex rv = (RelationTypeVertex) element;
        Preconditions.checkArgument(consistency != ConsistencyModifier.FORK || !rv.multiplicity().isConstrained(), "Cannot apply FORK consistency mode to constraint relation type: %s", rv.name());
    } else if (element instanceof JanusGraphIndex) {
        IndexType index = ((JanusGraphIndexWrapper) element).getBaseIndex();
        if (index.isMixedIndex())
            throw new IllegalArgumentException("Cannot change consistency on mixed index: " + element);
    } else
        throw new IllegalArgumentException("Cannot change consistency of schema element: " + element);
    setTypeModifier(element, ModifierType.CONSISTENCY, consistency);
}
Also used : RelationType(org.janusgraph.core.RelationType) InternalRelationType(org.janusgraph.graphdb.internal.InternalRelationType) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) IndexType(org.janusgraph.graphdb.types.IndexType) CompositeIndexType(org.janusgraph.graphdb.types.CompositeIndexType) MixedIndexType(org.janusgraph.graphdb.types.MixedIndexType) RelationTypeVertex(org.janusgraph.graphdb.types.vertices.RelationTypeVertex)

Example 23 with RelationType

use of org.janusgraph.core.RelationType in project janusgraph by JanusGraph.

the class ManagementSystem method printIndexes.

private String printIndexes(boolean calledDirectly) {
    StringBuilder sb = new StringBuilder();
    String pattern = "%-30s | %-11s | %-9s | %-14s | %-10s %10s |%n";
    String relationPattern = "%-30s | %-11s | %-9s | %-14s | %-8s | %10s |%n";
    Iterable<JanusGraphIndex> vertexIndexes = getGraphIndexes(Vertex.class);
    Iterable<JanusGraphIndex> edgeIndexes = getGraphIndexes(Edge.class);
    Iterable<RelationType> relationTypes = getRelationTypes(RelationType.class);
    LinkedList<RelationTypeIndex> relationIndexes = new LinkedList<>();
    for (RelationType rt : relationTypes) {
        Iterable<RelationTypeIndex> rti = getRelationIndexes(rt);
        rti.forEach(relationIndexes::add);
    }
    if (calledDirectly) {
        sb.append(FIRSTDASH);
    } else {
        sb.append(DASHBREAK);
    }
    sb.append(String.format(pattern, "Graph Index (Vertex)", "Type", "Unique", "Backing", "Key:", "Status"));
    sb.append(DASHBREAK);
    sb.append(iterateIndexes(pattern, vertexIndexes));
    sb.append(DASHBREAK);
    sb.append(String.format(pattern, "Graph Index (Edge)", "Type", "Unique", "Backing", "Key:", "Status"));
    sb.append(DASHBREAK);
    sb.append(iterateIndexes(pattern, edgeIndexes));
    sb.append(DASHBREAK);
    sb.append(String.format(relationPattern, "Relation Index (VCI)", "Type", "Direction", "Sort Key", "Order", "Status"));
    sb.append(DASHBREAK);
    for (RelationTypeIndex ri : relationIndexes) {
        sb.append(String.format(relationPattern, ri.name(), ri.getType(), ri.getDirection(), ri.getSortKey()[0], ri.getSortOrder(), ri.getIndexStatus().name()));
    }
    if (!relationIndexes.isEmpty()) {
        sb.append(DASHBREAK);
    }
    return sb.toString();
}
Also used : RelationType(org.janusgraph.core.RelationType) InternalRelationType(org.janusgraph.graphdb.internal.InternalRelationType) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) RelationTypeIndex(org.janusgraph.core.schema.RelationTypeIndex) LinkedList(java.util.LinkedList)

Example 24 with RelationType

use of org.janusgraph.core.RelationType 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);
}
Also used : VertexLabel(org.janusgraph.core.VertexLabel) RelationType(org.janusgraph.core.RelationType) InternalRelationType(org.janusgraph.graphdb.internal.InternalRelationType) JanusGraphSchemaVertex(org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex) InternalRelationType(org.janusgraph.graphdb.internal.InternalRelationType) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) JanusGraphSchemaCategory(org.janusgraph.graphdb.internal.JanusGraphSchemaCategory) RelationTypeIndex(org.janusgraph.core.schema.RelationTypeIndex)

Example 25 with RelationType

use of org.janusgraph.core.RelationType in project janusgraph by JanusGraph.

the class ManagementSystem method buildRelationTypeIndex.

private RelationTypeIndex buildRelationTypeIndex(RelationType type, String name, Direction direction, Order sortOrder, PropertyKey... sortKeys) {
    Preconditions.checkArgument(type != null && direction != null && sortOrder != null && sortKeys != null);
    Preconditions.checkArgument(StringUtils.isNotBlank(name), "Name cannot be blank: %s", name);
    Token.verifyName(name);
    Preconditions.checkArgument(sortKeys.length > 0, "Need to specify sort keys");
    for (RelationType key : sortKeys) Preconditions.checkArgument(key != null, "Keys cannot be null");
    Preconditions.checkArgument(!(type instanceof EdgeLabel) || !((EdgeLabel) type).isUnidirected() || direction == Direction.OUT, "Can only index uni-directed labels in the out-direction: %s", type);
    Preconditions.checkArgument(!((InternalRelationType) type).multiplicity().isUnique(direction), "The relation type [%s] has a multiplicity or cardinality constraint in direction [%s] and can therefore not be indexed", type, direction);
    String composedName = composeRelationTypeIndexName(type, name);
    StandardRelationTypeMaker maker;
    if (type.isEdgeLabel()) {
        StandardEdgeLabelMaker lm = (StandardEdgeLabelMaker) transaction.makeEdgeLabel(composedName);
        lm.unidirected(direction);
        maker = lm;
    } else {
        assert type.isPropertyKey();
        assert direction == Direction.OUT;
        StandardPropertyKeyMaker lm = (StandardPropertyKeyMaker) transaction.makePropertyKey(composedName);
        lm.dataType(((PropertyKey) type).dataType());
        maker = lm;
    }
    boolean canIndexBeEnabled = type.isNew() || Arrays.stream(sortKeys).anyMatch(JanusGraphElement::isNew);
    maker.status(canIndexBeEnabled ? SchemaStatus.ENABLED : SchemaStatus.INSTALLED);
    maker.invisible();
    maker.multiplicity(Multiplicity.MULTI);
    maker.sortKey(sortKeys);
    maker.sortOrder(sortOrder);
    // Compose signature
    long[] typeSig = ((InternalRelationType) type).getSignature();
    Set<PropertyKey> signature = new HashSet<>(typeSig.length);
    for (long typeId : typeSig) {
        signature.add(transaction.getExistingPropertyKey(typeId));
    }
    for (RelationType sortType : sortKeys) {
        signature.remove(sortType);
    }
    if (!signature.isEmpty()) {
        PropertyKey[] sig = signature.toArray(new PropertyKey[signature.size()]);
        maker.signature(sig);
    }
    RelationType typeIndex = maker.make();
    addSchemaEdge(type, typeIndex, TypeDefinitionCategory.RELATIONTYPE_INDEX, null);
    RelationTypeIndexWrapper index = new RelationTypeIndexWrapper((InternalRelationType) typeIndex);
    if (!canIndexBeEnabled) {
        updateIndex(index, SchemaAction.REGISTER_INDEX);
    }
    return index;
}
Also used : StandardRelationTypeMaker(org.janusgraph.graphdb.types.StandardRelationTypeMaker) EdgeLabel(org.janusgraph.core.EdgeLabel) StandardEdgeLabelMaker(org.janusgraph.graphdb.types.StandardEdgeLabelMaker) RelationType(org.janusgraph.core.RelationType) InternalRelationType(org.janusgraph.graphdb.internal.InternalRelationType) StandardPropertyKeyMaker(org.janusgraph.graphdb.types.StandardPropertyKeyMaker) InternalRelationType(org.janusgraph.graphdb.internal.InternalRelationType) PropertyKey(org.janusgraph.core.PropertyKey) HashSet(java.util.HashSet)

Aggregations

RelationType (org.janusgraph.core.RelationType)30 InternalRelationType (org.janusgraph.graphdb.internal.InternalRelationType)22 PropertyKey (org.janusgraph.core.PropertyKey)10 JanusGraphRelation (org.janusgraph.core.JanusGraphRelation)6 JanusGraphIndex (org.janusgraph.core.schema.JanusGraphIndex)6 RelationTypeIndex (org.janusgraph.core.schema.RelationTypeIndex)6 StandardJanusGraphTx (org.janusgraph.graphdb.transaction.StandardJanusGraphTx)6 Map (java.util.Map)5 Direction (org.apache.tinkerpop.gremlin.structure.Direction)5 JanusGraphVertex (org.janusgraph.core.JanusGraphVertex)4 SystemRelationType (org.janusgraph.graphdb.types.system.SystemRelationType)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 EdgeLabel (org.janusgraph.core.EdgeLabel)3 VertexLabel (org.janusgraph.core.VertexLabel)3 PredicateCondition (org.janusgraph.graphdb.query.condition.PredicateCondition)3 ImplicitKey (org.janusgraph.graphdb.types.system.ImplicitKey)3 Preconditions (com.google.common.base.Preconditions)2 Collection (java.util.Collection)2 List (java.util.List)2