Search in sources :

Example 6 with JanusGraphSchemaVertex

use of org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex in project janusgraph by JanusGraph.

the class IndexRemoveJob method validateIndexStatus.

@Override
protected void validateIndexStatus() {
    if (!(index instanceof RelationTypeIndex || index instanceof JanusGraphIndex)) {
        throw new UnsupportedOperationException("Unsupported index found: " + index);
    }
    if (index instanceof JanusGraphIndex) {
        JanusGraphIndex graphIndex = (JanusGraphIndex) index;
        if (graphIndex.isMixedIndex())
            throw new UnsupportedOperationException("Cannot remove mixed indexes through JanusGraph. This can " + "only be accomplished in the indexing system directly.");
        CompositeIndexType indexType = (CompositeIndexType) managementSystem.getSchemaVertex(index).asIndexType();
        graphIndexId = indexType.getID();
    }
    // Must be a relation type index or a composite graph index
    JanusGraphSchemaVertex schemaVertex = managementSystem.getSchemaVertex(index);
    SchemaStatus actualStatus = schemaVertex.getStatus();
    Preconditions.checkArgument(actualStatus == SchemaStatus.DISABLED, "The index [%s] must be disabled before it can be removed", indexName);
}
Also used : CompositeIndexType(org.janusgraph.graphdb.types.CompositeIndexType) JanusGraphSchemaVertex(org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) RelationTypeIndex(org.janusgraph.core.schema.RelationTypeIndex) SchemaStatus(org.janusgraph.core.schema.SchemaStatus)

Example 7 with JanusGraphSchemaVertex

use of org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex in project janusgraph by JanusGraph.

the class VertexIDAssigner method assignIDs.

public void assignIDs(Iterable<InternalRelation> addedRelations) {
    if (!placementStrategy.supportsBulkPlacement()) {
        for (InternalRelation relation : addedRelations) {
            for (int i = 0; i < relation.getArity(); i++) {
                InternalVertex vertex = relation.getVertex(i);
                if (!vertex.hasId()) {
                    assignID(vertex, getVertexIDType(vertex));
                }
            }
            assignID(relation);
        }
    } else {
        // 2) only assign ids to (user) vertices
        Map<InternalVertex, PartitionAssignment> assignments = new HashMap<>();
        for (InternalRelation relation : addedRelations) {
            for (int i = 0; i < relation.getArity(); i++) {
                InternalVertex vertex = relation.getVertex(i);
                if (!vertex.hasId()) {
                    // Those are assigned ids immediately in the transaction
                    assert !(vertex instanceof JanusGraphSchemaVertex);
                    if (vertex.vertexLabel().isPartitioned())
                        // Assign partitioned vertex ids immediately
                        assignID(vertex, getVertexIDType(vertex));
                    else
                        assignments.put(vertex, PartitionAssignment.EMPTY);
                }
            }
        }
        log.trace("Bulk id assignment for {} vertices", assignments.size());
        for (int attempt = 0; attempt < MAX_PARTITION_RENEW_ATTEMPTS && (assignments != null && !assignments.isEmpty()); attempt++) {
            placementStrategy.getPartitions(assignments);
            Map<InternalVertex, PartitionAssignment> leftOvers = null;
            Iterator<Map.Entry<InternalVertex, PartitionAssignment>> iterator = assignments.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<InternalVertex, PartitionAssignment> entry = iterator.next();
                try {
                    assignID(entry.getKey(), entry.getValue().getPartitionID(), getVertexIDType(entry.getKey()));
                    Preconditions.checkArgument(entry.getKey().hasId());
                } catch (IDPoolExhaustedException e) {
                    if (leftOvers == null)
                        leftOvers = new HashMap<>();
                    leftOvers.put(entry.getKey(), PartitionAssignment.EMPTY);
                    break;
                }
            }
            if (leftOvers != null) {
                while (iterator.hasNext()) leftOvers.put(iterator.next().getKey(), PartitionAssignment.EMPTY);
                log.debug("Exhausted ID Pool in bulk assignment. Left-over vertices {}", leftOvers.size());
            }
            assignments = leftOvers;
        }
        if (assignments != null && !assignments.isEmpty())
            throw new IDPoolExhaustedException("Could not find non-exhausted partition ID Pool after " + MAX_PARTITION_RENEW_ATTEMPTS + " attempts");
        // 3) assign ids to relations
        for (InternalRelation relation : addedRelations) {
            assignID(relation);
        }
    }
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) InternalRelation(org.janusgraph.graphdb.internal.InternalRelation) InternalVertex(org.janusgraph.graphdb.internal.InternalVertex) JanusGraphSchemaVertex(org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex) ConcurrentMap(java.util.concurrent.ConcurrentMap) ImmutableMap(com.google.common.collect.ImmutableMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 8 with JanusGraphSchemaVertex

use of org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex in project janusgraph by JanusGraph.

the class VertexIDAssigner method assignID.

private void assignID(InternalElement element, IDManager.VertexIDType vertexIDType) {
    for (int attempt = 0; attempt < MAX_PARTITION_RENEW_ATTEMPTS; attempt++) {
        long partitionID = -1;
        if (element instanceof JanusGraphSchemaVertex) {
            partitionID = IDManager.SCHEMA_PARTITION;
        } else if (element instanceof JanusGraphVertex) {
            if (vertexIDType == IDManager.VertexIDType.PartitionedVertex)
                partitionID = IDManager.PARTITIONED_VERTEX_PARTITION;
            else
                partitionID = placementStrategy.getPartition(element);
        } else if (element instanceof InternalRelation) {
            InternalRelation relation = (InternalRelation) element;
            if (attempt < relation.getLen()) {
                // On the first attempts, try to use partition of incident vertices
                InternalVertex incident = relation.getVertex(attempt);
                Preconditions.checkArgument(incident.hasId());
                if (!IDManager.VertexIDType.PartitionedVertex.is(incident.longId()) || relation.isProperty()) {
                    partitionID = getPartitionID(incident);
                } else {
                    continue;
                }
            } else {
                partitionID = placementStrategy.getPartition(element);
            }
        }
        try {
            assignID(element, partitionID, vertexIDType);
        } catch (IDPoolExhaustedException e) {
            // try again on a different partition
            continue;
        }
        assert element.hasId();
        // Check if we should assign a different representative of a potential partitioned vertex
        if (element instanceof InternalRelation) {
            InternalRelation relation = (InternalRelation) element;
            if (relation.isProperty() && isPartitionedAt(relation, 0)) {
                // Always assign properties to the canonical representative of a partitioned vertex
                InternalVertex vertex = relation.getVertex(0);
                ((ReassignableRelation) relation).setVertexAt(0, vertex.tx().getInternalVertex(idManager.getCanonicalVertexId(vertex.longId())));
            } else if (relation.isEdge()) {
                for (int pos = 0; pos < relation.getArity(); pos++) {
                    if (isPartitionedAt(relation, pos)) {
                        InternalVertex incident = relation.getVertex(pos);
                        long newPartition;
                        int otherPosition = (pos + 1) % 2;
                        if (((InternalRelationType) relation.getType()).multiplicity().isUnique(EdgeDirection.fromPosition(pos))) {
                            // If the relation is unique in the direction, we assign it to the canonical vertex...
                            newPartition = idManager.getPartitionId(idManager.getCanonicalVertexId(incident.longId()));
                        } else if (!isPartitionedAt(relation, otherPosition)) {
                            // ...else, we assign it to the partition of the non-partitioned vertex...
                            newPartition = getPartitionID(relation.getVertex(otherPosition));
                        } else {
                            // ...and if such does not exists (i.e. both end vertices are partitioned) we use the hash of the relation id
                            newPartition = idManager.getPartitionHashForId(relation.longId());
                        }
                        if (idManager.getPartitionId(incident.longId()) != newPartition) {
                            ((ReassignableRelation) relation).setVertexAt(pos, incident.tx().getOtherPartitionVertex(incident, newPartition));
                        }
                    }
                }
            }
        }
        return;
    }
    throw new IDPoolExhaustedException("Could not find non-exhausted partition ID Pool after " + MAX_PARTITION_RENEW_ATTEMPTS + " attempts");
}
Also used : ReassignableRelation(org.janusgraph.graphdb.relations.ReassignableRelation) InternalVertex(org.janusgraph.graphdb.internal.InternalVertex) JanusGraphSchemaVertex(org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex) InternalRelationType(org.janusgraph.graphdb.internal.InternalRelationType) InternalRelation(org.janusgraph.graphdb.internal.InternalRelation)

Example 9 with JanusGraphSchemaVertex

use of org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex in project janusgraph by JanusGraph.

the class StandardJanusGraphTx method makeSchemaVertex.

/*
     * ------------------------------------ Schema Handling ------------------------------------
     */
public final JanusGraphSchemaVertex makeSchemaVertex(JanusGraphSchemaCategory schemaCategory, String name, TypeDefinitionMap definition) {
    verifyOpen();
    Preconditions.checkArgument(!schemaCategory.hasName() || StringUtils.isNotBlank(name), "Need to provide a valid name for type [%s]", schemaCategory);
    schemaCategory.verifyValidDefinition(definition);
    JanusGraphSchemaVertex schemaVertex;
    if (schemaCategory.isRelationType()) {
        if (schemaCategory == JanusGraphSchemaCategory.PROPERTYKEY) {
            schemaVertex = new PropertyKeyVertex(this, IDManager.getTemporaryVertexID(IDManager.VertexIDType.UserPropertyKey, temporaryIds.nextID()), ElementLifeCycle.New);
        } else {
            assert schemaCategory == JanusGraphSchemaCategory.EDGELABEL;
            schemaVertex = new EdgeLabelVertex(this, IDManager.getTemporaryVertexID(IDManager.VertexIDType.UserEdgeLabel, temporaryIds.nextID()), ElementLifeCycle.New);
        }
    } else if (schemaCategory == JanusGraphSchemaCategory.VERTEXLABEL) {
        schemaVertex = new VertexLabelVertex(this, IDManager.getTemporaryVertexID(IDManager.VertexIDType.GenericSchemaType, temporaryIds.nextID()), ElementLifeCycle.New);
    } else {
        schemaVertex = new JanusGraphSchemaVertex(this, IDManager.getTemporaryVertexID(IDManager.VertexIDType.GenericSchemaType, temporaryIds.nextID()), ElementLifeCycle.New);
    }
    graph.assignID(schemaVertex, BaseVertexLabel.DEFAULT_VERTEXLABEL);
    Preconditions.checkArgument(schemaVertex.longId() > 0);
    if (schemaCategory.hasName())
        addProperty(schemaVertex, BaseKey.SchemaName, schemaCategory.getSchemaName(name));
    addProperty(schemaVertex, BaseKey.VertexExists, Boolean.TRUE);
    addProperty(schemaVertex, BaseKey.SchemaCategory, schemaCategory);
    updateSchemaVertex(schemaVertex);
    addProperty(schemaVertex, BaseKey.SchemaUpdateTime, times.getTime(times.getTime()));
    for (Map.Entry<TypeDefinitionCategory, Object> def : definition.entrySet()) {
        JanusGraphVertexProperty p = addProperty(schemaVertex, BaseKey.SchemaDefinitionProperty, def.getValue());
        p.property(BaseKey.SchemaDefinitionDesc.name(), TypeDefinitionDescription.of(def.getKey()));
    }
    vertexCache.add(schemaVertex, schemaVertex.longId());
    if (schemaCategory.hasName())
        newTypeCache.put(schemaCategory.getSchemaName(name), schemaVertex.longId());
    return schemaVertex;
}
Also used : EdgeLabelVertex(org.janusgraph.graphdb.types.vertices.EdgeLabelVertex) JanusGraphSchemaVertex(org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex) PropertyKeyVertex(org.janusgraph.graphdb.types.vertices.PropertyKeyVertex) NonBlockingHashMap(org.cliffc.high_scale_lib.NonBlockingHashMap)

Example 10 with JanusGraphSchemaVertex

use of org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex 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);
    if (schemaVertex.name().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));
    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)

Aggregations

JanusGraphSchemaVertex (org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex)15 JanusGraphIndex (org.janusgraph.core.schema.JanusGraphIndex)4 CompositeIndexType (org.janusgraph.graphdb.types.CompositeIndexType)4 IndexType (org.janusgraph.graphdb.types.IndexType)4 MixedIndexType (org.janusgraph.graphdb.types.MixedIndexType)4 TypeDefinitionMap (org.janusgraph.graphdb.types.TypeDefinitionMap)4 JanusGraphException (org.janusgraph.core.JanusGraphException)3 RelationTypeIndex (org.janusgraph.core.schema.RelationTypeIndex)3 InternalRelationType (org.janusgraph.graphdb.internal.InternalRelationType)3 IndexTypeWrapper (org.janusgraph.graphdb.types.indextype.IndexTypeWrapper)3 PropertyKey (org.janusgraph.core.PropertyKey)2 RelationType (org.janusgraph.core.RelationType)2 Parameter (org.janusgraph.core.schema.Parameter)2 InternalRelation (org.janusgraph.graphdb.internal.InternalRelation)2 InternalVertex (org.janusgraph.graphdb.internal.InternalVertex)2 SchemaSource (org.janusgraph.graphdb.types.SchemaSource)2 PropertyKeyVertex (org.janusgraph.graphdb.types.vertices.PropertyKeyVertex)2 Preconditions (com.google.common.base.Preconditions)1 Predicate (com.google.common.base.Predicate)1 Predicates (com.google.common.base.Predicates)1